Example usage for org.objectweb.asm MethodVisitor visitMethodInsn

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

Introduction

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

Prototype

@Deprecated
public void visitMethodInsn(final int opcode, final String owner, final String name, final String descriptor) 

Source Link

Document

Visits a method instruction.

Usage

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

License:Apache License

/**
 * Creates bytecode for evaluating this function. The bytecode is generated using the
 * specified method visitor. The generated bytecode produces a result of the specified
 * type./* w ww.j  a  va  2s.c om*/
 * 
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    type = resolveType(this.value1, this.value2);
    value1.compile(visitor, type);
    value2.compile(visitor, type);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "max",
            type == Type.DOUBLE ? "(DD)D" : "(II)I");
}

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

License:Apache License

/**
 * Creates bytecode for evaluating this function. The bytecode is generated using the
 * specified method visitor. The generated bytecode produces a result of the specified
 * type./*from w w w.java 2  s .co m*/
 * 
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    type = resolveType(this.value1, this.value2);
    value1.compile(visitor, type);
    value2.compile(visitor, type);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "min",
            type == Type.DOUBLE ? "(DD)D" : "(II)I");
}

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

License:Apache License

/**
 * Compiles bytecode for evaluating this not equals operator producing
 * a string as result. The specified method visitor is used for generating
 * bytecode.//from   w ww .  j a v  a  2  s .  c om
 * 
 * @param visitor the method visitor
 */
private void compileString(MethodVisitor visitor) {
    Label trueLabel = new Label();
    Label falseLabel = new Label();
    Label nullLabel = new Label();

    value1.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ASTORE, 1);
    value2.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ASTORE, 2);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitVarInsn(Opcodes.ALOAD, 2);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
    visitor.visitJumpInsn(Opcodes.IFNE, falseLabel);

    visitor.visitLabel(nullLabel);
    visitor.visitLdcInsn(true);
    visitor.visitJumpInsn(Opcodes.GOTO, trueLabel);

    visitor.visitLabel(falseLabel);
    visitor.visitLdcInsn(false);

    visitor.visitLabel(trueLabel);
}

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

License:Apache License

/**
 * Creates bytecode for evaluating this function. The specified method visitor is
 * used for creating bytecode.//from   www  .j  a v a2s. c o m
 * 
 * @param visitor the methos visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    value1.compile(visitor, Type.DOUBLE);
    value2.compile(visitor, Type.DOUBLE);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "pow", "(DD)D");
}

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

License:Apache License

/**
 * Creates bytecode for evaluating this right function. The specified method visitor
 * and type are used for generating bytecode.
 * /*from w ww  .  ja  v a  2 s  . c  o m*/
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    value.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ASTORE, 1);

    Label nullLabel = new Label();
    Label notNullLabel = new Label();

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I");
    value2.compile(visitor, Type.INT);
    visitor.visitInsn(Opcodes.ISUB);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "substring", "(I)Ljava/lang/String;");
    visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

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

    visitor.visitLabel(notNullLabel);
}

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

License:Apache License

/**
 * Creates bytecode for evaluating this substring function. The specified method visitor
 * and type are used for generating bytecode.
 * /*from  w w w. jav  a2  s . c o  m*/
 * @param visitor the method visitor
 * @param type the type
 */
void compile(MethodVisitor visitor, Type type) {
    value.compile(visitor, Type.STRING);
    visitor.visitVarInsn(Opcodes.ASTORE, 1);
    value2.compile(visitor, Type.INT);
    visitor.visitVarInsn(Opcodes.ISTORE, 2);
    value3.compile(visitor, Type.INT);
    visitor.visitVarInsn(Opcodes.ISTORE, 3);

    Label nullLabel = new Label();
    Label notNullLabel = new Label();

    // check for null string
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel);

    // start < 0
    Label label1 = new Label();
    visitor.visitLdcInsn(0);
    visitor.visitVarInsn(Opcodes.ILOAD, 2);
    visitor.visitJumpInsn(Opcodes.IF_ICMPLT, label1);
    visitor.visitLdcInsn(0);
    visitor.visitVarInsn(Opcodes.ISTORE, 2);

    // start > value.length
    Label label2 = new Label();
    visitor.visitLabel(label1);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I");
    visitor.visitVarInsn(Opcodes.ILOAD, 2);
    visitor.visitJumpInsn(Opcodes.IF_ICMPGT, label2);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I");
    visitor.visitVarInsn(Opcodes.ISTORE, 2);

    // end < start
    Label label3 = new Label();
    visitor.visitLabel(label2);
    visitor.visitVarInsn(Opcodes.ILOAD, 2);
    visitor.visitVarInsn(Opcodes.ILOAD, 3);
    visitor.visitJumpInsn(Opcodes.IF_ICMPLT, label3);
    visitor.visitVarInsn(Opcodes.ILOAD, 2);
    visitor.visitVarInsn(Opcodes.ISTORE, 3);

    // end > value1.length
    Label label4 = new Label();
    visitor.visitLabel(label3);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I");
    visitor.visitVarInsn(Opcodes.ILOAD, 3);
    visitor.visitJumpInsn(Opcodes.IF_ICMPGT, label4);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I");
    visitor.visitVarInsn(Opcodes.ISTORE, 3);

    // substr
    visitor.visitLabel(label4);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitVarInsn(Opcodes.ILOAD, 2);
    visitor.visitVarInsn(Opcodes.ILOAD, 3);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;");
    visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel);

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

    visitor.visitLabel(notNullLabel);

}

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

License:Apache License

/**
 * Creates bytecode for this for each tag part using the specified class writer and method visitor.
 * /*from  w  ww.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
 * @param visitor the method visitor
 * @throws XspException if unable to create bytecode
 */
void compile(String intClassName, ClassWriter writer, MethodVisitor visitor) throws XspException {
    if (this.setValue == null) {
        throw new XspException(
                "Attribute set is missing for tag + " + getName() + " at " + getLocationDescriptor());
    }

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

    this.methodName = "forEach" + 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.ForEachTagPart.java

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this for each tag part.
 * //  w w  w  .j  a  va 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);

    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 for this format date tag part using the specified class writer and method visitor.
 * //from  ww  w  .ja v  a  2s .  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.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 = "formatDate" + 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.FormatDateTagPart.java

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this format date tag part.
 * /* w ww  .ja 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/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);
}