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:org.fabric3.implementation.bytecode.reflection.BytecodeConsumerInvokerFactory.java

License:Open Source License

private void writeTargetInvoke(Method method, String internalTargetName, ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "invoke",
            "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS);
    mv.visitCode();//from w  w w  .j a v  a2s . co  m
    Label label1 = new Label();
    mv.visitLabel(label1);
    mv.visitLineNumber(9, label1);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, internalTargetName);

    if (method.getParameterTypes().length == 1) {
        // single argument method, load the parameter passes on to the stack
        Class<?> paramType = method.getParameterTypes()[0];
        mv.visitVarInsn(Opcodes.ALOAD, 2);

        writeParam(paramType, mv);

    } else if (method.getParameterTypes().length > 1) {
        // multi-argument method: cast the parameter to an object array and then load each element on the stack to be passed as params
        mv.visitVarInsn(Opcodes.ALOAD, 2);

        mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
        mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/Object;");
        mv.visitVarInsn(Opcodes.ASTORE, 3);

        int pos = 0;
        mv.visitVarInsn(Opcodes.ALOAD, 3);
        for (Class<?> paramType : method.getParameterTypes()) {
            mv.visitInsn(Opcodes.ICONST_0 + pos);
            mv.visitInsn(Opcodes.AALOAD);

            writeParam(paramType, mv);

            if (pos < method.getParameterTypes().length - 1) {
                mv.visitVarInsn(Opcodes.ALOAD, 3);
            }
            pos++;
        }
    }

    // invoke the instance
    String methodName = method.getName();
    String methodDescriptor = Type.getMethodDescriptor(method);

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, internalTargetName, methodName, methodDescriptor);

    Class<?> returnType = method.getReturnType();
    writeReturn(returnType, mv);

    Label label2 = new Label();
    mv.visitLabel(label2);
    String descriptor = Type.getDescriptor(ServiceInvoker.class);

    mv.visitLocalVariable("this", descriptor, null, label1, label2, 0);
    mv.visitLocalVariable("instance", "Ljava/lang/Object;", null, label1, label2, 1);
    mv.visitLocalVariable("arg", "Ljava/lang/Object;", null, label1, label2, 2);
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:org.fabric3.implementation.bytecode.reflection.BytecodeConsumerInvokerFactory.java

License:Open Source License

private void writeParam(Class<?> paramType, MethodVisitor mv) {
    if (Integer.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Integer");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
    } else if (Boolean.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Boolean");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
    } else if (Double.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Double");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
    } else if (Float.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Float");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
    } else if (Short.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Short");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
    } else if (Byte.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Byte");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
    } else if (Long.TYPE.equals(paramType)) {
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/Long");
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
    } else {//from   ww w.j av  a2  s .  co  m
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(paramType));
    }
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompilerForPrecisionBigDecimals_Base.java

License:Open Source License

@Override
protected int compileComparison(int _ifOpcode, int _comparisonOpcode) throws CompilerException {
    mv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, BNAME, "compareTo", B2I);
    return _ifOpcode;
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompilerForStrings_Base.java

License:Open Source License

@Override
protected int compileComparison(int _ifOpcode, int _comparisonOpcode) throws CompilerException {
    switch (_ifOpcode) {

    /*//  w  ww.ja  v  a  2 s .c  o  m
     * This may seem counter-intuitive here, but the contract is to return 0 for equality.
     * Boolean true, however, is 1. So we invert the test for EQ and NE.
     */

    case Opcodes.IFEQ:
        mv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, SNAME, "equalsIgnoreCase", S2Z);
        return Opcodes.IFNE;

    case Opcodes.IFNE:
        mv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, SNAME, "equalsIgnoreCase", S2Z);
        return Opcodes.IFEQ;

    default:
        mv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, SNAME, "compareToIgnoreCase", S2I);
        return _ifOpcode;

    }
}

From source file:org.formulacompiler.compiler.internal.bytecode.MethodCompiler.java

License:Open Source License

final void compileCall(GeneratorAdapter _mv) {
    _mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, sectionInContext().classInternalName(), methodName(),
            this.methodDescriptor);
}

From source file:org.formulacompiler.compiler.internal.bytecode.MethodCompiler.java

License:Open Source License

final void compileInputGetterCall(CallFrame _callChainToCall) throws CompilerException {
    final CallFrame[] frames = _callChainToCall.getFrames();
    final boolean isStatic = Modifier.isStatic(frames[0].getMethod().getModifiers());

    if (!isStatic) {
        mv().loadThis();/*from   w w w .j a  va 2s .c  o  m*/
        mv().getField(section().classType(), ByteCodeEngineCompiler.INPUTS_MEMBER_NAME, section().inputType());
    }

    Class contextClass = section().inputClass();
    for (CallFrame frame : frames) {
        final Method method = frame.getMethod();
        final Object[] args = frame.getArgs();
        if (null != args) {
            final Class[] types = method.getParameterTypes();
            for (int i = 0; i < args.length; i++) {
                final Object arg = args[i];
                final Class type = types[i];
                if (arg instanceof CellModel) {
                    final CellModel argCell = (CellModel) arg;
                    final ExpressionCompiler ex = expressionCompilerFor(type);
                    ex.compileRef(argCell);
                    ex.compileConversionTo(type);
                } else {
                    pushConstParam(type, arg);
                }
            }
        }
        final int opcode;
        if (contextClass.isInterface())
            opcode = Opcodes.INVOKEINTERFACE;
        else if (isStatic)
            opcode = Opcodes.INVOKESTATIC;
        else
            opcode = Opcodes.INVOKEVIRTUAL;

        mv().visitMethodInsn(opcode, Type.getType(contextClass).getInternalName(), method.getName(),
                Type.getMethodDescriptor(method));

        contextClass = method.getReturnType();
    }
}

From source file:org.formulacompiler.compiler.internal.bytecode.OutputDistributorCompiler.java

License:Open Source License

String compileCase(CallFrame _callFrame) throws UnsupportedDataType {
    final String caseMethodName = this.caseMethodPrefix + this.nextCaseNumber++;

    final Label next = mv().newLabel();
    for (int i = 0; i < this.params.length; i++) {
        final Class argClass = this.params[i];
        final Type argType = this.paramTypes[i];
        final Object argValue = _callFrame.getArgs()[i];

        mv().loadArg(i);//from  w  w  w  .ja  v a 2  s  .c  o  m

        if (argClass == Integer.TYPE) {
            mv().push(((Number) argValue).intValue());
            mv().ifCmp(argType, mv().NE, next);
        } else if (argClass == Long.TYPE) {
            mv().push(((Number) argValue).longValue());
            mv().ifCmp(argType, mv().NE, next);
        } else if (argClass.isPrimitive()) {
            throw new CompilerException.UnsupportedDataType("The type '" + argClass
                    + "' is not supported as an output parameter type for '" + this.method + "'.");
        } else {
            mv().visitLdcInsn(argValue);
            mv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
            mv().ifZCmp(mv().EQ, next);
        }

    }

    mv().loadThis();
    mv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, this.section.classInternalName(), caseMethodName,
            this.getterDescriptor);
    mv().returnValue();

    mv().visitLabel(next);

    return caseMethodName;
}

From source file:org.formulacompiler.compiler.internal.bytecode.SectionCompiler.java

License:Open Source License

public void compileCallToGetterFor(GeneratorAdapter _mv, SubSectionCompiler _sub) {
    _mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classInternalName(), _sub.getterName(), _sub.getterDescriptor());
}

From source file:org.formulacompiler.compiler.internal.bytecode.SubSectionOutputAccessorCompiler.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w w w .j  a v  a  2 s  .  co  m*/
protected void compileBody() throws CompilerException {
    final SubSectionCompiler sub = this.sub;
    final GeneratorAdapter mv = mv();

    final CallFrame outputCall = this.callToImplement;
    final Class outputContainerClass = outputCall.getMethod().getReturnType();

    // get$Sect0()
    mv.loadThis();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, section().classInternalName(), sub.getterName(),
            sub.getterDescriptor());

    if (outputContainerClass.isArray()) {
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        // Detail[] arr = get$Sect0();
        final int l_arr = mv.newLocal(sub.arrayType());
        mv.storeLocal(l_arr);

        final int l_len = mv.newLocal(Type.INT_TYPE);
        mv.loadLocal(l_arr);
        mv.arrayLength();
        mv.storeLocal(l_len);

        // List lst = new ArrayList( arr.length );
        final int l_lst = mv.newLocal(ARRAYLIST_CLASS);
        mv.newInstance(ARRAYLIST_CLASS);
        mv.dup();
        mv.loadLocal(l_len);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, ARRAYLIST_CLASS.getInternalName(), "<init>", "(I)V");
        mv.storeLocal(l_lst);

        // for (int i = 0; i < len; i++) {
        final int l_i = mv.newLocal(Type.INT_TYPE);
        mv.push(0);
        mv.storeLocal(l_i);
        final Label test = mv.newLabel();
        mv.goTo(test);
        final Label again = mv.mark();

        // lst.add( arr[ i ] );
        mv.loadLocal(l_lst);
        mv.loadLocal(l_arr);
        mv.loadLocal(l_i);
        mv.arrayLoad(sub.classType());
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ARRAYLIST_CLASS.getInternalName(), "add",
                "(Ljava/lang/Object;)Z");
        mv.pop();

        // } // for
        mv.iinc(l_i, 1);
        mv.mark(test);
        mv.loadLocal(l_i);
        mv.loadLocal(l_len);
        mv.ifCmp(Type.INT_TYPE, mv.LT, again);

        mv.loadLocal(l_lst);
        if (outputContainerClass.isAssignableFrom(List.class)) {
            // return lst;
            mv.visitInsn(Opcodes.ARETURN);
        } else if (outputContainerClass.isAssignableFrom(Iterator.class)) {
            // return lst.iterator();
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, ARRAYLIST_CLASS.getInternalName(), "iterator",
                    "()" + ITERATOR_INTF.getDescriptor());
            mv.visitInsn(Opcodes.ARETURN);
        } else {
            throw new CompilerException.UnsupportedDataType("The return type of '" + outputCall.getMethod()
                    + "' is not supported as input to a repeating section.");
        }
    }
}

From source file:org.formulacompiler.compiler.internal.bytecode.TypeCompilerForScaledBigDecimals.java

License:Open Source License

@Override
final void compileAdjustment(GeneratorAdapter _mv) {
    if (needsAdjustment()) {
        _mv.push(this.fixedScale);
        _mv.push(this.roundingMode);
        _mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, BNAME, "setScale", "(II)" + B);
    }//from   w w w  .ja va 2  s.  c  om
}