List of usage examples for org.objectweb.asm.tree MethodNode visitLabel
@Override public void visitLabel(final Label label)
From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java
License:Apache License
/** * Creates the field-getter getter method in the specified {@code ClassNode} * * @param cn The ClassNode//from w ww. j av a 2 s .co m */ private void createFieldGetter(ClassNode cn) { MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldGetter", "(Ljava/lang/String;)Ljava/util/function/Supplier;", null, null); // Create a check for all labeled fields in the cache fieldCache.forEach((id, fn) -> { MethodNode handle; { // Create lambda handle method handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldGetter$" + current++, "()Ljava/lang/Object;", null, null); // Get the field value handle.visitVarInsn(ALOAD, 0); handle.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc); // If the field is a primitive type, get the object representation String object = getObject(fn.desc); if (!object.equals(fn.desc)) handle.visitMethodInsn(INVOKESTATIC, object, "valueOf", "(" + fn.desc + ")L" + object + ";", false); // Return the value handle.visitInsn(ARETURN); // Add the handle method to the class cn.methods.add(handle); } // Create label for IF statement jump Label skip = new Label(); // Compare the target value with the expected value mn.visitVarInsn(ALOAD, 1); mn.visitLdcInsn(id); mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false); // Jump if the input doesn't match the expected value mn.visitJumpInsn(IFEQ, skip); // Return the getter mn.visitVarInsn(ALOAD, 0); mn.visitInvokeDynamicInsn("get", "(L" + cn.name + ";)Ljava/util/function/Supplier;", // Define the bootstrap method METAFACTORY, // Fill the remaining 3 args for the method Type.getMethodType("()Ljava/lang/Object;"), createMethodHandle(H_INVOKESPECIAL, cn, handle), Type.getMethodType("()Ljava/lang/Object;")); mn.visitInsn(ARETURN); // Indicate where the IF statement should jump to if it fails mn.visitLabel(skip); }); mn.visitInsn(ACONST_NULL); mn.visitInsn(ARETURN); cn.methods.add(mn); }
From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java
License:Apache License
/** * Creates the field-setter getter method in the specified {@code ClassNode} * * @see ValueAccessor/* w ww. j a v a 2 s . co m*/ * * @param cn The ClassNode */ private void createFieldSetter(ClassNode cn) { MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldSetter", "(Ljava/lang/String;)Ljava/util/function/Consumer;", null, null); // Create a check for all labeled fields in the cache fieldCache.forEach((id, fn) -> { MethodNode handle; { // Create lambda handle method handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldSetter$" + current++, "(Ljava/lang/Object;)V", null, null); handle.visitVarInsn(ALOAD, 0); handle.visitVarInsn(ALOAD, 1); handle.visitTypeInsn(CHECKCAST, getStrippedDesc(getObject(fn.desc))); // If the field is a primitive type, get the primitive value String object = getObject(fn.desc); if (!object.equals(fn.desc)) handle.visitMethodInsn(INVOKEVIRTUAL, object, getClassName(fn.desc) + "Value", "()" + fn.desc, false); // Set the field value handle.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc); handle.visitInsn(RETURN); // Add the handle method to the class cn.methods.add(handle); } // Create label for IF statement jump Label skip = new Label(); // Compare the target value with the expected value mn.visitVarInsn(ALOAD, 1); mn.visitLdcInsn(id); mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false); // Jump if the input doesn't match the expected value mn.visitJumpInsn(IFEQ, skip); // Return the setter mn.visitVarInsn(ALOAD, 0); mn.visitInvokeDynamicInsn("accept", "(L" + cn.name + ";)Ljava/util/function/Consumer;", // Define the bootstrap method METAFACTORY, // Fill the remaining 3 args for the method Type.getMethodType("(Ljava/lang/Object;)V"), createMethodHandle(H_INVOKESPECIAL, cn, handle), Type.getMethodType("(Ljava/lang/Object;)V")); mn.visitInsn(ARETURN); // Indicate where the IF statement should jump to if it fails mn.visitLabel(skip); }); mn.visitInsn(ACONST_NULL); mn.visitInsn(ARETURN); cn.methods.add(mn); }
From source file:com.seovic.pof.PortableTypeGenerator.java
License:Apache License
private void implementReadExternal() { int index = 0; MethodNode mn = new MethodNode(ACC_PRIVATE, "readExternal", "(Lcom/tangosol/io/pof/PofReader;)V", null, new String[] { "java/io/IOException" }); mn.visitCode();/*from w w w. ja v a 2 s . co m*/ for (int version : properties.keySet()) { mn.visitVarInsn(ALOAD, 1); mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", "getVersionId", "()I"); mn.visitIntInsn(BIPUSH, version); Label l = new Label(); mn.visitJumpInsn(IF_ICMPLT, l); SortedSet<FieldNode> fields = properties.get(version); for (FieldNode fn : fields) { Type type = Type.getType(fn.desc); if (isDebugEnabled()) { mn.visitLdcInsn("reading attribute " + index + " (" + fn.name + ") from POF stream"); mn.visitIntInsn(BIPUSH, 7); mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log", "(Ljava/lang/String;I)V"); } mn.visitVarInsn(ALOAD, 0); mn.visitVarInsn(ALOAD, 1); mn.visitIntInsn(BIPUSH, index++); ReadMethod readMethod = getReadMethod(fn, type); readMethod.createTemplate(mn, fn, type); mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", readMethod.getName(), readMethod.getDescriptor()); if (type.getSort() == Type.OBJECT || "readObjectArray".equals(readMethod.getName())) { mn.visitTypeInsn(CHECKCAST, type.getInternalName()); } mn.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc); } mn.visitLabel(l); mn.visitFrame(F_SAME, 0, null, 0, null); } mn.visitInsn(RETURN); mn.visitMaxs(0, 0); mn.visitEnd(); if (!hasMethod(mn)) { cn.methods.add(mn); } LOG.debug("Implemented method: " + mn.name); }
From source file:de.sanandrew.core.manpack.transformer.TransformBadPotionsATN.java
License:Creative Commons License
private byte[] transformPotion(byte[] bytes) { ClassNode cn = ASMHelper.createClassNode(bytes); if (ASMHelper.hasClassMethodName(cn, ASMNames.M_isBadEffect)) { return bytes; }/* w ww . j a v a 2 s. c o m*/ MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, ASMNames.M_isBadEffect, "()Z", null, null); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/potion/Potion", ASMNames.F_isBadEffect, "Z"); method.visitInsn(Opcodes.IRETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("this", "Lnet/minecraft/potion/Potion;", null, l0, l1, 0); method.visitMaxs(0, 0); method.visitEnd(); cn.methods.add(method); bytes = ASMHelper.createBytes(cn, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); return bytes; }
From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java
License:Creative Commons License
private static byte[] transformAccessors(byte[] bytes) { ClassNode clazz = ASMHelper.createClassNode(bytes); int complete = 0; for (MethodNode method : clazz.methods) { switch (method.name) { case "getELAttackingPlayer": { method.instructions.clear(); method.visitCode();/*from w w w . j a va 2 s . c o m*/ Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "_SAP_getAttackingPlayer", "()Lnet/minecraft/entity/player/EntityPlayer;", false); method.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0); method.visitMaxs(0, 0); method.visitEnd(); complete++; continue; } case "setELAttackingPlayer": { method.instructions.clear(); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 1); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "_SAP_setAttackingPlayer", "(Lnet/minecraft/entity/player/EntityPlayer;)V", false); Label l1 = new Label(); method.visitLabel(l1); method.visitInsn(Opcodes.RETURN); Label l2 = new Label(); method.visitLabel(l2); method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 0); method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1); method.visitMaxs(0, 0); method.visitEnd(); complete++; continue; } case "getELRecentlyHit": { method.instructions.clear(); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "_SAP_getRecentlyHit", "()I", false); method.visitInsn(Opcodes.IRETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0); method.visitMaxs(0, 0); method.visitEnd(); complete++; continue; } case "setELRecentlyHit": { method.instructions.clear(); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 1); method.visitVarInsn(Opcodes.ILOAD, 0); method.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "net/minecraft/entity/EntityLivingBase", "_SAP_setRecentlyHit", "(I)V", false); Label l1 = new Label(); method.visitLabel(l1); method.visitInsn(Opcodes.RETURN); Label l2 = new Label(); method.visitLabel(l2); method.visitLocalVariable("hit", "I", null, l0, l2, 0); method.visitLocalVariable("entity", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 1); method.visitMaxs(0, 0); method.visitEnd(); complete++; continue; } } if (complete >= 4) { break; } } bytes = ASMHelper.createBytes(clazz, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); return bytes; }
From source file:de.sanandrew.core.manpack.transformer.TransformELBAttackingPlayer.java
License:Creative Commons License
private static byte[] transformAttackingPlayer(byte[] bytes) { ClassNode clazz = ASMHelper.createClassNode(bytes); /** ADD GETTER FOR ATTACKING PLAYER **/ {//from w w w. ja v a2 s. c o m MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getAttackingPlayer", "()Lnet/minecraft/entity/player/EntityPlayer;", null, null); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;"); method.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0); method.visitMaxs(0, 0); method.visitEnd(); clazz.methods.add(method); } /** ADD SETTER FOR ATTACKING PLAYER **/ { MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setAttackingPlayer", "(Lnet/minecraft/entity/player/EntityPlayer;)V", null, null); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitVarInsn(Opcodes.ALOAD, 1); method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_attackingPlayer, "Lnet/minecraft/entity/player/EntityPlayer;"); Label l1 = new Label(); method.visitLabel(l1); method.visitInsn(Opcodes.RETURN); Label l2 = new Label(); method.visitLabel(l2); method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0); method.visitLocalVariable("player", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l2, 1); method.visitMaxs(0, 0); method.visitEnd(); clazz.methods.add(method); } /** ADD GETTER FOR RECENTLY HIT **/ { MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_getRecentlyHit", "()I", null, null); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitFieldInsn(Opcodes.GETFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit, "I"); method.visitInsn(Opcodes.IRETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l1, 0); method.visitMaxs(0, 0); method.visitEnd(); clazz.methods.add(method); } /** ADD SETTER FOR RECENTLY HIT **/ { MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, "_SAP_setRecentlyHit", "(I)V", null, null); method.visitCode(); Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); method.visitVarInsn(Opcodes.ILOAD, 1); method.visitFieldInsn(Opcodes.PUTFIELD, "net/minecraft/entity/EntityLivingBase", ASMNames.F_recentlyHit, "I"); Label l1 = new Label(); method.visitLabel(l1); method.visitInsn(Opcodes.RETURN); Label l2 = new Label(); method.visitLabel(l2); method.visitLocalVariable("this", "Lnet/minecraft/entity/EntityLivingBase;", null, l0, l2, 0); method.visitLocalVariable("hit", "I", null, l0, l2, 1); method.visitMaxs(0, 0); method.visitEnd(); clazz.methods.add(method); } bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS); return bytes; }
From source file:de.sanandrew.core.manpack.transformer.TransformEntityCollision.java
License:Creative Commons License
private static byte[] transformEntity(byte[] bytes) { ClassNode clazz = ASMHelper.createClassNode(bytes); MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_ENTITY_GET_BOUNDING_BOX); method.visitCode();// www.j a v a2s .co m Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 2); method.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0); method.visitLocalVariable("entity", ASMNames.CL_T_ENTITY, null, l0, l1, 1); method.visitLocalVariable("oldAABB", ASMNames.CL_T_AXIS_ALIGNED_BB, null, l0, l1, 2); 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:de.sanandrew.core.manpack.transformer.TransformEntityThrowable.java
License:Creative Commons License
private static byte[] transformLqThrowable(byte[] bytes) { ClassNode classNode = ASMHelper.createClassNode(bytes); MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID); method.visitCode();/* w w w. j av a 2 s .com*/ Label label1 = new Label(); method.visitLabel(label1); method.visitInsn(Opcodes.ICONST_0); method.visitInsn(Opcodes.IRETURN); Label label2 = new Label(); method.visitLabel(label2); method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_THROWABLE, null, label1, label2, 0); method.visitMaxs(0, 0); method.visitEnd(); classNode.methods.add(method); method = ASMHelper.findMethod(classNode, ASMNames.MD_THROWABLE_ON_UPDATE); InsnList needle = new InsnList(); needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_MOTION_Z)); needle.add(new InsnNode(Opcodes.DADD)); needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKESTATIC, ASMNames.MD_VEC3_CREATE_VECTOR_HELPER, false)); needle.add(new VarInsnNode(Opcodes.ASTORE, 2)); needle.add(new LabelNode()); needle.add(new LineNumberNode(-1, new LabelNode())); needle.add(new VarInsnNode(Opcodes.ALOAD, 0)); needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ)); needle.add(new VarInsnNode(Opcodes.ALOAD, 1)); needle.add(new VarInsnNode(Opcodes.ALOAD, 2)); needle.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS, false)); needle.add(new VarInsnNode(Opcodes.ASTORE, -1)); VarInsnNode insertPoint = (VarInsnNode) ASMHelper.findLastNodeFromNeedle(method.instructions, needle); InsnList injectList = new InsnList(); injectList.add(new LabelNode()); injectList.add(new VarInsnNode(Opcodes.ALOAD, 0)); injectList.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_THROWABLE_WORLD_OBJ)); injectList.add(new VarInsnNode(Opcodes.ALOAD, 1)); injectList.add(new VarInsnNode(Opcodes.ALOAD, 2)); injectList.add(new VarInsnNode(Opcodes.ALOAD, 0)); injectList.add( ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_SAP_CAN_IMPACT_ON_LIQUID, false)); injectList.add( ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_WORLD_RAY_TRACE_BLOCKS_Z, false)); injectList.add(new VarInsnNode(Opcodes.ASTORE, insertPoint.var)); method.instructions.insert(insertPoint, injectList); return ASMHelper.createBytes(classNode, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS); }
From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java
License:Creative Commons License
private static MethodNode injectMethodGetCustomArmorItem() { MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PRIVATE, ASMNames.MD_SAP_GET_CUSTOM_ARMOR_ITEM); method.visitCode();/*www . ja v a 2 s . c om*/ Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 0); ASMHelper.visitFieldInsn(method, Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER); method.visitIntInsn(Opcodes.BIPUSH, 23); ASMHelper.visitMethodInsn(method, Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_GET_OBJ_STACK, false); method.visitInsn(Opcodes.ARETURN); Label l1 = new Label(); method.visitLabel(l1); method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_HORSE, null, l0, l1, 0); method.visitMaxs(2, 1); method.visitEnd(); return method; }
From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java
License:Creative Commons License
private static MethodNode injectMethodSetCustomArmorItem() { MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PRIVATE, ASMNames.MD_SAP_SET_CUSTOM_ARMOR_ITEM); method.visitCode();/*from w ww .j a va2s.c om*/ Label l0 = new Label(); method.visitLabel(l0); method.visitVarInsn(Opcodes.ALOAD, 1); Label l1 = new Label(); method.visitJumpInsn(Opcodes.IFNONNULL, l1); method.visitTypeInsn(Opcodes.NEW, ASMNames.CL_ITEM_STACK); method.visitInsn(Opcodes.DUP); ASMHelper.visitFieldInsn(method, Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_SHOVEL); method.visitInsn(Opcodes.ICONST_0); ASMHelper.visitMethodInsn(method, Opcodes.INVOKESPECIAL, ASMNames.MD_ITEMSTACK_INIT, false); method.visitVarInsn(Opcodes.ASTORE, 1); method.visitLabel(l1); method.visitFrame(Opcodes.F_SAME, 0, null, 0, null); method.visitVarInsn(Opcodes.ALOAD, 0); ASMHelper.visitFieldInsn(method, Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER); method.visitIntInsn(Opcodes.BIPUSH, 23); method.visitVarInsn(Opcodes.ALOAD, 1); ASMHelper.visitMethodInsn(method, Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_UPDATE_OBJ, false); Label l3 = new Label(); method.visitLabel(l3); method.visitInsn(Opcodes.RETURN); Label l4 = new Label(); method.visitLabel(l4); method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_HORSE, null, l0, l3, 0); method.visitLocalVariable("stack", ASMNames.CL_T_ITEM_STACK, null, l0, l3, 1); method.visitMaxs(5, 2); method.visitEnd(); return method; }