Example usage for org.objectweb.asm.tree InsnList add

List of usage examples for org.objectweb.asm.tree InsnList add

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList add.

Prototype

public void add(final InsnList insnList) 

Source Link

Document

Adds the given instructions to the end of this list.

Usage

From source file:de.scoopgmbh.copper.instrument.TryCatchBlockHandler.java

License:Apache License

@SuppressWarnings("unchecked")
public void instrument(ClassNode cn) {
    //if (1 == 1) return;

    for (MethodNode m : (List<MethodNode>) cn.methods) {
        if (!m.exceptions.contains(INTERRUPT_EXCEPTION_NAME) || m.tryCatchBlocks.isEmpty()) {
            continue;
        }/*  ww w .ja va 2 s . c o m*/
        logger.info("Instrument " + cn.name + "." + m.name);
        HashSet<Label> labels = new HashSet<Label>();
        for (TryCatchBlockNode catchNode : (List<TryCatchBlockNode>) m.tryCatchBlocks) {
            if (labels.contains(catchNode.handler.getLabel())) {
                // some handlers share their handling code - check it out to prevent double instrumentation
                logger.info("skipping node");
                continue;
            }
            labels.add(catchNode.handler.getLabel());

            LabelNode labelNode = catchNode.handler;
            AbstractInsnNode lineNumberNode = labelNode.getNext() instanceof LineNumberNode
                    ? labelNode.getNext()
                    : labelNode;
            FrameNode frameNode = (FrameNode) lineNumberNode.getNext();
            VarInsnNode varInsnNode = (VarInsnNode) frameNode.getNext();
            AbstractInsnNode insertPoint = varInsnNode;

            if (catchNode.type == null) {
                // this is probably a finally block;
                if (insertPoint.getNext() != null && insertPoint.getNext() instanceof LabelNode) {
                    insertPoint = insertPoint.getNext();
                }
            }

            LabelNode labelNode4ifeg = new LabelNode();
            InsnList newCode = new InsnList();
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.INSTANCEOF, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new JumpInsnNode(Opcodes.IFEQ, labelNode4ifeg));
            newCode.add(new VarInsnNode(Opcodes.ALOAD, varInsnNode.var));
            newCode.add(new TypeInsnNode(Opcodes.CHECKCAST, INTERRUPT_EXCEPTION_NAME));
            newCode.add(new InsnNode(Opcodes.ATHROW));
            newCode.add(labelNode4ifeg);
            m.instructions.insert(insertPoint, newCode);
        }
    }
}

From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilder.java

License:Open Source License

private static int createInstructions(final int param, final FieldNode field, final ClassNode node,
        final InsnList instructions) {
    final AbstractInsnNode nThis = new VarInsnNode(Opcodes.ALOAD, 0);
    final int opcode = Opcodes.ALOAD;
    final int nextParam = param + 1;
    final AbstractInsnNode unboxing = getUnboxingNode(field);

    instructions.add(nThis);
    final AbstractInsnNode nParam = new VarInsnNode(opcode, param);
    instructions.add(nParam);//from   w  ww.j  av a2s  . c o  m

    if (unboxing != null) {
        instructions.add(unboxing);
    }

    final AbstractInsnNode nPut = new FieldInsnNode(Opcodes.PUTFIELD, node.name, field.name, field.desc);
    instructions.add(nPut);

    return nextParam;
}

From source file:de.tuberlin.uebb.jbop.access.ConstructorBuilder.java

License:Open Source License

private static MethodNode createMethodNode(final ClassNode node) {
    final MethodNode constructor = new MethodNode();
    constructor.access = Opcodes.ACC_PUBLIC;
    constructor.name = "<init>";
    constructor.exceptions = Collections.emptyList();
    final InsnList list = new InsnList();
    // currently only call to noarg super constructor is supported
    final AbstractInsnNode nThis = new VarInsnNode(Opcodes.ALOAD, 0);
    final AbstractInsnNode nSuperConstructor = new MethodInsnNode(Opcodes.INVOKESPECIAL, node.superName,
            "<init>", "()V");
    list.add(nThis);
    list.add(nSuperConstructor);//from w  ww  .j a va2  s .c  om
    constructor.instructions = list;
    return constructor;
}

From source file:de.tuberlin.uebb.jbop.optimizer.array.FieldArrayValueInlinerTest.java

License:Open Source License

private InsnList initArray() {

    final InsnList list = new InsnList();
    list.add(new VarInsnNode(ALOAD, 0));
    list.add(new InsnNode(ICONST_1));
    list.add(new TypeInsnNode(ANEWARRAY, "de/tuberlin/uebb/jbop/optimizer/array/A"));
    list.add(new InsnNode(DUP));
    list.add(new InsnNode(ICONST_0));
    list.add(new TypeInsnNode(NEW, "de/tuberlin/uebb/jbop/optimizer/array/A"));
    list.add(new InsnNode(DUP));
    list.add(new MethodInsnNode(INVOKESPECIAL, "de/tuberlin/uebb/jbop/optimizer/array/A", "<init>", "()V"));
    list.add(new InsnNode(AASTORE));
    list.add(new FieldInsnNode(PUTFIELD, "de/tuberlin/uebb/jbop/optimizer/array/ChainedTestClass", "a",
            "[Lde/tuberlin/uebb/jbop/optimizer/array/A;"));
    return list;//from   w  w  w.jav a  2 s  .c o m
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Appends a Constructor with the given descriptors.
 * /*from w w w.  j  av a 2s  .  co m*/
 * @param constructorDesc
 *          the constructor desc
 * @param superConstructorDesc
 *          the super constructor desc
 * @return the class node builder
 */
public ClassNodeBuilder addConstructor(final String constructorDesc, final String superConstructorDesc) {
    if (isInterface) {
        return this;
    }
    addMethod("<init>", constructorDesc);//
    lastConstructorVarIndex = 1;
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final Type methodType = Type.getMethodType(superConstructorDesc);
    for (final Type parameterType : methodType.getArgumentTypes()) {
        list.add(new VarInsnNode(parameterType.getOpcode(ILOAD), lastConstructorVarIndex));
        lastConstructorVarIndex += parameterType.getSize();
    }
    list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.superName, "<init>", superConstructorDesc));
    list.add(new InsnNode(Opcodes.RETURN));
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Initializes a classField in the default lastConstructor
 * (eg: if fieldType of field "field" is "TestObject", field = new TestObject() is called).
 * /*from  w  w  w .ja  v a2s  . co m*/
 * Numbers and Strings are used to assign "field", every other value leads to new Object.
 * 
 * @param object
 *          the object to Use.
 * @return the abstract optimizer test
 */
public ClassNodeBuilder initWith(final Object object) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    if (object instanceof String || object instanceof Number || object instanceof Boolean
            || object instanceof Character) {
        list.add(new VarInsnNode(Opcodes.ALOAD, 0));
        final AbstractInsnNode numberNode = NodeHelper.getInsnNodeFor(object);
        list.add(numberNode);
        if (lastField.desc.startsWith("L") && object instanceof Number) {
            list.add(ConstructorBuilder.getBoxingNode(lastField));
        }
        list.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, lastField.name, lastField.desc));
    } else {
        list.add(new VarInsnNode(ALOAD, 0));
        list.add(newObject(lastField.desc));
        list.add(new MethodInsnNode(INVOKESPECIAL,
                StringUtils.removeEnd(StringUtils.removeStart(lastField.desc, "L"), ";"), "<init>", "()V"));
        list.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, lastField.name, lastField.desc));

    }
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Creates Instruction to instantiate a new Object of type "desc".
 * /*from   www  . j  a  va2 s  .c  om*/
 * @param desc
 *          the desc
 * @return the insn list
 */
private InsnList newObject(final String desc) {
    final InsnList list = new InsnList();
    final String fieldDesc = StringUtils.removeEnd(StringUtils.removeStart(desc, "L"), ";");
    final TypeInsnNode node = new TypeInsnNode(Opcodes.NEW, fieldDesc);
    list.add(node);
    list.add(new InsnNode(DUP));
    return list;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Inits the multi array with.//w w  w .  ja  va 2 s  .c  o  m
 * 
 * @param value
 *          the value
 * @param indexes
 *          the indexes
 * @return the class node builder
 */
public ClassNodeBuilder initMultiArrayWith(final Object value, final int... indexes) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    list.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, lastField.name, lastField.desc));
    for (int i = 0; i < indexes.length - 1; ++i) {
        list.add(NodeHelper.getInsnNodeFor(indexes[i]));
        list.add(new InsnNode(Opcodes.AALOAD));
    }
    list.add(NodeHelper.getInsnNodeFor(indexes[indexes.length - 1]));
    list.add(NodeHelper.getInsnNodeFor(value));
    final Type elementType = Type.getType(lastField.desc).getElementType();
    if (elementType.getDescriptor().startsWith("L")) {
        list.add(new InsnNode(Opcodes.AASTORE));
    } else {
        list.add(new InsnNode(toPrimitive(Type.getType(value.getClass())).getOpcode(Opcodes.IASTORE)));

    }
    addToConstructor(list);
    return this;
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

private void initArrayInternal(final int opcode, final Object... values) {
    final InsnList list = new InsnList();
    final int length = values.length;
    initArray(length);/*  ww  w .  j a v  a2s .c  o  m*/
    int index = 0;
    for (final Object number : values) {
        list.add(new VarInsnNode(Opcodes.ALOAD, lastConstructorVarIndex));
        final AbstractInsnNode indexNode = NodeHelper.getInsnNodeFor(index++);
        list.add(indexNode);
        final AbstractInsnNode numberNode = NodeHelper.getInsnNodeFor(number);
        list.add(numberNode);
        if (number instanceof Number && opcode == Opcodes.ASTORE) {
            list.add(ConstructorBuilder.getBoxingNode(lastField));
        }
        list.add(new InsnNode(opcode));
    }
    addToConstructor(list);
}

From source file:de.tuberlin.uebb.jbop.optimizer.ClassNodeBuilder.java

License:Open Source License

/**
 * Initializes a classField of type Array in the default lastConstructor with the given length.
 * (eg: if fieldType of field "field" is "double[]", field = new double[length] is called).
 * //from w w w. j av a 2  s. c o m
 * @param length
 *          the length
 * @return the abstract optimizer test
 */
public ClassNodeBuilder initArray(final int... length) {
    if (isInterface) {
        return this;
    }
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final AbstractInsnNode node;
    if (length.length == 1) {
        final Type elementType = Type.getType(lastField.desc).getElementType();
        list.add(NodeHelper.getInsnNodeFor(Integer.valueOf(length[0])));
        if (elementType.getDescriptor().startsWith("L")) {
            node = new TypeInsnNode(Opcodes.ANEWARRAY, elementType.getInternalName());
        } else {
            node = new IntInsnNode(Opcodes.NEWARRAY, getSort(elementType));
        }
    } else {
        for (final int currentLength : length) {
            list.add(NodeHelper.getInsnNodeFor(Integer.valueOf(currentLength)));
        }
        node = new MultiANewArrayInsnNode(lastField.desc, length.length);
    }
    list.add(node);
    list.add(new VarInsnNode(Opcodes.ASTORE, constructorVarIndex));
    list.add(new VarInsnNode(Opcodes.ALOAD, constructorVarIndex));
    lastConstructorVarIndex = constructorVarIndex;
    constructorVarIndex++;
    list.add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, lastField.name, lastField.desc));
    addToConstructor(list);
    return this;
}