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.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createFloatHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getFloatMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(F)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);/*from www .  j  a  v a  2  s . c o m*/
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "F", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;
}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createStringHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getStringMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(Ljava/lang/String;)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);/*from w w  w .  j  a va 2s.co  m*/
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "Ljava/lang/String;", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;
}

From source file:org.copperengine.core.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;
        }/*  w  w  w . j  a v a2s .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:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractCreateDispatchCodeAdapter.java

License:Open Source License

protected InsnList getDispatchCode(MethodNode method, int joinPointId, int boundMethodId) {
    InsnList instructions = new InsnList();

    // teams = TeamManager.getTeams(joinpointId)
    instructions.add(createLoadIntConstant(joinPointId));

    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ClassNames.TEAM_MANAGER_SLASH,
            ConstantMembers.getTeams.getName(), ConstantMembers.getTeams.getSignature(), false));

    instructions.add(createInstructionsToCheackTeams(method));

    // get the first team
    instructions.add(new InsnNode(Opcodes.DUP));
    instructions.add(new InsnNode(Opcodes.ICONST_0));
    instructions.add(new InsnNode(Opcodes.AALOAD));
    instructions.add(new InsnNode(Opcodes.SWAP));
    if (isStatic) {
        instructions.add(new InsnNode(Opcodes.ACONST_NULL));
    } else {//from  w  w  w .jav a  2s .  c o m
        // put "this" on the stack and cast it to IBoundBase2
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, ClassNames.I_BOUND_BASE_SLASH));
    }
    instructions.add(new InsnNode(Opcodes.SWAP));
    // start index
    instructions.add(new InsnNode(Opcodes.ICONST_0));

    // TeamManager.getCallinIds(joinpointId)
    instructions.add(createLoadIntConstant(joinPointId));

    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, ClassNames.TEAM_MANAGER_SLASH,
            ConstantMembers.getCallinIds.getName(), ConstantMembers.getCallinIds.getSignature(), false));

    instructions.add(createLoadIntConstant(boundMethodId));
    args = Type.getArgumentTypes(method.desc);

    // box the arguments
    instructions.add(getBoxedArguments(args));

    instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, ClassNames.ITEAM_SLASH,
            ConstantMembers.callAllBindingsTeam.getName(), ConstantMembers.callAllBindingsTeam.getSignature(),
            true));

    Type returnType = Type.getReturnType(method.desc);
    instructions.add(getUnboxingInstructionsForReturnValue(returnType));

    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractCreateDispatchCodeAdapter.java

License:Open Source License

protected InsnList createInstructionsToCheackTeams(MethodNode method) {
    // if (teams == null) {
    //       break;
    // }/*from ww  w. j  a v a 2  s.  co  m*/
    InsnList instructions = new InsnList();
    instructions.add(new InsnNode(Opcodes.DUP));
    LabelNode label = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, label));
    instructions.add(new InsnNode(Opcodes.POP));
    instructions.add(new JumpInsnNode(Opcodes.GOTO, findBreakLabel(method.instructions)));
    instructions.add(label);
    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Returns instructions, that are needed to pack all arguments of a method
 * in an {@link Object} Array//from w w  w  .jav a 2s. com
 * @param args The Types of the arguments
 * @param isStatic is this method static or not
 * @return
 */
protected InsnList getBoxingInstructions(Type[] args, boolean isStatic) {
    int firstArgIndex = 1;
    if (isStatic) {
        firstArgIndex = 0;
    }
    InsnList instructions = new InsnList();
    instructions.add(createLoadIntConstant(args.length));
    instructions.add(new TypeInsnNode(Opcodes.ANEWARRAY, ClassNames.OBJECT_SLASH));
    for (int i = 0, slot = 0; i < args.length; slot += args[i++].getSize()) {
        instructions.add(new InsnNode(Opcodes.DUP));
        instructions.add(createLoadIntConstant(i));
        instructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ILOAD), slot + firstArgIndex));
        if (args[i].getSort() != Type.OBJECT && args[i].getSort() != Type.ARRAY) {
            instructions.add(AsmTypeHelper.getBoxingInstructionForType(args[i]));
        }
        instructions.add(new InsnNode(Opcodes.AASTORE));
    }

    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Returns the instructions, that are needed to convert 
 * a return value of the type {@link Object} to the real type
 * @param returnType the real type/*  w ww .  j  av  a2s  .  com*/
 * @return
 */
protected InsnList getUnboxingInstructionsForReturnValue(Type returnType) {
    InsnList instructions = new InsnList();
    switch (returnType.getSort()) {
    case Type.VOID:
        instructions.add(new InsnNode(Opcodes.POP));
        instructions.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.ARRAY: // fallthrough
    case Type.OBJECT:
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, returnType.getInternalName()));
        instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        String objectType = AsmTypeHelper.getObjectType(returnType);
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
        instructions.add(AsmTypeHelper.getUnboxingInstructionForType(returnType, objectType));
        instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN)));
    }
    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * This method could be used to generate debug outputs in the generated code in the form: <br>
 * <code>//from w ww  . j  a va2 s .  c om
 * Sytsem.out.println(message);
 * </code>
 * @param message
 * @return
 */
protected InsnList getInstructionsForDebugOutput(String message) {
    InsnList instructions = new InsnList();
    instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
    instructions.add(new LdcInsnNode(message));
    instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",
            "(Ljava/lang/String;)V", false));
    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Adds instructions to put all arguments of a method on the stack.
 * @param instructions// w  w  w . j a  va 2s .  c  o m
 * @param args
 * @param isStatic
 */
protected void addInstructionsForLoadArguments(InsnList instructions, Type[] args, boolean isStatic) {
    int firstArgIndex = 1;
    if (isStatic) {
        firstArgIndex = 0;
    }
    // put "this" on the stack for an non-static method
    if (!isStatic) {
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    }
    for (int i = 0, slot = firstArgIndex; i < args.length; slot += args[i++].getSize()) {
        instructions.add(new IntInsnNode(args[i].getOpcode(Opcodes.ILOAD), slot));
    }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java

License:Open Source License

void genGetInitializedRoleSet(InsnList instructions, int targetLocal) {
    // x = this._OT$roleSet 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, ConstantMembers.OT_ROLE_SET,
            ConstantMembers.HASH_SET_FIELD_TYPE));

    instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal));
    instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal));

    // if (x == null) {
    LabelNode skipInstantiation = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNONNULL, skipInstantiation));

    // this._OT$roleSet = new HashSet();
    instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new TypeInsnNode(Opcodes.NEW, ClassNames.HASH_SET_SLASH));
    instructions.add(new InsnNode(Opcodes.DUP));
    instructions/*  w w  w . ja v a  2 s. c o m*/
            .add(new MethodInsnNode(Opcodes.INVOKESPECIAL, ClassNames.HASH_SET_SLASH, "<init>", "()V", false));

    instructions.add(new IntInsnNode(Opcodes.ASTORE, targetLocal));
    instructions.add(new IntInsnNode(Opcodes.ALOAD, targetLocal));
    instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, ConstantMembers.OT_ROLE_SET,
            ConstantMembers.HASH_SET_FIELD_TYPE));

    instructions.add(skipInstantiation);
    // }
}