List of usage examples for org.objectweb.asm.tree MethodNode visitLocalVariable
@Override public void visitLocalVariable(final String name, final String descriptor, final String signature, final Label start, final Label end, final int index)
From source file:de.sanandrew.core.manpack.transformer.TransformPlayerDismountCtrl.java
License:Creative Commons License
/** * Transforms the Entity.class by adding a new method called _SAP_canDismountOnInput.<br> * This method can be overridden by any entity to control wether or not the rider can dismount via sneaking (usually by pressing LSHIFT for the player). * * @param bytes the class bytes to be transformed * @return the transformed class bytes// www .j av a2s . c o m */ private static byte[] transformEntity(byte[] bytes) { ClassNode clazz = ASMHelper.createClassNode(bytes); MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_DISMOUNT_ON_INPUT); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitInsn(Opcodes.ICONST_1); method.visitInsn(Opcodes.IRETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0); method.visitLocalVariable("player", ASMNames.CL_T_ENTITY_PLAYER, null, l0, l1, 1); method.visitMaxs(1, 2); method.visitEnd(); clazz.methods.add(method); bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS); return bytes; }
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 ww. jav a 2 s .com*/ 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.petercashel.jmsDd.util.ASMTransformer.java
License:Apache License
public static byte[] transform(String name, byte[] bytes) { if (debug)/*from w w w . java 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:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java
License:Open Source License
@Override protected boolean transform() { // void _OT$addRemoveRole(Object role, boolean isAdding) { MethodNode method = getMethod(ConstantMembers.addOrRemoveRole); final int ROLE_SLOT = 1, IS_ADDING_SLOT = 2; Label start = new Label(), end = new Label(); method.instructions.clear();// ww w . j a v a 2s. co m method.visitLabel(start); // set = <initialized _OT$roleSet;> final int SET_SLOT = 3; method.visitLocalVariable("set", "Ljava/util/Set;", null, start, end, SET_SLOT); genGetInitializedRoleSet(method.instructions, SET_SLOT); // if (isAdding) { method.instructions.add(new IntInsnNode(Opcodes.ILOAD, IS_ADDING_SLOT)); LabelNode jumpToRemove = new LabelNode(); method.instructions.add(new JumpInsnNode(Opcodes.IFEQ, jumpToRemove)); // set.add(role); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT)); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT)); method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "add", "(Ljava/lang/Object;)Z", false)); method.instructions.add(new InsnNode(Opcodes.POP)); LabelNode jumpToEnd = new LabelNode(); method.instructions.add(new JumpInsnNode(Opcodes.GOTO, jumpToEnd)); // } else { method.instructions.add(jumpToRemove); // set.remove(role); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT)); method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT)); method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "remove", "(Ljava/lang/Object;)Z", false)); method.instructions.add(new InsnNode(Opcodes.POP)); method.instructions.add(jumpToEnd); // } method.instructions.add(new InsnNode(Opcodes.RETURN)); method.visitLabel(end); // } // maxs are computed, maxStack from flow, maxLocals from localVariable-slots return true; }