Example usage for org.objectweb.asm Opcodes RETURN

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

Introduction

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

Prototype

int RETURN

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

Click Source Link

Usage

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

/**
 *    public int myMethod(int i)/*from ww  w. j a  v  a  2 s  .  com*/
   {
 try
 {
    return _sw_prototype_original_myMethod(i)
 }
 finally
 {
    Capturer.enable();
 }
   }
        
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;

    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
            methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;

    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;

    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;

    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);

    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();

    final InsnList wInstructions = wrappingMethodNode.instructions;

    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor 

        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;

        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

        int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);

            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }

    int varReturnValue = 0;

    final Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));

    } else {

        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));

        //--- create "Object returnValue = null;"

        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }

        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;

            // long/double take two registers
            if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
                varReturnValue++;
            }
        }

        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }

    int var = 0;

    // --- L0
    wInstructions.add(l0);

    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className,
            wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));

    // --- construct call to wrapped methode

    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }

    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            var++;
        }
    }

    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }

    var++;

    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));

        // --- L1

        wInstructions.add(l1);

        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));

        // FIXME <--- DUPLICATE CODE

        // --- L2

        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result

        this.addBoxingStmt(wInstructions, returnType);

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));

        this.addUnBoxingStmt(wInstructions, returnType);

        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var

        // --- L1

        wInstructions.add(l1);

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));

        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);

        //---- L2

        wInstructions.add(l2);

        wInstructions.add(
                new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) },
                        1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}

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

License:Open Source License

/**
 * Creates a no-args constructor./*  www  .j av  a 2s. c om*/
 *
 * @param cw the class writer
 */
public static void writeConstructor(ClassWriter cw, Class<?> superType) {
    String descriptor = Type.getDescriptor(ServiceInvoker.class);

    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    Label label = new Label();
    mv.visitLabel(label);
    mv.visitLineNumber(6, label);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(superType), "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitLocalVariable("this", descriptor, null, label, l1, 0);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

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

License:Open Source License

private void finalizeStaticInitializer() {
    if (this.initializer != null) {
        final GeneratorAdapter ma = this.initializer;
        ma.visitInsn(Opcodes.RETURN);
        endMethod(ma);/*from  w  w w.  j  a v a  2s  . co  m*/
        this.initializer = null;
    }
}

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

License:Open Source License

private void buildDefaultConstructor(Type _parentType) {
    final GeneratorAdapter mv = newMethod(Opcodes.ACC_PUBLIC, "<init>", ENV_CONSTRUCTOR_SIG);
    mv.visitCode();//w ww  .j a va2  s  .co  m
    mv.loadThis();
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, _parentType.getInternalName(), "<init>", "()V");
    mv.loadThis();
    mv.loadArg(0);
    mv.putField(this.classType(), ByteCodeEngineCompiler.ENV_MEMBER_NAME, ByteCodeEngineCompiler.ENV_CLASS);
    mv.visitInsn(Opcodes.RETURN);
    endMethod(mv);
}

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

License:Open Source License

@Override
protected void buildConstructorWithInputs() throws CompilerException {

    this.constructor = new MethodCompiler(RootSectionCompiler.this, 0, "<init>",
            "(" + inputType().getDescriptor() + ENV_DESC + ")V") {
        @Override/*from ww w  .ja  v  a2s  . co m*/
        protected void compileBody() throws CompilerException {
            final GeneratorAdapter mv = mv();

            // super( _inputs ); or super();
            callInheritedConstructor(mv, 1);

            // this.environment = _environment;
            mv.loadThis();
            mv.loadArg(1);
            mv.putField(section().classType(), ENV_MEMBER_NAME, ENV_CLASS);

            // this.inputs = _inputs;
            if (hasInputs()) {
                mv.loadThis();
                mv.loadArg(0);
                storeInputs(mv);
            }

            //this.sectionInfo = new SectionInfoImpl(...);
            if (isComputationListenerEnabled()) {
                mv.loadThis();
                mv.push(-1); //section index
                final ExpressionCompilerForNumbers c = numericCompiler();
                c.compile_util_createSectionInfo(model().getName(), null, BROKEN_REF, BROKEN_REF, null,
                        BROKEN_REF, BROKEN_REF);
                mv.putField(section().classType(), SECTION_INFO_MEMBER_NAME, SECTION_INFO_CLASS);
            }

            if (RootSectionCompiler.this.computationTimeCompiled) {
                mv.loadThis();
                mv.newInstance(COMP_TIME_CLASS);
                mv.dup();
                mv.invokeConstructor(COMP_TIME_CLASS, EMPTY_CONSTRUCTOR_METHOD);
                mv.putField(section().classType(), COMP_TIME_MEMBER_NAME, COMP_TIME_CLASS);
            }

            if (RootSectionCompiler.this.computationModeCompiled) {
                mv.loadThis();
                final ComputationMode computationMode = model().getEngine().getComputationMode();
                mv.getStatic(COMP_MODE_CLASS, computationMode.name(), COMP_MODE_CLASS);
                mv.putField(classType(), COMP_MODE_MEMBER_NAME, COMP_MODE_CLASS);
            }

            mv.visitInsn(Opcodes.RETURN);
        }
    };
}

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

License:Open Source License

private void finalizeReset() {
    if (this.resetter != null) {
        this.resetter.visitInsn(Opcodes.RETURN);
        endMethod(this.resetter);
        this.resetter = null;
    }// w  ww.  ja v a2 s. com
}

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

License:Open Source License

@Override
protected void buildConstructorWithInputs() throws CompilerException {
    final StringBuilder descriptor = new StringBuilder("(");
    descriptor.append(inputType().getDescriptor());
    descriptor.append(parentType().getDescriptor());
    if (this.isComputationListenerEnabled())
        descriptor.append(INDEX_TYPE.getDescriptor());
    descriptor.append(")V");

    final MethodCompiler constructorCompiler = new MethodCompiler(this, 0, "<init>", descriptor.toString()) {
        @Override/*  w w w . ja  v a2  s . com*/
        protected void compileBody() throws CompilerException {
            final GeneratorAdapter mv = mv();

            // super( _inputs ); or super(); or super( _inputs, _parent );
            callInheritedConstructor(mv, 1);

            // this.parent = _parent;
            mv.loadThis();
            mv.loadArg(1);
            mv.putField(classType(), PARENT_MEMBER_NAME, parentType());
            // this.root = _parent.root();
            mv.loadThis();
            mv.loadArg(1);
            final Type rootType = rootSectionCompiler().classType();
            if (!(parentSectionCompiler() instanceof RootSectionCompiler)) {
                // parent.root is package visible
                mv.getField(parentType(), ROOT_MEMBER_NAME, rootType);
            }
            mv.putField(SubSectionCompiler.this.classType(), ROOT_MEMBER_NAME, rootType);

            // this.inputs = _inputs;
            if (hasInputs()) {
                mv.loadThis();
                mv.loadArg(0);
                storeInputs(mv);
            }

            //this.sectionInfo = new SectionInfoImpl(...);
            if (isComputationListenerEnabled()) {
                mv.loadThis();
                mv.loadArg(2); //section index
                final ExpressionCompilerForNumbers c = numericCompiler();
                final SectionModel sectionModel = model();
                final RangeAddress range = (RangeAddress) model().getSource();
                final CellAddress topLeft = range.getTopLeft();
                final CellAddress bottomRight = range.getBottomRight();
                c.compile_util_createSectionInfo(sectionModel.getName(), topLeft.getSheetName(),
                        topLeft.getRowIndex(), topLeft.getColumnIndex(), bottomRight.getSheetName(),
                        bottomRight.getRowIndex(), bottomRight.getColumnIndex());
                mv.putField(section().classType(), SECTION_INFO_MEMBER_NAME, SECTION_INFO_CLASS);
            }

            mv.visitInsn(Opcodes.RETURN);
        }
    };
    constructorCompiler.compile();
}

From source file:org.glassfish.pfl.tf.spi.Util.java

License:Open Source License

public void storeFromXReturn(MethodVisitor mv, int returnOpcode, LocalVariableNode holder) {

    switch (returnOpcode) {
    case Opcodes.RETURN:
        // NOP/*from w w  w  . j  a  va  2  s  .  c  om*/
        break;
    case Opcodes.ARETURN:
        mv.visitVarInsn(Opcodes.ASTORE, holder.index);
        break;
    case Opcodes.IRETURN:
        mv.visitVarInsn(Opcodes.ISTORE, holder.index);
        break;
    case Opcodes.LRETURN:
        mv.visitVarInsn(Opcodes.LSTORE, holder.index);
        break;
    case Opcodes.FRETURN:
        mv.visitVarInsn(Opcodes.FSTORE, holder.index);
        break;
    case Opcodes.DRETURN:
        mv.visitVarInsn(Opcodes.DSTORE, holder.index);
        break;
    }
}

From source file:org.glassfish.pfl.tf.spi.Util.java

License:Open Source License

public void loadFromXReturn(MethodVisitor mv, int returnOpcode, LocalVariableNode holder) {

    switch (returnOpcode) {
    case Opcodes.RETURN:
        // NOP//from  w ww.  j  a  v a2  s.  c  om
        break;
    case Opcodes.ARETURN:
        mv.visitVarInsn(Opcodes.ALOAD, holder.index);
        break;
    case Opcodes.IRETURN:
        mv.visitVarInsn(Opcodes.ILOAD, holder.index);
        break;
    case Opcodes.LRETURN:
        mv.visitVarInsn(Opcodes.LLOAD, holder.index);
        break;
    case Opcodes.FRETURN:
        mv.visitVarInsn(Opcodes.FLOAD, holder.index);
        break;
    case Opcodes.DRETURN:
        mv.visitVarInsn(Opcodes.DLOAD, holder.index);
        break;
    }
}

From source file:org.glassfish.pfl.tf.tools.enhancer.ClassEnhancer.java

License:Open Source License

@Override
public void visitEnd() {
    info(2, "visitEnd");
    // Add the additional fields
    final String desc = Type.getDescriptor(SynchronizedHolder.class);

    final int acc = Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC;

    // Signature is actually L../SynchronizedHolder;<L.../MethodMonitor;>
    // where the ... are replaced with appropriate packages.  Not
    // that we actually need a signature here.
    final String sig = null;

    for (String fname : ecd.getAnnotationToHolderName().values()) {
        info(2, "Adding field " + fname + " of type " + desc);
        cv.visitField(acc, fname, desc, sig, null);
    }//from w w  w .j a  v  a 2s .  c o  m

    if (!hasStaticInitializer) {
        info(2, "creating static init");
        int siacc = Opcodes.ACC_STATIC + Opcodes.ACC_PRIVATE;
        MethodVisitor mv = cv.visitMethod(siacc, "<clinit>", "()V", null, null);
        if (util.getDebug()) {
            mv = new SimpleMethodTracer(mv, util);
        }
        MethodAdapter ma = new StaticInitVisitor(siacc, "()V", mv, util, ecd);

        ma.visitCode();
        ma.visitInsn(Opcodes.RETURN); // Only if creating a <clinit>!

        ma.visitMaxs(0, 0);
        ma.visitEnd();
    }

    super.visitEnd();

    ecd.updateInfoDesc();
}