Example usage for org.objectweb.asm Opcodes DUP

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

Introduction

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

Prototype

int DUP

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

Click Source Link

Usage

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

License:Apache License

private void insnMachine(AbstractInsnNode node, List<AbstractInsnNode> removeList) {
    if (node.getOpcode() == Opcodes.NEW) {
        if (status == INIT) {
            status = FIND_NEW;//  ww w.j av a 2s . c  o  m
            removeList.add(node);
        } else {
            throw new RuntimeException("find new in status " + status);
        }
    }

    if (node.getOpcode() == Opcodes.DUP) {
        if (status == FIND_NEW) {
            status = INIT;
            removeList.add(node);
        }
    }

    if (node.getOpcode() == Opcodes.INVOKESPECIAL && node instanceof MethodInsnNode) {
        if (status == INIT) {
            MethodInsnNode methodInsnNode = (MethodInsnNode) node;
            if ("<init>".equals(methodInsnNode.name)) {
                if (getMethodAccessRight(methodInsnNode.owner, methodInsnNode.name,
                        methodInsnNode.desc) == AccessRight.PUBLIC) {
                    int removeSize = removeList.size();
                    removeList.remove(removeSize - 1);
                    removeList.remove(removeSize - 2);
                }
            }

        }
    }

}

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.");
    }/*from  ww w .  ja v a2s. 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.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

void dup(final InsnList insnList) {
    insnList.add(new InsnNode(Opcodes.DUP));
}

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.
 * /*w ww  .  j  ava  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",
            "(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 for serializing a bean property which returns a collection of opaque objects.
 * /*from w  w w. jav  a2  s. c  o  m*/
 * @param visitor method visitor used for creating bytecode
 * @param returnType return type of get method in bean
 * @param intBeanClazzName binary name of bean
 * @param methodName binary name of get method in bean
 */
private void createObjectCollectionSerializationCode(MethodVisitor visitor, Class<?> returnType,
        String intBeanClazzName, String methodName, String propertyName) {
    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/JsonObjectCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/JsonObjectCollectionSerializer",
            "<init>", "()V");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonObjectCollectionSerializer",
            "serialize", "(Ljava/util/Collection;)Lorg/json/JSONArray;");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitLdcInsn(propertyName);
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/json/JSONObject", "put",
            "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;");
}

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

License:Apache License

/**
 * Creates bytecode for serializing a bean property which returns a collection of beans that are serializable. Bean serializability
 * is determined as described in the class description.
 * /*from   w ww .  ja v  a 2s .c o m*/
 * @param visitor method visitor used for creating bytecode
 * @param intBeanClazzName binary class name of bean
 * @param methodName binary name of get method in bean returning collection
 * @param returnType return type of get method in bean
 * @param collectionBeanType class of serializable bean found in collection
 */
private void createBeanCollectionSerializationCode(MethodVisitor visitor, String intBeanClazzName,
        String methodName, String propertyName, Class<?> returnType, Class<?> collectionBeanType) {
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonSerializer",
            "getSerializerFactory", "()Lcom/nginious/http/serialize/SerializerFactoryImpl;");
    visitor.visitLdcInsn(collectionBeanType.getName());
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;)Ljava/lang/Class;");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/SerializerFactoryImpl",
            "createJsonSerializer", "(Ljava/lang/Class;)Lcom/nginious/http/serialize/JsonSerializer;");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/JsonBeanCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/JsonBeanCollectionSerializer",
            "<init>", "(Lcom/nginious/http/serialize/JsonSerializer;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/JsonBeanCollectionSerializer",
            "serialize", "(Ljava/util/Collection;)Lorg/json/JSONArray;");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitLdcInsn(propertyName);
    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/json/JSONObject", "put",
            "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;");
}

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.
 * //from   w  ww .  j a  v a2 s  . c om
 * @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.XmlDeserializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link XmlDeserializer#deserialize(java.util.HashMap)} method for
 * the deserializer class being created.
 * //w w w .java 2s .co 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/http/serialize/SerializerException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PUBLIC, "deserialize",
            "(Ljava/util/HashMap;)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.XmlSerializerCreator.java

License:Apache License

/**
 * Creates bytecode for serializing a bean property which returns a collection of opaque objects.
 * /*  w ww  . j a v  a2 s.c o  m*/
 * @param visitor method visitor used for creating bytecode
 * @param intBeanClazzName binary name of bean
 * @param methodName binary name of get method in bean
 * @param returnType return type of get method in bean
 */
private void createObjectCollectionSerializationCode(MethodVisitor visitor, String intBeanClazzName,
        String methodName, String propertyName, Class<?> returnType) {
    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/XmlObjectCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitLdcInsn(propertyName);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/XmlObjectCollectionSerializer",
            "<init>", "(Ljava/lang/String;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");

    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/XmlObjectCollectionSerializer",
            "serialize", "(Ljavax/xml/transform/sax/TransformerHandler;Ljava/util/Collection;)V");
}

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

License:Apache License

/**
 * Creates bytecode for serializing a bean property which returns a collection of beans that are serializable. Bean serializability
 * is determined as described in the class description.
 * /*from   w  ww .ja v  a 2  s.  c o  m*/
 * @param visitor method visitor used for creating bytecode
 * @param intBeanClazzName binary class name of bean
 * @param methodName binary name of get method in bean returning collection
 * @param returnType return type of get method in bean
 * @param collectionBeanType class of serializable bean found in collection
 */
private void createBeanCollectionSerializationCode(MethodVisitor visitor, String intBeanClazzName,
        String methodName, String propertyName, Class<?> returnType, Class<?> collectionBeanType) {
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/XmlSerializer",
            "getSerializerFactory", "()Lcom/nginious/http/serialize/SerializerFactoryImpl;");
    visitor.visitLdcInsn(collectionBeanType.getName());
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;)Ljava/lang/Class;");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/SerializerFactoryImpl",
            "createXmlSerializer", "(Ljava/lang/Class;)Lcom/nginious/http/serialize/XmlSerializer;");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/serialize/XmlBeanCollectionSerializer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitLdcInsn(propertyName);
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/serialize/XmlBeanCollectionSerializer",
            "<init>", "(Ljava/lang/String;Lcom/nginious/http/serialize/XmlSerializer;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);

    String intReturnClazzName = returnType.getName().replace('.', '/');
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intBeanClazzName, methodName,
            "()L" + intReturnClazzName + ";");

    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/nginious/http/serialize/XmlBeanCollectionSerializer",
            "serialize", "(Ljavax/xml/transform/sax/TransformerHandler;Ljava/util/Collection;)V");
}