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

The following examples show how to use org.objectweb.asm.MethodVisitor#visitLocalVariable() . 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: IfTests.java    From Despector with MIT License 7 votes vote down vote up
@Test
public void testAnd() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label ret = new Label();
    Label body = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitLabel(body);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(ret);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, ret, 0);
    mv.visitLocalVariable("b", "Z", null, start, ret, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a && b) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 2
Source File: OperatorTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testMultiply() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IMUL);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n"
            + "    i = a * b\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 3
Source File: OperatorTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testIntConstant() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(I)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(BIPUSH, 65);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = 65;";
    Assert.assertEquals(good, insn);
}
 
Example 4
Source File: WhileTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testWhileInverse() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(I)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.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);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "while (i < 5) {\n"
            + "    org.spongepowered.test.decompile.WhileTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 5
Source File: OperatorTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testDiv() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(III)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitIntInsn(ILOAD, 1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitInsn(IDIV);
    mv.visitIntInsn(ISTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "I", null, start, end, 0);
    mv.visitLocalVariable("a", "I", null, start, end, 1);
    mv.visitLocalVariable("b", "I", null, start, end, 2);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "test_mth");
    String good = "fun test_mth(i: Int, a: Int, b: Int) {\n"
            + "    i = a / b\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 6
Source File: LambdaTest.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testProducer() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    if (TestHelper.IS_ECLIPSE) {
        mv.visitInvokeDynamicInsn("call", "()Ljava/util/concurrent/Callable;",
                new Handle(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;"),
                        new Handle(H_INVOKESTATIC, "org/spongepowered/test/ast/LambdaTest", "lambda$2", "()Ljava/lang/Object;"),
                        Type.getType("()Ljava/lang/Object;")});
    } else {
        // javac adds an extra decoration to the lambda method name with the
        // name of the calling method
        mv.visitInvokeDynamicInsn("call", "()Ljava/util/concurrent/Callable;",
                new Handle(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;"),
                        new Handle(H_INVOKESTATIC, "org/spongepowered/test/ast/LambdaTest", "lambda$test_producer$2", "()Ljava/lang/Object;"),
                        Type.getType("()Ljava/lang/Object;")});
    }
    mv.visitVarInsn(ASTORE, 1);
    Label l1 = new Label();
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/concurrent/Callable", "call", "()Ljava/lang/Object;", true);
    mv.visitInsn(POP);
    mv.visitInsn(RETURN);
    Label end = new Label();
    mv.visitLabel(end);
    mv.visitLocalVariable("this", "Lorg/spongepowered/test/ast/LambdaTest;", null, start, end, 0);
    mv.visitLocalVariable("r", "Ljava/util/concurrent/Callable;", "Ljava/util/concurrent/Callable<Ljava/lang/Object;>;", l1, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "java.util.concurrent.Callable<Object> r = () -> null;\n"
            + "r.call();";
    Assert.assertEquals(good, insn);
}
 
Example 7
Source File: DoWhileTests.java    From Despector with MIT License 5 votes vote down vote up
public void testDoWhileContinue() {
    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.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, l2);
    mv.visitJumpInsn(GOTO, l1);
    mv.visitLabel(l2);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    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 = "do {\n"
            + "    DoWhileTests.body();\n"
            + "    if (a) {\n"
            + "        continue;\n"
            + "    }\n"
            + "    DoWhileTests.body();\n"
            + "} while (i < 5);";
    Assert.assertEquals(good, insn);
}
 
Example 8
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testPOS() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label ret = new Label();
    Label body = new Label();
    Label l1 = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFNE, l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitLabel(l1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitJumpInsn(IFNE, body);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitLabel(body);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(ret);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, ret, 0);
    mv.visitLocalVariable("b", "Z", null, start, ret, 1);
    mv.visitLocalVariable("c", "Z", null, start, ret, 2);
    mv.visitLocalVariable("d", "Z", null, start, ret, 3);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if ((a || b) && (c || d)) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 9
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testMultipleOr() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label ret = new Label();
    Label body = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFNE, body);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFNE, body);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitLabel(body);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(ret);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, ret, 0);
    mv.visitLocalVariable("b", "Z", null, start, ret, 1);
    mv.visitLocalVariable("c", "Z", null, start, ret, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a || b || c) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 10
Source File: TryCatchTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testTryPipeCatch() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l4 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NullPointerException");
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/OutOfMemoryError");
    mv.visitLabel(l0);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l4);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NullPointerException", "printStackTrace", "()V", false);
    mv.visitLabel(l4);
    mv.visitInsn(RETURN);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitLocalVariable("e", "Ljava/lang/NullPointerException;", null, l2, l4, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "try {\n"
            + "    org.spongepowered.test.decompile.TryCatchTests.body();\n"
            + "} catch (NullPointerException | OutOfMemoryError e) {\n"
            + "    e.printStackTrace();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 11
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testSimpleIf() {
    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.visitInsn(IRETURN);
    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): Int {\n"
            + "    if (a > b) {\n"
            + "        return a\n"
            + "    }\n\n"
            + "    return b\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 12
Source File: WhileTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testForInverse() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(I)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.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 0);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitIincInsn(0, 1);
    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);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "for (i = 0; i < 5; i++) {\n"
            + "    org.spongepowered.test.decompile.WhileTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 13
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testSOP() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label ret = new Label();
    Label body = new Label();
    Label l1 = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFNE, body);
    mv.visitLabel(l1);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitVarInsn(ILOAD, 3);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitLabel(body);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(ret);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, ret, 0);
    mv.visitLocalVariable("b", "Z", null, start, ret, 1);
    mv.visitLocalVariable("c", "Z", null, start, ret, 2);
    mv.visitLocalVariable("d", "Z", null, start, ret, 3);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a && b || c && d) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\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 testIfReturns() {
    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.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitLabel(l1);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, end);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitInsn(RETURN);
    mv.visitLabel(end);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    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"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "    return;\n"
            + "}\n\n"
            + "if (b) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "    return;\n"
            + "}\n\n"
            + "org.spongepowered.test.decompile.IfTests.body();";
    Assert.assertEquals(good, insn);
}
 
Example 15
Source File: DefaultApplicationFactory.java    From thorntail with Apache License 2.0 5 votes vote down vote up
static byte[] basicClassBytes() {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(52, ACC_PUBLIC + ACC_SUPER, "org/wildfly/swarm/jaxrs/runtime/DefaultApplication", null, "javax/ws/rs/core/Application", null);

    cw.visitSource("DefaultApplication.java", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(23, l0);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "Lorg/wildfly/swarm/jaxrs/runtime/DefaultApplication;", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();

}
 
Example 16
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testOrAnd() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZZZ)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label ret = new Label();
    Label body = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFNE, body);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitVarInsn(ILOAD, 2);
    mv.visitJumpInsn(IFEQ, ret);
    mv.visitLabel(body);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(ret);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, ret, 0);
    mv.visitLocalVariable("b", "Z", null, start, ret, 1);
    mv.visitLocalVariable("c", "Z", null, start, ret, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a || b && c) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 17
Source File: BytecodeMethodEntryEmitter.java    From Despector with MIT License 4 votes vote down vote up
@Override
public boolean emit(BytecodeEmitterContext ctx, MethodEntry ast) {
    int acc = 0;
    switch (ast.getAccessModifier()) {
    case PUBLIC:
        acc |= ACC_PUBLIC;
        break;
    case PRIVATE:
        acc |= ACC_PRIVATE;
        break;
    case PROTECTED:
        acc |= ACC_PROTECTED;
        break;
    default:
    }
    if (ast.isStatic()) {
        acc |= ACC_STATIC;
    }
    if (ast.isAbstract()) {
        acc |= ACC_ABSTRACT;
    }
    if (ast.isBridge()) {
        acc |= ACC_BRIDGE;
    }
    if (ast.isDeprecated()) {
        acc |= ACC_DEPRECATED;
    }
    if (ast.isFinal()) {
        acc |= ACC_FINAL;
    }
    if (ast.isNative()) {
        acc |= ACC_NATIVE;
    }
    if (ast.isStrictFp()) {
        acc |= ACC_STRICT;
    }
    if (ast.isSynchronized()) {
        acc |= ACC_SYNCHRONIZED;
    }
    if (ast.isSynthetic()) {
        acc |= ACC_SYNTHETIC;
    }
    if (ast.isVarargs()) {
        acc |= ACC_VARARGS;
    }
    MethodVisitor mv = ctx.getClassWriter().visitMethod(acc, ast.getName(), ast.getDescription(), null, null);
    ctx.setMethodVisitor(mv);
    mv.visitCode();
    Label start = new Label();
    mv.visitLabel(start);
    ctx.resetMaxs();
    for (Statement stmt : ast.getInstructions().getStatements()) {
        ctx.emitStatement(stmt);
    }
    Label end = new Label();
    mv.visitLabel(end);

    // TODO the indices we have for locals are in terms of the ir list and
    // are therefore useless now
    int maxLocal = 0;
    for (LocalInstance local : ast.getLocals().getAllInstances()) {
        if (local.getType().getDescriptor().startsWith("L")) {
            mv.visitLocalVariable(local.getName(), local.getType().getDescriptor(), local.getType().toString(), start, end, local.getIndex());
        } else {
            mv.visitLocalVariable(local.getName(), local.getType().getDescriptor(), null, start, end, local.getIndex());
        }
        maxLocal = Math.max(maxLocal, local.getIndex() + 1);
    }
    mv.visitMaxs(ctx.getMaxs(), maxLocal);
    mv.visitEnd();
    return true;
}
 
Example 18
Source File: TryCatchTests.java    From Despector with MIT License 4 votes vote down vote up
@Test
public void testTryMultiCatch() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    Label l4 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/NullPointerException");
    mv.visitTryCatchBlock(l0, l1, l3, "java/lang/OutOfMemoryError");
    mv.visitLabel(l0);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(l1);
    mv.visitJumpInsn(GOTO, l4);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NullPointerException", "printStackTrace", "()V", false);
    mv.visitJumpInsn(GOTO, l4);
    mv.visitLabel(l3);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/OutOfMemoryError", "printStackTrace", "()V", false);
    mv.visitLabel(l4);
    mv.visitInsn(RETURN);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitLocalVariable("e", "Ljava/lang/NullPointerException;", null, l2, l3, 2);
    mv.visitLocalVariable("e", "Ljava/lang/OutOfMemoryError;", null, l3, l4, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "try {\n"
            + "    org.spongepowered.test.decompile.TryCatchTests.body();\n"
            + "} catch (NullPointerException e) {\n"
            + "    e.printStackTrace();\n"
            + "} catch (OutOfMemoryError e) {\n"
            + "    e.printStackTrace();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 19
Source File: TryCatchTests.java    From Despector with MIT License 4 votes vote down vote up
@Test
public void testTryCatchNested() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    Label l3 = new Label();
    Label l4 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
    mv.visitTryCatchBlock(l0, l3, l4, "java/lang/NullPointerException");
    mv.visitLabel(l0);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(l1);
    Label l5 = new Label();
    mv.visitJumpInsn(GOTO, l5);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 3);
    Label l6 = new Label();
    mv.visitLabel(l6);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V", false);
    mv.visitLabel(l3);
    mv.visitJumpInsn(GOTO, l5);
    mv.visitLabel(l4);
    mv.visitVarInsn(ASTORE, 3);
    Label l7 = new Label();
    mv.visitLabel(l7);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NullPointerException", "printStackTrace", "()V", false);
    mv.visitLabel(l5);
    mv.visitInsn(RETURN);
    Label l8 = new Label();
    mv.visitLabel(l8);
    mv.visitLocalVariable("e", "Ljava/lang/Exception;", null, l6, l3, 3);
    mv.visitLocalVariable("e", "Ljava/lang/NullPointerException;", null, l7, l5, 3);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "try {\n"
            + "    try {\n"
            + "        org.spongepowered.test.decompile.TryCatchTests.body();\n"
            + "    } catch (Exception e) {\n"
            + "        e.printStackTrace();\n"
            + "    }\n"
            + "} catch (NullPointerException e) {\n"
            + "    e.printStackTrace();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 20
Source File: LocalVariableNode.java    From JReFrameworker with MIT License 2 votes vote down vote up
/**
 * Makes the given visitor visit this local variable declaration.
 *
 * @param methodVisitor a method visitor.
 */
public void accept(final MethodVisitor methodVisitor) {
  methodVisitor.visitLocalVariable(
      name, desc, signature, start.getLabel(), end.getLabel(), index);
}