Example usage for org.objectweb.asm Opcodes F_FULL

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

Introduction

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

Prototype

int F_FULL

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

Click Source Link

Document

A compressed frame with complete frame data.

Usage

From source file:javaone2015.con7442.indyprotector.BootstrapMethodGenerator.java

License:Apache License

/**
 * Generate bootstrap method/*from  w  w w  . j a  v a  2s .  c o  m*/
 */
public void insertMethod(ClassVisitor target) {
    MethodVisitor mv = target.visitMethod(ACC_PRIVATE + ACC_STATIC, BSM_NAME, BSM_SIG, null, null);
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitInsn(ACONST_NULL);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 4);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
            false);
    mv.visitVarInsn(ASTORE, 8);
    mv.visitLdcInsn(Type.getType(targetClassName));
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false);
    mv.visitVarInsn(ASTORE, 9);
    mv.visitVarInsn(ALOAD, 6);
    mv.visitVarInsn(ALOAD, 9);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/invoke/MethodType", "fromMethodDescriptorString",
            "(Ljava/lang/String;Ljava/lang/ClassLoader;)Ljava/lang/invoke/MethodType;", false);
    mv.visitVarInsn(ASTORE, 10);
    mv.visitVarInsn(ILOAD, 3);
    Label l3 = new Label();
    Label l4 = new Label();
    Label l5 = new Label();
    mv.visitTableSwitchInsn(182, 185, l4, new Label[] { l3, l4, l5, l3 });
    mv.visitLabel(l5);
    mv.visitFrame(Opcodes.F_FULL, 11,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle", "java/lang/Class",
                    "java/lang/ClassLoader", "java/lang/invoke/MethodType" },
            0, new Object[] {});
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findStatic",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    Label l6 = new Label();
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l3);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 8);
    mv.visitVarInsn(ALOAD, 5);
    mv.visitVarInsn(ALOAD, 10);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandles$Lookup", "findVirtual",
            "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;",
            false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitJumpInsn(GOTO, l6);
    mv.visitLabel(l4);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l6);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "asType",
            "(Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/MethodHandle;", false);
    mv.visitVarInsn(ASTORE, 7);
    mv.visitLabel(l1);
    Label l7 = new Label();
    mv.visitJumpInsn(GOTO, l7);
    mv.visitLabel(l2);
    mv.visitFrame(Opcodes.F_FULL, 8,
            new Object[] { "java/lang/invoke/MethodHandles$Lookup", "java/lang/String",
                    "java/lang/invoke/MethodType", Opcodes.INTEGER, "java/lang/String", "java/lang/String",
                    "java/lang/String", "java/lang/invoke/MethodHandle" },
            1, new Object[] { "java/lang/Exception" });
    mv.visitVarInsn(ASTORE, 8);
    mv.visitTypeInsn(NEW, "java/lang/BootstrapMethodError");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/BootstrapMethodError", "<init>", "()V", false);
    mv.visitInsn(ATHROW);
    mv.visitLabel(l7);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitTypeInsn(NEW, "java/lang/invoke/ConstantCallSite");
    mv.visitInsn(DUP);
    mv.visitVarInsn(ALOAD, 7);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/invoke/ConstantCallSite", "<init>",
            "(Ljava/lang/invoke/MethodHandle;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(4, 11);
    mv.visitEnd();

}

From source file:lombok.patcher.scripts.SetSymbolDuringMethodCallScript.java

License:Open Source License

private void makeWrapperMethod(ClassVisitor cv, WrapperMethodDescriptor wmd) {
    MethodVisitor mv = cv.visitMethod(Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
            wmd.getWrapperName(), wmd.getWrapperDescriptor(), null, null);

    MethodLogistics logistics = new MethodLogistics(Opcodes.ACC_STATIC, wmd.getWrapperDescriptor());

    mv.visitCode();//from  ww w .j  av  a2s. com
    Label start = new Label();
    Label end = new Label();
    Label handler = new Label();
    mv.visitTryCatchBlock(start, end, handler, null);
    mv.visitLabel(start);
    mv.visitLdcInsn(symbol);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "push", "(Ljava/lang/String;)V", false);
    for (int i = 0; i < logistics.getParamCount(); i++) {
        logistics.generateLoadOpcodeForParam(i, mv);
    }
    mv.visitMethodInsn(wmd.getOpcode(), wmd.getOwner(), wmd.getName(), wmd.getTargetDescriptor(), wmd.isItf());
    mv.visitLabel(end);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "pop", "()V", false);
    logistics.generateReturnOpcode(mv);
    mv.visitLabel(handler);
    mv.visitFrame(Opcodes.F_FULL, 0, null, 1, new Object[] { "java/lang/Throwable" });
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "lombok/patcher/Symbols", "pop", "()V", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitMaxs(Math.max(1, logistics.getParamCount()), logistics.getParamCount());
    mv.visitEnd();
}

From source file:org.copperengine.core.instrument.BuildStackInfoAdapter.java

License:Apache License

@Override
public void visitFrame(int arg0, int arg1, Object[] arg2, int arg3, Object[] arg4) {
    savePreviousFrame();/*from  www .  ja  v a2s .c  om*/
    if (logger.isDebugEnabled())
        logger.debug("stackBefore: " + currentFrame.stack);
    if (logger.isDebugEnabled())
        logger.debug("localBefore: " + currentFrame.localsToString());
    currentFrame = new StackInfo(lastDeclaredFrame);
    switch (arg0) {
    case F_SAME:
        // representing frame with exactly the same locals as the previous frame and with the empty stack.
        currentFrame.stack.clear();
        break;
    case F_SAME1:
        // representing frame with exactly the same locals as the previous frame and with single value
        // on the stack (nStack is 1 and stack[0] contains value for the type of the stack item).
        Type t = StackInfo.deferLocalDesc(arg4[0]);
        currentFrame.stack.clear();
        currentFrame.stack.push(t);
        break;
    case F_APPEND:
        // representing frame with current locals are the same as the locals in the previous frame,
        // except that additional locals are defined (nLocal is 1, 2 or 3 and local elements contains
        // values representing added types).
        currentFrame.appendLocals(arg1, arg2);
        break;
    case F_CHOP:
        // Opcodes.F_CHOP representing frame with current locals are the same as the locals in the
        // previous frame, except that the last 1-3 locals are absent and with the empty stack (nLocals
        // is 1, 2 or 3).
        currentFrame.removeLocals(arg1);
        currentFrame.stack.clear();
        break;
    case Opcodes.F_FULL: // representing complete frame data.
    case Opcodes.F_NEW:
        currentFrame.clearFrame();
        currentFrame.appendLocals(arg1, arg2);
        currentFrame.appendStack(arg3, arg4);
        break;
    default:
        throw new BuildStackFrameException("Unkwnon frame type " + arg0);

    }
    lastDeclaredFrame = new StackInfo(currentFrame);
    if (logger.isDebugEnabled())
        logger.debug("stack: " + currentFrame.stack);
    if (logger.isDebugEnabled())
        logger.debug("local: " + currentFrame.localsToString());
    if (logger.isDebugEnabled())
        logger.debug("frame " + getFrameType(arg0) + " '" + arg1 + "' '" + Arrays.asList(arg2) + "' '" + arg3
                + "' '" + Arrays.asList(arg4) + "'");
    delegate.visitFrame(arg0, arg1, arg2, arg3, arg4);
}

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

License:Open Source License

/**
 *    public int myMethod(int i)/*from  w w  w.  j  a  va2s .c om*/
   {
 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.glassfish.pfl.tf.tools.enhancer.SimpleMethodTracer.java

License:Open Source License

private String getFrameType(int type) {
    switch (type) {
    case Opcodes.F_APPEND:
        return "APPEND";
    case Opcodes.F_CHOP:
        return "CHOP";
    case Opcodes.F_FULL:
        return "FULL";
    case Opcodes.F_NEW:
        return "NEW";
    case Opcodes.F_SAME:
        return "SAME";
    case Opcodes.F_SAME1:
        return "SAME1";
    }//from  w  w  w.  java 2  s.c  om
    return "BAD_FRAME_TYPE";
}

From source file:org.jacoco.core.internal.analysis.filter.AbstractMatcherTest.java

License:Open Source License

@Test
public void skipNonOpcodes() {
    m.visitFrame(Opcodes.F_FULL, 0, null, 0, null);
    final Label label = new Label();
    m.visitLabel(label);/*from   w w  w  .  ja v  a 2 s . co m*/
    m.visitLineNumber(42, label);
    m.visitInsn(Opcodes.NOP);

    // should skip all non opcodes
    matcher.cursor = m.instructions.getFirst();
    matcher.skipNonOpcodes();
    assertSame(m.instructions.getLast(), matcher.cursor);

    // should not change cursor when it points on instruction with opcode
    matcher.skipNonOpcodes();
    assertSame(m.instructions.getLast(), matcher.cursor);

    // should not do anything when cursor is null
    matcher.cursor = null;
    matcher.skipNonOpcodes();
}

From source file:org.jacoco.core.internal.flow.FrameSnapshotTest.java

License:Open Source License

@Test
public void should_capture_frame_when_frame_is_defined() {
    analyzer.visitInsn(Opcodes.FCONST_0);
    analyzer.visitVarInsn(Opcodes.FSTORE, 1);
    analyzer.visitInsn(Opcodes.ICONST_0);
    frame = FrameSnapshot.create(analyzer, 0);

    expectedVisitor.visitFrame(Opcodes.F_FULL, 2, arr("Foo", Opcodes.FLOAT), 1, arr(Opcodes.INTEGER));
}

From source file:org.jacoco.core.internal.flow.FrameSnapshotTest.java

License:Open Source License

@Test
public void should_combine_slots_when_doube_or_long_types_are_given() {
    analyzer.visitInsn(Opcodes.DCONST_0);
    analyzer.visitVarInsn(Opcodes.DSTORE, 1);
    analyzer.visitInsn(Opcodes.FCONST_0);
    analyzer.visitVarInsn(Opcodes.FSTORE, 3);

    analyzer.visitInsn(Opcodes.ICONST_0);
    analyzer.visitInsn(Opcodes.LCONST_0);
    analyzer.visitInsn(Opcodes.ICONST_0);
    analyzer.visitInsn(Opcodes.DCONST_0);
    frame = FrameSnapshot.create(analyzer, 0);

    final Object[] vars = arr("Foo", Opcodes.DOUBLE, Opcodes.FLOAT);
    final Object[] stack = arr(Opcodes.INTEGER, Opcodes.LONG, Opcodes.INTEGER, Opcodes.DOUBLE);
    expectedVisitor.visitFrame(Opcodes.F_FULL, 3, vars, 4, stack);
}

From source file:org.jacoco.core.internal.flow.FrameSnapshotTest.java

License:Open Source License

@Test
public void should_decrease_stack_when_popCount_is_given() {
    analyzer.visitInsn(Opcodes.ICONST_0);
    analyzer.visitInsn(Opcodes.LCONST_0);
    analyzer.visitInsn(Opcodes.ICONST_0);
    analyzer.visitInsn(Opcodes.ICONST_0);
    frame = FrameSnapshot.create(analyzer, 2);

    final Object[] stack = arr(Opcodes.INTEGER, Opcodes.LONG);
    expectedVisitor.visitFrame(Opcodes.F_FULL, 1, arr("Foo"), 2, stack);
}

From source file:org.jacoco.core.internal.flow.FrameSnapshotTest.java

License:Open Source License

/**
 * Test of <a href="https://gitlab.ow2.org/asm/asm/issues/317793">ASM
 * bug</a>: according to <a href=
 * "https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.10.1.9.aaload">JVMS
 * "4.10.1.9 Type Checking Instructions, AALOAD"</a> resulting type on the
 * operand stack should be null if the input array is null.
 *///w  ww  .  j av a  2 s .  c  o  m
@Test
public void after_aaload_stack_should_contain_null_when_input_array_is_null() {
    analyzer.visitInsn(Opcodes.ACONST_NULL);
    analyzer.visitInsn(Opcodes.ICONST_0);
    analyzer.visitInsn(Opcodes.AALOAD);
    frame = FrameSnapshot.create(analyzer, 0);

    final Object[] stack = arr(Opcodes.NULL);
    expectedVisitor.visitFrame(Opcodes.F_FULL, 1, arr("Foo"), 1, stack);
}