Example usage for org.objectweb.asm Opcodes GETSTATIC

List of usage examples for org.objectweb.asm Opcodes GETSTATIC

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes GETSTATIC.

Prototype

int GETSTATIC

To view the source code for org.objectweb.asm Opcodes GETSTATIC.

Click Source Link

Usage

From source file:com.github.megatronking.stringfog.plugin.StringFogClassVisitor.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (mv != null && !mIgnoreClass) {
        if ("<clinit>".equals(name)) {
            isClInitExists = true;/*from  ww  w .  j a v  a 2 s .  c o m*/
            // If clinit exists meaning the static fields (not final) would have be inited here.
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                private String lastStashCst;

                @Override
                public void visitCode() {
                    super.visitCode();
                    // Here init static final fields.
                    for (ClassStringField field : mStaticFinalFields) {
                        if (!canEncrypted(field.value)) {
                            continue;
                        }
                        String originValue = field.value;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                        super.visitFieldInsn(Opcodes.PUTSTATIC, mClassName, field.name,
                                ClassStringField.STRING_DESC);
                    }
                }

                @Override
                public void visitLdcInsn(Object cst) {
                    // Here init static or static final fields, but we must check field name int 'visitFieldInsn'
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        lastStashCst = (String) cst;
                        String originValue = lastStashCst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                    } else {
                        lastStashCst = null;
                        super.visitLdcInsn(cst);
                    }
                }

                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (mClassName.equals(owner) && lastStashCst != null) {
                        boolean isContain = false;
                        for (ClassStringField field : mStaticFields) {
                            if (field.name.equals(name)) {
                                isContain = true;
                                break;
                            }
                        }
                        if (!isContain) {
                            for (ClassStringField field : mStaticFinalFields) {
                                if (field.name.equals(name) && field.value == null) {
                                    field.value = lastStashCst;
                                    break;
                                }
                            }
                        }
                    }
                    lastStashCst = null;
                    super.visitFieldInsn(opcode, owner, name, desc);
                }
            };

        } else if ("<init>".equals(name)) {
            // Here init final(not static) and normal fields
            mv = new MethodVisitor(Opcodes.ASM5, mv) {
                @Override
                public void visitLdcInsn(Object cst) {
                    // We don't care about whether the field is final or normal
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        String originValue = (String) cst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                    } else {
                        super.visitLdcInsn(cst);
                    }
                }
            };
        } else {
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                @Override
                public void visitLdcInsn(Object cst) {
                    if (cst != null && cst instanceof String && canEncrypted((String) cst)) {
                        // If the value is a static final field
                        for (ClassStringField field : mStaticFinalFields) {
                            if (cst.equals(field.value)) {
                                super.visitFieldInsn(Opcodes.GETSTATIC, mClassName, field.name,
                                        ClassStringField.STRING_DESC);
                                return;
                            }
                        }
                        // If the value is a final field (not static)
                        for (ClassStringField field : mFinalFields) {
                            // if the value of a final field is null, we ignore it
                            if (cst.equals(field.value)) {
                                super.visitVarInsn(Opcodes.ALOAD, 0);
                                super.visitFieldInsn(Opcodes.GETFIELD, mClassName, field.name,
                                        "Ljava/lang/String;");
                                return;
                            }
                        }
                        // local variables
                        String originValue = (String) cst;
                        String encryptValue = mStringFogImpl.encrypt(originValue, mKey);
                        mMappingPrinter.output(getJavaClassName(), originValue, encryptValue);
                        super.visitLdcInsn(encryptValue);
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, mFogClassName, "decrypt",
                                "(Ljava/lang/String;)Ljava/lang/String;", false);
                        return;
                    }
                    super.visitLdcInsn(cst);
                }

            };
        }
    }
    return mv;
}

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java

License:Open Source License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    switch (opcode) {
    case Opcodes.GETSTATIC:
        pushDescriptor(desc);/*from   w ww.  j  av  a  2s .  com*/
        break;
    case Opcodes.PUTSTATIC:
        popDescriptor(desc);
        break;
    case Opcodes.GETFIELD:
        pop();
        pushDescriptor(desc);
        break;
    case Opcodes.PUTFIELD:
        popDescriptor(desc);
        pop();
        break;
    default:
        throw new RuntimeException(
                "Unhandled opcode " + opcode + ", owner=" + owner + ", name=" + name + ", desc" + desc);
    }
    super.visitFieldInsn(opcode, owner, name, desc);
}

From source file:com.google.devtools.build.wireless.testing.java.injector.WhiteBoxMethodAdapter.java

License:Apache License

/**
 * Injects visit putfield and putstatic instructions by logging the new value
 * which is stored into the field./* w  w  w .j  a v a2 s .  c  o m*/
 */
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    boolean instrument = false;

    Matcher fieldMatcher = fieldInclusionPattern.matcher(name);
    boolean matchField = fieldMatcher.find();

    if (matchOwnerClass && matchField && (isField(opcode) || isStaticField(opcode))) {
        instrument = true;
        logger.info("\t[SetField]: " + owner + "." + name + " desc " + desc);
    }

    if (instrument && isField(opcode)) {
        // duplicate and store the value
        stackServant.store(desc, accessedFieldValueIndexInStack);
        stackServant.duplicateAndStore("L" + owner + ";", accessedFieldOwnerIndexInStack);
        stackServant.load(desc, accessedFieldValueIndexInStack);
    }

    mv.visitFieldInsn(opcode, owner, name, desc);

    if (instrument) {
        printServant.startPrinting();
        printServant.printString("[WhiteBox][Set]\t");

        if (isField(opcode)) {
            stackServant.load("L" + owner + ";", accessedFieldOwnerIndexInStack);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, "toString",
                    "()L" + ClassNames.JAVA_LANG_STRING + ";");
            printServant.print("L" + ClassNames.JAVA_LANG_STRING + ";");
        } else {
            printServant.printString(owner);
        }

        printServant.printString("." + name + " Invoked=");

        timeServant.loadCurrentTimeMillis();
        printServant.print("J");

        // add value type to output string
        printServant.printString(" value=");

        // if the field is not static we must retrieve a pointer to the owner
        int code = -1;
        if (isField(opcode)) {
            stackServant.load("L" + owner + ";", accessedFieldOwnerIndexInStack);
            code = Opcodes.GETFIELD;
        } else {
            code = Opcodes.GETSTATIC;
        }
        mv.visitFieldInsn(code, owner, name, desc);
        printServant.println(desc);

        // save and print
        printServant.stopPrinting();
    }
}

From source file:com.google.singletondetector.visitors.SingletonUsageMethodVisitor.java

License:Apache License

@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
    if ((opcode == Opcodes.GETSTATIC || opcode == Opcodes.PUTSTATIC) && desc.startsWith("L")) {
        sd.fieldInstruction(owner);//  ww w.j  a  v a 2 s.  c  om
    }
}

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitFieldInsn(final int opcode, String owner, final String name, final String desc) {
    owner = namer.nameClass(owner);/*from w w  w  .j  av  a  2s .  co  m*/
    switch (opcode) {
    case Opcodes.PUTSTATIC:
        recorder.add(new PutFieldRunnable(repository, owner, name, desc, true));
        break;
    case Opcodes.PUTFIELD:
        recorder.add(new PutFieldRunnable(repository, owner, name, desc, false));
        break;
    case Opcodes.GETSTATIC:
        recorder.add(new GetFieldRunnable(repository, owner, name, desc, true));
        break;
    case Opcodes.GETFIELD:
        recorder.add(new GetFieldRunnable(repository, owner, name, desc, false));
        break;
    }
}

From source file:com.googlecode.ddom.weaver.inject.SingletonInjector.java

License:Apache License

public void generateFactoryMethodCode(MethodVisitor mv) {
    mv.visitCode();//from ww w. j  a  v  a 2 s. com
    mv.visitFieldInsn(Opcodes.GETSTATIC, singletonClassName, "INSTANCE", fieldDesc);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(1, 0);
    mv.visitEnd();
}

From source file:com.heliosdecompiler.appifier.Appifier.java

License:Apache License

private static byte[] transform(byte[] classBytes) {
    ClassReader reader = new ClassReader(classBytes);
    ClassWriter writer = new ClassWriter(Opcodes.ASM5);

    reader.accept(new ClassVisitor(Opcodes.ASM5, writer) {
        @Override/*from w w w .j a  v a2 s . c o  m*/
        public MethodVisitor visitMethod(int i, String s, String s1, String s2, String[] strings) {
            return new MethodVisitor(Opcodes.ASM5, writer.visitMethod(i, s, s1, s2, strings)) {
                @Override
                public void visitFieldInsn(int opcode, String owner, String name, String desc) {
                    if (opcode == Opcodes.GETSTATIC) {
                        if (owner.equals("java/lang/System")) {
                            if (desc.equals("Ljava/io/PrintStream;")) {
                                if (name.equals("out")) {
                                    super.visitFieldInsn(Opcodes.GETSTATIC,
                                            "com/heliosdecompiler/appifier/SystemHook", "out",
                                            "Ljava/lang/ThreadLocal;");
                                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "get",
                                            "()Ljava/lang/Object;", false);
                                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/io/PrintStream");
                                    return;
                                } else if (name.equals("err")) {
                                    super.visitFieldInsn(Opcodes.GETSTATIC,
                                            "com/heliosdecompiler/appifier/SystemHook", "err",
                                            "Ljava/lang/ThreadLocal;");
                                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ThreadLocal", "get",
                                            "()Ljava/lang/Object;", false);
                                    super.visitTypeInsn(Opcodes.CHECKCAST, "java/io/PrintStream");
                                    return;
                                }
                            }
                        }
                    }
                    super.visitFieldInsn(opcode, owner, name, desc);
                }
            };
        }
    }, 0);

    return writer.toByteArray();
}

From source file:com.khubla.jvmbasic.jvmbasicc.function.impl.rule.inputstmtRule.java

License:Open Source License

@Override
public boolean execute(GenerationContext generationContext) throws Exception {
    try {//from  www  . j  ava  2 s .co  m
        /*
         * process the contained nodes. This will push the String prompt onto the stack, and a variable name
         */
        Dispatcher.dispatchChildren(generationContext);
        /*
         * input with a prompt? INPUT <prompt> , <variable>
         */
        if (generationContext.getParseTree().getChildCount() == 4) {
            /*
             * swap the stack arguments so we have the prompt on top and the variable name next
             */
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "swap", "()V");
            /*
             * print the prompt
             */
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out",
                    "Ljava/io/PrintStream;");
            generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
            generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD,
                    generationContext.getClassName(), RTLHelper.EXECUTIONCONTEXT_NAME,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop",
                    "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                    "com/khubla/jvmbasic/jvmbasicrt/Value", "getAsString", "()Ljava/lang/String;");
            generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream",
                    "print", "(Ljava/lang/String;)V");
        }
        /*
         * read a line and push onto value stack
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
        generationContext.getMethodVisitor().visitInsn(Opcodes.DUP);
        generationContext.getMethodVisitor().visitLdcInsn("\"");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder",
                "<init>", "(Ljava/lang/String;)V");
        generationContext.getMethodVisitor().visitTypeInsn(Opcodes.NEW, "java/io/BufferedReader");
        generationContext.getMethodVisitor().visitInsn(Opcodes.DUP);
        generationContext.getMethodVisitor().visitTypeInsn(Opcodes.NEW, "java/io/InputStreamReader");
        generationContext.getMethodVisitor().visitInsn(Opcodes.DUP);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                "inputStream", "Ljava/io/InputStream;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESPECIAL, "java/io/InputStreamReader",
                "<init>", "(Ljava/io/InputStream;)V");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKESPECIAL, "java/io/BufferedReader",
                "<init>", "(Ljava/io/Reader;)V");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/BufferedReader",
                "readLine", "()Ljava/lang/String;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder",
                "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        generationContext.getMethodVisitor().visitLdcInsn("\"");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder",
                "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder",
                "toString", "()Ljava/lang/String;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "push", "(Ljava/lang/String;)V");
        /*
         * swap the stack arguments so we have the variable name on top and the value below it
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "swap", "()V");
        /*
         * set the variable
         */
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                "com/khubla/jvmbasic/jvmbasicrt/Value", "getString", "()Ljava/lang/String;");
        generationContext.getMethodVisitor().visitVarInsn(Opcodes.ALOAD, 0);
        generationContext.getMethodVisitor().visitFieldInsn(Opcodes.GETFIELD, generationContext.getClassName(),
                RTLHelper.EXECUTIONCONTEXT_NAME, RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT_TYPE);
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "pop", "()Lcom/khubla/jvmbasic/jvmbasicrt/Value;");
        generationContext.getMethodVisitor().visitMethodInsn(Opcodes.INVOKEVIRTUAL,
                RTLHelper.JASIC_RUNTIME_EXECUTIONCONTEXT, "setVariable",
                "(Ljava/lang/String;Lcom/khubla/jvmbasic/jvmbasicrt/Value;)V");
        return true;
    } catch (final Exception e) {
        throw new Exception("Exception in execute", e);
    }
}

From source file:com.khubla.jvmbasic.jvmbasicc.JVMBasicCompiler.java

License:Open Source License

/**
 * generate void main(String[])/*www.ja  v  a  2  s .co  m*/
 * <p>
 * <code>
 * public static void main(String[] args) {
 *   ExampleProgram exampleProgram = new ExampleProgram();
 *  try {
 *       exampleProgram.inputStream = System.in;
 *       exampleProgram.outputStream = System.out;
 *       exampleProgram.program();
 *   } catch (Exception e) {
 *       e.printStackTrace();
 *    }
 *  }
 * </code>
 * </p>
 */
protected void generateMain(String classname, ClassWriter classWriter) throws Exception {
    try {
        /*
         * make method
         */
        final MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,
                "main", "([Ljava/lang/String;)V", null, null);
        methodVisitor.visitCode();
        final Label l0 = new Label();
        final Label l1 = new Label();
        final Label l2 = new Label();
        methodVisitor.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
        final Label l3 = new Label();
        methodVisitor.visitLabel(l3);
        /*
         * declare a local instance of the class in the void() main, store as variable 1.
         */
        methodVisitor.visitTypeInsn(Opcodes.NEW, classname);
        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, classname, "<init>", "()V");
        methodVisitor.visitVarInsn(Opcodes.ASTORE, 1);
        /*
         * assign the input stream
         */
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "in", "Ljava/io/InputStream;");
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "inputStream", "Ljava/io/InputStream;");
        /*
         * assign the output stream
         */
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, classname, "outputStream", "Ljava/io/PrintStream;");
        /*
         * load the class instance from variable 1 and call "program"
         */
        methodVisitor.visitLabel(l0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classname, "program", "()V");
        methodVisitor.visitLabel(l1);
        final Label l4 = new Label();
        methodVisitor.visitJumpInsn(Opcodes.GOTO, l4);
        methodVisitor.visitLabel(l2);
        methodVisitor.visitFrame(Opcodes.F_FULL, 2, new Object[] { "[Ljava/lang/String;", classname }, 1,
                new Object[] { "java/lang/Exception" });
        methodVisitor.visitVarInsn(Opcodes.ASTORE, 2);
        final Label l5 = new Label();
        methodVisitor.visitLabel(l5);
        methodVisitor.visitLineNumber(21, l5);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 2);
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V");
        /*
         * return
         */
        methodVisitor.visitLabel(l4);
        methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        methodVisitor.visitInsn(Opcodes.RETURN);
        /*
         * declare the parameters
         */
        final Label l6 = new Label();
        methodVisitor.visitLabel(l6);
        methodVisitor.visitLocalVariable("args", "[Ljava/lang/String;", null, l3, l6, 0);
        methodVisitor.visitLocalVariable("exampleProgram", "L" + classname + ";", null, l0, l6, 1);
        methodVisitor.visitLocalVariable("e", "Ljava/lang/Exception;", null, l5, l4, 2);
        /*
         * done
         */
        methodVisitor.visitMaxs(2, 3);
        methodVisitor.visitEnd();
    } catch (final Exception e) {
        throw new Exception("Exception in generateMain", e);
    }
}

From source file:com.mebigfatguy.exagent.StackTraceMethodVisitor.java

License:Apache License

private void injectCallStackPopulation() {

    // ExAgent.METHOD_INFO.get();
    super.visitFieldInsn(Opcodes.GETSTATIC, EXASUPPORT_CLASS_NAME, "METHOD_INFO",
            signaturizeClass(THREADLOCAL_CLASS_NAME));
    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, THREADLOCAL_CLASS_NAME, "get", "()Ljava/lang/Object;", false);
    super.visitTypeInsn(Opcodes.CHECKCAST, LIST_CLASS_NAME);

    super.visitInsn(Opcodes.DUP);
    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, LIST_CLASS_NAME, "size", "()I", true);
    super.visitVarInsn(Opcodes.ISTORE, depthLocalSlot);

    //new MethodInfo(cls, name, parmMap);
    super.visitTypeInsn(Opcodes.NEW, METHODINFO_CLASS_NAME);
    super.visitInsn(Opcodes.DUP);
    super.visitLdcInsn(clsName.replace('.', '/'));
    super.visitLdcInsn(methodName);

    if (parms.isEmpty()) {
        super.visitMethodInsn(Opcodes.INVOKESTATIC, COLLECTIONS_CLASS_NAME, "emptyList", "()Ljava/util/List;",
                false);/*from  w ww . ja v a2  s  .  c  o  m*/
    } else {
        super.visitTypeInsn(Opcodes.NEW, ARRAYLIST_CLASS_NAME);
        super.visitInsn(Opcodes.DUP);
        super.visitIntInsn(Opcodes.BIPUSH, parms.size());
        super.visitMethodInsn(Opcodes.INVOKESPECIAL, ARRAYLIST_CLASS_NAME, CTOR_NAME, "(I)V", false);

        for (Parm parm : parms) {
            super.visitInsn(Opcodes.DUP);

            switch (parm.signature) {

            case "C":
                super.visitVarInsn(Opcodes.ILOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(C)Ljava/lang/String;", false);
                break;

            case "Z":
                super.visitVarInsn(Opcodes.ILOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(Z)Ljava/lang/String;", false);
                break;

            case "B":
            case "S":
            case "I":
                super.visitVarInsn(Opcodes.ILOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(I)Ljava/lang/String;", false);
                break;

            case "J":
                super.visitVarInsn(Opcodes.LLOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(J)Ljava/lang/String;", false);
                break;

            case "F":
                super.visitVarInsn(Opcodes.FLOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(F)Ljava/lang/String;", false);
                break;

            case "D":
                super.visitVarInsn(Opcodes.DLOAD, parm.register);
                super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                        "(D)Ljava/lang/String;", false);
                break;

            default:
                super.visitVarInsn(Opcodes.ALOAD, parm.register);
                if (parm.signature.startsWith("[")) {
                    char arrayElemTypeChar = parm.signature.charAt(1);
                    if ((arrayElemTypeChar == 'L') || (arrayElemTypeChar == '[')) {
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, ARRAYS_CLASS_NAME, "toString",
                                "([Ljava/lang/Object;)Ljava/lang/String;", false);
                    } else {
                        super.visitMethodInsn(Opcodes.INVOKESTATIC, ARRAYS_CLASS_NAME, "toString",
                                "([" + arrayElemTypeChar + ")Ljava/lang/String;", false);
                    }
                } else {
                    super.visitMethodInsn(Opcodes.INVOKESTATIC, STRING_CLASS_NAME, "valueOf",
                            "(Ljava/lang/Object;)Ljava/lang/String;", false);
                }
                break;
            }

            if (maxParmSize > 0) {
                super.visitInsn(Opcodes.DUP);
                super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_CLASS_NAME, "length", "()I", false);
                if (maxParmSize <= 127) {
                    super.visitIntInsn(Opcodes.BIPUSH, maxParmSize);
                } else {
                    super.visitLdcInsn(maxParmSize);
                }
                Label falseLabel = new Label();
                super.visitJumpInsn(Opcodes.IF_ICMPLE, falseLabel);
                super.visitIntInsn(Opcodes.BIPUSH, 0);
                super.visitLdcInsn(maxParmSize);
                super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STRING_CLASS_NAME, "substring",
                        "(II)Ljava/lang/String;", false);
                super.visitLabel(falseLabel);
            }

            super.visitMethodInsn(Opcodes.INVOKEINTERFACE, LIST_CLASS_NAME, "add", "(Ljava/lang/Object;)Z",
                    true);
            super.visitInsn(Opcodes.POP);
        }
    }

    super.visitMethodInsn(Opcodes.INVOKESPECIAL, METHODINFO_CLASS_NAME, CTOR_NAME,
            "(Ljava/lang/String;Ljava/lang/String;Ljava/util/List;)V", false);

    //add(methodInfo);
    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, LIST_CLASS_NAME, "add", "(Ljava/lang/Object;)Z", true);
    super.visitInsn(Opcodes.POP);
}