List of usage examples for org.objectweb.asm.tree MethodNode MethodNode
public MethodNode(final int api, final int access, final String name, final String descriptor, final String signature, final String[] exceptions)
From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java
License:MIT License
/** * Generate a forwarding method of the form * "T name() { return Class.forward(this); }". * * @param clazz Class to generate new method on * @param name Name of method to generate * @param forwardname Name of method to call * @param rettype Return type of method//from w w w .j ava2 s . c om * @param fowardtype Forward type * @param thistype Type to treat 'this' as for overload searching purposes */ public static void generateForwardingToStaticMethod(ClassNode clazz, String name, String forwardname, Type rettype, Type fowardtype, Type thistype) { MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()" + rettype.getDescriptor(), null, null); ASMHelper.populateForwardingToStaticMethod(method, forwardname, rettype, thistype, fowardtype); clazz.methods.add(method); }
From source file:com.thomas15v.packetlib.codegenerator.asm.ASMHelper.java
License:MIT License
/** * Generate a forwarding method of the form * "T name(S object) { return object.forward(); }". * * @param clazz Class to generate new method on * @param name Name of method to generate * @param forwardname Name of method to call * @param rettype Return type of method/* w w w .j a v a 2s . c o m*/ * @param argtype Type of object to call method on */ public static void generateForwardingMethod(ClassNode clazz, String name, String forwardname, Type rettype, Type argtype) { MethodNode method = new MethodNode(Opcodes.ASM5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, name, "()" + rettype.getDescriptor(), null, null); ASMHelper.populateForwardingMethod(method, forwardname, rettype, argtype, Type.getObjectType(clazz.name)); clazz.methods.add(method); }
From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java
License:Open Source License
@Override public InsnList optimize(final InsnList original, final MethodNode methodNode) throws JBOPClassException { LocalVariablesSorter sorter = new LocalVariablesSorter(methodNode.access, methodNode.desc, new EmptyMethodVisitor(Opcodes.ASM5)); methodNode.accept(sorter);//ww w. j a v a 2 s . c om if (getLength(methodNode) < maxInsns) { return clean(original); } final List<Block> blocks = getBlocks(original, methodNode); final String baseName = methodNode.name; final String[] exceptions = getExceptions(methodNode); final InsnList returnList = new InsnList(); InsnList list = returnList; Block block = blocks.get(0); block.renameInsns(block.getVarMap()); final String name = baseName + "__split__part__"; final Type endType = Type.getReturnType(methodNode.desc); for (int i = 1; i < blocks.size(); ++i) { final Map<Integer, Integer> paramRenameMap = block.getVarMap(); add(list, block.getInstructions(), original); block = blocks.get(i); block.renameInsns(paramRenameMap); final String methodDescriptor = block.getDescriptor(); final String newMethodName = name + block.getBlockNumber(); final MethodNode splitMethod = new MethodNode(Opcodes.ASM5, ACCESS, newMethodName, methodDescriptor, null, exceptions); additionalMethods.add(splitMethod); list.add(new VarInsnNode(Opcodes.ALOAD, 0)); list.add(block.getPushParameters()); list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, classNode.name, splitMethod.name, methodDescriptor)); list.add(new InsnNode(endType.getOpcode(IRETURN))); sorter = new LocalVariablesSorter(splitMethod.access, splitMethod.desc, new EmptyMethodVisitor(Opcodes.ASM5)); splitMethod.accept(sorter); list = splitMethod.instructions; } add(list, block.getInstructions(), original); return returnList; }
From source file:forgefix.ForgeFixTransformer.java
License:LGPL
@Override public byte[] transform(String name, String transformedName, byte[] bytes) { if (bytes == null) return bytes; if (transformedName.equals("com.xcompwiz.mystcraft.world.gen.structure.ComponentVillageArchivistHouse") || (transformedName.equals(//ww w .ja v a 2s. c o m "com.xcompwiz.mystcraft.world.gen.structure.ComponentScatteredFeatureSmallLibrary"))) { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode vanillaMethod = null; for (MethodNode method : classNode.methods) { if (method.name.equals("generateStructureLectern")) { vanillaMethod = method; } } MethodNode moddedMethod = new MethodNode(ASM4, ACC_PROTECTED, "generateStructureLectern", getMethodDescriptor(BOOLEAN_TYPE), null, null); moddedMethod.desc = "(Lnet/minecraft/world/World;Lnet/minecraft/world/gen/structure/StructureBoundingBox;Ljava/util/Random;IIIIILnet/minecraftforge/common/ChestGenHooks;)Z"; moddedMethod.instructions.add(new InsnNode(ICONST_0)); moddedMethod.instructions.add(new InsnNode(IRETURN)); if (moddedMethod != null) { classNode.methods.remove(vanillaMethod); moddedMethod.accept(classNode); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); classNode.accept(writer); return writer.toByteArray(); } } return bytes; }
From source file:jvstm.atomic.ProcessParNestAnnotations.java
License:Open Source License
private static MethodNode generateStaticCallableCreation(ClassNode classNode, String className, String callableClass, MethodNode mn) { MethodNode staticMethod = new MethodNode(V1_6, mn.access | ACC_STATIC, mn.name + "$static$callable$creator", "(L" + className + ";" + mn.desc.substring(1, mn.desc.indexOf(')') + 1) + "L" + callableClass + ";", mn.signature, new String[0]); InsnList content = new InsnList(); content.add(new TypeInsnNode(NEW, callableClass)); content.add(new InsnNode(DUP)); int pos = 0;//from ww w.jav a2s . c o m // Push the instance of the class being modified (first argument of this // synthetized method) content.add(new VarInsnNode(ALOAD, pos++)); // Push arguments of original method on the stack for callable creation for (Type t : Type.getArgumentTypes(mn.desc)) { content.add(new VarInsnNode(t.getOpcode(ILOAD), pos)); pos += t.getSize(); } // Instantiate the callable content.add(new MethodInsnNode(INVOKESPECIAL, callableClass, "<init>", getCallableCtorDesc(className, mn))); // Return it from the static method content.add(new InsnNode(ARETURN)); staticMethod.instructions.add(content); return staticMethod; }
From source file:me.themallard.bitmmo.impl.plugin.inputactiontracker.InputActionTrackerPlugin.java
License:Open Source License
private void createSetKeyDown(ClassNode cn) { InsnList insn = new InsnList(); MethodNode mn = new MethodNode(cn, ACC_PUBLIC, "setKeyDown", "(LHTMud/InputActionTracker$ActionType;Z)V", null, null);//from www . j a va2 s. com // ((Key) this.enumMap.get(actionType)).setKeyPressed(value); // aload0 // getfield this.enumMap Ljava/util/EnumMap; // aload1 // invokevirtual \ // java/util/EnumMap.get((Ljava/lang/Object;)Ljava/lang/Object;); // checkcast Key // aload2 // invokevirtual Key.setKeyPressed((Z)V); // return mn.visitMaxs(3, 3); insn.add(new VarInsnNode(ALOAD, 0)); FieldNode enumMap = cn.getField(null, "Ljava/util/EnumMap;"); insn.add(new FieldInsnNode(GETFIELD, cn.name, enumMap.name, enumMap.desc)); insn.add(new VarInsnNode(ALOAD, 1)); insn.add(new MethodInsnNode(INVOKEVIRTUAL, "java/util/EnumMap", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", false)); insn.add(new TypeInsnNode(CHECKCAST, "Key")); insn.add(new VarInsnNode(ILOAD, 2)); insn.add(new MethodInsnNode(INVOKEVIRTUAL, "Key", "setKeyPressed", "(Z)V", false)); insn.add(new InsnNode(RETURN)); mn.instructions.insert(insn); cn.methods.add(mn); }
From source file:naftoreiclag.dontdigleft.transformer.ResizePlayerBoudingBox.java
License:Open Source License
@Override public byte[] transform(String name, String transformedName, byte[] basicClass) { if (name.equals("net.minecraft.entity.player.EntityPlayer") || name.equals("xl")) { ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(basicClass); classReader.accept(classNode, 0); MethodNode mv = new MethodNode(ASM4, ACC_PROTECTED, "setSize", "(FF)V", null, null); mv.visitCode();//from w w w. j a va 2 s .c o m Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(FLOAD, 1); mv.visitLdcInsn(new Float("0.6")); mv.visitInsn(FCMPL); Label l1 = new Label(); mv.visitJumpInsn(IFNE, l1); mv.visitVarInsn(FLOAD, 2); mv.visitLdcInsn(new Float("1.8")); mv.visitInsn(FCMPL); mv.visitJumpInsn(IFNE, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitLdcInsn(new Float("0.6")); mv.visitVarInsn(FSTORE, 1); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLdcInsn(new Float("0.6")); mv.visitVarInsn(FSTORE, 2); mv.visitLabel(l1); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(FLOAD, 1); mv.visitVarInsn(FLOAD, 2); mv.visitMethodInsn(INVOKESPECIAL, "net/minecraft/entity/Entity", "setSize", "(FF)V"); Label l4 = new Label(); mv.visitLabel(l4); mv.visitInsn(RETURN); Label l5 = new Label(); mv.visitLabel(l5); mv.visitLocalVariable("this", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l5, 0); mv.visitLocalVariable("par1", "F", null, l0, l5, 1); mv.visitLocalVariable("par2", "F", null, l0, l5, 2); mv.visitMaxs(3, 3); mv.visitEnd(); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); mv.accept(cw); return cw.toByteArray(); } else { return basicClass; } }
From source file:net.enilink.composition.asm.meta.ClassInfo.java
License:Open Source License
public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) { MethodNode mn = new MethodNode(Opcodes.ASM5, access, name, desc, signature, exceptions) { public void visitEnd() { instructions.clear();// ww w .j a v a2 s. c om } }; methods.put(new Method(name, desc), mn); return mn; }
From source file:org.jacoco.core.internal.analysis.filter.AnnotationGeneratedFilterTest.java
License:Open Source License
@Test public void should_filter_methods_annotated_with_runtime_visible_org_groovy_transform_Generated() { final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null); m.visitAnnotation("Lgroovy/transform/Generated;", true); m.visitInsn(Opcodes.ICONST_0);/*from w w w. ja va 2 s . c om*/ m.visitInsn(Opcodes.IRETURN); filter.filter(m, context, output); assertMethodIgnored(m); }
From source file:org.jacoco.core.internal.analysis.filter.AnnotationGeneratedFilterTest.java
License:Open Source License
@Test public void should_filter_methods_annotated_with_runtime_invisible_lombok_Generated() { final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "hashCode", "()I", null, null); m.visitAnnotation("Llombok/Generated;", false); m.visitInsn(Opcodes.ICONST_0);/* w w w.ja v a 2 s. c om*/ m.visitInsn(Opcodes.IRETURN); filter.filter(m, context, output); assertMethodIgnored(m); }