Example usage for org.objectweb.asm MethodVisitor visitLdcInsn

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

Introduction

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

Prototype

public void visitLdcInsn(final Object value) 

Source Link

Document

Visits a LDC instruction.

Usage

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

License:Apache License

@RequiresNonNull("methodMetaInternalName")
private void generateMethodMetaConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/reflect/Method;)V", null, null);
    mv.visitCode();/*  www .ja v  a  2s.  co  m*/
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    if (config.isTraceEntryOrGreater()) {
        String messageTemplate = config.traceEntryMessageTemplate();
        if (messageTemplate.isEmpty() && config.isTransaction()) {
            messageTemplate = config.transactionNameTemplate();
        }
        if (messageTemplate.isEmpty()) {
            mv.visitLdcInsn("<no message template provided>");
        } else {
            mv.visitLdcInsn(messageTemplate);
        }
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/advicegen/MessageTemplate;",
                false);
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "messageTemplate",
            "Lorg/glowroot/agent/advicegen/MessageTemplate;");
    if (!config.transactionNameTemplate().isEmpty()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(config.transactionNameTemplate());
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/advicegen/MessageTemplate;",
                false);
        mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "transactionNameTemplate",
                "Lorg/glowroot/agent/advicegen/MessageTemplate;");
    }
    if (!config.transactionUserTemplate().isEmpty()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(config.transactionUserTemplate());
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/advicegen/MessageTemplate;",
                false);
        mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "transactionUserTemplate",
                "Lorg/glowroot/agent/advicegen/MessageTemplate;");
    }
    int i = 0;
    for (String attrTemplate : config.transactionAttributeTemplates().values()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(attrTemplate);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/advicegen/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/advicegen/MessageTemplate;",
                false);
        mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "transactionAttributeTemplate" + i++,
                "Lorg/glowroot/agent/advicegen/MessageTemplate;");
    }
    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();/*from   w ww  .  ja  va2s  . co  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 addCodeForOptionalTransactionAttributes(MethodVisitor mv) {
    if (!config.transactionType().isEmpty() && !config.isTransaction()) {
        mv.visitVarInsn(ALOAD, 0);// www.ja va 2  s . co  m
        mv.visitLdcInsn(config.transactionType());
        mv.visitLdcInsn(Priority.USER_CONFIG);
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext", "setTransactionType",
                "(Ljava/lang/String;I)V", true);
    }
    if (!config.transactionNameTemplate().isEmpty() && !config.isTransaction()) {
        addCodeForSetTransactionX(mv, "getTransactionNameTemplate", "setTransactionName");
    }
    if (!config.transactionUserTemplate().isEmpty()) {
        addCodeForSetTransactionX(mv, "getTransactionUserTemplate", "setTransactionUser");
    }
    int i = 0;
    for (String attrName : config.transactionAttributeTemplates().keySet()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(attrName);
        mv.visitVarInsn(ALOAD, 4);
        // methodMetaInternalName is non-null when transactionAttributeTemplates is
        // non-empty
        checkNotNull(methodMetaInternalName);
        mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, "getTransactionAttributeTemplate" + i++,
                "()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.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext",
                "addTransactionAttribute", "(Ljava/lang/String;Ljava/lang/String;)V", true);
    }
    Integer slowThresholdMillis = config.transactionSlowThresholdMillis();
    if (slowThresholdMillis != null) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(slowThresholdMillis.longValue());
        mv.visitFieldInsn(GETSTATIC, "java/util/concurrent/TimeUnit", "MILLISECONDS",
                "Ljava/util/concurrent/TimeUnit;");
        mv.visitLdcInsn(Priority.USER_CONFIG);
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext",
                "setTransactionSlowThreshold", "(JLjava/util/concurrent/TimeUnit;I)V", true);
    }
    if (config.transactionOuter()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext",
                "setTransactionOuter", "()V", true);
    }
}

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   w  w  w .  j av a2s .  c o 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/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.AdviceGenerator.java

License:Apache License

private void addCodeForSetTransactionX(MethodVisitor mv, String templateGetterName,
        String threadContextSetterName) {
    mv.visitVarInsn(ALOAD, 0);//from   w ww  .  ja  v a2  s . c  o m
    mv.visitVarInsn(ALOAD, 4);
    // methodMetaInternalName is non-null when transactionNameTemplate or
    // transactionUserTemplate are non-empty
    checkNotNull(methodMetaInternalName);
    mv.visitMethodInsn(INVOKEVIRTUAL, methodMetaInternalName, templateGetterName,
            "()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.visitLdcInsn(Priority.USER_CONFIG);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/ThreadContext", threadContextSetterName,
            "(Ljava/lang/String;I)V", true);
}

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

License:Apache License

@RequiresNonNull("methodMetaInternalName")
private void generateMethodMetaConstructor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/reflect/Method;)V", null, null);
    mv.visitCode();//from  w  ww.  j av  a 2  s.  co  m
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitVarInsn(ALOAD, 0);
    if (config.isTraceEntryOrGreater()) {
        String messageTemplate = config.traceEntryMessageTemplate();
        if (messageTemplate.isEmpty() && config.isTransaction()) {
            messageTemplate = config.transactionNameTemplate();
        }
        if (messageTemplate.isEmpty()) {
            mv.visitLdcInsn("<no message template provided>");
        } else {
            mv.visitLdcInsn(messageTemplate);
        }
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/weaving/MessageTemplate;",
                false);
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "messageTemplate",
            "Lorg/glowroot/agent/weaving/MessageTemplate;");
    if (!config.transactionNameTemplate().isEmpty()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(config.transactionNameTemplate());
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/weaving/MessageTemplate;",
                false);
        mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "transactionNameTemplate",
                "Lorg/glowroot/agent/weaving/MessageTemplate;");
    }
    if (!config.transactionUserTemplate().isEmpty()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(config.transactionUserTemplate());
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/weaving/MessageTemplate;",
                false);
        mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "transactionUserTemplate",
                "Lorg/glowroot/agent/weaving/MessageTemplate;");
    }
    int i = 0;
    for (String attrTemplate : config.transactionAttributeTemplates().values()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitLdcInsn(attrTemplate);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESTATIC, "org/glowroot/agent/weaving/MessageTemplate", "create",
                "(Ljava/lang/String;Ljava/lang/reflect/Method;)"
                        + "Lorg/glowroot/agent/weaving/MessageTemplate;",
                false);
        mv.visitFieldInsn(PUTFIELD, methodMetaInternalName, "transactionAttributeTemplate" + i++,
                "Lorg/glowroot/agent/weaving/MessageTemplate;");
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

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();// www. ja  v  a  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.agent.weaving.WeavingClassVisitor.java

License:Apache License

private static void loadObjectType(MethodVisitor mv, Type type, Type ownerType) {
    // may not have access to type in meta holder, so need to use Class.forName()
    // instead of class constant
    mv.visitLdcInsn(type.getClassName());
    mv.visitInsn(ICONST_0);//  ww  w  .  j a v a 2 s.  co  m
    mv.visitLdcInsn(ownerType);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", false);
}

From source file:org.graalvm.compiler.core.test.SubWordArrayStoreTest.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 String fieldName = "array";
    final String fieldDescriptor = "[" + kind.getTypeChar();

    FieldVisitor field = cw.visitField(ACC_PUBLIC | ACC_STATIC, fieldName, fieldDescriptor, null, null);
    field.visitEnd();/* w  w  w  . j  a  v  a  2  s  .  c o m*/

    MethodVisitor clinit = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    clinit.visitCode();
    clinit.visitIntInsn(BIPUSH, 16);
    clinit.visitIntInsn(NEWARRAY, toASMType(kind));
    clinit.visitFieldInsn(PUTSTATIC, internalClassName, fieldName, fieldDescriptor);
    clinit.visitInsn(RETURN);
    clinit.visitMaxs(1, 0);
    clinit.visitEnd();

    MethodVisitor snippet = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, SNIPPET, "()Z", null, null);
    snippet.visitCode();

    if (unsafeStore) {
        SubWordTestUtil.getUnsafe(snippet);
        snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
        snippet.visitLdcInsn(arrayBaseOffset(kind));
        snippet.visitLdcInsn(value);
        snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe",
                "put" + SubWordTestUtil.getUnsafePutMethodName(kind),
                "(Ljava/lang/Object;J" + kind.getTypeChar() + ")V", false);
    } else {
        snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
        snippet.visitInsn(ICONST_0);
        snippet.visitLdcInsn(value);
        snippet.visitInsn(toArrayStoreOpcode(kind));
    }

    if (unsafeLoad) {
        SubWordTestUtil.getUnsafe(snippet);
        snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
        snippet.visitLdcInsn(arrayBaseOffset(kind));
        snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe",
                "get" + SubWordTestUtil.getUnsafePutMethodName(kind),
                "(Ljava/lang/Object;J)" + kind.getTypeChar(), false);
    } else {
        snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
        snippet.visitInsn(ICONST_0);
        snippet.visitInsn(toArrayLoadOpcode(kind));
    }

    snippet.visitLdcInsn(value);
    SubWordTestUtil.convertToKind(snippet, kind);
    SubWordTestUtil.testEqual(snippet);

    snippet.visitMaxs(5, 0);
    snippet.visitEnd();

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

From source file:org.graalvm.compiler.core.test.SubWordFieldStoreTest.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 String fieldName = "field";
    final String fieldDescriptor = Character.toString(kind.getTypeChar());

    FieldVisitor field = cw.visitField(ACC_PUBLIC | ACC_STATIC, fieldName, fieldDescriptor, null, value);
    field.visitEnd();/*from w ww  .  ja va2  s . c o m*/

    MethodVisitor snippet = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, SNIPPET, "()Z", null,
            new String[] { "java/lang/NoSuchFieldException" });
    snippet.visitCode();

    if (unsafeStore) {
        snippet.visitLdcInsn(Type.getObjectType(internalClassName));
        snippet.visitLdcInsn(fieldName);
        snippet.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getField",
                "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
        snippet.visitVarInsn(ASTORE, 0);

        SubWordTestUtil.getUnsafe(snippet);
        snippet.visitVarInsn(ALOAD, 0);
        snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "staticFieldBase",
                "(Ljava/lang/reflect/Field;)Ljava/lang/Object;", false);
        snippet.visitVarInsn(ASTORE, 1);

        SubWordTestUtil.getUnsafe(snippet);
        snippet.visitVarInsn(ALOAD, 0);
        snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "staticFieldOffset",
                "(Ljava/lang/reflect/Field;)J", false);
        snippet.visitVarInsn(LSTORE, 2);

        SubWordTestUtil.getUnsafe(snippet);
        snippet.visitVarInsn(ALOAD, 1);
        snippet.visitVarInsn(LLOAD, 2);
        snippet.visitLdcInsn(value);
        snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe",
                "put" + SubWordTestUtil.getUnsafePutMethodName(kind),
                "(Ljava/lang/Object;J" + kind.getTypeChar() + ")V", false);
    } else {
        snippet.visitLdcInsn(value);
        snippet.visitFieldInsn(PUTSTATIC, internalClassName, fieldName, fieldDescriptor);
    }

    if (unsafeLoad) {
        if (!unsafeStore) {
            snippet.visitLdcInsn(Type.getObjectType(internalClassName));
            snippet.visitLdcInsn(fieldName);
            snippet.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getField",
                    "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
            snippet.visitVarInsn(ASTORE, 0);

            SubWordTestUtil.getUnsafe(snippet);
            snippet.visitVarInsn(ALOAD, 0);
            snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "staticFieldBase",
                    "(Ljava/lang/reflect/Field;)Ljava/lang/Object;", false);
            snippet.visitVarInsn(ASTORE, 1);

            SubWordTestUtil.getUnsafe(snippet);
            snippet.visitVarInsn(ALOAD, 0);
            snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "staticFieldOffset",
                    "(Ljava/lang/reflect/Field;)J", false);
            snippet.visitVarInsn(LSTORE, 2);
        }
        SubWordTestUtil.getUnsafe(snippet);
        snippet.visitVarInsn(ALOAD, 1);
        snippet.visitVarInsn(LLOAD, 2);
        snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe",
                "get" + SubWordTestUtil.getUnsafePutMethodName(kind),
                "(Ljava/lang/Object;J)" + kind.getTypeChar(), false);
    } else {
        snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
    }

    snippet.visitLdcInsn(value);
    SubWordTestUtil.convertToKind(snippet, kind);
    SubWordTestUtil.testEqual(snippet);

    snippet.visitMaxs(5, 4);
    snippet.visitEnd();

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