// add assert startup code
 private void initAssertions(MethodVisitor mv) {
   mv.visitLdcInsn(Type.getType("L" + myClassName + ";"));
   mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "desiredAssertionStatus", "()Z", false);
   Label l0 = new Label();
   mv.visitJumpInsn(IFNE, l0);
   mv.visitInsn(ICONST_1);
   Label l1 = new Label();
   mv.visitJumpInsn(GOTO, l1);
   mv.visitLabel(l0);
   mv.visitInsn(ICONST_0);
   mv.visitLabel(l1);
   mv.visitFieldInsn(PUTSTATIC, myClassName, ASSERTIONS_DISABLED_NAME, "Z");
 }
Пример #2
0
  private void generateConstInstance() {
    MethodVisitor mv =
        v.newMethod(
            OtherOrigin(element, funDescriptor),
            ACC_STATIC | ACC_SYNTHETIC,
            "<clinit>",
            "()V",
            null,
            ArrayUtil.EMPTY_STRING_ARRAY);
    InstructionAdapter iv = new InstructionAdapter(mv);

    v.newField(
        OtherOrigin(element, funDescriptor),
        ACC_STATIC | ACC_FINAL | ACC_PUBLIC,
        JvmAbi.INSTANCE_FIELD,
        asmType.getDescriptor(),
        null,
        null);

    if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
      mv.visitCode();
      iv.anew(asmType);
      iv.dup();
      iv.invokespecial(asmType.getInternalName(), "<init>", "()V", false);
      iv.putstatic(asmType.getInternalName(), JvmAbi.INSTANCE_FIELD, asmType.getDescriptor());
      mv.visitInsn(RETURN);
      FunctionCodegen.endVisit(mv, "<clinit>", element);
    }
  }
  // verify pattern and add compiled pattern to static cache
  private void initPatterns(MethodVisitor mv) {
    mv.visitIntInsn(BIPUSH, myPatterns.size());
    mv.visitTypeInsn(ANEWARRAY, "java/util/regex/Pattern");
    mv.visitFieldInsn(PUTSTATIC, myClassName, PATTERN_CACHE_NAME, JAVA_UTIL_REGEX_PATTERN);

    int i = 0;
    for (String pattern : myPatterns) {
      // check the pattern so we can rely on the pattern being valid at runtime
      try {
        Pattern.compile(pattern);
      } catch (Exception e) {
        throw new InstrumentationException("Illegal Pattern: " + pattern, e);
      }

      mv.visitFieldInsn(GETSTATIC, myClassName, PATTERN_CACHE_NAME, JAVA_UTIL_REGEX_PATTERN);
      mv.visitIntInsn(BIPUSH, i++);
      mv.visitLdcInsn(pattern);
      mv.visitMethodInsn(
          INVOKESTATIC,
          "java/util/regex/Pattern",
          "compile",
          "(Ljava/lang/String;)Ljava/util/regex/Pattern;",
          false);
      mv.visitInsn(AASTORE);
    }
  }
  private void createStaticInitializer() {
    final MethodVisitor mv = cv.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();

    patchStaticInitializer(mv);

    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
  }