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

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

Introduction

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

Prototype

InsnList

Source Link

Usage

From source file:de.sanandrew.core.manpack.transformer.TransformPlayerDismountCtrl.java

License:Creative Commons License

/**
 * Transforms the EntityPlayer.class by hooking into the updateRidden method and adding a call to _SAP_canDismountOnInput
 * in order for the ridden entity to control whether or not the rider can dismount via sneaking.
 *
 * @param bytes     the class bytes to be transformed
 * @return the transformed class bytes/*from   w  ww  .j a va 2 s. co m*/
 */
private static byte[] transformPlayer(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);
    MethodNode method = ASMHelper.findMethod(clazz, ASMNames.MD_PLAYER_UPDATE_RIDDEN);

    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_PLAYER_WORLD_OBJ));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_WORLD_IS_REMOTE));
    LabelNode ln1 = new LabelNode();
    needle.add(new JumpInsnNode(Opcodes.IFNE, ln1));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_PLAYER_IS_SNEAKING, false));
    needle.add(new JumpInsnNode(Opcodes.IFEQ, ln1));

    AbstractInsnNode insertPoint = ASMHelper.findLastNodeFromNeedle(method.instructions, needle);

    InsnList injectList = new InsnList();
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_PLAYER_RIDING_ENTITY));
    injectList.add(new VarInsnNode(Opcodes.ALOAD, 0));
    injectList.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_CAN_DISMOUNT_ON_INPUT, false));
    injectList.add(new JumpInsnNode(Opcodes.IFEQ, ((JumpInsnNode) insertPoint).label));

    method.instructions.insert(insertPoint, injectList);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);

    return bytes;
}

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;
        }/*from   w w  w .  j  a  v  a2  s . co  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

/**
 * Creates a constructor with all fields of <code>object</code> as parameters
 * and adds it to the method-List of <code>node</code>.
 * A List with all Values (in order of the parameters) is returned.
 * /*  w w w .ja  v  a2s  . c om*/
 * If such a constructor already exists, only the parameters are returned,
 * no change is made to to the classNode.
 * 
 * @param node
 *          the ClassNode
 * @param object
 *          the input Object
 * @return the value list
 * @throws JBOPClassException
 *           if the Constructor couldn't be created.
 */
public static List<Object> createConstructor(final ClassNode node, final Object object)
        throws JBOPClassException {
    final Class<? extends Object> clazz = object.getClass();
    int param = 1;
    final StringBuilder desc = new StringBuilder("(");
    final MethodNode constructor = createMethodNode(node);
    final List<Object> params = new ArrayList<>();
    for (final FieldNode field : node.fields) {
        final InsnList instructions = new InsnList();
        param = createInstructions(param, field, node, instructions);
        final Object value = getValue(clazz, field, object);
        params.add(value);
        constructor.instructions.add(instructions);
        desc.append(expand(field.desc, false));
    }
    constructor.instructions.add(new InsnNode(Opcodes.RETURN));
    desc.append(")V");
    constructor.desc = desc.toString();
    for (final MethodNode method : node.methods) {
        if ("<init>".equals(method.name)) {
            if (constructor.desc.equals(method.desc)) {
                return params;
            }
        }
    }
    node.methods.add(constructor);
    return params;
}

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);/* w  w w .  j  a  v a2s  .co  m*/
    list.add(nSuperConstructor);
    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 . j  ava  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.
 * /*ww  w  . j a v  a2s  .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. jav a 2 s  .c  o 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".
 * // ww  w  .  jav a  2  s. c  o m
 * @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.//from   ww  w  .  j  a  v a  2  s .  c om
 * 
 * @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);/*from   w w  w.j a  v a  2 s  .  co  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);
}