List of usage examples for org.objectweb.asm.tree MethodNode MethodNode
public MethodNode(final int access, final String name, final String descriptor, final String signature, final String[] exceptions)
From source file:net.enilink.composition.asm.ExtendedClassNode.java
License:Open Source License
@SuppressWarnings("unchecked") public MethodNodeGenerator getClassInitGen() { if (clinitGen == null) { MethodNode clinit = new MethodNode(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); methods.add(clinit);// w w w .j a v a2s .c om clinitGen = new MethodNodeGenerator(clinit); } return clinitGen; }
From source file:net.enilink.composition.asm.processors.BehaviourConstructorGenerator.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public void process(BehaviourClassNode classNode) throws Exception { FieldNode beanField = new FieldNode(ACC_PRIVATE, "_$bean", OBJECT_TYPE.getDescriptor(), null, null); classNode.fields.add(beanField);//w w w . ja v a 2 s. c o m MethodNode constructor = new MethodNode(ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { OBJECT_TYPE }), null, null); // call the constructor of the parent class MethodNodeGenerator gen = new MethodNodeGenerator(constructor); gen.loadThis(); gen.invokeConstructor(classNode.getParentClass().isInterface() ? OBJECT_TYPE : classNode.getParentType(), new Method("<init>", "()V")); gen.loadThis(); gen.loadArg(0); gen.putField(classNode.getType(), "_$bean", OBJECT_TYPE); gen.returnValue(); gen.endMethod(); classNode.methods.add(constructor); classNode.getConstructors().add(constructor); }
From source file:net.enilink.composition.asm.processors.BehaviourInterfaceImplementor.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public void process(BehaviourClassNode classNode) throws Exception { // add Behaviour interface if not already present if (!Behaviour.class.isAssignableFrom(classNode.getParentClass())) { classNode.addInterface(BEHAVIOUR_TYPE.getInternalName()); }/*from w ww . j a v a 2 s . c o m*/ MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_TRANSIENT, Behaviour.GET_ENTITY_METHOD, Type.getMethodDescriptor(OBJECT_TYPE, new Type[0]), null, null); MethodNodeGenerator gen = new MethodNodeGenerator(mn); gen.loadThis(); gen.getField(classNode.getType(), "_$bean", OBJECT_TYPE); gen.returnValue(); gen.endMethod(); classNode.methods.add(mn); }
From source file:net.enilink.composition.asm.processors.CompositeConstructorGenerator.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public void process(CompositeClassNode classNode) throws Exception { MethodNode constructor = new MethodNode(ACC_PUBLIC, "<init>", "()V", null, null); // call the constructor of the parent class MethodNodeGenerator gen = new MethodNodeGenerator(constructor); gen.loadThis();/*from w w w. j a va 2 s .c om*/ gen.invokeConstructor(classNode.getParentType(), new Method("<init>", "()V")); gen.returnValue(); gen.endMethod(); classNode.methods.add(constructor); classNode.getConstructors().add(constructor); }
From source file:net.enilink.composition.asm.util.InsnListGenerator.java
License:Open Source License
public InsnListGenerator(int access, String name, String desc, String signature, String[] exceptions) { this(new MethodNode(access, name, desc, signature, exceptions)); }
From source file:net.enilink.composition.asm.util.MethodNodeGenerator.java
License:Open Source License
public MethodNodeGenerator(int access, String name, String desc, String signature, String[] exceptions) { this(new MethodNode(access, name, desc, signature, exceptions)); }
From source file:net.enilink.composition.asm.util.MethodNodeGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") public InsnListGenerator createSubGenerator() { return new InsnListGenerator(new MethodNode(mn.access, mn.name, mn.desc, mn.signature, (String[]) mn.exceptions.toArray(new String[mn.exceptions.size()]))); }
From source file:net.petercashel.jmsDd.util.ASMTransformer.java
License:Apache License
public static byte[] transform(String name, byte[] bytes) { if (debug)/*from w w w. j a v a 2 s.c om*/ System.out.println(bytes.length); ClassNode classNode = new ClassNode(); String classNameASM = name.replace('.', '/'); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); boolean DoModInit = false; boolean HasInit = false; String initDesc = "()V"; boolean lockDesc = false; try { try { for (int i = 0; i < classNode.visibleAnnotations.size(); i++) { AnnotationNode ann = (AnnotationNode) classNode.visibleAnnotations.get(i); if (ann.desc.equalsIgnoreCase("Lnet/petercashel/jmsDd/module/Module;")) { try { if (debug) System.out.println("ANNOTE!"); DoModInit = true; Map<String, Object> values = asmList2Map(ann.values); ModuleSystem.modulesToLoad.put( values.get("ModuleName").toString().replace("[", "").replace("]", ""), classNode.name.replace("/", ".")); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { } try { if (DoModInit) { for (int i = 0; i < classNode.methods.size(); i++) { MethodNode m = (MethodNode) classNode.methods.get(i); if (m.name.contentEquals("<init>")) { initDesc = m.desc; if (m.desc.contentEquals("()V")) { HasInit = true; if (debug) System.out.println("Found <init>"); } } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } if (debug) System.out.println("Still alive?"); try { // L0 // LINENUMBER 43 L0 // ALOAD 0 // INVOKESPECIAL // CLASSNAME FOR ASM // ()V ////// This is effectically a super() call but to the discovered constructor // L1 // LINENUMBER 44 L1 // INVOKESTATIC net/petercashel/jmsDd/API/API$Impl.getAPI ()Lnet/petercashel/jmsDd/API/API; // ALOAD 0 // INVOKEINTERFACE net/petercashel/jmsDd/API/API.registerEventBus (Ljava/lang/Object;)V // L2 // LINENUMBER 46 L2 // RETURN // L3 // LOCALVARIABLE this // "L" + CLASSNAME FOR ASM + ";" // L0 L3 0 // LOCALVARIABLE e Lnet/petercashel/jmsDd/event/module/DummyEvent; L0 L3 1 // MAXSTACK = 2 // MAXLOCALS = 2 if (DoModInit) { if (HasInit) { if (debug) System.out.println("Adding Extra Constructor to " + name); MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null); Label L0 = new Label(); constructor.visitLabel(L0); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, classNameASM, "<init>", initDesc); Label L1 = new Label(); constructor.visitLabel(L1); constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl", "getAPI", "()Lnet/petercashel/jmsDd/API/API;"); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API", "registerEventBus", "(Ljava/lang/Object;)V"); Label L2 = new Label(); constructor.visitLabel(L2); constructor.visitInsn(Opcodes.RETURN); Label L3 = new Label(); constructor.visitLabel(L3); constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0); constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0, L3, 1); constructor.visitMaxs(2, 2); constructor.visitEnd(); classNode.methods.add(constructor); } else { System.err.println("WARNING! " + name + " Doesn't have a default no-args constructor. Module loader cannot chain load the constructor. \n If you are recieving this error and your module has no constructors defined, or a no-args constructor defined, \n please report the bug to the author of JMSDd."); MethodNode constructor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "(Lnet/petercashel/jmsDd/event/module/DummyEvent;)V", null, null); Label L0 = new Label(); constructor.visitLabel(L0); constructor.visitVarInsn(Opcodes.ALOAD, 0); //INVOKESPECIAL java/lang/Object.<init> ()V ////// There is no other constructor, call super() to Object. constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); Label L1 = new Label(); constructor.visitLabel(L1); constructor.visitMethodInsn(Opcodes.INVOKESTATIC, "net/petercashel/jmsDd/API/API$Impl", "getAPI", "()Lnet/petercashel/jmsDd/API/API;"); constructor.visitVarInsn(Opcodes.ALOAD, 0); constructor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "net/petercashel/jmsDd/API/API", "registerEventBus", "(Ljava/lang/Object;)V"); Label L2 = new Label(); constructor.visitLabel(L2); constructor.visitInsn(Opcodes.RETURN); Label L3 = new Label(); constructor.visitLabel(L3); constructor.visitLocalVariable("this", "L" + classNameASM + ";", null, L0, L3, 0); constructor.visitLocalVariable("e", "Lnet/petercashel/jmsDd/event/module/DummyEvent;", null, L0, L3, 1); constructor.visitMaxs(2, 2); constructor.visitEnd(); classNode.methods.add(constructor); } classNode.visitEnd(); ClassWriter wr = new ClassWriter(0); classNode.accept(wr); bytes = wr.toByteArray(); } } catch (Exception e) { e.printStackTrace(); } if (debug) System.out.println("Still alive."); if (debug) System.out.println(bytes.length); if (plugins.size() > 0) { for (ASMPlugin p : plugins) { try { bytes = p.transform(name, bytes); } catch (Exception e) { } } } return bytes; }
From source file:net.sandius.rembulan.compiler.gen.asm.ConstructorMethod.java
License:Apache License
public MethodNode methodNode() { MethodNode node = new MethodNode(ACC_PUBLIC, "<init>", methodType().getDescriptor(), null, null); InsnList il = node.instructions;/*from w w w . jav a2s . c o m*/ LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); node.localVariables .add(new LocalVariableNode("this", context.thisClassType().getDescriptor(), null, begin, end, 0)); il.add(begin); // superclass constructor il.add(new VarInsnNode(ALOAD, 0)); il.add(new MethodInsnNode(INVOKESPECIAL, context.superClassType().getInternalName(), "<init>", Type.getMethodType(Type.VOID_TYPE).getDescriptor(), false)); // initialise upvalue fields int idx = 0; for (UpVar uv : context.fn.upvals()) { String name = context.getUpvalueFieldName(uv); il.add(new VarInsnNode(ALOAD, 0)); // this il.add(new VarInsnNode(ALOAD, 1 + idx)); // upvalue #i il.add(new FieldInsnNode(PUTFIELD, context.thisClassType().getInternalName(), name, Type.getDescriptor(Variable.class))); node.localVariables .add(new LocalVariableNode(name, Type.getDescriptor(Variable.class), null, begin, end, idx)); idx++; } // instantiate fields for closures that have no open upvalues for (RunMethod.ClosureFieldInstance cfi : runMethod.closureFields()) { context.fields().add(cfi.fieldNode()); il.add(cfi.instantiateInsns()); } il.add(new InsnNode(RETURN)); il.add(end); node.maxStack = 2; node.maxLocals = context.fn.upvals().size() + 1; return node; }
From source file:net.sandius.rembulan.compiler.gen.asm.InvokeMethod.java
License:Apache License
public MethodNode methodNode() { MethodNode node = new MethodNode(ACC_PUBLIC, "invoke", context.invokeMethodType().getDescriptor(), null, runMethod.throwsExceptions()); InsnList il = node.instructions;//from w w w .ja v a 2 s. c o m List<LocalVariableNode> locals = node.localVariables; LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); int invokeKind = context.kind(); il.add(begin); // a (slotIdx -> paramIdx) map int[] slotParamMap = new int[context.slots.numSlots()]; Arrays.fill(slotParamMap, -1); for (int paramIdx = 0; paramIdx < context.fn.params().size(); paramIdx++) { int slotIdx = context.slots.slotOf(context.fn.params().get(paramIdx)); assert (slotParamMap[slotIdx] == -1); slotParamMap[slotIdx] = paramIdx; } if (invokeKind > 0) { il.add(new VarInsnNode(ALOAD, 0)); // this il.add(new VarInsnNode(ALOAD, 1)); // context il.add(ASMUtils.loadInt(0)); // resumption point // we have (invokeKind - 1) standalone parameters, mapping them onto numSlots for (int paramIdx : slotParamMap) { if (paramIdx < 0) { // slot unused il.add(new InsnNode(ACONST_NULL)); } else { // used by the parameter #paramIdx Var param = context.fn.params().get(paramIdx); boolean reified = context.types.isReified(param); if (reified) { il.add(new TypeInsnNode(NEW, Type.getInternalName(Variable.class))); il.add(new InsnNode(DUP)); } il.add(new VarInsnNode(ALOAD, 2 + paramIdx)); if (reified) { il.add(VariableMethods.constructor()); } } } } else { // variable number of parameters, encoded in an array at position 2 int lv_varargsSize = 3; int lv_varargs = 4; int numParams = context.numOfParameters(); if (context.isVararg()) { LabelNode l_v_begin = new LabelNode(); LabelNode l_v_nonempty = new LabelNode(); LabelNode l_v_empty = new LabelNode(); LabelNode l_v_done = new LabelNode(); il.add(new VarInsnNode(ALOAD, 2)); il.add(new InsnNode(ARRAYLENGTH)); if (numParams > 0) { il.add(ASMUtils.loadInt(context.numOfParameters())); il.add(new InsnNode(ISUB)); } il.add(new VarInsnNode(ISTORE, lv_varargsSize)); il.add(l_v_begin); il.add(new VarInsnNode(ILOAD, lv_varargsSize)); il.add(new JumpInsnNode(IFLE, l_v_empty)); // nonempty varargs // varargs = new Object[varargsSize]; il.add(new VarInsnNode(ILOAD, lv_varargsSize)); il.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Object.class))); il.add(new VarInsnNode(ASTORE, lv_varargs)); il.add(l_v_nonempty); // call System.arraycopy(src, srcPos, dest, destPos, len) il.add(new VarInsnNode(ALOAD, 2)); // src il.add(ASMUtils.loadInt(numParams)); // srcPos il.add(new VarInsnNode(ALOAD, lv_varargs)); // dest il.add(ASMUtils.loadInt(0)); // destPos il.add(new VarInsnNode(ILOAD, lv_varargsSize)); // len il.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(System.class), "arraycopy", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class), Type.INT_TYPE, Type.getType(Object.class), Type.INT_TYPE, Type.INT_TYPE), false)); il.add(new JumpInsnNode(GOTO, l_v_done)); // empty varargs il.add(l_v_empty); il.add(new FrameNode(F_APPEND, 1, new Object[] { Opcodes.INTEGER }, 0, null)); // varargs = new Object[0]; il.add(ASMUtils.loadInt(0)); il.add(new TypeInsnNode(ANEWARRAY, Type.getInternalName(Object.class))); il.add(new VarInsnNode(ASTORE, lv_varargs)); il.add(l_v_done); il.add(new FrameNode(F_APPEND, 1, new Object[] { ASMUtils.arrayTypeFor(Object.class).getInternalName() }, 0, null)); locals.add(new LocalVariableNode("sz", Type.INT_TYPE.getDescriptor(), null, l_v_begin, end, lv_varargsSize)); locals.add(new LocalVariableNode("varargs", ASMUtils.arrayTypeFor(Object.class).getDescriptor(), null, l_v_nonempty, l_v_empty, lv_varargs)); locals.add(new LocalVariableNode("varargs", ASMUtils.arrayTypeFor(Object.class).getDescriptor(), null, l_v_done, end, lv_varargs)); } // load #numOfParameters, mapping them onto #numOfRegisters int lv_param_offset = context.isVararg() ? lv_varargs + 1 : lv_varargsSize; if (numParams > 0) { // initialise parameter slot variables to null for (int i = 0; i < numParams; i++) { LabelNode l = new LabelNode(); int lv = lv_param_offset + i; il.add(new InsnNode(ACONST_NULL)); il.add(new VarInsnNode(ASTORE, lv)); il.add(l); il.add(new FrameNode(F_APPEND, 1, new Object[] { Type.getInternalName(Object.class) }, 0, null)); locals.add( new LocalVariableNode("arg_" + i, Type.getDescriptor(Object.class), null, l, end, lv)); } // insert switch for filling parameter slots LabelNode[] l_s_table = new LabelNode[numParams + 1]; for (int i = 0; i < numParams + 1; i++) { l_s_table[i] = new LabelNode(); } il.add(new VarInsnNode(ALOAD, 2)); il.add(new InsnNode(ARRAYLENGTH)); il.add(new TableSwitchInsnNode(0, numParams, l_s_table[numParams], l_s_table)); for (int i = numParams; i >= 0; i--) { // length of args is at least i; may assign into param (i - 1) int paramIdx = i - 1; il.add(l_s_table[i]); il.add(new FrameNode(F_SAME, 0, null, 0, null)); if (paramIdx >= 0) { // assign into param #paramIdx il.add(new VarInsnNode(ALOAD, 2)); il.add(ASMUtils.loadInt(paramIdx)); il.add(new InsnNode(AALOAD)); il.add(new VarInsnNode(ASTORE, lv_param_offset + paramIdx)); } } } // now assemble the run() method invocation, filling in nulls for non-parameter slots il.add(new VarInsnNode(ALOAD, 0)); // this il.add(new VarInsnNode(ALOAD, 1)); // context il.add(ASMUtils.loadInt(0)); // resumption point if (context.isVararg()) { il.add(new VarInsnNode(ALOAD, lv_varargs)); } for (int paramIdx : slotParamMap) { if (paramIdx < 0) { // slot not used by a parameter il.add(new InsnNode(ACONST_NULL)); } else { // slot is parameter #paramIdx Var param = context.fn.params().get(paramIdx); boolean reified = context.types.isReified(param); if (reified) { il.add(new TypeInsnNode(NEW, Type.getInternalName(Variable.class))); il.add(new InsnNode(DUP)); } il.add(new VarInsnNode(ALOAD, lv_param_offset + paramIdx)); if (reified) { il.add(VariableMethods.constructor()); } } } } il.add(runMethod.methodInvokeInsn()); il.add(new InsnNode(RETURN)); il.add(end); locals.add(new LocalVariableNode("this", context.thisClassType().getDescriptor(), null, begin, end, 0)); locals.add( new LocalVariableNode("context", Type.getDescriptor(ExecutionContext.class), null, begin, end, 1)); if (invokeKind > 0) { for (int i = 0; i < invokeKind; i++) { locals.add(new LocalVariableNode("arg_" + i, Type.getDescriptor(Object.class), null, begin, end, 2 + i)); } // TODO: maxLocals, maxStack } else { locals.add(new LocalVariableNode("args", ASMUtils.arrayTypeFor(Object.class).getDescriptor(), null, begin, end, 2)); // TODO: maxLocals, maxStack } return node; }