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:com.builtbroken.mc.patch.ClassTransformer.java

/** Fixes {@link net.minecraft.world.World#notifyBlockOfNeighborChange(int, int, int, Block)} causing chunks to load */
private void injectNotifyBlockOfNeighborChange(ClassNode cn) {
    final MethodNode method = ASMUtility.getMethod(cn, "notifyBlockOfNeighborChange", "func_147460_e");

    if (method != null) {
        final InsnList edit = new InsnList();
        edit.add(new VarInsnNode(ALOAD, 0));
        edit.add(new VarInsnNode(ILOAD, 1)); //TODO update as needed
        edit.add(new VarInsnNode(ILOAD, 2));
        edit.add(new VarInsnNode(ILOAD, 3));
        edit.add(new VarInsnNode(ALOAD, 4));
        edit.add(new MethodInsnNode(INVOKESTATIC, HOOK_CLASS, "notifyBlockOfNeighborChange",
                "(Lnet/minecraft/world/World;IIILnet/minecraft/block/Block;)V", false));
        edit.add(new InsnNode(RETURN));
        MethodInsnNode m = ASMUtility.getMethodeNode(method, "onNeighborBlockChange", "func_149695_a");
        method.instructions.insertBefore(m, edit);
        method.instructions.remove(m);/*from   ww w.ja  v a  2 s  .c om*/
    } else {
        CoreMod.logger.error(
                "Failed to find 'public void notifyBlockOfNeighborChange(int, int, int, Block)' in World.class");
    }
}

From source file:com.builtbroken.profiler.asm.WorldTransformer.java

/** {@link World#setBlock(int, int, int, Block, int, int)} */
private void injectSetBlock(ClassNode cn) {
    MethodNode setBlockMethod = getMethod(cn, "setBlock", "(IIIL" + getName(CLASS_KEY_BLOCK) + ";II)Z");

    if (setBlockMethod != null) {
        //Create method call
        final InsnList nodeAdd = new InsnList();

        nodeAdd.add(new VarInsnNode(Opcodes.ALOAD, 0));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 1));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 2));
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 3));
        nodeAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onBlockChange",
                "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));

        //Inject method call at top of method
        setBlockMethod.instructions.insertBefore(setBlockMethod.instructions.get(0), nodeAdd);

        //Locate all return points from the method
        List<AbstractInsnNode> returnNodes = new ArrayList();
        for (int i = 0; i < setBlockMethod.instructions.size(); i++) {
            AbstractInsnNode ain = setBlockMethod.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNodes.add(ain);//www  .  j a v  a  2 s .  co m
            }
        }

        //Inject calls in front of return points
        for (AbstractInsnNode node : returnNodes) {
            //Create method call
            final InsnList nodeAdd2 = new InsnList();
            nodeAdd2.add(new VarInsnNode(Opcodes.ALOAD, 0));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 1));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 2));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 3));
            nodeAdd2.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onPostBlockChange",
                    "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));
            //Inject method call before return node
            setBlockMethod.instructions.insertBefore(node, nodeAdd2);
        }
    }
}

From source file:com.builtbroken.profiler.asm.WorldTransformer.java

/** {@link World#setBlockMetadataWithNotify(int, int, int, int, int)} */
private void injectSetBlockWithMeta(ClassNode cn) {
    MethodNode setBlockMetaMethod = getMethod(cn, "setBlockMetadataWithNotify", "(IIIII)Z");

    if (setBlockMetaMethod != null) {
        final InsnList nodeAdd = new InsnList();

        nodeAdd.add(new VarInsnNode(Opcodes.ALOAD, 0)); //this
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 1)); //x
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 2)); //y
        nodeAdd.add(new VarInsnNode(Opcodes.ILOAD, 3)); //z
        nodeAdd.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onBlockMetaChange",
                "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));

        setBlockMetaMethod.instructions.insertBefore(setBlockMetaMethod.instructions.get(0), nodeAdd);

        //Locate all return points from the method
        List<AbstractInsnNode> returnNodes = new ArrayList();
        for (int i = 0; i < setBlockMetaMethod.instructions.size(); i++) {
            AbstractInsnNode ain = setBlockMetaMethod.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNodes.add(ain);/*from   w  w  w .ja  v  a  2 s .  c  o m*/
            }
        }

        //Inject calls in front of return points
        for (AbstractInsnNode node : returnNodes) {
            //Create method call
            final InsnList nodeAdd2 = new InsnList();
            nodeAdd2.add(new VarInsnNode(Opcodes.ALOAD, 0));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 1));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 2));
            nodeAdd2.add(new VarInsnNode(Opcodes.ILOAD, 3));
            nodeAdd2.add(new MethodInsnNode(Opcodes.INVOKESTATIC, BLOCK_HOOK_CLASS, "onPostBlockMetaChange",
                    "(L" + getName(CLASS_KEY_WORLD) + ";III)V", false));
            //Inject method call before return node
            setBlockMetaMethod.instructions.insertBefore(node, nodeAdd2);
        }
    }
}

From source file:com.dragome.callbackevictor.serverside.bytecode.transformation.asm.ContinuationMethodAnalyzer.java

License:Apache License

@SuppressWarnings("unchecked")
void moveNew() throws AnalyzerException {
    SourceInterpreter i = new SourceInterpreter();
    Analyzer a = new Analyzer(i);
    a.analyze(className, this);

    final HashMap<AbstractInsnNode, MethodInsnNode> movable = new HashMap<AbstractInsnNode, MethodInsnNode>();

    Frame[] frames = a.getFrames();
    for (int j = 0; j < methods.size(); j++) {
        MethodInsnNode mnode = methods.get(j);
        // require to move NEW instruction
        int n = instructions.indexOf(mnode);
        Frame f = frames[n];//from   ww w.  ja  v a 2  s  .c  om
        Type[] args = Type.getArgumentTypes(mnode.desc);

        SourceValue v = (SourceValue) f.getStack(f.getStackSize() - args.length - 1);
        Set<AbstractInsnNode> insns = v.insns;
        for (final AbstractInsnNode ins : insns) {
            if (ins.getOpcode() == NEW) {
                movable.put(ins, mnode);
            } else {
                // other known patterns
                int n1 = instructions.indexOf(ins);
                if (ins.getOpcode() == DUP) { // <init> with params
                    AbstractInsnNode ins1 = instructions.get(n1 - 1);
                    if (ins1.getOpcode() == NEW) {
                        movable.put(ins1, mnode);
                    }
                } else if (ins.getOpcode() == SWAP) { // in exception handler
                    AbstractInsnNode ins1 = instructions.get(n1 - 1);
                    AbstractInsnNode ins2 = instructions.get(n1 - 2);
                    if (ins1.getOpcode() == DUP_X1 && ins2.getOpcode() == NEW) {
                        movable.put(ins2, mnode);
                    }
                }
            }
        }
    }

    int updateMaxStack = 0;
    for (final Map.Entry<AbstractInsnNode, MethodInsnNode> e : movable.entrySet()) {
        AbstractInsnNode node1 = e.getKey();
        int n1 = instructions.indexOf(node1);
        AbstractInsnNode node2 = instructions.get(n1 + 1);
        AbstractInsnNode node3 = instructions.get(n1 + 2);
        int producer = node2.getOpcode();

        instructions.remove(node1); // NEW
        boolean requireDup = false;
        if (producer == DUP) {
            instructions.remove(node2); // DUP
            requireDup = true;
        } else if (producer == DUP_X1) {
            instructions.remove(node2); // DUP_X1
            instructions.remove(node3); // SWAP
            requireDup = true;
        }

        MethodInsnNode mnode = (MethodInsnNode) e.getValue();
        AbstractInsnNode nm = mnode;

        int varOffset = stackRecorderVar + 1;
        Type[] args = Type.getArgumentTypes(mnode.desc);

        // optimizations for some common cases
        if (args.length == 0) {
            final InsnList doNew = new InsnList();
            doNew.add(node1); // NEW
            if (requireDup)
                doNew.add(new InsnNode(DUP));
            instructions.insertBefore(nm, doNew);
            nm = doNew.getLast();
            continue;
        }

        if (args.length == 1 && args[0].getSize() == 1) {
            final InsnList doNew = new InsnList();
            doNew.add(node1); // NEW
            if (requireDup) {
                doNew.add(new InsnNode(DUP));
                doNew.add(new InsnNode(DUP2_X1));
                doNew.add(new InsnNode(POP2));
                updateMaxStack = updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
            } else
                doNew.add(new InsnNode(SWAP));
            instructions.insertBefore(nm, doNew);
            nm = doNew.getLast();
            continue;
        }

        // TODO this one untested!
        if ((args.length == 1 && args[0].getSize() == 2)
                || (args.length == 2 && args[0].getSize() == 1 && args[1].getSize() == 1)) {
            final InsnList doNew = new InsnList();
            doNew.add(node1); // NEW
            if (requireDup) {
                doNew.add(new InsnNode(DUP));
                doNew.add(new InsnNode(DUP2_X2));
                doNew.add(new InsnNode(POP2));
                updateMaxStack = updateMaxStack < 2 ? 2 : updateMaxStack; // a two extra slots for temp values
            } else {
                doNew.add(new InsnNode(DUP_X2));
                doNew.add(new InsnNode(POP));
                updateMaxStack = updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for temp value
            }
            instructions.insertBefore(nm, doNew);
            nm = doNew.getLast();
            continue;
        }

        final InsnList doNew = new InsnList();
        // generic code using temporary locals
        // save stack
        for (int j = args.length - 1; j >= 0; j--) {
            Type type = args[j];

            doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
            varOffset += type.getSize();
        }
        if (varOffset > maxLocals) {
            maxLocals = varOffset;
        }

        doNew.add(node1); // NEW

        if (requireDup)
            doNew.add(new InsnNode(DUP));

        // restore stack
        for (int j = 0; j < args.length; j++) {
            Type type = args[j];
            varOffset -= type.getSize();

            doNew.add(new VarInsnNode(type.getOpcode(ILOAD), varOffset));

            // clean up store to avoid memory leak?
            if (type.getSort() == Type.OBJECT || type.getSort() == Type.ARRAY) {
                updateMaxStack = updateMaxStack < 1 ? 1 : updateMaxStack; // an extra slot for ACONST_NULL

                doNew.add(new InsnNode(ACONST_NULL));

                doNew.add(new VarInsnNode(type.getOpcode(ISTORE), varOffset));
            }
        }
        instructions.insertBefore(nm, doNew);
        nm = doNew.getLast();
    }

    maxStack += updateMaxStack;
}

From source file:com.github.antag99.retinazer.weaver.SystemProcessor.java

License:Open Source License

@Override
public void visitEnd() {
    for (FlyweightField field : flyweightFields)
        field.fieldNode.accept(cv);/* ww  w.  j  a  v  a 2 s . com*/

    if (setupMethod == null) {
        setupMethod = new MethodNode(ACC_PROTECTED, "setup", "()V", null, null);
        setupMethod.instructions.add(new VarInsnNode(ALOAD, 0));
        setupMethod.instructions
                .add(new MethodInsnNode(INVOKESPECIAL, metadata.superName, "setup", "()V", false));
        setupMethod.instructions.add(new InsnNode(RETURN));
    }

    InsnList insns = new InsnList();
    for (FlyweightField field : flyweightFields) {
        String mapperName = field.mapper.componentType.getInternalName() + "$Mapper";
        insns.add(new VarInsnNode(ALOAD, 0));
        insns.add(new InsnNode(DUP));
        insns.add(new FieldInsnNode(GETFIELD, metadata.internalName, field.mapper.name,
                "Lcom/github/antag99/retinazer/Mapper;"));
        insns.add(new TypeInsnNode(CHECKCAST, mapperName));
        insns.add(new MethodInsnNode(INVOKEVIRTUAL, mapperName, "createFlyweight",
                "()" + field.mapper.componentType.getDescriptor(), false));
        insns.add(
                new FieldInsnNode(PUTFIELD, metadata.internalName, field.fieldNode.name, field.fieldNode.desc));
    }

    setupMethod.instructions.insertBefore(setupMethod.instructions.getFirst(), insns);
    setupMethod.accept(cv);

    super.visitEnd();
}

From source file:com.github.fge.grappa.misc.AsmUtils.java

License:Open Source License

public static InsnList createArgumentLoaders(String methodDescriptor) {
    Objects.requireNonNull(methodDescriptor, "methodDescriptor");

    InsnList instructions = new InsnList();
    Type[] types = Type.getArgumentTypes(methodDescriptor);

    int opcode;//from  w  w  w . j  a v a  2 s .  c o m
    VarInsnNode node;

    for (int i = 0; i < types.length; i++) {
        opcode = LoadingOpcode.forType(types[i]);
        node = new VarInsnNode(opcode, i + 1);
        instructions.add(node);
    }
    return instructions;
}

From source file:com.github.wreulicke.bean.validation.ASMMethodParameterValidationInjector.java

License:Open Source License

private void inject(MethodNode node, ClassNode clazzNode) {
    if (Modifier.isStatic(node.access) || Modifier.isAbstract(node.access) || Modifier.isAbstract(node.access)
            || "<init>".equals(node.name) || "<clinit>".equals(node.name)) {
        return;//  w w w  . j  a v a  2 s .  c  o  m
    }

    InsnList list = new InsnList();
    int index = node.localVariables.size();
    list.add(new MethodInsnNode(INVOKESTATIC, "javax/validation/Validation", "buildDefaultValidatorFactory",
            "()Ljavax/validation/ValidatorFactory;", false));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/ValidatorFactory", "getValidator",
            "()Ljavax/validation/Validator;", true));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/Validator", "forExecutables",
            "()Ljavax/validation/executable/ExecutableValidator;", true));

    list.add(new VarInsnNode(Opcodes.ASTORE, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));

    list.add(new LdcInsnNode(Type.getType("L" + clazzNode.name + ";")));
    list.add(new LdcInsnNode(node.name));

    @SuppressWarnings("unchecked")
    List<LocalVariableNode> variables = node.localVariables;
    Type[] args = Type.getArgumentTypes(node.desc);

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));

    for (int i = 0; i < args.length; i++) {
        int paramIndex = 1 + i;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new LdcInsnNode(Type.getType(variables.get(paramIndex).desc)));
        list.add(new InsnNode(Opcodes.AASTORE));
    }

    list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false));

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    for (int i = 0; i < args.length; i++) {
        int paramIndex = i + 1;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new VarInsnNode(Opcodes.ALOAD, paramIndex));
        list.add(new InsnNode(Opcodes.AASTORE));
    }
    list.add(new InsnNode(Opcodes.ICONST_0));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/executable/ExecutableValidator",
            "validateParameters",
            "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/util/Set;",
            true));
    list.add(new MethodInsnNode(INVOKESTATIC, "com/github/wreulicke/bean/validation/Constraints",
            "throwIfNeeded", "(Ljava/util/Set;)V", false));
    node.instructions.insert(list);
}

From source file:com.lodgon.parboiled.transform.AsmUtils.java

License:Open Source License

public static InsnList createArgumentLoaders(String methodDescriptor) {
    checkArgNotNull(methodDescriptor, "methodDescriptor");
    InsnList instructions = new InsnList();
    Type[] types = Type.getArgumentTypes(methodDescriptor);
    for (int i = 0; i < types.length; i++) {
        instructions.add(new VarInsnNode(getLoadingOpcode(types[i]), i + 1));
    }// w  w w.j a v  a2s .  c  om
    return instructions;
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java

License:Apache License

public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }/*from   w  w w.  j a  va  2 s . c  om*/

    // no argument is ().
    final String desc = "()" + fieldNode.getDesc();
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    // get fieldNode.
    instructions
            .add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return of type.
    final Type type = Type.getType(fieldNode.getDesc());
    instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN)));

    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java

License:Apache License

public void addSetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }//from  w  w  w.j  ava2s.  c om

    // void is V.
    final String desc = "(" + fieldNode.getDesc() + ")V";
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final Type type = Type.getType(fieldNode.getDesc());
    // put field.
    instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
    instructions
            .add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return.
    instructions.add(new InsnNode(Opcodes.RETURN));

    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}