Example usage for org.objectweb.asm MethodVisitor visitInsn

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

Introduction

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

Prototype

public void visitInsn(final int opcode) 

Source Link

Document

Visits a zero operand instruction.

Usage

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 . j  ava2s  . 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.
 * /*  w ww .j a  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");
}

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

License:Apache License

/**
 * Creates bytecode which implements the {@link XmlSerializer#serializeProperties(javax.xml.transform.sax.TransformerHandler, Object)}
 * method for the serializer class being created.
 * /*from w  w w .ja va 2 s  .c o m*/
 * @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",
            "(Ljavax/xml/transform/sax/TransformerHandler;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, 0);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitTypeInsn(Opcodes.CHECKCAST, intBeanClazzName);
    visitor.visitIntInsn(Opcodes.ASTORE, 3);
    return visitor;
}

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.
 * /*from w  w  w  . java 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");
}

From source file:com.nginious.http.xsp.expr.AddOperator.java

License:Apache License

/**
 * Creates bytecode for adding the two values. The bytecode is generated
 * using the specified method visitor.//from w ww . ja v a  2 s  . c  o m
 * 
 * @param visitor the method visitor for generating bytecode
 * @param type the type to when generating bytecode for adding the values
 */
void compile(MethodVisitor visitor, Type type) {
    type = resolveType(this.value1, this.value2);
    value1.compile(visitor, type);
    value2.compile(visitor, type);

    if (type == Type.DOUBLE) {
        visitor.visitInsn(Opcodes.DADD);
    } else {
        visitor.visitInsn(Opcodes.IADD);
    }
}

From source file:com.nginious.http.xsp.expr.AndOperator.java

License:Apache License

/**
 * Creates bytecode for evaluating this and operator. The bytecode is generated using
 * the specified method visitor. The result of the evaluation produces the specified
 * type./*from   ww w .  jav a 2s  . c  om*/
 * 
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    Label labelFalse = new Label();
    Label labelEnd = new Label();

    // Test first value
    value1.compile(visitor, type);
    visitor.visitInsn(Opcodes.ICONST_1);
    visitor.visitJumpInsn(Opcodes.IF_ICMPNE, labelFalse);

    // Test second value
    value2.compile(visitor, type);
    visitor.visitInsn(Opcodes.ICONST_1);
    visitor.visitJumpInsn(Opcodes.IF_ICMPNE, labelFalse);

    // True
    visitor.visitLdcInsn(true);
    visitor.visitJumpInsn(Opcodes.GOTO, labelEnd);

    // False
    visitor.visitLabel(labelFalse);
    visitor.visitLdcInsn(false);

    visitor.visitLabel(labelEnd);
}

From source file:com.nginious.http.xsp.expr.AttributeValue.java

License:Apache License

/**
 * Creates bytecode for evaluating this attribute value. The bytecode is created using the
 * specified method visitor and creates a result of specified type.
 * /*from   w  ww. j av  a 2 s  . co m*/
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    visitor.visitLdcInsn(this.name);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "getVariable",
            "(Ljava/lang/String;)Ljava/lang/Object;");

    if (type != Type.ANY) {
        visitor.visitInsn(Opcodes.DUP);
    }

    if (type == Type.STRING) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitInsn(Opcodes.ACONST_NULL);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.INT) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.DOUBLE) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble",
                "(Ljava/lang/String;)D");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0.0d);

        visitor.visitLabel(notNullLabel);
    }
}

From source file:com.nginious.http.xsp.expr.BeanValue.java

License:Apache License

/**
 * Creates bytecode for evaluating this bean value. The bytecode is generated using the
 * specified method visitor and produces a value of the specified type.
 * //  ww w.j  ava 2  s  .c o  m
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    visitor.visitLdcInsn(this.beanName);
    visitor.visitLdcInsn(this.propertyName);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/BeanValue", "getValue",
            "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;");

    if (type != Type.ANY) {
        visitor.visitInsn(Opcodes.DUP);
    }

    // visitor.visitJumpInsn(Opcodes.IFNONNULL, null);

    if (type == Type.DOUBLE) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "parseDouble",
                "(Ljava/lang/String;)D");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0.0d);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.INT) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitLdcInsn(0);

        visitor.visitLabel(notNullLabel);
    } else if (type == Type.STRING) {
        Label nullLabel = new Label();
        Label notNullLabel = new Label();

        visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;");
        visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

        visitor.visitLabel(nullLabel);
        visitor.visitInsn(Opcodes.POP);
        visitor.visitInsn(Opcodes.ACONST_NULL);

        visitor.visitLabel(notNullLabel);
    }
}

From source file:com.nginious.http.xsp.expr.DivOperator.java

License:Apache License

/**
 * Creates bytecode for evaluating this operator. The bytecode is created
 * using the specified method visitor. The bytecode produces a result
 * of the specified type./*  w  w  w  .  j a  v  a2 s.  co m*/
 * 
 * @param visitor the method visitor
 * @param type the result type
 */
void compile(MethodVisitor visitor, Type type) {
    type = resolveType(this.value1, this.value2);
    value1.compile(visitor, type);
    value2.compile(visitor, type);

    if (type == Type.DOUBLE) {
        visitor.visitInsn(Opcodes.DDIV);
    } else {
        visitor.visitInsn(Opcodes.IDIV);
    }
}

From source file:com.nginious.http.xsp.expr.EqualsOperator.java

License:Apache License

/**
 * Compiles bytecode for evaluating this equals operator producing
 * a double as result. The specified method visitor is used for generating
 * bytecode.//from  www .java 2 s.  c  o  m
 * 
 * @param visitor the method visitor
 */
private void compileDouble(MethodVisitor visitor) {
    Label trueLabel = new Label();
    Label falseLabel = new Label();

    value1.compile(visitor, Type.DOUBLE);
    value2.compile(visitor, Type.DOUBLE);
    visitor.visitInsn(Opcodes.DCMPL);
    visitor.visitJumpInsn(Opcodes.IFEQ, trueLabel);

    visitor.visitLdcInsn(false);
    visitor.visitJumpInsn(Opcodes.GOTO, falseLabel);

    visitor.visitLabel(trueLabel);
    visitor.visitLdcInsn(true);

    visitor.visitLabel(falseLabel);
}