Example usage for org.objectweb.asm Opcodes INVOKEINTERFACE

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

Introduction

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

Prototype

int INVOKEINTERFACE

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

Click Source Link

Usage

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.
 * //  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);

    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.MessageTagPart.java

License:Apache License

/**
 * Creates bytecode in a separate method for evaluating this message tag part.
 * //from  w w  w.  ja va2s . 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 {
    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.nway.spring.jdbc.bean.AsmBeanProcessor.java

License:Apache License

/**
 *
 *
 * @param mv ${@link MethodVisitor}//from   w w w .  j  a v a2  s .  co 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   w  ww. j a  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);
}

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

License:Apache License

private void visitMethodWrap(MethodVisitor mv, int index, String beanSignature, int beanType,
        String writeMethod, String processorName) {

    mv.visitVarInsn(Opcodes.ALOAD, 3);/*from w  ww .  j a  v a 2  s. c  o m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);

    visitInsn(mv, index);

    switch (beanType) {
    case PROPERTY_TYPE_INTEGER:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getInt", "(I)I", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "integerValue", "(IZ)Ljava/lang/Integer;",
                false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Integer;)V", false);
        break;
    case PROPERTY_TYPE_LONG:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getLong", "(I)J", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "longValue", "(JZ)Ljava/lang/Long;", false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Long;)V", false);
        break;
    case PROPERTY_TYPE_BOOLEAN:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getBoolean", "(I)Z", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "booleanValue", "(ZZ)Ljava/lang/Boolean;",
                false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Boolean;)V", false);
        break;
    case PROPERTY_TYPE_FLOAT:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getFloat", "(I)F", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "floatValue", "(FZ)Ljava/lang/Float;", false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Float;)V", false);
        break;
    case PROPERTY_TYPE_DOUBLE:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getDouble", "(I)D", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "doubleValue", "(DZ)Ljava/lang/Double;",
                false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Double;)V", false);
        break;
    case PROPERTY_TYPE_BYTE:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getByte", "(I)B", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "byteValue", "(BZ)Ljava/lang/Byte;", false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Byte;)V", false);
        break;
    case PROPERTY_TYPE_SHORT:
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "getShort", "(I)S", true);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/sql/ResultSet", "wasNull", "()Z", true);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, processorName, "shortValue", "(SZ)Ljava/lang/Short;", false);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, beanSignature, writeMethod, "(Ljava/lang/Short;)V", false);
        break;
    }
}

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

License:Open Source License

/**
 * Calls a method with a set of arguments. After execution the stack may have an extra item pushed on it: the object that was returned
 * by this method (if any)./*from w ww .  j a v  a2 s. c om*/
 * @param method method to call
 * @param args method argument instruction lists -- each instruction list must leave one item on the stack of the type expected
 * by the method (note that if this is a non-static method, the first argument must always evaluate to the "this" pointer/reference)
 * @return instructions to invoke a method
 * @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 method}
 */
public static InsnList call(Method method, InsnList... args) {
    Validate.notNull(method);
    Validate.notNull(args);
    Validate.noNullElements(args);

    InsnList ret = new InsnList();

    for (InsnList arg : args) {
        ret.add(arg);
    }

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

    if ((method.getModifiers() & Modifier.STATIC) == Modifier.STATIC) {
        Validate.isTrue(method.getParameterCount() == args.length);
        ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, clsType.getInternalName(), method.getName(),
                methodType.getDescriptor(), false));
    } else if (method.getDeclaringClass().isInterface()) {
        Validate.isTrue(method.getParameterCount() + 1 == args.length);
        ret.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, clsType.getInternalName(), method.getName(),
                methodType.getDescriptor(), true));
    } else {
        Validate.isTrue(method.getParameterCount() + 1 == args.length);
        ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, clsType.getInternalName(), method.getName(),
                methodType.getDescriptor(), false));
    }

    return ret;
}

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

License:Open Source License

/**
 * Get the number of arguments required for an invocation of some method. This includes the 'this' argument for non-static methods.
 * <p>//from  ww w  .  j ava 2 s  . com
 * NOTE THAT THIS IS NOT THE NUMBER OF ITEMS ON THE STACK. If the method takes in doubles or longs, each double or long encountered
 * would be 2 items on the stack. This method returns the number of arguments required for the method to be invoked, not the number of
 * items required to be on the stack for the method to be invoked.
 * @param invokeNode the invocation instruction (either normal invocation or invokedynamic)
 * @return number of arguments required by this method
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code invokeNode} is neither of type {@link MethodInsnNode} nor {@link InvokeDynamicInsnNode},
 * or if type of invocation ({@link MethodInsnNode}) cannot be determined
 */
public static int getArgumentCountRequiredForInvocation(AbstractInsnNode invokeNode) {
    Validate.notNull(invokeNode);

    if (invokeNode instanceof MethodInsnNode) {
        MethodInsnNode methodInsnNode = (MethodInsnNode) invokeNode;
        int extra;
        int paramCount;

        switch (methodInsnNode.getOpcode()) {
        case Opcodes.INVOKEVIRTUAL:
        case Opcodes.INVOKESPECIAL:
        case Opcodes.INVOKEINTERFACE:
            extra = 1;
            break;
        case Opcodes.INVOKESTATIC:
            extra = 0;
            break;
        default:
            throw new IllegalArgumentException(); // unknown invocation type? probably badly generated instruction node
        }
        Type methodType = Type.getType(methodInsnNode.desc);
        paramCount = methodType.getArgumentTypes().length;

        return paramCount + extra;
    } else if (invokeNode instanceof InvokeDynamicInsnNode) {
        InvokeDynamicInsnNode invokeDynamicInsnNode = (InvokeDynamicInsnNode) invokeNode;
        int paramCount;

        Type methodType = Type.getType(invokeDynamicInsnNode.desc);
        paramCount = methodType.getArgumentTypes().length;

        return paramCount;
    } else {
        throw new IllegalArgumentException();
    }
}

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

License:Open Source License

/**
 * Get the number of items that need to be on the stack for an invocation of some method.
 * @param invokeNode the invocation instruction (either normal invocation or invokedynamic)
 * @return number of items required on the stack for this method
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code invokeNode} is neither of type {@link MethodInsnNode} nor {@link InvokeDynamicInsnNode},
 * or if type of invocation ({@link MethodInsnNode}) cannot be determined
 *///w w w.  ja v a2 s. co  m
public static int getRequiredStackCountForInvocation(AbstractInsnNode invokeNode) {
    Validate.notNull(invokeNode);

    if (invokeNode instanceof MethodInsnNode) {
        MethodInsnNode methodInsnNode = (MethodInsnNode) invokeNode;
        int extra;
        int paramCount;

        switch (methodInsnNode.getOpcode()) {
        case Opcodes.INVOKEVIRTUAL:
        case Opcodes.INVOKESPECIAL:
        case Opcodes.INVOKEINTERFACE:
            extra = 1;
            break;
        case Opcodes.INVOKESTATIC:
            extra = 0;
            break;
        default:
            throw new IllegalArgumentException(); // unknown invocation type? probably badly generated instruction node
        }
        Type methodType = Type.getType(methodInsnNode.desc);
        paramCount = methodType.getArgumentTypes().length;

        return paramCount + extra;
    } else if (invokeNode instanceof InvokeDynamicInsnNode) {
        InvokeDynamicInsnNode invokeDynamicInsnNode = (InvokeDynamicInsnNode) invokeNode;
        int paramCount;

        Type methodType = Type.getType(invokeDynamicInsnNode.desc);
        paramCount = methodType.getArgumentTypes().length;

        return paramCount;
    } else {
        throw new IllegalArgumentException();
    }
}

From source file:com.spotify.missinglink.ClassLoader.java

License:Apache License

private static void handleMethodCall(Set<CalledMethod> thisCalls, int lineNumber, MethodInsnNode insn) {
    boolean isStatic;
    switch (insn.getOpcode()) {
    case Opcodes.INVOKEVIRTUAL:
    case Opcodes.INVOKEINTERFACE:
        isStatic = false;/*from   w w w  .j a v  a 2  s  .c  o m*/
        break;
    case Opcodes.INVOKESPECIAL:
        isStatic = false;
        break;
    case Opcodes.INVOKESTATIC:
        isStatic = true;
        break;
    default:
        throw new RuntimeException("Unexpected method call opcode: " + insn.getOpcode());
    }
    if (insn.owner.charAt(0) != '[') {
        thisCalls.add(new CalledMethodBuilder().owner(TypeDescriptors.fromClassName(insn.owner))
                .descriptor(MethodDescriptors.fromDesc(insn.desc, insn.name)).isStatic(isStatic)
                .lineNumber(lineNumber).build());
    }
}

From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.Inlining.java

License:Open Source License

public static boolean isBuiltinInterfaceMethod(ByteCodeMethodVisitor bcmv, Insn insn) {
    if (insn instanceof MethodInsn) {
        MethodInsn mi = (MethodInsn) insn;
        if ((mi.opcode == Opcodes.INVOKEINTERFACE) && (isCompilerBuiltin(mi.owner))) {
            return true;
        }//from w w  w  . j  av a2  s .c o  m
    }
    return false;
}