Example usage for org.objectweb.asm MethodVisitor visitJumpInsn

List of usage examples for org.objectweb.asm MethodVisitor visitJumpInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitJumpInsn.

Prototype

public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Document

Visits a jump instruction.

Usage

From source file:org.glowroot.agent.advicegen.AdviceGenerator.java

License:Apache License

private void addOnReturnMethod(ClassWriter cw) {
    boolean entryOrTimer = !config.traceEntryEnabledProperty().isEmpty();
    String travelerType = entryOrTimer ? "Ljava/lang/Object;" : "Lorg/glowroot/agent/plugin/api/TraceEntry;";
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "onReturn",
            "(Lorg/glowroot/agent/plugin/api/weaving/OptionalReturn;" + travelerType + ")V", null, null);
    mv.visitParameterAnnotation(0, "Lorg/glowroot/agent/plugin/api/weaving/BindOptionalReturn;", true)
            .visitEnd();/* w ww . jav a2s  .  co  m*/
    mv.visitParameterAnnotation(1, "Lorg/glowroot/agent/plugin/api/weaving/BindTraveler;", true).visitEnd();
    int travelerParamIndex = 1;
    visitAnnotation(mv, "Lorg/glowroot/agent/plugin/api/weaving/OnReturn;");
    mv.visitCode();
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitVarInsn(ALOAD, travelerParamIndex);
        // TraceEntryImpl implements both TraceEntry and Timer so cannot check instanceof Timer
        // to differentiate here (but can check isntanceof TraceEntry)
        mv.visitTypeInsn(INSTANCEOF, "org/glowroot/agent/plugin/api/TraceEntry");
        Label label = new Label();
        mv.visitJumpInsn(IFNE, label);
        mv.visitVarInsn(ALOAD, travelerParamIndex);
        mv.visitTypeInsn(CHECKCAST, "org/glowroot/agent/plugin/api/Timer");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/Timer", "stop", "()V", true);
        mv.visitInsn(RETURN);
        mv.visitLabel(label);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/weaving/OptionalReturn", "isVoid", "()Z",
            true);
    Label notVoidLabel = new Label();
    Label endIfLabel = new Label();
    mv.visitJumpInsn(IFEQ, notVoidLabel);
    mv.visitLdcInsn("void");
    mv.visitJumpInsn(GOTO, endIfLabel);
    mv.visitLabel(notVoidLabel);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/weaving/OptionalReturn", "getValue",
            "()Ljava/lang/Object;", true);
    mv.visitLabel(endIfLabel);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/GenericMessageSupplier",
            "updateWithReturnValue", "(Lorg/glowroot/agent/plugin/api/TraceEntry;Ljava/lang/Object;)V", false);
    mv.visitVarInsn(ALOAD, travelerParamIndex);
    Integer stackTraceThresholdMillis = config.traceEntryStackThresholdMillis();
    if (stackTraceThresholdMillis == null) {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/TraceEntry", "end", "()V", true);
    } else {
        mv.visitLdcInsn(stackTraceThresholdMillis.longValue());
        mv.visitFieldInsn(GETSTATIC, "java/util/concurrent/TimeUnit", "MILLISECONDS",
                "Ljava/util/concurrent/TimeUnit;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/TraceEntry", "endWithStackTrace",
                "(JLjava/util/concurrent/TimeUnit;)V", true);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.agent.advicegen.AdviceGenerator.java

License:Apache License

private void addOnThrowMethod(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "onThrow",
            "(Ljava/lang/Throwable;Lorg/glowroot/agent/plugin/api/TraceEntry;)V", null, null);
    visitAnnotation(mv, "Lorg/glowroot/agent/plugin/api/weaving/OnThrow;");
    mv.visitParameterAnnotation(0, "Lorg/glowroot/agent/plugin/api/weaving/BindThrowable;", true).visitEnd();
    mv.visitParameterAnnotation(1, "Lorg/glowroot/agent/plugin/api/weaving/BindTraveler;", true).visitEnd();
    mv.visitCode();//  www.j ava 2  s  .c  o  m
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitVarInsn(ALOAD, 1);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitInsn(RETURN);
        mv.visitLabel(l0);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/TraceEntry", "endWithError",
            "(Ljava/lang/Throwable;)V", true);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.agent.weaving.AdviceGenerator.java

License:Apache License

@RequiresNonNull("methodMetaInternalName")
private void addOnBeforeMethod(ClassWriter cw) {
    MethodVisitor mv = visitOnBeforeMethod(cw, "Lorg/glowroot/agent/plugin/api/TraceEntry;");
    mv.visitCode();/*ww  w.  j  a  va  2 s .  c o m*/
    if (!config.traceEntryEnabledProperty().isEmpty() && pluginId != null) {
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "entryEnabled",
                "Lorg/glowroot/agent/plugin/api/config/BooleanProperty;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/config/BooleanProperty", "value",
                "()Z", true);
        Label label = new Label();
        mv.visitJumpInsn(IFNE, label);
        // entryEnabled is false, collect timer only
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETSTATIC, adviceInternalName, "timerName",
                "Lorg/glowroot/agent/plugin/api/TimerName;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext", "startTimer",
                "(Lorg/glowroot/agent/plugin/api/TimerName;)" + "Lorg/glowroot/agent/plugin/api/Timer;", true);
        mv.visitInsn(ARETURN);
        mv.visitLabel(label);
    }
    mv.visitVarInsn(ALOAD, 0);
    if (config.isTransaction()) {
        String transactionType = config.transactionType();
        if (transactionType.isEmpty()) {
            mv.visitLdcInsn("<no transaction type provided>");
        } else {
            mv.visitLdcInsn(transactionType);
        }
        mv.visitVarInsn(ALOAD, 4);
        mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getTransactionNameTemplate",
                "()Lorg/glowroot/agent/weaving/MessageTemplate;", false);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitVarInsn(ALOAD, 3);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/GenericMessageSupplier", "create",
                "(Lorg/glowroot/agent/weaving/MessageTemplate;"
                        + "Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)"
                        + "Lorg/glowroot/agent/weaving/GenericMessageSupplier;",
                false);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/glowroot/agent/weaving/GenericMessageSupplier", "getMessageText",
                "()Ljava/lang/String;", false);
    }
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getMessageTemplate",
            "()Lorg/glowroot/agent/weaving/MessageTemplate;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/GenericMessageSupplier", "create",
            "(Lorg/glowroot/agent/weaving/MessageTemplate;"
                    + "Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)"
                    + "Lorg/glowroot/agent/weaving/GenericMessageSupplier;",
            false);
    mv.visitFieldInsn(GETSTATIC, adviceInternalName, "timerName", "Lorg/glowroot/agent/plugin/api/TimerName;");
    if (config.isTransaction()) {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/OptionalThreadContext",
                "startTransaction",
                "(Ljava/lang/String;Ljava/lang/String;" + "Lorg/glowroot/agent/plugin/api/MessageSupplier;"
                        + "Lorg/glowroot/agent/plugin/api/TimerName;)"
                        + "Lorg/glowroot/agent/plugin/api/TraceEntry;",
                true);
    } else {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext", "startTraceEntry",
                "(Lorg/glowroot/agent/plugin/api/MessageSupplier;"
                        + "Lorg/glowroot/agent/plugin/api/TimerName;)"
                        + "Lorg/glowroot/agent/plugin/api/TraceEntry;",
                true);
    }
    addCodeForOptionalTransactionAttributes(mv);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.agent.weaving.AdviceGenerator.java

License:Apache License

private void addOnReturnMethod(ClassWriter cw) {
    boolean entryOrTimer = !config.traceEntryEnabledProperty().isEmpty();
    String travelerType = entryOrTimer ? "Ljava/lang/Object;" : "Lorg/glowroot/agent/plugin/api/TraceEntry;";
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "onReturn",
            "(Lorg/glowroot/agent/plugin/api/weaving/OptionalReturn;" + travelerType + ")V", null, null);
    mv.visitParameterAnnotation(0, "Lorg/glowroot/agent/plugin/api/weaving/BindOptionalReturn;", true)
            .visitEnd();//from  ww w  . ja va  2s  . c  om
    mv.visitParameterAnnotation(1, "Lorg/glowroot/agent/plugin/api/weaving/BindTraveler;", true).visitEnd();
    int travelerParamIndex = 1;
    visitAnnotation(mv, "Lorg/glowroot/agent/plugin/api/weaving/OnReturn;");
    mv.visitCode();
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitVarInsn(ALOAD, travelerParamIndex);
        // TraceEntryImpl implements both TraceEntry and Timer so cannot check instanceof Timer
        // to differentiate here (but can check isntanceof TraceEntry)
        mv.visitTypeInsn(INSTANCEOF, "org/glowroot/agent/plugin/api/TraceEntry");
        Label label = new Label();
        mv.visitJumpInsn(IFNE, label);
        mv.visitVarInsn(ALOAD, travelerParamIndex);
        mv.visitTypeInsn(CHECKCAST, "org/glowroot/agent/plugin/api/Timer");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/Timer", "stop", "()V", true);
        mv.visitInsn(RETURN);
        mv.visitLabel(label);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/weaving/OptionalReturn", "isVoid", "()Z",
            true);
    Label notVoidLabel = new Label();
    Label endIfLabel = new Label();
    mv.visitJumpInsn(IFEQ, notVoidLabel);
    mv.visitLdcInsn("void");
    mv.visitJumpInsn(GOTO, endIfLabel);
    mv.visitLabel(notVoidLabel);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/weaving/OptionalReturn", "getValue",
            "()Ljava/lang/Object;", true);
    mv.visitLabel(endIfLabel);
    mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/GenericMessageSupplier",
            "updateWithReturnValue", "(Lorg/glowroot/agent/plugin/api/TraceEntry;Ljava/lang/Object;)V", false);
    mv.visitVarInsn(ALOAD, travelerParamIndex);
    Integer stackTraceThresholdMillis = config.traceEntryStackThresholdMillis();
    if (stackTraceThresholdMillis == null) {
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/TraceEntry", "end", "()V", true);
    } else {
        mv.visitLdcInsn(stackTraceThresholdMillis.longValue());
        mv.visitFieldInsn(GETSTATIC, "java/util/concurrent/TimeUnit", "MILLISECONDS",
                "Ljava/util/concurrent/TimeUnit;");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/TraceEntry", "endWithStackTrace",
                "(JLjava/util/concurrent/TimeUnit;)V", true);
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.glowroot.agent.weaving.GenerateMoreHackedConstructorBytecode.java

License:Apache License

static LazyDefinedClass generateMoreHackedConstructorBytecode() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    // 1.7+ since testing frames
    cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "MoreHackedConstructorBytecode", null,
            Test.class.getName().replace('.', '/'), new String[] {});

    {/* ww  w.  j ava 2s. c  o  m*/
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label trueLabel = new Label();
        mv.visitInsn(ICONST_0);
        mv.visitJumpInsn(IFEQ, trueLabel);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ATHROW);
        mv.visitLabel(trueLabel);
        mv.visitFrame(F_SAME, 0, null, 0, null);

        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, Test.class.getName().replace('.', '/'), "<init>", "()V", false);

        mv.visitInsn(RETURN);

        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return ImmutableLazyDefinedClass.builder().type(Type.getObjectType("MoreHackedConstructorBytecode"))
            .bytes(cw.toByteArray()).build();
}

From source file:org.glowroot.agent.weaving.GenerateStillMoreNotPerfectBytecode.java

License:Apache License

static LazyDefinedClass generateStillMoreNotPerfectBytecode() throws Exception {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;

    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "StillMoreNotPerfectBytecode", null, "java/lang/Object",
            new String[] { Test.class.getName().replace('.', '/') });

    {/*from   www. j  a va2 s  .  c  o m*/
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "execute", "()V", null, null);
        mv.visitCode();
        Label label = new Label();
        mv.visitJumpInsn(GOTO, label);
        // need to generate more than Short.MAX_VALUE instructions so that asm has to generate
        // special instructions in org.objectweb.asm.Label.resolve() and do a second pass at the
        // end of org.objectweb.asm.ClassWriter.toByteArray(), which ended up causing
        // java.lang.NegativeArraySizeException at
        // org.objectweb.asm.Frame.merge(Frame.java:1197)
        // before frames were ignored for Java 6 bytecode in
        // WeavingMethodVisitor.visitImplicitFrame()
        for (int i = 0; i < 20000; i++) {
            mv.visitInsn(ICONST_1);
            mv.visitInsn(POP);
        }
        Label label2 = new Label();
        mv.visitLabel(label2);
        mv.visitInsn(POP);
        mv.visitLabel(label);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();

    return ImmutableLazyDefinedClass.builder().type(Type.getObjectType("StillMoreNotPerfectBytecode"))
            .bytes(cw.toByteArray()).build();
}

From source file:org.glowroot.agent.weaving.WeavingClassVisitor.java

License:Apache License

@RequiresNonNull({ "type", "metaHolderInternalName", "loader" })
private void generateMetaHolder() throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, metaHolderInternalName, null, "java/lang/Object", null);
    Type metaHolderType = Type.getObjectType(metaHolderInternalName);
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();//from ww  w .  ja va  2  s  .com
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/ClassNotFoundException");
    mv.visitLabel(l0);
    for (Type classMetaType : classMetaTypes) {
        String classMetaInternalName = classMetaType.getInternalName();
        String classMetaFieldName = "glowroot$class$meta$" + classMetaInternalName.replace('/', '$');
        FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, classMetaFieldName,
                "L" + classMetaInternalName + ";", null, null);
        fv.visitEnd();
        mv.visitTypeInsn(NEW, classMetaInternalName);
        mv.visitInsn(DUP);
        loadType(mv, type, metaHolderType);
        mv.visitMethodInsn(INVOKESPECIAL, classMetaInternalName, "<init>", "(Ljava/lang/Class;)V", false);
        mv.visitFieldInsn(PUTSTATIC, metaHolderInternalName, classMetaFieldName,
                "L" + classMetaInternalName + ";");
    }
    for (MethodMetaGroup methodMetaGroup : methodMetaGroups) {
        for (Type methodMetaType : methodMetaGroup.methodMetaTypes()) {
            String methodMetaInternalName = methodMetaType.getInternalName();
            String methodMetaFieldName = "glowroot$method$meta$" + methodMetaGroup.uniqueNum() + '$'
                    + methodMetaInternalName.replace('/', '$');
            FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, methodMetaFieldName,
                    "L" + methodMetaInternalName + ";", null, null);
            fv.visitEnd();
            mv.visitTypeInsn(NEW, methodMetaInternalName);
            mv.visitInsn(DUP);
            loadType(mv, type, metaHolderType);
            mv.visitLdcInsn(methodMetaGroup.methodName());
            mv.visitIntInsn(BIPUSH, methodMetaGroup.methodParameterTypes().size());
            mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
            for (int i = 0; i < methodMetaGroup.methodParameterTypes().size(); i++) {
                mv.visitInsn(DUP);
                mv.visitIntInsn(BIPUSH, i);
                loadType(mv, methodMetaGroup.methodParameterTypes().get(i), metaHolderType);
                mv.visitInsn(AASTORE);
            }
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod",
                    "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false);
            mv.visitMethodInsn(INVOKESPECIAL, methodMetaInternalName, "<init>", "(Ljava/lang/reflect/Method;)V",
                    false);
            mv.visitFieldInsn(PUTSTATIC, metaHolderInternalName, methodMetaFieldName,
                    "L" + methodMetaInternalName + ";");
        }
    }
    // this is just try/catch ClassNotFoundException/re-throw AssertionError
    mv.visitLabel(l1);
    Label l3 = new Label();
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/ClassNotFoundException" });
    mv.visitVarInsn(ASTORE, 0);
    mv.visitTypeInsn(NEW, "java/lang/AssertionError");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/AssertionError", "<init>", "(Ljava/lang/Object;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    byte[] bytes = cw.toByteArray();
    ClassLoaders.defineClass(ClassNames.fromInternalName(metaHolderInternalName), bytes, loader);
}

From source file:org.glowroot.weaving.WeavingClassVisitor.java

License:Apache License

@RequiresNonNull({ "type", "metaHolderInternalName", "loader" })
private void generateMetaHolder() throws Exception {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, metaHolderInternalName, null, "java/lang/Object", null);
    Type metaHolderType = Type.getObjectType(metaHolderInternalName);
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();//  w  w  w. ja v  a  2 s  .  c  o m
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/ClassNotFoundException");
    mv.visitLabel(l0);
    for (Type classMetaType : classMetaTypes) {
        String classMetaInternalName = classMetaType.getInternalName();
        String classMetaFieldName = "glowroot$class$meta$" + classMetaInternalName.replace('/', '$');
        FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, classMetaFieldName,
                "L" + classMetaInternalName + ";", null, null);
        fv.visitEnd();
        mv.visitTypeInsn(NEW, classMetaInternalName);
        mv.visitInsn(DUP);
        loadType(mv, type, metaHolderType);
        mv.visitMethodInsn(INVOKESPECIAL, classMetaInternalName, "<init>", "(Ljava/lang/Class;)V", false);
        mv.visitFieldInsn(PUTSTATIC, metaHolderInternalName, classMetaFieldName,
                "L" + classMetaInternalName + ";");
    }
    for (MethodMetaGroup methodMetaGroup : methodMetaGroups) {
        for (Type methodMetaType : methodMetaGroup.methodMetaTypes()) {
            String methodMetaInternalName = methodMetaType.getInternalName();
            String methodMetaFieldName = "glowroot$method$meta$" + methodMetaGroup.uniqueNum() + '$'
                    + methodMetaInternalName.replace('/', '$');
            FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, methodMetaFieldName,
                    "L" + methodMetaInternalName + ";", null, null);
            fv.visitEnd();
            mv.visitTypeInsn(NEW, methodMetaInternalName);
            mv.visitInsn(DUP);
            loadType(mv, type, metaHolderType);
            loadType(mv, methodMetaGroup.returnType(), metaHolderType);

            mv.visitIntInsn(BIPUSH, methodMetaGroup.parameterTypes().size());
            mv.visitTypeInsn(ANEWARRAY, "java/lang/Class");
            for (int i = 0; i < methodMetaGroup.parameterTypes().size(); i++) {
                mv.visitInsn(DUP);
                mv.visitIntInsn(BIPUSH, i);
                loadType(mv, methodMetaGroup.parameterTypes().get(i), metaHolderType);
                mv.visitInsn(AASTORE);
            }

            mv.visitMethodInsn(INVOKESPECIAL, methodMetaInternalName, "<init>",
                    "(Ljava/lang/Class;Ljava/lang/Class;[Ljava/lang/Class;)V", false);
            mv.visitFieldInsn(PUTSTATIC, metaHolderInternalName, methodMetaFieldName,
                    "L" + methodMetaInternalName + ";");
        }
    }
    // this is just try/catch ClassNotFoundException/re-throw AssertionError
    mv.visitLabel(l1);
    Label l3 = new Label();
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitFrame(F_SAME1, 0, null, 1, new Object[] { "java/lang/ClassNotFoundException" });
    mv.visitVarInsn(ASTORE, 0);
    mv.visitTypeInsn(NEW, "java/lang/AssertionError");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/AssertionError", "<init>", "(Ljava/lang/Object;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitFrame(F_SAME, 0, null, 0, null);

    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    cw.visitEnd();
    byte[] bytes = cw.toByteArray();
    ClassLoaders.defineClass(ClassNames.fromInternalName(metaHolderInternalName), bytes, loader);
}

From source file:org.graalvm.compiler.core.test.SubWordInputTest2.java

License:Open Source License

@Override
protected byte[] generateClass(String internalClassName) {
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(52, ACC_SUPER | ACC_PUBLIC, internalClassName, null, "java/lang/Object", null);

    final char typeChar = kind.getTypeChar();
    String getDescriptor = "(" + typeChar + ")" + "Z";
    MethodVisitor get = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, GET, getDescriptor, null, null);
    get.visitCode();/*from   w  ww  .  ja  va2 s . co m*/
    get.visitVarInsn(ILOAD, 0);
    Label label = new Label();
    get.visitJumpInsn(IFGE, label);
    get.visitInsn(ICONST_0);
    get.visitInsn(IRETURN);
    get.visitLabel(label);
    get.visitInsn(ICONST_1);
    get.visitInsn(IRETURN);
    get.visitMaxs(1, 1);
    get.visitEnd();

    MethodVisitor snippet = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, WRAPPER, "(I)Z", null, null);
    snippet.visitCode();
    snippet.visitVarInsn(ILOAD, 0);
    snippet.visitMethodInsn(INVOKESTATIC, internalClassName, GET, getDescriptor, false);
    snippet.visitInsn(IRETURN);
    snippet.visitMaxs(1, 1);
    snippet.visitEnd();

    cw.visitEnd();
    return cw.toByteArray();
}

From source file:org.graalvm.compiler.core.test.SubWordTestUtil.java

License:Open Source License

static void testEqual(MethodVisitor snippet) {
    Label label = new Label();
    snippet.visitJumpInsn(IF_ICMPNE, label);
    snippet.visitInsn(ICONST_1);//from  w  w w . j a  va 2  s  .c o  m
    snippet.visitInsn(IRETURN);
    snippet.visitLabel(label);
    snippet.visitInsn(ICONST_0);
    snippet.visitInsn(IRETURN);
}