Example usage for org.objectweb.asm MethodVisitor visitCode

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

Introduction

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

Prototype

public void visitCode() 

Source Link

Document

Starts the visit of the method's code, if any (i.e.

Usage

From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java

License:Apache License

/**
 * Turns this class into an override class that can be loaded by our custom class loader:
 * <ul>/*from ww w  .  java 2  s  .  co m*/
 * <li>Make the class name be OriginalName$override</li>
 * <li>Ensure the class derives from java.lang.Object, no other inheritance</li>
 * <li>Ensure the class has a public parameterless constructor that is a noop.</li>
 * </ul>
 */
@Override
public void visit(int version, int access, String name, String signature, String superName,
        String[] interfaces) {
    super.visit(version, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name + OVERRIDE_SUFFIX, signature,
            "java/lang/Object", new String[] { CHANGE_TYPE.getInternalName() });

    visitedClassName = name;
    visitedSuperName = superName;
    instanceToStaticDescPrefix = "(L" + visitedClassName + ";";

    // Create empty constructor
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();

    //        super.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_STATIC,
    //                "$obsolete", "Z", null, null);
}

From source file:com.mogujie.instantrun.IncrementalChangeVisitor.java

License:Apache License

public void addSupportMethod() {
    int access = Opcodes.ACC_PUBLIC;
    Method m = new Method("isSupport", "(I)Z");
    MethodVisitor mv = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null);

    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 1);/*from   ww  w . j a va  2  s .  co  m*/
    //        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);

    int[] hashArray = new int[fixMtds.size()];
    Label[] labelArray = new Label[fixMtds.size()];
    Label l0 = new Label();
    Label l1 = new Label();
    for (int i = 0; i < fixMtds.size(); i++) {
        hashArray[i] = AcesoProguardMap.instance().getClassData(visitedClassName).getMtdIndex(fixMtds.get(i));
        labelArray[i] = l0;
    }

    mv.visitLookupSwitchInsn(l1, hashArray, labelArray);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitLabel(l1);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 2);
    mv.visitEnd();

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

}

From source file:com.mogujie.instantrun.IncrementalTool.java

License:Apache License

public static byte[] getPatchFileContents(ImmutableList<String> patchFileContents,
        ImmutableList<Integer> patchIndexContents) {
    if (patchFileContents.size() != patchIndexContents.size()) {
        throw new GradleException("patchFileContents's size is " + patchFileContents.size()
                + ", but patchIndexContents's size is " + patchIndexContents.size()
                + ", please check the changed classes.");
    }// w w w  . j ava 2  s .  c  o m
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, IncrementalVisitor.APP_PATCHES_LOADER_IMPL,
            null, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, "<init>",
                "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClasses", "()[Ljava/lang/String;", null, null);
        mv.visitCode();

        mv.visitIntInsn(Opcodes.SIPUSH, patchFileContents.size());
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        for (int index = 0; index < patchFileContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.SIPUSH, index);
            mv.visitLdcInsn(patchFileContents.get(index));
            mv.visitInsn(Opcodes.AASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClassIndexes", "()[I", null, null);
        mv.visitCode();

        mv.visitIntInsn(Opcodes.SIPUSH, patchIndexContents.size());
        mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
        for (int index = 0; index < patchIndexContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.SIPUSH, index);
            mv.visitLdcInsn(patchIndexContents.get(index));
            mv.visitInsn(Opcodes.IASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();

}

From source file:com.mogujie.instantrun.SuperHelperVisitor.java

License:Apache License

public void start() {
    visit(Opcodes.V1_7, ACC_PUBLIC + ACC_SUPER, visitor.visitedClassName + "$helper", null,
            visitor.visitedSuperName, null);
    for (int nodeIndex = 0; nodeIndex < superNode.methods.size(); nodeIndex++) {
        MethodNode methodNode = (MethodNode) superNode.methods.get(nodeIndex);
        if ("<init>".equals(methodNode.name)) {
            String[] exceptions = null;
            if (methodNode.exceptions != null) {
                exceptions = (String[]) methodNode.exceptions.toArray(new String[0]);
            }/*from w ww .j  a v a  2s  .c o  m*/
            MethodVisitor mv = visitMethod(ACC_PUBLIC, methodNode.name, methodNode.desc, methodNode.signature,
                    exceptions);
            mv.visitCode();
            Type[] args = Type.getArgumentTypes(methodNode.desc);
            List<LocalVariable> variables = ByteCodeUtils.toLocalVariables(Arrays.asList(args));
            mv.visitVarInsn(ALOAD, 0);
            int local = 1;
            for (int i = 0; i < variables.size(); i++) {
                mv.visitVarInsn(variables.get(i).type.getOpcode(Opcodes.ILOAD), variables.get(i).var + 1);
                local = variables.get(i).var + 1 + variables.get(i).type.getSize();
            }
            mv.visitMethodInsn(INVOKESPECIAL, superNode.name, methodNode.name, methodNode.desc, false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(local, local);
            mv.visitEnd();
        }
    }

    for (InstantMethod method : visitor.superMethods) {
        MethodVisitor mv = visitMethod(ACC_PUBLIC + ACC_STATIC, method.getName(), method.getDescriptor(), null,
                null);
        mv.visitCode();
        Type[] args = Type.getArgumentTypes(method.getDescriptor());

        List<LocalVariable> variables = ByteCodeUtils.toLocalVariables(Arrays.asList(args));
        int totSize = 1;
        for (LocalVariable variable : variables) {

            mv.visitVarInsn(variable.type.getOpcode(Opcodes.ILOAD), variable.var);
            totSize = variable.var;
        }

        mv.visitMethodInsn(INVOKESPECIAL, method.getOwner(), method.getName(), method.getOriDesc(), false);

        Type returnType = Type.getReturnType(method.getDescriptor());
        mv.visitInsn(returnType.getOpcode(Opcodes.IRETURN));
        mv.visitMaxs(totSize + 1, totSize + 1);
        mv.visitEnd();
    }

    visitEnd();
}

From source file:com.mto.asm.helloworld.ASMCodeGenerator.java

License:Open Source License

/**
 * Generate the byte code of a simple HelloWorld program
 *
 * public class HelloWorld//from   w w w .  j a va 2 s. com
 * {
 *    public void sayHello()
 *    {
 *       System.out.println("Hello World");
 *    }
 * }
 * @return
 */
public byte[] generateHelloWorld() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "com/mto/asm/helloworld/HelloWorld", null, "java/lang/Object", null);

    cw.visitSource("HelloWorld.java", null);

    mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(25, l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", "Lcom/mto/asm/helloworld/HelloWorld;", null, l0, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    mv = cw.visitMethod(ACC_PUBLIC, "sayHello", "()V", null, null);
    mv.visitCode();
    l0 = new Label();
    mv.visitLabel(l0);
    mv.visitLineNumber(29, l0);
    mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn("Hello World");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLineNumber(30, l1);
    mv.visitInsn(RETURN);
    Label l2 = new Label();
    mv.visitLabel(l2);
    mv.visitLocalVariable("this", "Lcom/mto/asm/helloworld/HelloWorld;", null, l0, l2, 0);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:com.mulberry.athena.asm.ASMTest.java

License:Open Source License

@Test
public void generateClass() throws Exception {
    ClassWriter classWriter = new ClassWriter(0);
    ClassVisitor cv = new TraceClassVisitor(classWriter, new PrintWriter(System.out));
    //FieldVisitor fv;
    MethodVisitor mv;
    //AnnotationVisitor av0;

    cv.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Testing", null, "java/lang/Object", null);
    cv.visitSource("Testing.java", null);
    {/*from  w  w  w .  j  a v a2 s  .c  om*/
        mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(1, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "LTesting;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(3, l0);
        mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("Works!");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(4, l1);
        mv.visitInsn(RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2, 0);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
    }
    cv.visitEnd();

    Class<?> clazz = new DynamicClassLoader().defineClass("Testing", classWriter.toByteArray());
    java.lang.reflect.Method method = clazz.getMethod("main", String[].class);
    final String[] arguments = new String[] { "hello", "world" };
    method.invoke(clazz, (Object) arguments);
}

From source file:com.nginious.http.serialize.JsonDeserializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link JsonDeserializer#deserialize(org.json.JSONObject)} method for
 * the deserializer class being created.
 * //from   w  w  w . java 2 s .  com
 * @param writer class byte code writer
 * @param intBeanClazzName name of deserializer class being generated
 * @return a method visitor for writing bytecode inside the generated method
 */
private MethodVisitor createDeserializeMethod(ClassWriter writer, String intBeanClazzName) {
    String[] exceptions = { "com/nginious/serialize/SerializerException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize",
            "(Lorg/json/JSONObject;)Ljava/lang/Object;", null, exceptions);
    visitor.visitCode();

    visitor.visitTypeInsn(Opcodes.NEW, intBeanClazzName);
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, intBeanClazzName, "<init>", "()V");
    visitor.visitVarInsn(Opcodes.ASTORE, 3);
    return visitor;
}

From source file:com.nginious.http.serialize.JsonSerializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link JsonSerializer#serializeProperties(org.json.JSONObject, Object)}
 * method for the serializer class being created.
 * /* ww w  .  j  a  v a 2 s.  c  om*/
 * @param writer class byte code writer
 * @param intBeanClazzName binary name of serializer class being generated
 * @return a method visitor for writing bytecode inside the generated method
 */
private MethodVisitor createSerializeMethod(ClassWriter writer, String intBeanClazzName) {
    String[] exceptions = { "com/nginious/serialize/SerializerException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "serializeProperties",
            "(Lorg/json/JSONObject;Ljava/lang/Object;)V", null, exceptions);
    visitor.visitCode();

    Label label = new Label();
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitJumpInsn(Opcodes.IFNONNULL, label);
    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitLabel(label);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitTypeInsn(Opcodes.CHECKCAST, intBeanClazzName);
    visitor.visitIntInsn(Opcodes.ASTORE, 3);

    return visitor;
}

From source file:com.nginious.http.serialize.QueryDeserializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link QueryDeserializer#deserialize(se.netdigital.http.HttpRequest)} method for
 * the deserializer class being created.
 * //w  w  w.java 2  s  . c  o  m
 * @param writer class byte code writer
 * @param intBeanClazzName name of deserializer class being generated
 * @return a method visitor for writing bytecode inside the generated method
 */
private MethodVisitor createDeserializeMethod(ClassWriter writer, String intBeanClazzName) {
    String[] exceptions = { "com/nginious/serialize/SerializerException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize",
            "(Lcom/nginious/http/HttpRequest;)Ljava/lang/Object;", null, exceptions);
    visitor.visitCode();

    visitor.visitTypeInsn(Opcodes.NEW, intBeanClazzName);
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, intBeanClazzName, "<init>", "()V");
    visitor.visitVarInsn(Opcodes.ASTORE, 3);
    return visitor;
}

From source file:com.nginious.http.serialize.Serialization.java

License:Apache License

/**
 * Creates bytecode for a no argument constructor. The created bytecode also calls the no argument constructor
 * in the superclass with the specified name.
 * /*from  www  .ja  va 2s .  com*/
 * @param writer bytecode writer
 * @param superclassName binary name of superclass
 */
static void createConstructor(ClassWriter writer, String superclassName) {
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    visitor.visitCode();
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassName, "<init>", "()V");
    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitMaxs(1, 1);
    visitor.visitEnd();
}