Example usage for org.objectweb.asm Opcodes LONG

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

Introduction

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

Prototype

Integer LONG

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

Click Source Link

Usage

From source file:org.jacoco.core.internal.instr.ProbeVariableInserterTest.java

License:Open Source License

@Test
public void testVisitFrame() {
    final FrameRecorder rec = new FrameRecorder();
    ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

    // The first (implicit) frame must not be modified:
    i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG }, 0, new Object[0]);
    assertEquals(Arrays.asList((Object) "LFoo;", Opcodes.LONG), rec.frame.local);

    // Starting from the second frame on the probe variable is inserted:
    i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG, "Ljava/lang/String;" }, 0,
            new Object[0]);
    assertEquals(Arrays.asList((Object) "LFoo;", Opcodes.LONG, "[Z", "Ljava/lang/String;"), rec.frame.local);
}

From source file:org.jacoco.core.internal.instr.ProbeVariableInserterTest.java

License:Open Source License

@Test
public void testInsertProbeFrame() {
    final FrameRecorder rec = new FrameRecorder();
    ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

    // The first (implicit) frame:
    i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG }, 0, new Object[0]);

    // There must be a label which is a multi-jump target:
    Label label = new Label();
    LabelInfo.setTarget(label);//from   w w  w.j  a  v  a 2  s  .com
    LabelInfo.setTarget(label);
    i.visitLabel(label);

    // Insert a frame for this label:
    i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG, "Ljava/lang/String;" }, 0,
            new Object[0]);

    // Insert this frame again:
    rec.frame = null;
    i.insertProbeFrame(label);

    assertEquals(Arrays.asList((Object) "LFoo;", Opcodes.LONG, "[Z", "Ljava/lang/String;"), rec.frame.local);
}

From source file:org.jacoco.core.internal.instr.ProbeVariableInserterTest.java

License:Open Source License

@Test
public void testInsertProbeFrameNoFrameForLabel() {
    final FrameRecorder rec = new FrameRecorder();
    ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

    // The first (implicit) frame:
    i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG }, 0, new Object[0]);

    // There must be a label which is a multi-jump target:
    Label label = new Label();
    LabelInfo.setTarget(label);/*www. j  ava2 s.co m*/
    LabelInfo.setTarget(label);
    i.visitLabel(label);

    // Insert a frame for this label:
    i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG, "Ljava/lang/String;" }, 0,
            new Object[0]);

    // Try to insert a frame for a different label:
    rec.frame = null;
    i.insertProbeFrame(new Label());
    assertNull(rec.frame);
}

From source file:org.jacoco.core.internal.instr.ProbeVariableInserterTest.java

License:Open Source License

@Test
public void testInsertProbeFrameNoMultiTarget() {
    final FrameRecorder rec = new FrameRecorder();
    ProbeVariableInserter i = new ProbeVariableInserter(0, "(J)V", rec);

    // The first (implicit) frame:
    i.visitFrame(Opcodes.F_NEW, 2, new Object[] { "LFoo;", Opcodes.LONG }, 0, new Object[0]);

    // The frame for this label isn't required:
    Label label = new Label();
    i.visitLabel(label);//from  w w w.j  a  v  a2s .co m

    // Insert a frame for this label:
    i.visitFrame(Opcodes.F_NEW, 3, new Object[] { "LFoo;", Opcodes.LONG, "Ljava/lang/String;" }, 0,
            new Object[0]);

    // Inserting a frame for the label shouldn't work:
    rec.frame = null;
    i.insertProbeFrame(label);
    assertNull(rec.frame);
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

private void dumpType(StringBuffer buffer, Object t) {

    if (t == Opcodes.TOP) {
        buffer.append("TOP");
    } else if (t == null) {
        buffer.append("null");
    } else if (t == Opcodes.INTEGER) {
        buffer.append("int");
    } else if (t == Opcodes.FLOAT) {
        buffer.append("float");
    } else if (t == Opcodes.DOUBLE) {
        buffer.append("double");
    } else if (t == Opcodes.LONG) {
        buffer.append("long");
    } else if (t == Opcodes.NULL) {
        buffer.append("null");
    } else if (t == Opcodes.UNINITIALIZED_THIS) {
        buffer.append("uninit_this");
    } else if (t instanceof String) {
        buffer.append((String) t);
    } else {//  w  w  w. j  a va2  s  . co m
        buffer.append(((Label) t).getOffset());
    }
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

public void visitFrame(final int type, final int nLocal, final Object[] local, final int nStack,
        final Object[] stack) {
    if (type != Opcodes.F_NEW) { // uncompressed frame
        throw new IllegalStateException("ClassReader.accept() should be called with EXPAND_FRAMES flag");
    }/*from w w  w. j  a  v  a 2 s  . c o m*/

    // dumpFrame(nLocal, local, nStack, stack);

    // adjust the local types array

    int toRemove = localTypes.size();

    for (int i = toRemove; i > 0; i--) {
        localTypes.remove(i - 1);
    }

    int nextLocal = 0;

    for (int i = 0; i < nLocal; i++) {
        Object t = local[i];
        if (t == Opcodes.TOP) {
            localTypes.add(null);
        } else if (t == null) {
            localTypes.add(null);
        } else if (t == Opcodes.INTEGER) {
            localTypes.add(Type.INT_TYPE);
        } else if (t == Opcodes.FLOAT) {
            localTypes.add(Type.FLOAT_TYPE);
        } else if (t == Opcodes.DOUBLE) {
            localTypes.add(Type.DOUBLE_TYPE);
            nextLocal++;
            localTypes.add(null);
        } else if (t == Opcodes.LONG) {
            localTypes.add(Type.LONG_TYPE);
            nextLocal++;
            localTypes.add(null);
        } else if (t == Opcodes.NULL) {
            localTypes.add(null);
        } else if (t == Opcodes.UNINITIALIZED_THIS) {
            localTypes.add(null);
        } else if (t instanceof String) {
            localTypes.add(Type.getObjectType((String) t));
        } else {
            localTypes.add(null);
        }
        nextLocal++;
    }

    this.nextLocal = nextLocal;

    mv.visitFrame(type, nLocal, local, nStack, stack);
}

From source file:org.spongepowered.asm.util.Locals.java

License:MIT License

/**
 * <p>Attempts to identify available locals at an arbitrary point in the
 * bytecode specified by node.</p>
 * //w ww.jav  a 2s. co m
 * <p>This method builds an approximate view of the locals available at an
 * arbitrary point in the bytecode by examining the following features in
 * the bytecode:</p> 
 * <ul>
 *   <li>Any available stack map frames</li>
 *   <li>STORE opcodes</li>
 *   <li>The local variable table</li>
 * </ul>
 * 
 * <p>Inference proceeds by walking the bytecode from the start of the
 * method looking for stack frames and STORE opcodes. When either of these
 * is encountered, an attempt is made to cross-reference the values in the
 * stack map or STORE opcode with the value in the local variable table
 * which covers the code range. Stack map frames overwrite the entire
 * simulated local variable table with their own value types, STORE opcodes
 * overwrite only the local slot to which they pertain. Values in the
 * simulated locals array are spaced according to their size (unlike the
 * representation in FrameNode) and this TOP, NULL and UNINTITIALIZED_THIS
 * opcodes will be represented as null values in the simulated frame.</p>
 * 
 * <p>This code does not currently simulate the prescribed JVM behaviour
 * where overwriting the second slot of a DOUBLE or LONG actually
 * invalidates the DOUBLE or LONG stored in the previous location, so we
 * have to hope (for now) that this behaviour isn't emitted by the compiler
 * or any upstream transformers. I may have to re-think this strategy if
 * this situation is encountered in the wild.</p>
 * 
 * @param classNode ClassNode containing the method, used to initialise the
 *      implicit "this" reference in simple methods with no stack frames
 * @param method MethodNode to explore
 * @param node Node indicating the position at which to determine the locals
 *      state. The locals will be enumerated UP TO the specified node, so
 *      bear in mind that if the specified node is itself a STORE opcode,
 *      then we will be looking at the state of the locals PRIOR to its
 *      invocation
 * @return A sparse array containing a view (hopefully) of the locals at the
 *      specified location
 */
public static LocalVariableNode[] getLocalsAt(ClassNode classNode, MethodNode method, AbstractInsnNode node) {
    LocalVariableNode[] frame = new LocalVariableNode[method.maxLocals];
    int local = 0, index = 0;

    // Initialise implicit "this" reference in non-static methods
    if ((method.access & Opcodes.ACC_STATIC) == 0) {
        frame[local++] = new LocalVariableNode("this", classNode.name, null, null, null, 0);
    }

    // Initialise method arguments
    for (Type argType : Type.getArgumentTypes(method.desc)) {
        frame[local] = new LocalVariableNode("arg" + index++, argType.toString(), null, null, null, local);
        local += argType.getSize();
    }

    for (Iterator<AbstractInsnNode> iter = method.instructions.iterator(); iter.hasNext();) {
        AbstractInsnNode insn = iter.next();
        if (insn instanceof FrameNode) {
            FrameNode frameNode = (FrameNode) insn;

            // localPos tracks the location in the frame node's locals list, which doesn't leave space for TOP entries
            for (int localPos = 0, framePos = 0; framePos < frame.length; framePos++, localPos++) {
                // Get the local at the current position in the FrameNode's locals list
                final Object localType = (localPos < frameNode.local.size()) ? frameNode.local.get(localPos)
                        : null;

                if (localType instanceof String) { // String refers to a reference type
                    frame[framePos] = Locals.getLocalVariableAt(classNode, method, node, framePos);
                } else if (localType instanceof Integer) { // Integer refers to a primitive type or other marker
                    boolean isMarkerType = localType == Opcodes.UNINITIALIZED_THIS || localType == Opcodes.TOP
                            || localType == Opcodes.NULL;
                    boolean is32bitValue = localType == Opcodes.INTEGER || localType == Opcodes.FLOAT;
                    boolean is64bitValue = localType == Opcodes.DOUBLE || localType == Opcodes.LONG;
                    if (isMarkerType) {
                        frame[framePos] = null;
                    } else if (is32bitValue || is64bitValue) {
                        frame[framePos] = Locals.getLocalVariableAt(classNode, method, node, framePos);

                        if (is64bitValue) {
                            framePos++;
                            frame[framePos] = null; // TOP
                        }
                    } else {
                        throw new RuntimeException(
                                "Unrecognised locals opcode " + localType + " in locals array at position "
                                        + localPos + " in " + classNode.name + "." + method.name + method.desc);
                    }
                } else if (localType == null) {
                    frame[framePos] = null;
                } else {
                    throw new RuntimeException("Invalid value " + localType + " in locals array at position "
                            + localPos + " in " + classNode.name + "." + method.name + method.desc);
                }
            }
        } else if (insn instanceof VarInsnNode) {
            VarInsnNode varNode = (VarInsnNode) insn;
            frame[varNode.var] = Locals.getLocalVariableAt(classNode, method, node, varNode.var);
        } else if (insn == node) {
            break;
        }
    }

    return frame;
}

From source file:org.yx.asm.AsmUtils.java

License:Apache License

public static List<Object> getImplicitFrame(String desc) {
    List<Object> locals = new ArrayList<>();
    if (desc.isEmpty()) {
        return locals;
    }//from   w  ww.  j  ava 2 s  .c  om
    int i = 0;
    while (desc.length() > i) {
        int j = i;
        switch (desc.charAt(i++)) {
        case 'Z':
        case 'C':
        case 'B':
        case 'S':
        case 'I':
            locals.add(Opcodes.INTEGER);
            break;
        case 'F':
            locals.add(Opcodes.FLOAT);
            break;
        case 'J':
            locals.add(Opcodes.LONG);
            break;
        case 'D':
            locals.add(Opcodes.DOUBLE);
            break;
        case '[':
            while (desc.charAt(i) == '[') {
                ++i;
            }
            if (desc.charAt(i) == 'L') {
                ++i;
                while (desc.charAt(i) != ';') {
                    ++i;
                }
            }
            locals.add(desc.substring(j, ++i));
            break;
        case 'L':
            while (desc.charAt(i) != ';') {
                ++i;
            }
            locals.add(desc.substring(j + 1, i++));
            break;
        default:
            break;
        }
    }

    return locals;
}

From source file:org.yx.asm.ProxyMethodWritor.java

License:Apache License

private static int load(MethodVisitor mv, Object type, int frameIndex) {
    if (Opcodes.INTEGER.equals(type)) {
        mv.visitVarInsn(ILOAD, frameIndex);
        return frameIndex + SINGLE;
    }//from  w w  w  .j a  v a2  s  . c o m
    if (Opcodes.LONG.equals(type)) {
        mv.visitVarInsn(LLOAD, frameIndex);
        return frameIndex + WIDTH;
    }
    if (Opcodes.FLOAT.equals(type)) {
        mv.visitVarInsn(FLOAD, frameIndex);
        return frameIndex + SINGLE;
    }
    if (Opcodes.DOUBLE.equals(type)) {
        mv.visitVarInsn(DLOAD, frameIndex);
        return frameIndex + WIDTH;
    }
    mv.visitVarInsn(ALOAD, frameIndex);
    return frameIndex + SINGLE;
}

From source file:org.yx.asm.ProxyMethodWritor.java

License:Apache License

private static void store(MethodVisitor mv, Object type, int frameIndex) {
    if (Opcodes.INTEGER.equals(type)) {
        mv.visitVarInsn(ISTORE, frameIndex);
    } else if (Opcodes.LONG.equals(type)) {
        mv.visitVarInsn(LSTORE, frameIndex);
    } else if (Opcodes.FLOAT.equals(type)) {
        mv.visitVarInsn(FSTORE, frameIndex);
    } else if (Opcodes.DOUBLE.equals(type)) {
        mv.visitVarInsn(DSTORE, frameIndex);
    } else {//from w w  w.  j a va  2 s . com
        mv.visitVarInsn(ASTORE, frameIndex);
    }
}