Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

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

Introduction

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

Prototype

int INVOKEVIRTUAL

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

Click Source Link

Usage

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  ww w.ja v a 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);

    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 for this format number tag part using the specified class writer and method visitor. 
 * //from ww w .j av a2s.  com
 * @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 {
    if (this.value == null) {
        throw new XspException(
                "Attribute value is missing for tag + " + getName() + " at " + getLocationDescriptor());
    }

    if (this.pattern == null) {
        throw new XspException(
                "Attribute pattern is missing for tag + " + getName() + " at " + getLocationDescriptor());
    }

    if (pattern.isExpression()) {
        throw new XspException("Expressions are not allowed in attribute pattern for tag + " + getName()
                + " at " + getLocationDescriptor());
    }

    if (this.var != null && var.isExpression()) {
        throw new XspException("Expressions are not allowed in attribute var for tag + " + getName() + " at "
                + getLocationDescriptor());
    }

    this.methodName = "formatNumber" + methodNameCounter.getAndIncrement();
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intClassName, this.methodName,
            "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V");
}

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  a2s  .c  om*/
 * @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.IfTagPart.java

License:Apache License

/**
 * Creates bytecode for this if tag part using the specified  class writer and method visitor. A separate
 * method is created within the class to evaluate the expression in the test attribute.
 * //from w  w  w.j  ava2  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 {
    if (this.part == null) {
        throw new XspException(
                "Attribute test is missing for tag " + getName() + " at " + getLocationDescriptor());
    }

    this.methodName = "ifTest" + methodNameCounter.getAndIncrement();
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intClassName, this.methodName, "()Z");
    Label falseLabel = new Label();
    visitor.visitJumpInsn(Opcodes.IFEQ, falseLabel);

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

    visitor.visitLabel(falseLabel);
}

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

License:Apache License

/**
 * Creates bytecode for this message tag part using the specified class writer and method visitor.
 * //from   w  ww  .j av  a  2  s .  c om
 * @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 {
    if (this.key == null) {
        throw new XspException(
                "Attribute key is missing for tag + " + getName() + " at " + getLocationDescriptor());
    }

    if (this.bundle == null) {
        throw new XspException(
                "Attribute bundle is missing for tag + " + getName() + " at " + getLocationDescriptor());
    }

    if (this.var != null && var.isExpression()) {
        throw new XspException("Expressions are not allowed in attribute var for tag + " + getName() + " at "
                + getLocationDescriptor());
    }

    this.methodName = "message" + methodNameCounter.getAndIncrement();
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitVarInsn(Opcodes.ALOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intClassName, this.methodName,
            "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V");
}

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

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this message tag part.
 * /*from   w  w 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 {
    MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName,
            "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null,
            null);
    visitor.visitCode();

    if (this.var != null) {
        visitor.visitVarInsn(Opcodes.ALOAD, 1);
        var.compile(visitor, Type.STRING);
    } else {
        visitor.visitVarInsn(Opcodes.ALOAD, 3);
    }

    bundle.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpResponse", "getLocale",
            "()Ljava/util/Locale;");
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/ResourceBundle", "getBundle",
            "(Ljava/lang/String;Ljava/util/Locale;)Ljava/util/ResourceBundle;");

    key.compile(visitor, Type.STRING);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/ResourceBundle", "getString",
            "(Ljava/lang/String;)Ljava/lang/String;");

    if (this.args != null) {
        try {
            String expression = args.getExpressionContent();
            ExpressionParser parser = new ExpressionParser();
            TreeExpression expr = parser.parse(expression);

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

            expr.compile(visitor, Type.ANY);
            visitor.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
            visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/text/MessageFormat", "format",
                    "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
        } catch (ExpressionException e) {
            throw new XspException("Invalid expression in attribute value in tag " + getName() + " at line "
                    + getLocationDescriptor(), e);
        }
    }

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

    visitor.visitInsn(Opcodes.RETURN);
    visitor.visitMaxs(5, 5);
    visitor.visitEnd();

    super.compileMethod(intClassName, writer);
}

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

License:Apache License

/**
 * Creates bytecode for this static part using the specified  class writer and method visitor. If this static
 * part is an expression a separate method is created with bytecode to evaluate the expression.
 * //from  w w  w . ja  v  a 2s .  com
 * @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 {
    if (isExpression()) {
        this.methodName = "staticExpr" + methodNameCounter.getAndIncrement();
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitVarInsn(Opcodes.ALOAD, 3);
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, intClassName, this.methodName,
                "(Ljava/lang/StringBuffer;)V");
        compileMethod(intClassName, writer);
    } else {
        String contentStr = new String(content, start, len);
        visitor.visitVarInsn(Opcodes.ALOAD, 3);
        visitor.visitLdcInsn(contentStr);
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
        visitor.visitInsn(Opcodes.POP);
    }
}

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

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating an expression.
 * //from ww w  .  j a  v  a  2s. 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 {
    if (!isExpression()) {
        return;
    }

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

        MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName,
                "(Ljava/lang/StringBuffer;)V", null, null);
        visitor.visitCode();
        visitor.visitVarInsn(Opcodes.ALOAD, 1);

        if (expr.getType() == Type.ANY) {
            expr.compile(visitor, Type.STRING);
        } else {
            expr.compile(visitor);
        }

        if (expr.getType() == Type.BOOLEAN) {
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                    "(Z)Ljava/lang/StringBuffer;");
        } else if (expr.getType() == Type.DOUBLE) {
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                    "(D)Ljava/lang/StringBuffer;");
        } else if (expr.getType() == Type.INT) {
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                    "(I)Ljava/lang/StringBuffer;");
        } else if (expr.getType() == Type.STRING || expr.getType() == Type.ANY) {
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append",
                    "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
        }

        visitor.visitInsn(Opcodes.POP);
        visitor.visitInsn(Opcodes.RETURN);
        visitor.visitMaxs(5, 5);
        visitor.visitEnd();
    } catch (ExpressionException e) {
        throw new XspException("Invalid expression at " + getLocationDescriptor(), e);
    }
}

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

License:Apache License

/**
 *
 *
 * @param mv ${@link MethodVisitor}//  ww w  .  j  av a2 s .  c  o  m
 * @param index rs.getDate(index)
 * @param beanSignature com/nway/commons/dbutils/test/User
 * @param rsTypeDesc Ljava/sql/Date;
 * @param beanTypeDesc Ljava/util/Date;
 * @param rsMethod getDate
 * @param writeMethod setDate
 */
private void visitMethod(MethodVisitor mv, int index, String beanSignature, String beanTypeDesc,
        String rsMethod, String writeMethod) {

    mv.visitVarInsn(Opcodes.ALOAD, 3);
    mv.visitVarInsn(Opcodes.ALOAD, 1);

    visitInsn(mv, index);

    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", rsMethod, "(I)" + beanTypeDesc, true);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(" + beanTypeDesc + ")V", false);
}

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

License:Apache License

private void visitMethodCast(MethodVisitor mv, int index, String beanSignature, int beanType,
        String beanTypeDesc, String writeMethod) {

    mv.visitVarInsn(Opcodes.ALOAD, 3);/*from  ww  w .  ja  v a2 s  .c o  m*/
    mv.visitVarInsn(Opcodes.ALOAD, 1);

    visitInsn(mv, index);

    switch (beanType) {
    case PROPERTY_TYPE_DATE:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getTimestamp",
                "(I)Ljava/sql/Timestamp;", true);
        break;
    default:
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/springframework/jdbc/support/JdbcUtils",
                "getResultSetValue", "(Ljava/sql/ResultSet;I)Ljava/lang/Object;", false);
        mv.visitTypeInsn(Opcodes.CHECKCAST, beanTypeDesc);
        break;
    }

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(L" + beanTypeDesc + ";)V", false);
}