Example usage for org.objectweb.asm.tree MethodNode MethodNode

List of usage examples for org.objectweb.asm.tree MethodNode MethodNode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode MethodNode.

Prototype

public MethodNode(final int access, final String name, final String descriptor, final String signature,
        final String[] exceptions) 

Source Link

Document

Constructs a new MethodNode .

Usage

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

License:Open Source License

/**
 * Creates a new class containing the new static field.
 *
 * @param classNode        containing the old class.
 * @param fieldNode        containing the old field.
 * @param initInstructions a list of instructions that goes into <clinit>.
 * @param className        the name of the new class to be generated.
 * @return an array of bytes which builds the new class.
 *//*from   w  w w .j  a  v  a2  s  .  co  m*/
@SuppressWarnings("unchecked")
public static byte[] newFieldClass(ClassNode classNode, FieldNode fieldNode, InsnList initInstructions,
        String className) {
    ClassNode newClass = new ClassNode();
    newClass.version = classNode.version;
    newClass.access = Opcodes.ACC_PUBLIC;
    newClass.signature = "L" + className + ";";
    newClass.name = className;
    newClass.superName = "java/lang/Object";
    newClass.fields.add(
            new FieldNode(fieldNode.access, fieldNode.name, fieldNode.desc, fieldNode.desc, fieldNode.value));
    if (initInstructions != null) {
        if (initInstructions.size() > 0) {
            MethodNode mn = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
            InsnList il = mn.instructions;
            il.add(new LabelNode());
            il.add(initInstructions);
            il.add(new FieldInsnNode(Opcodes.PUTSTATIC, className, fieldNode.name, fieldNode.desc));
            il.add(new InsnNode(Opcodes.RETURN));
            newClass.methods.add(mn);
        }
    }

    ClassWriter newCWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    newClass.accept(newCWriter);
    return newCWriter.toByteArray();
}

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

License:Open Source License

public static MethodNode createObjectHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getObjectMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "([Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);//from  w  w w  .  j a  va  2s  .  c om
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("args", "[Ljava/lang/Object;", 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 createIntHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getIntMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(I)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);//ww w  .  j a  va  2 s  .  c om
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "I", 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 createLongHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getLongMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(J)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);/*  w  w  w. j  a  va2 s.  co  m*/
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "J", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 3;
    return mn;
}

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);//from www.  jav  a  2s . c om
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    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);//from ww w. j  ava2 s.  co  m
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    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.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

/**
 *    public int myMethod(int i)/*from   www  .j ava  2 s .c o  m*/
   {
 try
 {
    return _sw_prototype_original_myMethod(i)
 }
 finally
 {
    Capturer.enable();
 }
   }
        
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;

    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
            methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;

    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;

    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;

    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);

    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();

    final InsnList wInstructions = wrappingMethodNode.instructions;

    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor 

        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;

        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

        int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);

            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }

    int varReturnValue = 0;

    final Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));

    } else {

        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));

        //--- create "Object returnValue = null;"

        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }

        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;

            // long/double take two registers
            if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
                varReturnValue++;
            }
        }

        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }

    int var = 0;

    // --- L0
    wInstructions.add(l0);

    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className,
            wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));

    // --- construct call to wrapped methode

    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }

    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            var++;
        }
    }

    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }

    var++;

    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));

        // --- L1

        wInstructions.add(l1);

        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));

        // FIXME <--- DUPLICATE CODE

        // --- L2

        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result

        this.addBoxingStmt(wInstructions, returnType);

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));

        this.addUnBoxingStmt(wInstructions, returnType);

        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var

        // --- L1

        wInstructions.add(l1);

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));

        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);

        //---- L2

        wInstructions.add(l2);

        wInstructions.add(
                new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) },
                        1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(ClassDefinition node) throws ASTVisitorException {
    ClassNode classNode = new ClassNode();
    cn = classNode;/* ww w .  j  av  a 2s .c o m*/

    cn.version = Opcodes.V1_5;
    cn.superName = "java/lang/Object";
    cn.name = node.getIdentifier();
    cn.access = Opcodes.ACC_PUBLIC;

    // create constructor
    mn = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mn.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    mn.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false));
    mn.instructions.add(new InsnNode(Opcodes.RETURN));

    mn.maxLocals = 1;
    mn.maxStack = 1;
    classNode.methods.add(mn);

    node.getFfDefinitions().accept(this);

    // IMPORTANT: this should be dynamically calculated
    // use COMPUTE_MAXS when computing the ClassWriter,
    // e.g. new ClassWriter(ClassWriter.COMPUTE_MAXS)
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    //         ClassVisitor ca = new CheckClassAdapter(cw);
    //         cn.accept(new CheckClassAdapter(ca));
    TraceClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out));

    cn.accept(cv);
    //get code
    byte code[] = cw.toByteArray();

    Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName())
            .info("Writing " + node.getIdentifier() + " class to .class file");
    // update to file
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(node.getIdentifier() + ".class");
        fos.write(code);
        fos.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()).log(Level.SEVERE, null, ex);
    }

    cn = null;
    System.out.println("##### finished writing to file");

}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

@Override
public void visit(FunctionDefinition node) throws ASTVisitorException {
    fd = node;//from  w ww .j  a  v  a 2  s  . c o m
    //fix the signature       
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ " + node.getIdentifier()
            + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    if (node.getStorageSpecifier() == null) { //if the function is static
        LocalIndexPool safeLocalIndexPool = ASTUtils.getSafeLocalIndexPool(node);
        int localIndex = safeLocalIndexPool.getLocalIndex();
    }
    LocalIndexPool safeLocalIndexPool = ASTUtils.getSafeLocalIndexPool(node);
    String methodType = Type.getMethodDescriptor(node.getType().getTypeSpecifier(),
            node.getParameters().getParameterTypes());
    int accessor = Opcodes.ACC_PUBLIC;
    if (ASTUtils.getIsStatic(node)) {
        accessor = accessor + Opcodes.ACC_STATIC;
    }
    MethodNode methodNode;
    if (node.getIdentifier().equals("main")) {
        methodNode = new MethodNode(accessor, node.getIdentifier(), "([Ljava/lang/String;)V", null, null);
    } else {
        methodNode = new MethodNode(accessor, node.getIdentifier(), methodType, null, null);
    }
    mn = methodNode;

    node.getParameters().accept(this);
    node.getCompoundStatement().accept(this);

    mn.instructions.add(new InsnNode(Opcodes.RETURN));
    //            mn.maxLocals = 30;
    //        mn.maxStack = 30;

    cn.methods.add(mn);

    fd = null;
    mn = null;
}

From source file:org.jacoco.core.internal.flow.MethodSanitizerTest.java

License:Open Source License

@Before
public void setup() {
    actual = new MethodNode(0, "test", "()V", null, null);
    expected = new MethodNode(0, "test", "()V", null, null);
    sanitizer = new MethodSanitizer(actual, 0, "test", "()V", null, null);
}