Example usage for org.objectweb.asm Opcodes NEW

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

Introduction

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

Prototype

int NEW

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

Click Source Link

Usage

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodVariables.java

License:Apache License

void newInstance(final InsnList instructions, final Type type) {
    instructions.add(new TypeInsnNode(Opcodes.NEW, type.getInternalName()));
}

From source file:com.netease.hearttouch.hthotfix.refclass.RefMethodVisitor.java

License:MIT License

@Override
public void visitTypeInsn(int opcode, String type) {
    super.visitTypeInsn(opcode, type);

    //new-bb-187/*from w w w.  j a v  a 2s . c  o m*/
    //create new object of type identified by class reference in constant pool index
    //???,????
    if (opcode == Opcodes.NEW) {
        context.checkInPatchClasses(type);
    }
}

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  w w.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.j ava 2 s. co 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  www .  ja v  a  2 s . com*/
 * @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.
 * /* w ww .  jav a2 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.XmlDeserializerCreator.java

License:Apache License

/**
 * Creates bytecode which implements the {@link XmlDeserializer#deserialize(java.util.HashMap)} method for
 * the deserializer class being created.
 * // ww w.  ja  va2  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/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.
 * /*from  w w w . ja  va2  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  w  w . j  a v a2s. 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");
}

From source file:com.nginious.http.xsp.DocumentPart.java

License:Apache License

/**
 * Creates bytecode for this document tag part using the specified class writer and method visitor.
 * // w w w  .  ja  va 2  s . c o  m
 * @param intClassName the binary class name of the class being created
 * @param writer the class writer
 * @param visitor the method visitor
 * @throws XspException if unable to create bytecode
 */
void compile(String intClassName, ClassWriter writer, MethodVisitor visitor) throws XspException {
    // 0 = this, 1 = HttpRequest, 2 = HttpResponse, 3 = StringBuffer

    // StringBuffer buffer = new StringBuffer();
    visitor.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuffer");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "()V");
    visitor.visitVarInsn(Opcodes.ASTORE, 3);

    for (XspPart part : contentParts) {
        part.compile(intClassName, writer, visitor);
    }

    // response.setContentLength(buffer.length());
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "length", "()I");
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "setContentLength",
            "(I)V");

    // response.addHeader(contentType);
    String contentType = getMetaContent("Content-Type");

    if (contentType == null) {
        contentType = "application/unknown";
    }

    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitLdcInsn("Content-Type");
    visitor.visitLdcInsn(contentType);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "addHeader",
            "(Ljava/lang/String;Ljava/lang/String;)V");

    // PrintWriter writer = response.getWriter();
    // writer.print(buffer.toString());
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "getWriter",
            "()Ljava/io/PrintWriter;");
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "toString",
            "()Ljava/lang/String;");
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintWriter", "print", "(Ljava/lang/String;)V");
}