List of usage examples for org.objectweb.asm Opcodes GOTO
int GOTO
To view the source code for org.objectweb.asm Opcodes GOTO.
Click Source Link
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 w ww . j a v 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.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. * /*from w w w. j ava 2 s. c o m*/ * 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. * /*from w ww.j a v a2s . com*/ * 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.scoopgmbh.copper.instrument.BuildStackInfoAdapter.java
License:Apache License
@Override public void visitJumpInsn(int arg0, Label arg1) { savePreviousFrame();//from ww w. j a va 2 s.c o m switch (arg0) { case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPNE: currentFrame.popStack(); case Opcodes.IFEQ: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFLE: case Opcodes.IFLT: case Opcodes.IFNE: case Opcodes.IFNONNULL: case Opcodes.IFNULL: currentFrame.popStack(); case Opcodes.GOTO: forwardFrames.put(arg1, new StackInfo(currentFrame)); break; case Opcodes.JSR: currentFrame.pushStack(retAddressType); forwardFrames.put(arg1, new StackInfo(currentFrame)); break; default: logger.debug("Unhandled: "); } if (logger.isDebugEnabled()) logger.debug("jumpInsn " + getOpCode(arg0) + " " + arg1); delegate.visitJumpInsn(arg0, arg1); }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInliner.java
License:Open Source License
private void removeNodes(final AbstractInsnNode currentNode, final AbstractInsnNode node1, final AbstractInsnNode node3, final AbstractInsnNode node4, final InsnList list, final Iterator<AbstractInsnNode> iterator, // final boolean eval) { list.remove(node1);/*from w w w . j a va 2 s .c om*/ if ((node3 != null) && (node3 != node1)) { list.remove(node3); } if ((node4 != null) && (node4 != node1) && (node4 != node3)) { list.remove(node4); } AbstractInsnNode label = ((JumpInsnNode) currentNode).label; list.remove(currentNode); if (!eval) { final AbstractInsnNode previousOfLabel = NodeHelper.getPrevious(label); if (previousOfLabel.getOpcode() == Opcodes.GOTO) { while (iterator.hasNext()) { final AbstractInsnNode node = iterator.next(); if (node == label) { break; } } label = ((JumpInsnNode) previousOfLabel).label; list.remove(previousOfLabel); } else { return; } } while (iterator.hasNext()) { final AbstractInsnNode node = iterator.next(); list.remove(node); if (node == label) { break; } } }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java
License:Open Source License
/** * Tests that constantIfInliner is working correctly. * //from w w w .j a va2s. c o m * Input is * * <pre> * if(1>2) * ... * else * ... * </pre> * * @throws JBOPClassException * the jBOP class exception */ @Test public void testConstantIfInlinerIF_ICMPLEWithElseChooseIf() throws JBOPClassException { // INIT final LabelNode label = new LabelNode(); final LabelNode label2 = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_2)).// addInsn(new InsnNode(Opcodes.ICONST_1)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPLE, label)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(new JumpInsnNode(Opcodes.GOTO, label2)).// addInsn(label).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(label2).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(10, method.instructions.size()); final InsnList optimized = constantIfInliner.optimize(method.instructions, method); // ASSERT assertEquals(4, optimized.size()); assertEquals(Opcodes.NOP, optimized.get(0).getOpcode()); assertEquals(Opcodes.NOP, optimized.get(1).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.controlflow.ConstantIfInlinerTest.java
License:Open Source License
/** * Tests that constantIfInliner is working correctly. * /*w w w. jav a2 s . c o m*/ * Input is * * <pre> * if(2>1) * ... * else * ... * </pre> * * @throws JBOPClassException * the jBOP class exception */ @Test public void testConstantIfInlinerIF_ICMPLEWithElseChooseElse() throws JBOPClassException { // INIT final LabelNode label = new LabelNode(); final LabelNode label2 = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_2)).// addInsn(new InsnNode(Opcodes.ICONST_1)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPLE, label)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(new JumpInsnNode(Opcodes.GOTO, label2)).// addInsn(label).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(new InsnNode(Opcodes.NOP)).// addInsn(label2).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(10, method.instructions.size()); final InsnList optimized = constantIfInliner.optimize(method.instructions, method); // ASSERT assertEquals(3, optimized.size()); assertEquals(Opcodes.NOP, optimized.get(0).getOpcode()); assertEquals(-1, optimized.get(1).getOpcode()); }
From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoopUnrollerTest.java
License:Open Source License
/** * Tests that ForLoopUnroller is working correctly. * /* w w w .ja v a2s. c o m*/ * Used is a loop of the kind: * * for(int i=0; i< 3; ++i) */ @Test public void testForLoopUnrollerStrictForward() { // INIT final LabelNode label1 = new LabelNode(); final LabelNode label2 = new LabelNode(); builder.addInsn(new InsnNode(Opcodes.ICONST_0)).// addInsn(new VarInsnNode(Opcodes.ISTORE, 1)).// addInsn(new JumpInsnNode(Opcodes.GOTO, label1)).// addInsn(label2).// addInsn(new VarInsnNode(Opcodes.ILOAD, 1)).// addInsn(new VarInsnNode(Opcodes.ILOAD, 1)).// addInsn(new InsnNode(Opcodes.IADD)).// addInsn(new IincInsnNode(1, 1)).// addInsn(label1).// addInsn(NodeHelper.getInsnNodeFor(3)).// addInsn(new VarInsnNode(Opcodes.ILOAD, 1)).// addInsn(new JumpInsnNode(Opcodes.IF_ICMPLT, label2)).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(13, method.instructions.size()); final InsnList optimized = optimizer.optimize(method.instructions, method); // ASSERT assertEquals(19, optimized.size()); int node = 0; for (int i = 0; i < 3; ++i) { assertEquals(i, NodeHelper.getNumberValue(optimized.get(node)).intValue()); node++; assertEquals(Opcodes.ISTORE, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.ILOAD, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.ILOAD, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.IADD, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.NOP, optimized.get(node).getOpcode()); // this is the SkipMarkNode node++; } }
From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoopUnrollerTest.java
License:Open Source License
/** * Tests that ForLoopUnroller is working correctly. * /*from www. j a v a 2s . co m*/ * Used is a loop of the kind: * * for(int i=6; i> 0; i=i-2) */ @Test public void testForLoopUnrollerBackward() { // INIT final LabelNode label1 = new LabelNode(); final LabelNode label2 = new LabelNode(); builder.addInsn(NodeHelper.getInsnNodeFor(6)).// addInsn(new VarInsnNode(Opcodes.ISTORE, 1)).// addInsn(new JumpInsnNode(Opcodes.GOTO, label1)).// addInsn(label2).// addInsn(new VarInsnNode(Opcodes.ILOAD, 1)).// addInsn(new VarInsnNode(Opcodes.ILOAD, 1)).// addInsn(new InsnNode(Opcodes.IADD)).// addInsn(new IincInsnNode(1, -2)).// addInsn(label1).// addInsn(new VarInsnNode(Opcodes.ILOAD, 1)).// addInsn(new JumpInsnNode(Opcodes.IFGT, label2)).// addInsn(new InsnNode(Opcodes.RETURN)); // RUN assertEquals(12, method.instructions.size()); final InsnList optimized = optimizer.optimize(method.instructions, method); // ASSERT assertEquals(19, optimized.size()); int node = 0; for (int i = 6; i > 0; i -= 2) { assertEquals(i, NodeHelper.getNumberValue(optimized.get(node)).intValue()); node++; assertEquals(Opcodes.ISTORE, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.ILOAD, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.ILOAD, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.IADD, optimized.get(node).getOpcode()); node++; assertEquals(Opcodes.NOP, optimized.get(node).getOpcode()); node++; } }
From source file:de.tuberlin.uebb.jbop.optimizer.utils.NodeHelper.java
License:Open Source License
/** * Checks if node is goto./*from ww w.j a v a 2 s . c om*/ * * @param node * the node * @return true if node1 is goto */ public static boolean isGoto(final AbstractInsnNode node) { if (node == null) { return false; } if (node instanceof JumpInsnNode) { return node.getOpcode() == Opcodes.GOTO; } return false; }