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:com.offbynull.coroutines.instrumenter.ContinuationPointInstructionUtils.java

License:Open Source License

static InsnList throwThrowableInVariable(Variable variable) {
    Validate.notNull(variable);//from  w  w  w .ja va 2s  .  c  o  m
    Validate.isTrue(variable.getType().equals(Type.getType(Object.class)));

    InsnList ret = new InsnList();

    ret.add(loadVar(variable)); // load it in to the returnValObj
    ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Throwable"));
    ret.add(new InsnNode(Opcodes.ATHROW));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.DebugGenerators.java

License:Open Source License

/**
 * Generates instructions for generating marker instructions. These marker instructions are meant to be is useful for debugging
 * instrumented code. For example, you can spot a specific portion of instrumented code by looking for specific markers in the assembly
 * output./*  w w  w.  j  a  v  a2 s . c o m*/
 * @param markerType marker type (determines what kind of instructions are generated)
 * @param text text to print out
 * @return instructions to call System.out.println with a string constant
 * @throws NullPointerException if any argument is {@code null}
 */
public static InsnList debugMarker(MarkerType markerType, String text) {
    Validate.notNull(markerType);
    Validate.notNull(text);

    InsnList ret = new InsnList();

    switch (markerType) {
    case NONE:
        break;
    case CONSTANT:
        ret.add(new LdcInsnNode(text));
        ret.add(new InsnNode(Opcodes.POP));
        break;
    case STDOUT:
        ret.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
        ret.add(new LdcInsnNode(text));
        ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
                "(Ljava/lang/String;)V", false));
        break;
    default:
        throw new IllegalStateException();
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.DebugGenerators.java

License:Open Source License

/**
 * Generates instructions for printing out a string using {@link System#out}. This is useful for debugging. For example, you
 * can print out lines around your instrumented code to make sure that what you think is being run is actually being run.
 * @param text debug text generation instruction list -- must leave a String on the stack
 * @return instructions to call System.out.println with a string constant
 * @throws NullPointerException if any argument is {@code null}
 *//*  w w  w .jav  a2s .c o m*/
public static InsnList debugPrint(InsnList text) {
    Validate.notNull(text);

    InsnList ret = new InsnList();

    ret.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
    ret.add(text);
    ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V",
            false));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Combines multiple instructions in to a single instruction list. You can include normal instructions ({@link AbstractInsnNode}),
 * instruction lists ({@link InsnList}), or instruction generators ({@link InstructionGenerator}).
 * @param insns instructions/*from  ww  w.jav a2s.  c  om*/
 * @throws NullPointerException if any argument is {@code null} or contains {@code null} 
 * @return merged instructions
 */
public static InsnList merge(Object... insns) {
    Validate.notNull(insns);
    Validate.noNullElements(insns);

    InsnList ret = new InsnList();
    for (Object insn : insns) {
        if (insn instanceof AbstractInsnNode) {
            // add single instruction
            AbstractInsnNode insnNode = (AbstractInsnNode) insn;
            ret.add(insnNode);
        } else if (insn instanceof InsnList) {
            // add instruction list
            InsnList insnList = (InsnList) insn;
            for (int i = 0; i < insnList.size(); i++) {
                Validate.notNull(insnList.get(i));
            }
            ret.add((InsnList) insn);
        } else if (insn instanceof InstructionGenerator) {
            // generate conditional merger instruction list and add
            InsnList insnList = ((InstructionGenerator) insn).generate();
            for (int i = 0; i < insnList.size(); i++) {
                Validate.notNull(insnList.get(i));
            }
            ret.add(insnList);
        } else {
            // unrecognized
            throw new IllegalArgumentException();
        }
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Copies a local variable on to the stack.
 * @param variable variable within the local variable table to load from
 * @return instructions to load a local variable on to the stack
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if {@code variable} has been released
 *///from ww w .ja v  a 2 s  .  co m
public static InsnList loadVar(Variable variable) {
    Validate.notNull(variable);

    InsnList ret = new InsnList();
    switch (variable.getType().getSort()) {
    case Type.BOOLEAN:
    case Type.BYTE:
    case Type.CHAR:
    case Type.SHORT:
    case Type.INT:
        ret.add(new VarInsnNode(Opcodes.ILOAD, variable.getIndex()));
        break;
    case Type.LONG:
        ret.add(new VarInsnNode(Opcodes.LLOAD, variable.getIndex()));
        break;
    case Type.FLOAT:
        ret.add(new VarInsnNode(Opcodes.FLOAD, variable.getIndex()));
        break;
    case Type.DOUBLE:
        ret.add(new VarInsnNode(Opcodes.DLOAD, variable.getIndex()));
        break;
    case Type.OBJECT:
    case Type.ARRAY:
        ret.add(new VarInsnNode(Opcodes.ALOAD, variable.getIndex()));
        // If required, do it outside this method
        //                ret.add(new TypeInsnNode(Opcodes.CHECKCAST, variable.getType().getInternalName()));
        break;
    default:
        throw new IllegalStateException(); // should never happen, there is code in Variable/VariableTable to make sure invalid
                                           // types aren't set
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Creates a new object array on the stack.
 * @param size instructions to calculate the size of the new array -- must leave an int on the stack
 * @return instructions to create a new object array -- will leave the object array on the stack
 * @throws NullPointerException if any argument is {@code null}
 *//* w  ww .  ja  va2 s .c  o  m*/
public static InsnList createNewObjectArray(InsnList size) {
    Validate.notNull(size);

    InsnList ret = new InsnList();

    ret.add(size);
    ret.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Gets the size of an array and puts it on to the stack.
 * @param arrayRef instructions to get the array -- must leave an array on the stack
 * @return instructions to get the size of the array -- will size an int on the stack
 * @throws NullPointerException if any argument is {@code null}
 *//*from w ww  . j  ava2 s . co m*/
public static InsnList loadArrayLength(InsnList arrayRef) {
    Validate.notNull(arrayRef);

    InsnList ret = new InsnList();
    ret.add(arrayRef);
    ret.add(new InsnNode(Opcodes.ARRAYLENGTH));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Adds two integers together and puts the result on to the stack.
 * @param lhs instructions to generate the first operand -- must leave an int on the stack
 * @param rhs instructions to generate the second operand -- must leave an int on the stack
 * @return instructions to add the two numbers together -- will leave an int on the stack
 * @throws NullPointerException if any argument is {@code null}
 *//*w  ww . j  a v  a 2s  .co  m*/
public static InsnList addIntegers(InsnList lhs, InsnList rhs) {
    Validate.notNull(lhs);
    Validate.notNull(rhs);

    InsnList ret = new InsnList();

    ret.add(lhs);
    ret.add(rhs);
    ret.add(new InsnNode(Opcodes.IADD));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Generates instructions to throw an exception of type {@link RuntimeException} with a constant message.
 * @param message message of exception// w ww .j ava 2 s. c o  m
 * @return instructions to throw an exception
 * @throws NullPointerException if any argument is {@code null}
 */
public static InsnList throwRuntimeException(String message) {
    Validate.notNull(message);

    InsnList ret = new InsnList();

    ret.add(new TypeInsnNode(Opcodes.NEW, "java/lang/RuntimeException"));
    ret.add(new InsnNode(Opcodes.DUP));
    ret.add(new LdcInsnNode(message));
    ret.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
            "(Ljava/lang/String;)V", false));
    ret.add(new InsnNode(Opcodes.ATHROW));

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.generators.GenericGenerators.java

License:Open Source License

/**
 * Generates instructions to throw the exception type at top of stack. You may run in to problems if the item on top of the stack isn't
 * a {@link Throwable} type. /*ww  w .  java  2  s . com*/
 * @return instructions to throw an exception
 * @throws NullPointerException if any argument is {@code null}
 */
public static InsnList throwThrowable() {
    InsnList ret = new InsnList();

    //        ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Throwable")); // If required, do it outside this method
    ret.add(new InsnNode(Opcodes.ATHROW));

    return ret;
}