Java Code Examples for org.objectweb.asm.MethodVisitor#visitJumpInsn()

The following examples show how to use org.objectweb.asm.MethodVisitor#visitJumpInsn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeavingClassVisitor.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private static void addLoadClassConditional(MethodVisitor mv, Object[] locals) {
    Label label0 = new Label();
    Label label1 = new Label();
    Label label2 = new Label();
    mv.visitTryCatchBlock(label0, label1, label2, "java/lang/ClassNotFoundException");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitLdcInsn("org.glowroot.agent");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z",
            false);
    Label label3 = new Label();
    mv.visitJumpInsn(IFEQ, label3);
    mv.visitLabel(label0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(ACONST_NULL);
    mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName",
            "(Ljava/lang/String;ZLjava/lang/ClassLoader;)Ljava/lang/Class;", false);
    mv.visitLabel(label1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(label2);
    mv.visitFrame(F_NEW, locals.length, locals, 1,
            new Object[] {"java/lang/ClassNotFoundException"});
    mv.visitInsn(POP);
    mv.visitLabel(label3);
    mv.visitFrame(F_NEW, locals.length, locals, 0, new Object[0]);
}
 
Example 2
Source File: TernaryTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testSimpleTernary() {
    TestMethodBuilder builder = new TestMethodBuilder("maxOf", "(II)I");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    Label l1 = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IF_ICMPLE, l1);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitLabel(end);
    mv.visitInsn(IRETURN);
    mv.visitLocalVariable("a", "I", null, start, end, 0);
    mv.visitLocalVariable("b", "I", null, start, end, 1);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "maxOf");
    String good = "fun maxOf(a: Int, b: Int) = if (a > b) a else b";
    Assert.assertEquals(good, insn);
}
 
Example 3
Source File: EqualsMethod.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
    methodVisitor.visitJumpInsn(Opcodes.GOTO, endOfBlock);
    methodVisitor.visitLabel(secondValueNull);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME1, EMPTY.length, EMPTY, REFERENCE.length, REFERENCE);
    }
    methodVisitor.visitJumpInsn(Opcodes.IFNULL, endOfBlock);
    methodVisitor.visitLabel(firstValueNull);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
    }
    methodVisitor.visitInsn(Opcodes.ICONST_0);
    methodVisitor.visitInsn(Opcodes.IRETURN);
    methodVisitor.visitLabel(endOfBlock);
    if (implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V6)) {
        methodVisitor.visitFrame(Opcodes.F_SAME, EMPTY.length, EMPTY, EMPTY.length, EMPTY);
    }
    return new Size(0, 0);
}
 
Example 4
Source File: TernaryTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testSimple() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZI)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitIntInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitIntInsn(BIPUSH, 6);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_3);
    mv.visitLabel(l2);
    mv.visitIntInsn(ISTORE, 1);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("i", "I", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = a ? 6 : 3;";
    Assert.assertEquals(good, insn);
}
 
Example 5
Source File: TernaryTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testReturned() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZI)I");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    mv.visitIntInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitIntInsn(BIPUSH, 6);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(l1);
    mv.visitInsn(ICONST_3);
    mv.visitLabel(end);
    mv.visitInsn(IRETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("i", "I", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "return a ? 6 : 3;";
    Assert.assertEquals(good, insn);
}
 
Example 6
Source File: DoWhileTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testDoWhileOr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFNE, l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFNE, l1);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("b", "Z", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "do {\n"
            + "    org.spongepowered.test.decompile.DoWhileTests.body();\n"
            + "} while (a || b);";
    Assert.assertEquals(good, insn);
}
 
Example 7
Source File: IterateSequence.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void generate(CodeEmitter code) {
    Label done = new Label();
    Label next = new Label();
    MethodVisitor mv = code.getMethodVisitor();
    BytecodeExpression tgt = code.evaluateOnce(target);
    code.exec(tgt);
    code.emitInstanceCheck(tgt.getType(), Iterable.class, done);
    AssignableValue item = this.item == null ? code.allocate(valueType) : this.item;
    AssignableValue iterator = code.allocate(Iterator.class);
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterable.class), "iterator", Type.getMethodDescriptor(Type.getType(Iterator.class)), true);
    code.exec(iterator.write(code.adapt(Iterator.class)));
    mv.visitLabel(next);
    code.exec(iterator.read());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterator.class), "hasNext", Type.getMethodDescriptor(Type.BOOLEAN_TYPE), true);
    mv.visitJumpInsn(Opcodes.IFEQ, done);
    code.exec(iterator.read());
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Iterator.class), "next", Type.getMethodDescriptor(Type.getType(Object.class)), true);
    code.cast(valueType, AnyTypeWidget.getInstance()); // , next);   // don't skip nulls
    code.exec(item.write(item.getType()));
    loop.item(code, item.read(), done, next);
    mv.visitJumpInsn(Opcodes.GOTO, next);
    mv.visitLabel(done);
}
 
Example 8
Source File: ExpressionHandler.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
@Override
public BytecodeExpression guarded(final BytecodeExpression target, final BytecodeExpression ifTargetIsNotNull, final BytecodeExpression ifTargetIsNull) {
    if (!target.getType().isNullable()) {
        return ifTargetIsNotNull;
    }
    return new BaseTypeExpression(unify(ifTargetIsNotNull.getType(), ifTargetIsNull.getType())) {
        @Override
        public void generate(CodeEmitter code) {
            final MethodVisitor mv = code.getMethodVisitor();
            Label isNull = new Label();
            Label done = new Label();
            code.exec(target);
            code.nullTest(target.getType(), isNull);
            code.pop(target.getType());
            code.exec(ifTargetIsNotNull);
            code.cast(getType(), ifTargetIsNotNull.getType(), isNull);
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isNull);
            code.exec(ifTargetIsNull);
            code.cast(getType(), ifTargetIsNull.getType());
            mv.visitLabel(done);
        }
    };
}
 
Example 9
Source File: WhileTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testWhileBreakInverse() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(IZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, l2);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(l2);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitInsn(ICONST_5);
    mv.visitJumpInsn(IF_ICMPLT, l1);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "Z", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "while (i < 5) {\n"
            + "    org.spongepowered.test.decompile.WhileTests.body();\n"
            + "    if (a) {\n"
            + "        break;\n"
            + "    }\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 10
Source File: HashCodeMethod.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Context implementationContext) {
    methodVisitor.visitVarInsn(Opcodes.ASTORE, instrumentedMethod.getStackSize());
    methodVisitor.visitVarInsn(Opcodes.ALOAD, instrumentedMethod.getStackSize());
    methodVisitor.visitJumpInsn(Opcodes.IFNULL, label);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, instrumentedMethod.getStackSize());
    return new Size(0, 0);
}
 
Example 11
Source File: DgmConverter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void createIsValidMethodMethod(CachedMethod method, ClassWriter cw, String className) {
    MethodVisitor mv;
    if (method.getParamsCount() == 2 && method.getParameterTypes()[0].isNumber && method.getParameterTypes()[1].isNumber) {
        // 1 param meta method
        mv = cw.visitMethod(ACC_PUBLIC, "isValidMethod", "([Ljava/lang/Class;)Z", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNULL, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKEVIRTUAL, className, "getParameterTypes", "()[Lorg/codehaus/groovy/reflection/CachedClass;", false);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ICONST_0);
        mv.visitInsn(AALOAD);
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/CachedClass", "isAssignableFrom", "(Ljava/lang/Class;)Z", false);
        Label l1 = new Label();
        mv.visitJumpInsn(IFEQ, l1);
        mv.visitLabel(l0);
        mv.visitInsn(ICONST_1);
        Label l2 = new Label();
        mv.visitJumpInsn(GOTO, l2);
        mv.visitLabel(l1);
        mv.visitInsn(ICONST_0);
        mv.visitLabel(l2);
        mv.visitInsn(IRETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}
 
Example 12
Source File: IfBS.java    From turin-programming-language with Apache License 2.0 5 votes vote down vote up
@Override
public void operate(MethodVisitor mv) {
    pushIfCondition.operate(mv);
    Label end = new Label();

    // if equal to zero (if false) jump away
    Label ifFailed = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, ifFailed);

    // if branch
    ifBody.operate(mv);
    mv.visitJumpInsn(Opcodes.GOTO, end);

    // then branch
    mv.visitLabel(ifFailed);

    for (int i=0; i<elifConditions.size();i++){
        Label thisElifSkipped = new Label();
        elifConditions.get(i).operate(mv);
        mv.visitJumpInsn(Opcodes.IFEQ, thisElifSkipped);

        // enter the elig
        elifBodys.get(i).operate(mv);
        mv.visitJumpInsn(Opcodes.GOTO, end);

        mv.visitLabel(thisElifSkipped);
    }

    // else branch
    if (elseBody != null) {
        elseBody.operate(mv);
    }

    // enf of the statement
    mv.visitLabel(end);
}
 
Example 13
Source File: WhileTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testWhileBreak() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(IZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitInsn(ICONST_5);
    mv.visitJumpInsn(IF_ICMPGE, end);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, l2);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(l2);
    mv.visitJumpInsn(GOTO, l1);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "Z", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "while (i < 5) {\n"
            + "    org.spongepowered.test.decompile.WhileTests.body();\n"
            + "    if (a) {\n"
            + "        break;\n"
            + "    }\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 14
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testNestedIf2Optimized() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label else_ = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, else_);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, end);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(else_);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("b", "Z", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a) {\n"
            + "    if (b) {\n"
            + "        org.spongepowered.test.decompile.IfTests.body();\n"
            + "    }\n"
            + "} else {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 15
Source File: WhileTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testWhileContinueInverse() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(IZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l1);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, l2);
    mv.visitJumpInsn(GOTO, l3);
    mv.visitLabel(l2);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(l3);
    mv.visitVarInsn(ILOAD, 0);
    mv.visitInsn(ICONST_5);
    mv.visitJumpInsn(IF_ICMPLT, l1);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "Z", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "while (i < 5) {\n"
            + "    org.spongepowered.test.decompile.WhileTests.body();\n"
            + "    if (a) {\n"
            + "        continue;\n"
            + "    }\n\n"
            + "    org.spongepowered.test.decompile.WhileTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 16
Source File: BooleanExpressionTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(final GroovyCodeVisitor visitor) {
    if (visitor instanceof AsmClassGenerator) {
        AsmClassGenerator acg = (AsmClassGenerator) visitor;
        WriterController controller = acg.getController();
        OperandStack os = controller.getOperandStack();

        if (type.equals(ClassHelper.boolean_TYPE)) {
            expression.visit(visitor);
            os.doGroovyCast(ClassHelper.boolean_TYPE);
            return;
        }
        if (type.equals(ClassHelper.Boolean_TYPE)) {
            MethodVisitor mv = controller.getMethodVisitor();
            expression.visit(visitor);
            Label unbox = new Label();
            Label exit = new Label();
            // check for null
            mv.visitInsn(DUP);
            mv.visitJumpInsn(IFNONNULL, unbox);
            mv.visitInsn(POP);
            mv.visitInsn(ICONST_0);
            mv.visitJumpInsn(GOTO, exit);
            mv.visitLabel(unbox);
            // unbox
            // GROOVY-6270
            if (!os.getTopOperand().equals(type)) BytecodeHelper.doCast(mv, type);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
            mv.visitLabel(exit);
            os.replace(ClassHelper.boolean_TYPE);
            return;
        }
        ClassNode top = type;
        if (ClassHelper.isPrimitiveType(top)) {
            expression.visit(visitor);
            // in case of null safe invocation, it is possible that what was supposed to be a primitive type
            // becomes the "null" constant, so we need to recheck
            top = controller.getOperandStack().getTopOperand();
            if (ClassHelper.isPrimitiveType(top)) {
                BytecodeHelper.convertPrimitiveToBoolean(controller.getMethodVisitor(), top);
                controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
                return;
            }
        }
        List<MethodNode> asBoolean = findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), top, "asBoolean", ClassNode.EMPTY_ARRAY);
        if (asBoolean.size() == 1) {
            MethodNode node = asBoolean.get(0);
            if (node instanceof ExtensionMethodNode) {
                MethodNode dgmNode = ((ExtensionMethodNode) node).getExtensionMethodNode();
                ClassNode owner = dgmNode.getParameters()[0].getType();
                if (ClassHelper.OBJECT_TYPE.equals(owner)) {
                    // we may inline a var!=null check instead of calling a helper method iff
                    // (1) the class doesn't define an asBoolean method (already tested)
                    // (2) no subclass defines an asBoolean method
                    // For (2), we check that we are in one of those cases
                    // (a) a final class
                    // (b) a private inner class without subclass
                    if (Modifier.isFinal(top.getModifiers())
                            || (top instanceof InnerClassNode
                            && Modifier.isPrivate(top.getModifiers())
                            && !isExtended(top, top.getOuterClass().getInnerClasses()))
                            ) {
                        CompareToNullExpression expr = new CompareToNullExpression(
                                expression, false
                        );
                        expr.visit(acg);
                        return;
                    }
                }
            }
        }
        super.visit(visitor);
    } else {
        super.visit(visitor);
    }
}
 
Example 17
Source File: ProxyGeneratorAdapter.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected MethodVisitor makeDelegateToClosureCall(final String name, final String desc, final String signature, final String[] exceptions, final int accessFlags) {
        MethodVisitor mv = super.visitMethod(accessFlags, name, desc, signature, exceptions);
//        TraceMethodVisitor tmv = new TraceMethodVisitor(mv);
//        mv = tmv;
        mv.visitCode();
        int stackSize = 0;
        // method body should be:
        //  this.$delegate$closure$methodName.call(new Object[] { method arguments })
        Type[] args = Type.getArgumentTypes(desc);
        int arrayStore = args.length + 1;
        BytecodeHelper.pushConstant(mv, args.length);
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object"); // stack size = 1
        stackSize = 1;
        int idx = 1;
        for (int i = 0; i < args.length; i++) {
            Type arg = args[i];
            mv.visitInsn(DUP); // stack size = 2
            BytecodeHelper.pushConstant(mv, i); // array index, stack size = 3
            // primitive types must be boxed
            boxPrimitiveType(mv, idx, arg);
            idx += registerLen(arg);
            stackSize = Math.max(4, 3 + registerLen(arg));
            mv.visitInsn(AASTORE); // store value into array
        }
        mv.visitVarInsn(ASTORE, arrayStore); // store array
        int arrayIndex = arrayStore;
        mv.visitVarInsn(ALOAD, 0); // load this
        mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map
        mv.visitLdcInsn(name); // load method name
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        arrayStore++;
        mv.visitVarInsn(ASTORE, arrayStore);
        // if null, test if wildcard exists
        Label notNull = new Label();
        mv.visitIntInsn(ALOAD, arrayStore);
        mv.visitJumpInsn(IFNONNULL, notNull);
        mv.visitVarInsn(ALOAD, 0); // load this
        mv.visitFieldInsn(GETFIELD, proxyName, CLOSURES_MAP_FIELD, "Ljava/util/Map;"); // load closure map
        mv.visitLdcInsn("*"); // load wildcard
        mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
        mv.visitVarInsn(ASTORE, arrayStore);
        mv.visitLabel(notNull);
        mv.visitVarInsn(ALOAD, arrayStore);
        mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(this.getClass()), "ensureClosure", "(Ljava/lang/Object;)Lgroovy/lang/Closure;", false);
        mv.visitVarInsn(ALOAD, arrayIndex); // load argument array
        stackSize++;
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Closure", "call", "([Ljava/lang/Object;)Ljava/lang/Object;", false); // call closure
        unwrapResult(mv, desc);
        mv.visitMaxs(stackSize, arrayStore + 1);
        mv.visitEnd();
//        System.out.println("tmv.getText() = " + tmv.getText());
        return null;
    }
 
Example 18
Source File: FMLPatcher.java    From Launcher with GNU General Public License v3.0 4 votes vote down vote up
private static byte[] gen(final String name, final String exName) { // "cpw/mods/fml/SafeExitJVMLegacy", "exit"

        final ClassWriter classWriter = new ClassWriter(0);
        MethodVisitor methodVisitor;

        classWriter.visit(V1_8, ACC_PUBLIC | ACC_SUPER, name, null, "java/lang/Object", null);

        {
            methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            methodVisitor.visitCode();
            methodVisitor.visitVarInsn(ALOAD, 0);
            methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            methodVisitor.visitInsn(RETURN);
            methodVisitor.visitMaxs(1, 1);
            methodVisitor.visitEnd();
        }
        {
            methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, exName, "(I)V", null, null);
            methodVisitor.visitCode();
            final Label label0 = new Label();
            final Label label1 = new Label();
            final Label label2 = new Label();
            methodVisitor.visitTryCatchBlock(label0, label1, label2, "java/lang/Throwable");
            methodVisitor.visitLabel(label0);
            methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Runtime", "getRuntime", "()Ljava/lang/Runtime;",
                    false);
            methodVisitor.visitVarInsn(ILOAD, 0);
            methodVisitor.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Runtime", "halt", "(I)V", false);
            methodVisitor.visitLabel(label1);
            final Label label3 = new Label();
            methodVisitor.visitJumpInsn(GOTO, label3);
            methodVisitor.visitLabel(label2);
            methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{"java/lang/Throwable"});
            methodVisitor.visitVarInsn(ASTORE, 1);
            methodVisitor.visitVarInsn(ILOAD, 0);
            methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/System", "exit", "(I)V", false);
            methodVisitor.visitLabel(label3);
            methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            methodVisitor.visitInsn(RETURN);
            methodVisitor.visitMaxs(2, 2);
            methodVisitor.visitEnd();
        }
        classWriter.visitEnd();
        return classWriter.toByteArray();
    }
 
Example 19
Source File: StatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected void writeForLoopWithClosureList(final ForStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitForLoop");
    writeStatementLabel(statement);

    MethodVisitor mv = controller.getMethodVisitor();
    controller.getCompileStack().pushLoop(statement.getVariableScope(), statement.getStatementLabels());

    ClosureListExpression clExpr = (ClosureListExpression) statement.getCollectionExpression();
    controller.getCompileStack().pushVariableScope(clExpr.getVariableScope());

    List<Expression> expressions = clExpr.getExpressions();
    int size = expressions.size();

    // middle element is condition, lower half is init, higher half is increment
    int condIndex = (size - 1) / 2;

    // visit init
    for (int i = 0; i < condIndex; i += 1) {
        visitExpressionOfLoopStatement(expressions.get(i));
    }

    Label continueLabel = controller.getCompileStack().getContinueLabel();
    Label breakLabel = controller.getCompileStack().getBreakLabel();

    Label cond = new Label();
    mv.visitLabel(cond);
    // visit condition leave boolean on stack
    {
        int mark = controller.getOperandStack().getStackLength();
        Expression condExpr = expressions.get(condIndex);
        condExpr.visit(controller.getAcg());
        controller.getOperandStack().castToBool(mark, true);
    }
    // jump if we don't want to continue
    // note: ifeq tests for ==0, a boolean is 0 if it is false
    controller.getOperandStack().jump(IFEQ, breakLabel);

    // Generate the loop body
    statement.getLoopBlock().visit(controller.getAcg());

    // visit increment
    mv.visitLabel(continueLabel);
    // fix for being on the wrong line when debugging for loop
    controller.getAcg().onLineNumber(statement, "increment condition");
    for (int i = condIndex + 1; i < size; i += 1) {
        visitExpressionOfLoopStatement(expressions.get(i));
    }

    // jump to test the condition again
    mv.visitJumpInsn(GOTO, cond);

    // loop end
    mv.visitLabel(breakLabel);

    controller.getCompileStack().pop();
    controller.getCompileStack().pop();
}
 
Example 20
Source File: StaticTypesStatementWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void writeOptimizedForEachLoop(
        CompileStack compileStack,
        OperandStack operandStack,
        MethodVisitor mv,
        ForStatement loop,
        Expression collectionExpression,
        ClassNode collectionType,
        Parameter loopVariable) {
    BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);

    Label continueLabel = compileStack.getContinueLabel();
    Label breakLabel = compileStack.getBreakLabel();

    AsmClassGenerator acg = controller.getAcg();

    // load array on stack
    collectionExpression.visit(acg);
    mv.visitInsn(DUP);
    int array = compileStack.defineTemporaryVariable("$arr", collectionType, true);
    mv.visitJumpInsn(IFNULL, breakLabel);

    // $len = array.length
    mv.visitVarInsn(ALOAD, array);
    mv.visitInsn(ARRAYLENGTH);
    operandStack.push(ClassHelper.int_TYPE);
    int arrayLen = compileStack.defineTemporaryVariable("$len", ClassHelper.int_TYPE, true);

    // $idx = 0
    mv.visitInsn(ICONST_0);
    operandStack.push(ClassHelper.int_TYPE);
    int loopIdx = compileStack.defineTemporaryVariable("$idx", ClassHelper.int_TYPE, true);

    mv.visitLabel(continueLabel);
    // $idx<$len?
    mv.visitVarInsn(ILOAD, loopIdx);
    mv.visitVarInsn(ILOAD, arrayLen);
    mv.visitJumpInsn(IF_ICMPGE, breakLabel);

    // get array element
    loadFromArray(mv, variable, array, loopIdx);

    // $idx++
    mv.visitIincInsn(loopIdx, 1);

    // loop body
    loop.getLoopBlock().visit(acg);

    mv.visitJumpInsn(GOTO, continueLabel);

    mv.visitLabel(breakLabel);

    compileStack.removeVar(loopIdx);
    compileStack.removeVar(arrayLen);
    compileStack.removeVar(array);
}