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.github.fge.grappa.misc.AsmUtils.java

License:Open Source License

public static InsnList createArgumentLoaders(String methodDescriptor) {
    Objects.requireNonNull(methodDescriptor, "methodDescriptor");

    InsnList instructions = new InsnList();
    Type[] types = Type.getArgumentTypes(methodDescriptor);

    int opcode;//  ww w. jav  a 2 s  .  c o m
    VarInsnNode node;

    for (int i = 0; i < types.length; i++) {
        opcode = LoadingOpcode.forType(types[i]);
        node = new VarInsnNode(opcode, i + 1);
        instructions.add(node);
    }
    return instructions;
}

From source file:com.github.fge.grappa.transform.generate.ConstructorGenerator.java

License:Apache License

private static void createConstuctor(ParserClassNode classNode, MethodNode constructor) {
    List<String> exceptions = constructor.exceptions;
    MethodNode newConstructor = new MethodNode(ACC_PUBLIC, constructor.name, constructor.desc,
            constructor.signature, exceptions.toArray(new String[exceptions.size()]));

    InsnList instructions = newConstructor.instructions;

    CodeBlock block = CodeBlock.newCodeBlock().aload(0).addAll(createArgumentLoaders(constructor.desc))
            .invokespecial(classNode.getParentType().getInternalName(), "<init>", constructor.desc).rawReturn();

    instructions.add(block.getInstructionList());

    classNode.methods.add(newConstructor);
}

From source file:com.github.fge.grappa.transform.generate.ConstructorGenerator.java

License:Apache License

private static void createNewInstanceMethod(ParserClassNode classNode) {
    // TODO: replace with Code{Block,GenUtils}
    String desc = "()L" + Type.getType(BaseParser.class).getInternalName() + ';';
    MethodNode method = new MethodNode(ACC_PUBLIC, "newInstance", desc, null, null);
    InsnList instructions = method.instructions;

    instructions.add(new TypeInsnNode(NEW, classNode.name));
    instructions.add(new InsnNode(DUP));
    instructions.add(new MethodInsnNode(INVOKESPECIAL, classNode.name, "<init>", "()V", false));
    instructions.add(new InsnNode(ARETURN));

    classNode.methods.add(method);/*from  w  w w .  j av  a 2  s .c  o m*/
}

From source file:com.github.wreulicke.bean.validation.ASMMethodParameterValidationInjector.java

License:Open Source License

private void inject(MethodNode node, ClassNode clazzNode) {
    if (Modifier.isStatic(node.access) || Modifier.isAbstract(node.access) || Modifier.isAbstract(node.access)
            || "<init>".equals(node.name) || "<clinit>".equals(node.name)) {
        return;/* w w w  . ja  v a  2s.c  om*/
    }

    InsnList list = new InsnList();
    int index = node.localVariables.size();
    list.add(new MethodInsnNode(INVOKESTATIC, "javax/validation/Validation", "buildDefaultValidatorFactory",
            "()Ljavax/validation/ValidatorFactory;", false));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/ValidatorFactory", "getValidator",
            "()Ljavax/validation/Validator;", true));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/Validator", "forExecutables",
            "()Ljavax/validation/executable/ExecutableValidator;", true));

    list.add(new VarInsnNode(Opcodes.ASTORE, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, index));
    list.add(new VarInsnNode(Opcodes.ALOAD, 0));

    list.add(new LdcInsnNode(Type.getType("L" + clazzNode.name + ";")));
    list.add(new LdcInsnNode(node.name));

    @SuppressWarnings("unchecked")
    List<LocalVariableNode> variables = node.localVariables;
    Type[] args = Type.getArgumentTypes(node.desc);

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));

    for (int i = 0; i < args.length; i++) {
        int paramIndex = 1 + i;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new LdcInsnNode(Type.getType(variables.get(paramIndex).desc)));
        list.add(new InsnNode(Opcodes.AASTORE));
    }

    list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getMethod",
            "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;", false));

    list.add(new LdcInsnNode(args.length));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    for (int i = 0; i < args.length; i++) {
        int paramIndex = i + 1;
        list.add(new InsnNode(Opcodes.DUP));
        list.add(new LdcInsnNode(i));
        list.add(new VarInsnNode(Opcodes.ALOAD, paramIndex));
        list.add(new InsnNode(Opcodes.AASTORE));
    }
    list.add(new InsnNode(Opcodes.ICONST_0));
    list.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Class"));
    list.add(new MethodInsnNode(INVOKEINTERFACE, "javax/validation/executable/ExecutableValidator",
            "validateParameters",
            "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/util/Set;",
            true));
    list.add(new MethodInsnNode(INVOKESTATIC, "com/github/wreulicke/bean/validation/Constraints",
            "throwIfNeeded", "(Ljava/util/Set;)V", false));
    node.instructions.insert(list);
}

From source file:com.lodgon.parboiled.transform.AsmUtils.java

License:Open Source License

public static InsnList createArgumentLoaders(String methodDescriptor) {
    checkArgNotNull(methodDescriptor, "methodDescriptor");
    InsnList instructions = new InsnList();
    Type[] types = Type.getArgumentTypes(methodDescriptor);
    for (int i = 0; i < types.length; i++) {
        instructions.add(new VarInsnNode(getLoadingOpcode(types[i]), i + 1));
    }/*from w  w w .  j a  va 2 s .c  om*/
    return instructions;
}

From source file:com.lodgon.parboiled.transform.ConstructorGenerator.java

License:Apache License

@SuppressWarnings({ "unchecked" })
private void createConstuctor(ParserClassNode classNode, MethodNode constructor) {
    MethodNode newConstructor = new MethodNode(ACC_PUBLIC, constructor.name, constructor.desc,
            constructor.signature,//from  www .  j a v  a2s  . co  m
            (String[]) constructor.exceptions.toArray(new String[constructor.exceptions.size()]));

    InsnList instructions = newConstructor.instructions;
    instructions.add(new VarInsnNode(ALOAD, 0));
    instructions.add(createArgumentLoaders(constructor.desc));
    instructions.add(new MethodInsnNode(INVOKESPECIAL, classNode.getParentType().getInternalName(), "<init>",
            constructor.desc));
    instructions.add(new InsnNode(RETURN));

    classNode.methods.add(newConstructor);
}

From source file:com.lodgon.parboiled.transform.ConstructorGenerator.java

License:Apache License

@SuppressWarnings({ "unchecked" })
private void createNewInstanceMethod(ParserClassNode classNode) {
    MethodNode method = new MethodNode(ACC_PUBLIC, "newInstance",
            "()L" + Types.BASE_PARSER.getInternalName() + ';', null, null);
    InsnList instructions = method.instructions;
    instructions.add(new TypeInsnNode(NEW, classNode.name));
    instructions.add(new InsnNode(DUP));
    instructions.add(new MethodInsnNode(INVOKESPECIAL, classNode.name, "<init>", "()V"));
    instructions.add(new InsnNode(ARETURN));

    classNode.methods.add(method);/*  w  w w .ja  v a2s.com*/
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java

License:Apache License

public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }//w w w  . j av a 2s  .c o m

    // no argument is ().
    final String desc = "()" + fieldNode.getDesc();
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    // get fieldNode.
    instructions
            .add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return of type.
    final Type type = Type.getType(fieldNode.getDesc());
    instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN)));

    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter.java

License:Apache License

public void addSetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }//from   w w  w . j a  v a 2 s. com

    // void is V.
    final String desc = "(" + fieldNode.getDesc() + ")V";
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    final Type type = Type.getType(fieldNode.getDesc());
    // put field.
    instructions.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), 1));
    instructions
            .add(new FieldInsnNode(Opcodes.PUTFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return.
    instructions.add(new InsnNode(Opcodes.RETURN));

    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}

From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter.java

License:Apache License

public void addDelegator(final String superClassInternalName) {
    if (superClassInternalName == null) {
        throw new IllegalArgumentException("super class internal name must not be null.");
    }//from w  w w  .  java 2  s. c  o  m

    final InsnList instructions = this.methodNode.instructions;
    if (isStatic()) {
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke static
        instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, superClassInternalName, this.methodNode.name,
                this.methodNode.desc, false));
    } else {
        // load this
        this.methodVariables.loadVar(instructions, 0);
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke special
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, superClassInternalName, this.methodNode.name,
                this.methodNode.desc, false));
    }
    // return
    this.methodVariables.returnValue(instructions);
}