List of usage examples for org.objectweb.asm Label Label
public Label()
From source file:de.dynamicfiles.projects.gradle.plugins.javafx.tasks.internal.MonkeyPatcher.java
License:Apache License
private static void doMonkeyPatchFileHandleLeak(ClassReader classReader, ClassWriter classWriter) { classReader.accept(new ClassVisitor(Opcodes.ASM5, classWriter) { @Override//from www .j av a 2s . c o m public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (!(name.equals(METHOD_TO_MONKEY_PATCH) && desc.equals(METHOD_SIGNATURE_TO_MONKEY_PATCH))) { return super.visitMethod(access, name, desc, signature, exceptions); } // helpful source: http://web.cs.ucla.edu/~msb/cs239-tutorial/ // "We will do this using the Adapter Pattern. Adapters wrap an object, overriding some of its methods, and delegating to the others." // ugly adapter-pattern ... took me more time than I really can tell here <.< return getMonkeyPatchedFileHandleLeakMethodVisitor(access, name, desc, signature, exceptions); } private MethodVisitor getMonkeyPatchedFileHandleLeakMethodVisitor(int access, String name, String desc, String signature, String[] exceptions) { return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) { /* TODO improve detection of lambda-positions, numbers might vary on different compile-versions */ @Override public void visitCode() { // This mostly got generated from ASM itself, except some adjustments for lambda-IDs and removed "visitMaxs()"-call String javalangThrowable = "java/lang/Throwable"; String javaioFile = "java/io/File"; String javalangString = "java/lang/String"; Label l0 = new Label(); Label l1 = new Label(); Label l2 = new Label(); mv.visitTryCatchBlock(l0, l1, l2, javalangThrowable); Label l3 = new Label(); Label l4 = new Label(); Label l5 = new Label(); mv.visitTryCatchBlock(l3, l4, l5, javalangThrowable); Label l6 = new Label(); mv.visitTryCatchBlock(l3, l4, l6, null); Label l7 = new Label(); Label l8 = new Label(); Label l9 = new Label(); mv.visitTryCatchBlock(l7, l8, l9, javalangThrowable); Label l10 = new Label(); mv.visitTryCatchBlock(l5, l10, l6, null); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, 3); mv.visitVarInsn(Opcodes.ALOAD, 2); Label l11 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l11); mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "isDirectory", "()Z", false); Label l12 = new Label(); mv.visitJumpInsn(Opcodes.IFNE, l12); mv.visitLabel(l11); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { javalangString }, 0, null); mv.visitTypeInsn(Opcodes.NEW, javaioFile); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("java.home"); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "getProperty", "(Ljava/lang/String;)Ljava/lang/String;", false); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, javaioFile, "<init>", "(Ljava/lang/String;)V", false); mv.visitVarInsn(Opcodes.ASTORE, 2); mv.visitLabel(l12); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitFieldInsn(Opcodes.GETSTATIC, "com/oracle/tools/packager/windows/WinAppBundler", "VS_VERS", "[Ljava/lang/String;"); mv.visitVarInsn(Opcodes.ASTORE, 4); mv.visitVarInsn(Opcodes.ALOAD, 4); mv.visitInsn(Opcodes.ARRAYLENGTH); mv.visitVarInsn(Opcodes.ISTORE, 5); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ISTORE, 6); Label l13 = new Label(); mv.visitLabel(l13); mv.visitFrame(Opcodes.F_APPEND, 3, new Object[] { "[Ljava/lang/String;", Opcodes.INTEGER, Opcodes.INTEGER }, 0, null); mv.visitVarInsn(Opcodes.ILOAD, 6); mv.visitVarInsn(Opcodes.ILOAD, 5); Label l14 = new Label(); mv.visitJumpInsn(Opcodes.IF_ICMPGE, l14); mv.visitVarInsn(Opcodes.ALOAD, 4); mv.visitVarInsn(Opcodes.ILOAD, 6); mv.visitInsn(Opcodes.AALOAD); mv.visitVarInsn(Opcodes.ASTORE, 7); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 7); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/oracle/tools/packager/windows/WinAppBundler", "copyMSVCDLLs", "(Ljava/io/File;Ljava/lang/String;)Z", false); Label l15 = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, l15); mv.visitVarInsn(Opcodes.ALOAD, 7); mv.visitVarInsn(Opcodes.ASTORE, 3); mv.visitJumpInsn(Opcodes.GOTO, l14); mv.visitLabel(l15); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitIincInsn(6, 1); mv.visitJumpInsn(Opcodes.GOTO, l13); mv.visitLabel(l14); mv.visitFrame(Opcodes.F_CHOP, 3, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 3); Label l16 = new Label(); mv.visitJumpInsn(Opcodes.IFNONNULL, l16); mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException"); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("Not found MSVC dlls"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(l16); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitTypeInsn(Opcodes.NEW, "java/util/concurrent/atomic/AtomicReference"); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/concurrent/atomic/AtomicReference", "<init>", "()V", false); mv.visitVarInsn(Opcodes.ASTORE, 4); mv.visitVarInsn(Opcodes.ALOAD, 3); mv.visitVarInsn(Opcodes.ASTORE, 5); mv.visitVarInsn(Opcodes.ALOAD, 2); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javaioFile, "toPath", "()Ljava/nio/file/Path;", false); mv.visitLdcInsn("bin"); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/nio/file/Path", "resolve", "(Ljava/lang/String;)Ljava/nio/file/Path;", true); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/nio/file/Files", "list", "(Ljava/nio/file/Path;)Ljava/util/stream/Stream;", false); mv.visitVarInsn(Opcodes.ASTORE, 6); mv.visitInsn(Opcodes.ACONST_NULL); mv.visitVarInsn(Opcodes.ASTORE, 7); mv.visitLabel(l3); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitInvokeDynamicInsn("test", "()Ljava/util/function/Predicate;", new Handle( Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"), new Object[] { Type.getType("(Ljava/lang/Object;)Z"), new Handle(Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler", "lambda$copyMSVCDLLs$261", "(Ljava/nio/file/Path;)Z"), Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter", "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true); mv.visitVarInsn(Opcodes.ALOAD, 5); mv.visitInvokeDynamicInsn("test", "(Ljava/lang/String;)Ljava/util/function/Predicate;", new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"), new Object[] { Type.getType("(Ljava/lang/Object;)Z"), new Handle( Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler", "lambda$copyMSVCDLLs$262", "(Ljava/lang/String;Ljava/nio/file/Path;)Z"), Type.getType("(Ljava/nio/file/Path;)Z") }); // modified lambda-name mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "filter", "(Ljava/util/function/Predicate;)Ljava/util/stream/Stream;", true); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitVarInsn(Opcodes.ALOAD, 4); mv.visitInvokeDynamicInsn("accept", "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;)Ljava/util/function/Consumer;", new Handle(Opcodes.H_INVOKESTATIC, "java/lang/invoke/LambdaMetafactory", "metafactory", "(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;"), new Object[] { Type.getType("(Ljava/lang/Object;)V"), new Handle( Opcodes.H_INVOKESTATIC, "com/oracle/tools/packager/windows/WinAppBundler", "lambda$copyMSVCDLLs$263", "(Ljava/io/File;Ljava/util/concurrent/atomic/AtomicReference;Ljava/nio/file/Path;)V"), Type.getType("(Ljava/nio/file/Path;)V") }); // modified lambda-name mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "forEach", "(Ljava/util/function/Consumer;)V", true); mv.visitLabel(l4); mv.visitVarInsn(Opcodes.ALOAD, 6); Label l17 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l17); mv.visitVarInsn(Opcodes.ALOAD, 7); Label l18 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l18); mv.visitLabel(l0); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V", true); mv.visitLabel(l1); mv.visitJumpInsn(Opcodes.GOTO, l17); mv.visitLabel(l2); mv.visitFrame(Opcodes.F_FULL, 8, new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile, javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference", javalangString, "java/util/stream/Stream", javalangThrowable }, 1, new Object[] { javalangThrowable }); mv.visitVarInsn(Opcodes.ASTORE, 8); mv.visitVarInsn(Opcodes.ALOAD, 7); mv.visitVarInsn(Opcodes.ALOAD, 8); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed", "(Ljava/lang/Throwable;)V", false); mv.visitJumpInsn(Opcodes.GOTO, l17); mv.visitLabel(l18); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V", true); mv.visitJumpInsn(Opcodes.GOTO, l17); mv.visitLabel(l5); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable }); mv.visitVarInsn(Opcodes.ASTORE, 8); mv.visitVarInsn(Opcodes.ALOAD, 8); mv.visitVarInsn(Opcodes.ASTORE, 7); mv.visitVarInsn(Opcodes.ALOAD, 8); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(l6); mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { javalangThrowable }); mv.visitVarInsn(Opcodes.ASTORE, 9); mv.visitLabel(l10); mv.visitVarInsn(Opcodes.ALOAD, 6); Label l19 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l19); mv.visitVarInsn(Opcodes.ALOAD, 7); Label l20 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l20); mv.visitLabel(l7); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V", true); mv.visitLabel(l8); mv.visitJumpInsn(Opcodes.GOTO, l19); mv.visitLabel(l9); mv.visitFrame(Opcodes.F_FULL, 10, new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile, javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference", javalangString, "java/util/stream/Stream", javalangThrowable, Opcodes.TOP, javalangThrowable }, 1, new Object[] { javalangThrowable }); mv.visitVarInsn(Opcodes.ASTORE, 10); mv.visitVarInsn(Opcodes.ALOAD, 7); mv.visitVarInsn(Opcodes.ALOAD, 10); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, javalangThrowable, "addSuppressed", "(Ljava/lang/Throwable;)V", false); mv.visitJumpInsn(Opcodes.GOTO, l19); mv.visitLabel(l20); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "close", "()V", true); mv.visitLabel(l19); mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); mv.visitVarInsn(Opcodes.ALOAD, 9); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(l17); mv.visitFrame(Opcodes.F_FULL, 6, new Object[] { "com/oracle/tools/packager/windows/WinAppBundler", javaioFile, javaioFile, javalangString, "java/util/concurrent/atomic/AtomicReference", javalangString }, 0, new Object[] {}); mv.visitVarInsn(Opcodes.ALOAD, 4); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/concurrent/atomic/AtomicReference", "get", "()Ljava/lang/Object;", false); mv.visitTypeInsn(Opcodes.CHECKCAST, "java/io/IOException"); mv.visitVarInsn(Opcodes.ASTORE, 6); mv.visitVarInsn(Opcodes.ALOAD, 6); Label l21 = new Label(); mv.visitJumpInsn(Opcodes.IFNULL, l21); mv.visitVarInsn(Opcodes.ALOAD, 6); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(l21); mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/io/IOException" }, 0, null); mv.visitInsn(Opcodes.RETURN); } }; } }, ClassReader.EXPAND_FRAMES); // ClassReader.EXPAND_FRAMES required for Java 8 }
From source file:de.enough.polish.postcompile.java5.Java5ClassVisitor.java
License:Open Source License
public void visitEnd() { if (this.isEnumClass) { if (this.name_values == null) { throw new BuildException("This is not an enum class: " + this.classDesc); }//w ww.j a v a2 s. c o m // Generate new <clinit> method. int numValues = EnumManager.getInstance().getNumEnumValues(this.classDesc); Method m = Method.getMethod("void <clinit> ()"); MethodVisitor mv = super.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); GeneratorAdapter mg = new GeneratorAdapter(ACC_STATIC, m, mv); mg.push(numValues); mg.newArray(Type.INT_TYPE); if (numValues <= 3) { for (int i = 1; i < numValues; i++) { mg.dup(); mg.push(i); mg.push(i); mg.arrayStore(Type.INT_TYPE); } } else { Label labelInitializeField = new Label(); Label labelCheck = new Label(); Label labelDone = new Label(); mg.push(1); mg.storeLocal(0, Type.INT_TYPE); mg.goTo(labelCheck); mg.mark(labelInitializeField); mg.dup(); mg.loadLocal(0, Type.INT_TYPE); mg.dup(); mg.arrayStore(Type.INT_TYPE); mg.iinc(0, 1); mg.mark(labelCheck); mg.loadLocal(0, Type.INT_TYPE); mg.push(numValues); mg.ifICmp(GeneratorAdapter.LT, labelInitializeField); mg.mark(labelDone); } mg.putStatic(Type.getType(this.classDesc), this.name_values, Type.getType(int[].class)); mg.returnValue(); mg.endMethod(); } // Called super implementation of this method to really close this class. super.visitEnd(); }
From source file:de.fhkoeln.gm.cui.javahardener.CheckNullMethodVisitor.java
License:Open Source License
/** * Surrounds the original invoke call with an IFNULL check. * If the object is null a default value will be pushed to the stack. * /*ww w .ja v a2s . c om*/ * This method duplicates the current object reference with DUP before * checking it with IFNULL. The original instance will be removed with POP * before {@link #pushDefault(Type) the default value will pushed}. * * @param opcode * @param owner * @param name * @param desc */ private void invokeMethodWithoutArguments(int opcode, String owner, String name, String desc) { Label fallback = new Label(); Label behind = new Label(); // We surround the original call with an IFNULL check: super.visitInsn(Opcodes.DUP); // Duplicate stack pointer of the current object super.visitJumpInsn(Opcodes.IFNULL, fallback); // Skip method call if reference is null super.visitMethodInsn(opcode, owner, name, desc); // Original method call super.visitJumpInsn(Opcodes.GOTO, behind); // Jump over the reference is null path // But if not we need add a default value to the stack: super.visitLabel(fallback); // Reference is null path super.visitInsn(Opcodes.POP); // Pop the dup value from stack pushDefault(Type.getReturnType(desc)); super.visitLabel(behind); // Label to jump of the reference is null path }
From source file:de.fhkoeln.gm.cui.javahardener.CheckNullMethodVisitor.java
License:Open Source License
/** * Surrounds the original invoke call with an IFNULL check. * If the object is null a default value will be pushed to the stack. * // ww w. j ava 2 s . c o m * This method duplicates the current object reference with DUP2 and POP (to * remove the argument from the stack) before checking it with IFNULL. * The original instance reference and the argument will be removed with * POP2 before {@link #pushDefault(Type) the default value will pushed}. * * Notice: This works only for 32 bit arguments: Objects, integers, etc. * but doesn't work with long or double values. * * @param opcode * @param owner * @param name * @param desc */ private void invokeMethodWithOneArgument(int opcode, String owner, String name, String desc) { // Instead of the original invoke call: //super.visitMethodInsn(opcode, owner, name, desc); Label fallback = new Label(); Label behind = new Label(); // We surround the original call with an IFNULL check: super.visitInsn(Opcodes.DUP2); // Duplicate stack pointer of the current object AND the argument super.visitInsn(Opcodes.POP); // Remove the argument again super.visitJumpInsn(Opcodes.IFNULL, fallback); // Skip method call if reference is null super.visitMethodInsn(opcode, owner, name, desc); // Original method call super.visitJumpInsn(Opcodes.GOTO, behind); // Jump over the reference is null path // But if not we need add a default value to the stack: super.visitLabel(fallback); // Reference is null path super.visitInsn(Opcodes.POP2); // Pop the dup value from stack AND the argument pushDefault(Type.getReturnType(desc)); super.visitLabel(behind); // Label to jump of the reference is null path }
From source file:de.javanarior.vo.generator.ByteCodeGenerator.java
License:Apache License
/** * Generate a implementation of a value object. * * @param valueType//from w w w.j a v a2 s. c o m * - value object type * @param technicalType * - to which the value object is mapped * @param wrapperClass * - abstract wrapper class, correspond to the technical type * @return class name and byte code in a container */ /* CHECKSTYLE:OFF */ public static ByteCodeContainer generate(Class<?> valueType, Class<? extends Comparable<?>> technicalType, @SuppressWarnings("rawtypes") Class<? extends AbstractValue> wrapperClass) { /* CHECKSTYLE:ON */ if (!isInterface(valueType)) { throw new IllegalArgumentException("Could not generate implementation for class " + valueType.getName() + ". Please provide interface"); } ClassWriter classWriter = new ClassWriter(0); MethodVisitor methodVisitor; classWriter.visit(Opcodes.V1_7, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, implementationClassName(valueType), "L" + parentClassName(wrapperClass) + "<" + addTypeDiscriptor(valueType) + ">;" + addTypeDiscriptor(valueType), parentClassName(wrapperClass), implementedInterfaces(valueType)); methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR, methodDescriptor(technicalType), null, null); methodVisitor.visitCode(); Label label0 = new Label(); methodVisitor.visitLabel(label0); /* CHECKSTYLE:OFF */ methodVisitor.visitLineNumber(8, label0); /* CHECKSTYLE:ON */ methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); methodVisitor.visitVarInsn(getILOADOpCode(technicalType), 1); methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, parentClassName(wrapperClass), CONSTRUCTOR, methodDescriptor(technicalType), false); Label label1 = new Label(); methodVisitor.visitLabel(label1); /* CHECKSTYLE:OFF */ methodVisitor.visitLineNumber(9, label1); /* CHECKSTYLE:ON */ methodVisitor.visitInsn(Opcodes.RETURN); Label label2 = new Label(); methodVisitor.visitLabel(label2); methodVisitor.visitLocalVariable("this", addTypeSignature(implementationClassName(valueType)), null, label0, label2, 0); methodVisitor.visitLocalVariable("value", getType(technicalType), null, label0, label2, 1); int stackSize = getStackSize(Type.getDescriptor(technicalType)); methodVisitor.visitMaxs(stackSize, stackSize); methodVisitor.visitEnd(); classWriter.visitEnd(); return new ByteCodeContainer(binaryClassName(valueType), classWriter.toByteArray()); }
From source file:de.kandid.model.Emitter.java
License:Apache License
/** * Assembles a class that implements the given interface by generating the byte code. * @param interfaze the interface to implement * @return the class// w ww .jav a 2s.c om */ private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) { String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$'); String nameInterface = Type.getInternalName(interfaze); // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(0); cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) }); // Generate default construcotor MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); cv.visitVarInsn(ALOAD, 0); cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V"); cv.visitInsn(RETURN); cv.visitMaxs(1, 1); // Generate methods Method[] methods = interfaze.getMethods(); for (int i = 0; i < methods.length; ++i) { final Method m = methods[i]; if (m.getReturnType() != void.class) throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value"); final String descMethod = Type.getMethodDescriptor(m); final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null, null); final Type[] argTypes = Type.getArgumentTypes(m); // for (int i = 0; i < _end; i += 2) // ((Listener) _listeners[i]).send(...); int localStart = 1; // give one for "this" for (Type at : argTypes) localStart += at.getSize(); Label entry = new Label(); Label exit = new Label(); mw.visitLabel(entry); // _isFiring = true; mw.visitVarInsn(ALOAD, 0); mw.visitInsn(Opcodes.ICONST_1); mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor()); // setup local variables: i, _listeners, _end mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart); mw.visitInsn(Opcodes.ICONST_0); mw.visitIntInsn(Opcodes.ISTORE, localStart); mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray); mw.visitIntInsn(Opcodes.ASTORE, localStart + 1); mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor()); mw.visitIntInsn(Opcodes.ISTORE, localStart + 2); final Label condition = new Label(); mw.visitJumpInsn(GOTO, condition); final Label loop = new Label(); mw.visitLabel(loop); //((Listener) _listeners[i]).doSomething() mw.visitIntInsn(Opcodes.ALOAD, localStart + 1); mw.visitIntInsn(Opcodes.ILOAD, localStart); mw.visitInsn(Opcodes.AALOAD); mw.visitTypeInsn(CHECKCAST, nameInterface); int offs = 1; // give one for "this" for (Type at : argTypes) { mw.visitVarInsn(at.getOpcode(ILOAD), offs); offs += at.getSize(); } mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod); // i += 2 mw.visitIincInsn(localStart, 2); // if (i < end) goto loop mw.visitLabel(condition); mw.visitIntInsn(Opcodes.ILOAD, localStart); mw.visitIntInsn(Opcodes.ILOAD, localStart + 2); mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop); // _isFiring = false; mw.visitVarInsn(ALOAD, 0); mw.visitInsn(Opcodes.ICONST_0); mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor()); mw.visitLabel(exit); mw.visitInsn(RETURN); mw.visitMaxs(localStart + 2, localStart + 3); mw.visitEnd(); } cw.visitEnd(); return _loader.loadClass(cw, nameClass.replace('/', '.')); }
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; }/*from www .ja v a 2s. c om*/ 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 av a 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 **/ {/* www .ja va2 s. co 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();//w ww. j a v a2s . c o 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; }