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:org.coldswap.util.AutoBoxing.java

License:Open Source License

private static InsnList boxFloat() {
    InsnList il = new InsnList();
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"));
    return il;/*from w  w w .  jav  a 2s  .c  o  m*/
}

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

License:Open Source License

private static InsnList unboxFloat() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "floatValue", "()F"));
    return il;/* w  w w. j a  v a  2s  .c o  m*/
}

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

License:Open Source License

private static InsnList boxDouble() {
    InsnList il = new InsnList();
    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;"));
    return il;//from   ww  w  .  j  a va2 s  .c o  m
}

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

License:Open Source License

private static InsnList unboxDouble() {
    InsnList il = new InsnList();
    il.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Number"));
    il.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Number", "doubleValue", "()D"));
    return il;/*from ww  w .j a va  2 s.c om*/
}

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;
        }//from   www.  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: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 {/*w  w  w .  j a v  a2s .c om*/
        // 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;
    // }/*w  w w .j  a v  a  2s  .c  om*/
    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  a2s . 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/*from  w ww . j a v a2  s. co m*/
 * @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>//  www.j a  v a2  s  .c o  m
 * 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;
}