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:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void loadAndConvertToObject(final InsnList il, final Type type, final int argLocation) {
    if (type.equals(Type.BOOLEAN_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
                "(Z)Ljava/lang/Boolean;"));
    } else if (type.equals(Type.CHAR_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf",
                "(C)Ljava/lang/Character;"));
    } else if (type.equals(Type.BYTE_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"));
    } else if (type.equals(Type.SHORT_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"));
    } else if (type.equals(Type.INT_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
                "(I)Ljava/lang/Integer;"));
    } else if (type.equals(Type.FLOAT_TYPE)) {
        il.add(new VarInsnNode(Opcodes.FLOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"));
    } else if (type.equals(Type.LONG_TYPE)) {
        il.add(new VarInsnNode(Opcodes.LLOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"));
    } else if (type.equals(Type.DOUBLE_TYPE)) {
        il.add(new VarInsnNode(Opcodes.DLOAD, argLocation));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
                "(D)Ljava/lang/Double;"));
    } else {/*  w ww  . j  a v  a2  s . c  o m*/
        il.add(new VarInsnNode(Opcodes.ALOAD, argLocation));
    }
}

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

License:Open Source License

private void addBoxingStmt(final InsnList il, final Type type) {
    if (type.equals(Type.BOOLEAN_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
                "(Z)Ljava/lang/Boolean;"));
    } else if (type.equals(Type.CHAR_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf",
                "(C)Ljava/lang/Character;"));
    } else if (type.equals(Type.BYTE_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;"));
    } else if (type.equals(Type.SHORT_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;"));
    } else if (type.equals(Type.INT_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
                "(I)Ljava/lang/Integer;"));
    } else if (type.equals(Type.FLOAT_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"));
    } else if (type.equals(Type.LONG_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;"));
    } else if (type.equals(Type.DOUBLE_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
                "(D)Ljava/lang/Double;"));
    }/*from   w w  w  .  ja  v  a2  s .com*/
}

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

License:Open Source License

private void addUnBoxingStmt(final InsnList il, final Type type) {
    if (type.equals(Type.BOOLEAN_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z"));
    } else if (type.equals(Type.CHAR_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C"));
    } else if (type.equals(Type.BYTE_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B"));
    } else if (type.equals(Type.SHORT_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S"));
    } else if (type.equals(Type.INT_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I"));
    } else if (type.equals(Type.FLOAT_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F"));
    } else if (type.equals(Type.LONG_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J"));
    } else if (type.equals(Type.DOUBLE_TYPE)) {
        il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D"));
    }/*from   w w  w .  j  av a  2  s. com*/
}

From source file:org.jooby.internal.apitool.Insns.java

License:Apache License

private static InsnList instructions(final MethodNode method, final AbstractInsnNode n) {
    InsnList instructions = new InsnList();
    instructions.add(n);
    new Insn<>(method, n).next().forEach(instructions::add);
    return instructions;
}

From source file:org.spongepowered.asm.mixin.injection.callback.CallbackInjector.java

License:MIT License

/**
 * Generates a method which throws an error
 * /*from  w  w w  .j  a  v  a2  s . c  o  m*/
 * @param callback callback handle
 * @param errorClass error class to throw
 * @param message message for the error
 * @return generated method
 */
private MethodNode generateErrorMethod(Callback callback, String errorClass, String message) {
    MethodNode method = this.info.addMethod(this.methodNode.access, this.methodNode.name + "$missing",
            callback.getDescriptor());
    method.maxLocals = ASMHelper.getFirstNonArgLocalIndex(Type.getArgumentTypes(callback.getDescriptor()),
            !this.isStatic);
    method.maxStack = 3;
    InsnList insns = method.instructions;
    insns.add(new TypeInsnNode(Opcodes.NEW, errorClass));
    insns.add(new InsnNode(Opcodes.DUP));
    insns.add(new LdcInsnNode(message));
    insns.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, errorClass, "<init>", "(Ljava/lang/String;)V", false));
    insns.add(new InsnNode(Opcodes.ATHROW));
    return method;
}

From source file:org.spongepowered.asm.mixin.injection.code.Injector.java

License:MIT License

protected void invokeMethod(InsnList insns, MethodNode methodNode) {
    boolean isPrivate = (methodNode.access & Opcodes.ACC_PRIVATE) != 0;
    int invokeOpcode = this.isStatic ? Opcodes.INVOKESTATIC
            : isPrivate ? Opcodes.INVOKESPECIAL : Opcodes.INVOKEVIRTUAL;
    insns.add(new MethodInsnNode(invokeOpcode, this.classNode.name, methodNode.name, methodNode.desc, false));
}

From source file:org.spongepowered.asm.mixin.injection.invoke.InvokeInjector.java

License:MIT License

/**
 * @param args handler arguments/*from  ww  w.  j ava  2s .c o  m*/
 * @param insns InsnList to inject insns into
 * @param argMap Mapping of args to local variables
 * @param startArg Starting arg to consume
 * @param endArg Ending arg to consume
 */
protected void invokeHandlerWithArgs(Type[] args, InsnList insns, int[] argMap, int startArg, int endArg) {
    if (!this.isStatic) {
        insns.add(new VarInsnNode(Opcodes.ALOAD, 0));
    }
    this.pushArgs(args, insns, argMap, startArg, endArg);
    this.invokeHandler(insns);
}

From source file:org.spongepowered.asm.mixin.injection.invoke.InvokeInjector.java

License:MIT License

/**
 * Store args on the stack to their positions allocated based on argMap
 * /*  ww  w  .  ja v a 2 s .  com*/
 * @param args argument types
 * @param insns instruction list to generate insns into
 * @param argMap generated argmap containing local indices for all args
 * @param start Starting index
 * @param end Ending index
 */
protected void storeArgs(Type[] args, InsnList insns, int[] argMap, int start, int end) {
    for (int arg = end - 1; arg >= start; arg--) {
        insns.add(new VarInsnNode(args[arg].getOpcode(Opcodes.ISTORE), argMap[arg]));
    }
}

From source file:org.spongepowered.asm.mixin.injection.invoke.InvokeInjector.java

License:MIT License

/**
 * Load args onto the stack from their positions allocated in argMap
 * @param args argument types/* w  w  w .  j a  v  a2s .co m*/
 * @param insns instruction list to generate insns into
 * @param argMap generated argmap containing local indices for all args
 * @param start Starting index
 * @param end Ending index
 */
protected void pushArgs(Type[] args, InsnList insns, int[] argMap, int start, int end) {
    for (int arg = start; arg < end; arg++) {
        insns.add(new VarInsnNode(args[arg].getOpcode(Opcodes.ILOAD), argMap[arg]));
    }
}

From source file:org.spongepowered.asm.mixin.transformer.MixinTransformer.java

License:MIT License

/**
 * Get insns corresponding to the instance initialiser (hopefully) from the
 * supplied constructor.//w  ww .j av  a 2s .c  o m
 * 
 * TODO Potentially rewrite this to be less horrible.
 * 
 * @param mixin
 * @param ctor
 * @return initialiser bytecode extracted from the supplied constructor, or
 *      null if the constructor range could not be parsed
 */
private InsnList getInitialiser(MixinTargetContext mixin, MethodNode ctor) {
    // Find the range of line numbers which corresponds to the constructor body
    Range init = this.getConstructorRange(ctor);
    if (!init.isValid()) {
        return null;
    }

    // Now we know where the constructor is, look for insns which lie OUTSIDE the method body
    int line = 0;
    InsnList initialiser = new InsnList();
    boolean gatherNodes = false;
    int trimAtOpcode = -1;
    LabelNode optionalInsn = null;
    for (Iterator<AbstractInsnNode> iter = ctor.instructions.iterator(init.marker); iter.hasNext();) {
        AbstractInsnNode insn = iter.next();
        if (insn instanceof LineNumberNode) {
            line = ((LineNumberNode) insn).line;
            AbstractInsnNode next = ctor.instructions.get(ctor.instructions.indexOf(insn) + 1);
            if (line == init.end && next.getOpcode() != Opcodes.RETURN) {
                gatherNodes = true;
                trimAtOpcode = Opcodes.RETURN;
            } else {
                gatherNodes = init.excludes(line);
                trimAtOpcode = -1;
            }
        } else if (gatherNodes) {
            if (optionalInsn != null) {
                initialiser.add(optionalInsn);
                optionalInsn = null;
            }

            if (insn instanceof LabelNode) {
                optionalInsn = (LabelNode) insn;
            } else {
                int opcode = insn.getOpcode();
                if (opcode == trimAtOpcode) {
                    trimAtOpcode = -1;
                    continue;
                }
                for (int ivalidOp : MixinTransformer.INITIALISER_OPCODE_BLACKLIST) {
                    if (opcode == ivalidOp) {
                        // At the moment I don't handle any transient locals because I haven't seen any in the wild, but let's avoid writing
                        // code which will likely break things and fix it if a real test case ever appears
                        throw new InvalidMixinException(mixin,
                                "Cannot handle " + ASMHelper.getOpcodeName(opcode) + " opcode (0x"
                                        + Integer.toHexString(opcode).toUpperCase() + ") in class initialiser");
                    }
                }

                initialiser.add(insn);
            }
        }
    }

    // Check that the last insn is a PUTFIELD, if it's not then 
    AbstractInsnNode last = initialiser.getLast();
    if (last != null) {
        if (last.getOpcode() != Opcodes.PUTFIELD) {
            throw new InvalidMixinException(mixin, "Could not parse initialiser, expected 0xB5, found 0x"
                    + Integer.toHexString(last.getOpcode()));
        }
    }

    return initialiser;
}