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.nginious.http.xsp.DocumentPart.java

License:Apache License

/**
 * Creates bytecode for this document tag part using the specified class writer and method visitor.
 * /*from ww w .  j av a2 s  .co 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.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.
 * //w  ww. j  ava  2  s .  c  om
 * @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.
 * //from w w  w  .jav a2  s  . com
 * @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.ForEachTagPart.java

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this for each tag part.
 * //  w w w  .  java 2  s . co  m
 * @param intClassName the binary class name of the class being created
 * @param writer the class writer
 * @throws XspException if unable to create bytecode
 */
void compileMethod(String intClassName, ClassWriter writer) throws XspException {
    String[] exceptions = { "com/nginious/http/xsp/XspException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName,
            "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null,
            exceptions);
    visitor.visitCode();

    Label tryLabel = new Label();
    Label startCatchLabel = new Label();

    // Start try block
    visitor.visitTryCatchBlock(tryLabel, startCatchLabel, startCatchLabel, "java/lang/Exception");
    visitor.visitLabel(tryLabel);

    try {
        String expression = setValue.getExpressionContent();
        ExpressionParser parser = new ExpressionParser();
        TreeExpression expr = parser.parse(expression);

        if (expr.getType() != Type.ANY) {
            throw new XspException("Expression in attribute set in tag " + getName()
                    + " is not an attribute or bean property " + " at line " + getLocationDescriptor());
        }

        expr.compile(visitor, Type.ANY);
        visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/Collection");
        visitor.visitVarInsn(Opcodes.ASTORE, 4);
    } catch (ExpressionException e) {
        throw new XspException("Invalid expression in attribute set in tag " + getName() + " at line "
                + getLocationDescriptor(), e);
    }

    Label labelOut = new Label();
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitJumpInsn(Opcodes.IFNULL, labelOut);

    // Start
    if (this.start != null) {
        start.compile(visitor, Type.INT);
    } else {
        visitor.visitLdcInsn((int) 0);
    }

    visitor.visitVarInsn(Opcodes.ISTORE, 5);

    // End
    if (this.end != null) {
        end.compile(visitor, Type.INT);
    } else {
        visitor.visitVarInsn(Opcodes.ALOAD, 4);
        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size", "()I");
    }

    visitor.visitVarInsn(Opcodes.ISTORE, 6);

    // Step
    if (this.step != null) {
        step.compile(visitor, Type.INT);
    } else {
        visitor.visitLdcInsn((int) 1);
    }

    visitor.visitVarInsn(Opcodes.ISTORE, 7);

    // Current pos
    visitor.visitLdcInsn(0);
    visitor.visitVarInsn(Opcodes.ISTORE, 8);

    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "iterator",
            "()Ljava/util/Iterator;");
    visitor.visitVarInsn(Opcodes.ASTORE, 9);

    Label labelStart = new Label();

    // Start of loop
    visitor.visitLabel(labelStart);

    // iterator.hasNext();
    visitor.visitVarInsn(Opcodes.ALOAD, 9);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z");
    visitor.visitJumpInsn(Opcodes.IFEQ, labelOut);

    // iterator.next();
    visitor.visitVarInsn(Opcodes.ALOAD, 9);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;");
    visitor.visitVarInsn(Opcodes.ASTORE, 10);

    // pos >= start && pos <= end && (pos - start) % step == 0
    Label labelIncr = new Label();

    visitor.visitVarInsn(Opcodes.ILOAD, 8);
    visitor.visitVarInsn(Opcodes.ILOAD, 5);
    visitor.visitJumpInsn(Opcodes.IF_ICMPLT, labelIncr);

    visitor.visitVarInsn(Opcodes.ILOAD, 8);
    visitor.visitVarInsn(Opcodes.ILOAD, 6);
    visitor.visitJumpInsn(Opcodes.IF_ICMPGT, labelIncr);

    visitor.visitVarInsn(Opcodes.ILOAD, 8);
    visitor.visitVarInsn(Opcodes.ILOAD, 5);
    visitor.visitInsn(Opcodes.ISUB);
    visitor.visitVarInsn(Opcodes.ILOAD, 7);
    visitor.visitInsn(Opcodes.IREM);
    visitor.visitJumpInsn(Opcodes.IFNE, labelIncr);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    varName.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ALOAD, 10);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute",
            "(Ljava/lang/String;Ljava/lang/Object;)V");

    // Call sub parts
    for (XspPart part : this.contentParts) {
        part.compile(intClassName, writer, visitor);
    }

    // pos++
    visitor.visitLabel(labelIncr);
    visitor.visitIincInsn(8, 1);
    visitor.visitJumpInsn(Opcodes.GOTO, labelStart);

    visitor.visitLabel(labelOut);
    visitor.visitInsn(Opcodes.RETURN);

    visitor.visitLabel(startCatchLabel);

    visitor.visitVarInsn(Opcodes.ASTORE, 3);
    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/XspException");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitLdcInsn("Attribute set contains an invalid collection for tag " + getName() + " at "
            + getLocationDescriptor());
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/XspException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V");
    visitor.visitInsn(Opcodes.ATHROW);

    visitor.visitMaxs(11, 11);
    visitor.visitEnd();
}

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

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this format date tag part.
 * //from  w  w  w  .  jav a2  s.  co  m
 * @param intClassName the binary class name of the class being created
 * @param writer the class writer
 * @throws XspException if unable to create bytecode
 */
void compileMethod(String intClassName, ClassWriter writer) throws XspException {
    String[] exceptions = { "com/nginious/http/xsp/XspException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName,
            "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null,
            exceptions);
    visitor.visitCode();

    Label tryLabel = new Label();
    Label startCatchLabel = new Label();

    // Start try block
    visitor.visitTryCatchBlock(tryLabel, startCatchLabel, startCatchLabel, "java/lang/Exception");

    visitor.visitLabel(tryLabel);

    visitor.visitTypeInsn(Opcodes.NEW, "java/text/SimpleDateFormat");
    visitor.visitInsn(Opcodes.DUP);
    pattern.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "getLocale",
            "()Ljava/util/Locale;");
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/text/SimpleDateFormat", "<init>",
            "(Ljava/lang/String;Ljava/util/Locale;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    if (this.timeZone != null) {
        visitor.visitVarInsn(Opcodes.ALOAD, 4);
        String timeZoneDesc = timeZone.getStringContent();
        visitor.visitLdcInsn(timeZoneDesc);
        visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/TimeZone", "getTimeZone",
                "(Ljava/lang/String;)Ljava/util/TimeZone;");
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/text/SimpleDateFormat", "setTimeZone",
                "(Ljava/util/TimeZone;)V");
    }

    try {
        String expression = value.getExpressionContent();
        ExpressionParser parser = new ExpressionParser();
        TreeExpression expr = parser.parse(expression);

        if (expr.getType() != Type.ANY) {
            throw new XspException("Expression in attribute value in tag " + getName()
                    + " is not an attribute or bean property " + " at line " + getLocationDescriptor());
        }

        expr.compile(visitor, Type.ANY);
        visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/Date");
        visitor.visitVarInsn(Opcodes.ASTORE, 5);
    } catch (ExpressionException e) {
        throw new XspException("Invalid expression in attribute value in tag " + getName() + " at line "
                + getLocationDescriptor(), e);
    }

    Label nullLabel = new Label();
    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitVarInsn(Opcodes.ALOAD, 5);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/text/SimpleDateFormat", "format",
            "(Ljava/util/Date;)Ljava/lang/String;");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    if (this.var != null) {
        visitor.visitVarInsn(Opcodes.ALOAD, 1);
        var.compile(visitor, Type.STRING);
        visitor.visitVarInsn(Opcodes.ALOAD, 5);
        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute",
                "(Ljava/lang/String;Ljava/lang/Object;)V");
    } else {
        visitor.visitVarInsn(Opcodes.ALOAD, 3);
        visitor.visitVarInsn(Opcodes.ALOAD, 5);
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
        visitor.visitInsn(Opcodes.POP);
    }

    visitor.visitLabel(nullLabel);
    visitor.visitInsn(Opcodes.RETURN);

    visitor.visitLabel(startCatchLabel);

    visitor.visitVarInsn(Opcodes.ASTORE, 3);
    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/XspException");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitLdcInsn(
            "Attribute value contains an invalid date for tag " + getName() + " at " + getLocationDescriptor());
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/XspException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V");
    visitor.visitInsn(Opcodes.ATHROW);

    visitor.visitMaxs(6, 6);
    visitor.visitEnd();

    super.compileMethod(intClassName, writer);
}

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

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this format number tag part.
 * //from ww  w . j  a  v  a  2  s  .  c  o m
 * @param intClassName the binary class name of the class being created
 * @param writer the class writer
 * @throws XspException if unable to create bytecode
 */
void compileMethod(String intClassName, ClassWriter writer) throws XspException {
    String[] exceptions = { "com/nginious/http/xsp/XspException" };
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName,
            "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null,
            exceptions);
    visitor.visitCode();

    Label tryLabel = new Label();
    Label startCatchLabel = new Label();

    // Start try block
    visitor.visitTryCatchBlock(tryLabel, startCatchLabel, startCatchLabel, "java/lang/Exception");

    visitor.visitLabel(tryLabel);

    visitor.visitTypeInsn(Opcodes.NEW, "java/text/DecimalFormat");
    visitor.visitInsn(Opcodes.DUP);
    pattern.compile(visitor, Type.STRING);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/text/DecimalFormat", "<init>",
            "(Ljava/lang/String;)V");
    visitor.visitVarInsn(Opcodes.ASTORE, 4);

    ExpressionParser parser = null;
    TreeExpression expr = null;

    try {
        String expression = value.getExpressionContent();
        parser = new ExpressionParser();
        expr = parser.parse(expression);

        if (expr.getType() != Type.ANY) {
            throw new XspException("Expression in attribute value in tag " + getName()
                    + " is not an attribute or bean property " + " at line " + getLocationDescriptor());
        }
    } catch (ExpressionException e) {
        throw new XspException("Invalid expression in attribute value in tag " + getName() + " at line "
                + getLocationDescriptor(), e);
    }

    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    expr.compile(visitor, Type.DOUBLE);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/text/DecimalFormat", "format",
            "(D)Ljava/lang/String;");
    visitor.visitVarInsn(Opcodes.ASTORE, 5);

    if (this.var != null) {
        visitor.visitVarInsn(Opcodes.ALOAD, 1);
        var.compile(visitor, Type.STRING);
        visitor.visitVarInsn(Opcodes.ALOAD, 5);
        visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute",
                "(Ljava/lang/String;Ljava/lang/Object;)V");
    } else {
        visitor.visitVarInsn(Opcodes.ALOAD, 3);
        visitor.visitVarInsn(Opcodes.ALOAD, 5);
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
        visitor.visitInsn(Opcodes.POP);
    }

    visitor.visitInsn(Opcodes.RETURN);

    visitor.visitLabel(startCatchLabel);

    visitor.visitVarInsn(Opcodes.ASTORE, 4);
    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/XspException");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitLdcInsn("Attribute value contains an invalid number for tag " + getName() + " at "
            + getLocationDescriptor());
    visitor.visitVarInsn(Opcodes.ALOAD, 4);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/XspException", "<init>",
            "(Ljava/lang/String;Ljava/lang/Throwable;)V");
    visitor.visitInsn(Opcodes.ATHROW);

    visitor.visitMaxs(6, 6);
    visitor.visitEnd();

    super.compileMethod(intClassName, writer);
}

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

License:Apache License

/**
 * Creates subclass of {@link XspService} for the XSP file represented by the specified document
 * tree node structure.//w ww .  j ava2s .co  m
 * 
 * @param document the document tree node structure
 * @param srcFilePath the XSP file source path
 * @return a descriptor for the generated subclass
 * @throws XspException if unable to create subclass
 */
private ClassDescriptor compileService(DocumentPart document, String srcFilePath) throws XspException {
    ClassWriter writer = new ClassWriter(0);

    // Create class
    String packageName = document.getMetaContent("package");
    String intServiceClazzName = createIntServiceClassName(packageName, srcFilePath);
    String serviceClazzName = createServiceClassName(packageName, srcFilePath);
    writer.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, intServiceClazzName, "Lcom/nginious/http/xsp/XspService;",
            "com/nginious/http/xsp/XspService", null);

    // Create constructor
    createConstructor(writer, "com/nginious/http/xsp/XspService");

    // Create xsp service method
    MethodVisitor visitor = createXspMethod(writer);

    Label tryLabel = new Label();
    Label startCatchLabel = new Label();
    Label endCatchLabel = new Label();

    // Start try block
    visitor.visitTryCatchBlock(tryLabel, startCatchLabel, endCatchLabel, "java/lang/Throwable");

    visitor.visitLabel(tryLabel);

    visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/expr/HttpRequestVariables");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/expr/HttpRequestVariables", "<init>",
            "(Lcom/nginious/http/HttpRequest;)V");
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "setVariables",
            "(Lcom/nginious/http/xsp/expr/Variables;)V");

    document.compile(intServiceClazzName, writer, visitor);

    visitor.visitLabel(startCatchLabel);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables",
            "()V");

    visitor.visitLdcInsn(true);
    visitor.visitInsn(Opcodes.IRETURN);

    // Start finally block
    visitor.visitLabel(endCatchLabel);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/nginious/http/xsp/expr/Expression", "removeVariables",
            "()V");
    visitor.visitInsn(Opcodes.ATHROW);

    visitor.visitMaxs(12, 12);
    visitor.visitEnd();

    document.compileMethod(intServiceClazzName, writer);

    writer.visitEnd();
    byte[] clazzBytes = writer.toByteArray();
    return new ClassDescriptor(serviceClazzName, clazzBytes);
}

From source file:com.nway.spring.jdbc.bean.AsmBeanProcessor.java

License:Apache License

/**
 *
 * /*from w w w  .  j a v a  2 s .co m*/
 *
 * @param cw
 * @param mv
 * @param processorName com/nway/commons/dbutils/DynamicBeanProcessorImpl
 * @param beanName com/nway/commons/dbutils/test/User
 * @return [0]:bean[1]createBean
 */
private Object[] prepScript(ClassWriter cw, MethodVisitor mv, String processorName, String beanName) {

    Object[] lab = new Object[2];

    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, processorName, null,
            "com/nway/spring/jdbc/bean/DbBeanFactory", null);

    cw.visitSource(processorName.substring(processorName.lastIndexOf('/') + 1) + ".java", null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(6, l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nway/spring/jdbc/bean/DbBeanFactory", "<init>", "()V",
                false);
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + processorName + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createBean",
                "(Ljava/sql/ResultSet;Ljava/lang/Class;)Ljava/lang/Object;",
                "<T:Ljava/lang/Object;>(Ljava/sql/ResultSet;Ljava/lang/Class<TT;>;)TT;",
                new String[] { "java/sql/SQLException" });
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(10, l0);
        mv.visitTypeInsn(Opcodes.NEW, beanName);
        mv.visitInsn(Opcodes.DUP);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, beanName, "<init>", "()V", false);
        mv.visitVarInsn(Opcodes.ASTORE, 3);

        lab[0] = l0;
        lab[1] = mv;
    }

    return lab;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Calls a constructor with a set of arguments. After execution the stack should have an extra item pushed on it: the object that was
 * created by this constructor.// ww  w.  j a  v  a  2  s. co  m
 * @param constructor constructor to call
 * @param args constructor argument instruction lists -- each instruction list must leave one item on the stack of the type expected
 * by the constructor
 * @return instructions to invoke a constructor
 * @throws NullPointerException if any argument is {@code null} or array contains {@code null}
 * @throws IllegalArgumentException if the length of {@code args} doesn't match the number of parameters in {@code constructor}
 */
public static InsnList construct(Constructor<?> constructor, InsnList... args) {
    Validate.notNull(constructor);
    Validate.notNull(args);
    Validate.noNullElements(args);
    Validate.isTrue(constructor.getParameterCount() == args.length);

    InsnList ret = new InsnList();

    Type clsType = Type.getType(constructor.getDeclaringClass());
    Type methodType = Type.getType(constructor);

    ret.add(new TypeInsnNode(Opcodes.NEW, clsType.getInternalName()));
    ret.add(new InsnNode(Opcodes.DUP));
    for (InsnList arg : args) {
        ret.add(arg);
    }
    ret.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, clsType.getInternalName(), "<init>",
            methodType.getDescriptor(), false));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * For each element in an object array, performs an action.
 * @param counterVar parameter used to keep track of count in loop
 * @param arrayLenVar parameter used to keep track of array length
 * @param array object array instruction list -- must leave an array on top of the stack
 * @param action action to perform on each element -- element will be at top of stack and must be consumed by these instructions
 * @return instructions instruction list to perform some action if two ints are equal
 * @throws NullPointerException if any argument is {@code null}
 *//*from www .  j  av  a  2  s.  c  o  m*/
public static InsnList forEach(Variable counterVar, Variable arrayLenVar, InsnList array, InsnList action) {
    Validate.notNull(counterVar);
    Validate.notNull(arrayLenVar);
    Validate.notNull(array);
    Validate.notNull(action);
    Validate.isTrue(counterVar.getType().equals(Type.INT_TYPE));
    Validate.isTrue(arrayLenVar.getType().equals(Type.INT_TYPE));

    InsnList ret = new InsnList();

    LabelNode doneLabelNode = new LabelNode();
    LabelNode loopLabelNode = new LabelNode();

    // put zero in to counterVar
    ret.add(new LdcInsnNode(0)); // int
    ret.add(new VarInsnNode(Opcodes.ISTORE, counterVar.getIndex())); //

    // load array we'll be traversing over
    ret.add(array); // object[]

    // put array length in to arrayLenVar
    ret.add(new InsnNode(Opcodes.DUP)); // object[], object[]
    ret.add(new InsnNode(Opcodes.ARRAYLENGTH)); // object[], int
    ret.add(new VarInsnNode(Opcodes.ISTORE, arrayLenVar.getIndex())); // object[]

    // loopLabelNode: test if counterVar == arrayLenVar, if it does then jump to doneLabelNode
    ret.add(loopLabelNode);
    ret.add(new VarInsnNode(Opcodes.ILOAD, counterVar.getIndex())); // object[], int
    ret.add(new VarInsnNode(Opcodes.ILOAD, arrayLenVar.getIndex())); // object[], int, int
    ret.add(new JumpInsnNode(Opcodes.IF_ICMPEQ, doneLabelNode)); // object[]

    // load object from object[]
    ret.add(new InsnNode(Opcodes.DUP)); // object[], object[]
    ret.add(new VarInsnNode(Opcodes.ILOAD, counterVar.getIndex())); // object[], object[], int
    ret.add(new InsnNode(Opcodes.AALOAD)); // object[], object

    // call action
    ret.add(action); // object[]

    // increment counter var and goto loopLabelNode
    ret.add(new IincInsnNode(counterVar.getIndex(), 1)); // object[]
    ret.add(new JumpInsnNode(Opcodes.GOTO, loopLabelNode)); // object[]

    // doneLabelNode: pop object[] off of stack
    ret.add(doneLabelNode);
    ret.add(new InsnNode(Opcodes.POP)); //

    return ret;
}