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

The following examples show how to use org.objectweb.asm.MethodVisitor#visitLabel() . 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: UserlibCollisionTest.java    From AVM with MIT License 6 votes vote down vote up
private static byte[] getAvmPackageClassBytes() {

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

        classWriter.visit(V10, ACC_PUBLIC | ACC_SUPER, "avm/Main", null, "java/lang/Object", null);

        classWriter.visitSource("Main.java", null);
        {
            methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
            methodVisitor.visitCode();
            Label label0 = new Label();
            methodVisitor.visitLabel(label0);
            methodVisitor.visitLineNumber(3, label0);
            methodVisitor.visitVarInsn(ALOAD, 0);
            methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            methodVisitor.visitInsn(RETURN);
            methodVisitor.visitMaxs(1, 1);
            methodVisitor.visitEnd();
        }
        classWriter.visitEnd();

        return classWriter.toByteArray();
    }
 
Example 2
Source File: BooleanCompareExpression.java    From yql-plus with Apache License 2.0 6 votes vote down vote up
@Override
public void generate(CodeEmitter code) {
    code.exec(new CompareExpression(loc, leftExpr, rightExpr));
    MethodVisitor mv = code.getMethodVisitor();
    Label isTrue = new Label();
    Label done = new Label();
    switch (booleanComparison) {
        case LT:
            mv.visitJumpInsn(Opcodes.IFLT, isTrue);
            break;
        case LTEQ:
            mv.visitJumpInsn(Opcodes.IFLE, isTrue);
            break;
        case GT:
            mv.visitJumpInsn(Opcodes.IFGT, isTrue);
            break;
        case GTEQ:
            mv.visitJumpInsn(Opcodes.IFGE, isTrue);
            break;
    }
    code.emitBooleanConstant(false);
    mv.visitJumpInsn(Opcodes.GOTO, done);
    mv.visitLabel(isTrue);
    code.emitBooleanConstant(true);
    mv.visitLabel(done);
}
 
Example 3
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 4
Source File: Globalizer.java    From Concurnas with MIT License 6 votes vote down vote up
private static void addstaticFinalVars(MethodVisitor mv, ArrayList<FieldVisitorHolder> staticFinalVars, String putTo) {
	for(FieldVisitorHolder sfv : staticFinalVars) {
		mv.visitLabel(new Label());
		mv.visitVarInsn(ALOAD, 0);
		
		switch(sfv.desc) {
			case "Ljava/lang/String;": mv.visitLdcInsn(sfv.value.toString()); break;
			case "J": BytecodeGenUtils.longOpcode(mv, ((Long)sfv.value).longValue() ); break;
			case "F": BytecodeGenUtils.floatOpcode(mv, ((Float)sfv.value).floatValue() ); break;
			case "D": BytecodeGenUtils.doubleOpcode(mv, ((Double)sfv.value).doubleValue() ); break;
			default: BytecodeGenUtils.intOpcode(mv, ((Integer)sfv.value).intValue() );
		}	
		
		mv.visitFieldInsn(PUTFIELD, putTo, sfv.name, sfv.desc);
	}
}
 
Example 5
Source File: OperatorTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testTypeConstant() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(I)V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitLdcInsn(Type.getType("Ljava/lang/String;"));
    mv.visitIntInsn(ASTORE, 0);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("i", "Ljava/lang/Class;", null, start, end, 0);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = String.class;";
    Assert.assertEquals(good, insn);
}
 
Example 6
Source File: AdviceGenerator.java    From glowroot with Apache License 2.0 6 votes vote down vote up
private void addOnThrowMethod(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "onThrow",
            "(Ljava/lang/Throwable;Lorg/glowroot/agent/plugin/api/TraceEntry;)V", null, null);
    visitAnnotation(mv, "Lorg/glowroot/agent/plugin/api/weaving/OnThrow;");
    checkNotNull(mv.visitParameterAnnotation(0,
            "Lorg/glowroot/agent/plugin/api/weaving/BindThrowable;", true)).visitEnd();
    checkNotNull(mv.visitParameterAnnotation(1,
            "Lorg/glowroot/agent/plugin/api/weaving/BindTraveler;", true)).visitEnd();
    mv.visitCode();
    if (!config.traceEntryEnabledProperty().isEmpty()) {
        mv.visitVarInsn(ALOAD, 1);
        Label l0 = new Label();
        mv.visitJumpInsn(IFNONNULL, l0);
        mv.visitInsn(RETURN);
        mv.visitLabel(l0);
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEINTERFACE, "org/glowroot/agent/plugin/api/TraceEntry",
            "endWithError", "(Ljava/lang/Throwable;)V", true);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example 7
Source File: IfTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testElse() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(Z)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.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);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "} else {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 8
Source File: BasicTests.java    From Despector with MIT License 6 votes vote down vote up
@Test
public void testNoVoidReturnDeclared() {
    TestMethodBuilder builder = new TestMethodBuilder("main", "()V");
    MethodVisitor mv = builder.getGenerator();
    Label start = new Label();
    mv.visitLabel(start);
    Label end = new Label();
    mv.visitLdcInsn("Hello");
    mv.visitVarInsn(ASTORE, 1);
    mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", true);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);

    String insn = KotlinTestHelper.getMethodAsString(builder.finish(), "main");
    String good = "fun main() {\n"
            + "    val param1: String = \"Hello\"\n"
            + "    println(param1)\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 9
Source File: TernaryTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testNested2() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZIZ)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.visitIntInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitInsn(ICONST_3);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l1);
    mv.visitIntInsn(ILOAD, 2);
    mv.visitJumpInsn(IFEQ, l3);
    mv.visitInsn(ICONST_4);
    mv.visitJumpInsn(GOTO, l2);
    mv.visitLabel(l3);
    mv.visitInsn(ICONST_5);
    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);
    mv.visitLocalVariable("b", "Z", null, start, end, 2);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "i = a ? 3 : b ? 4 : 5;";
    Assert.assertEquals(good, insn);
}
 
Example 10
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testElif() {
    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();
    Label else2 = new Label();
    mv.visitVarInsn(ILOAD, 0);
    mv.visitJumpInsn(IFEQ, else_);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(else_);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitJumpInsn(IFEQ, else2);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitJumpInsn(GOTO, end);
    mv.visitLabel(else2);
    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"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "} else if (b) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "} else {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "}";
    Assert.assertEquals(good, insn);
}
 
Example 11
Source File: LineNumbersTest.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void generate(TraceClassVisitor cw) {
	MethodVisitor mv;

	cw.visit(V1_1, ACC_PUBLIC + ACC_SUPER, "soot/asm/backend/targets/LineNumbers",
			null, "java/lang/Object", null);
	cw.visitSource("LineNumbers.java", null);
	
	{
	mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
	mv.visitCode();
	Label l1 = new Label();
	mv.visitLabel(l1);
	mv.visitLineNumber(3, l1);
	mv.visitVarInsn(ALOAD, 0);
	Label l2 = new Label();
	mv.visitLabel(l2);
	mv.visitLineNumber(3, l2);
	mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
	Label l3 = new Label();
	mv.visitLabel(l3);
	mv.visitLineNumber(3, l3);
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
	mv.visitCode();
	Label l0 = new Label();
	mv.visitLabel(l0);
	mv.visitLineNumber(6, l0);
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	cw.visitEnd();

}
 
Example 12
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testNestedInstanceOf() {
    TestMethodBuilder builder = new TestMethodBuilder("test_mth", "(ZLjava/lang/Object;)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, end);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitVarInsn(ILOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, THIS_TYPE.getInternalName());
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitMethodInsn(INVOKESTATIC, THIS_TYPE.getInternalName(), "body", "()V", false);
    mv.visitLabel(l1);
    mv.visitLabel(end);
    mv.visitInsn(RETURN);
    mv.visitLocalVariable("a", "Z", null, start, end, 0);
    mv.visitLocalVariable("b", "Ljava/lang/Object;", null, start, end, 1);

    String insn = TestHelper.getAsString(builder.finish(), "test_mth");
    String good = "if (a) {\n"
            + "    org.spongepowered.test.decompile.IfTests.body();\n"
            + "    if (b instanceof org.spongepowered.test.decompile.IfTests) {\n"
            + "        org.spongepowered.test.decompile.IfTests.body();\n"
            + "    }\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 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 14
Source File: LoopingExceptionStrippingVisitorTest.java    From AVM with MIT License 5 votes vote down vote up
@Test
public void testBackwardFinally() throws Exception {
    String testClassName = "TestClass";
    ClassWriter writer = new ClassWriter(0);
    TryCatchCountingVisitor counter = new TryCatchCountingVisitor(writer);
    LoopingExceptionStrippingVisitor visitor = new LoopingExceptionStrippingVisitor();
    visitor.setDelegate(counter);
    visitor.visit(Opcodes.V10, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, testClassName, null, "java/lang/Object", null);
    
    // Create our labels.
    Label methodStart = new Label();
    Label start = new Label();
    Label end = new Label();
    Label handler = new Label();
    String type = null;
    
    // Write a target method.
    MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_STATIC, "targetMethod", "()V", null, null);
    methodVisitor.visitCode();
    methodVisitor.visitJumpInsn(Opcodes.GOTO, methodStart);
    methodVisitor.visitLabel(handler);
    methodVisitor.visitInsn(Opcodes.POP);
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitLabel(methodStart);
    methodVisitor.visitInsn(Opcodes.ACONST_NULL);
    methodVisitor.visitInsn(Opcodes.POP);
    methodVisitor.visitLabel(start);
    methodVisitor.visitInsn(Opcodes.ACONST_NULL);
    methodVisitor.visitInsn(Opcodes.POP);
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitLabel(end);
    methodVisitor.visitTryCatchBlock(start, end, handler, type);
    methodVisitor.visitMaxs(1, 0);
    methodVisitor.visitEnd();
    
    // Finish.
    visitor.visitEnd();
    
    Assert.assertEquals(0, counter.counter);
}
 
Example 15
Source File: IfTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testMultipleAnd() {
    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(IFEQ, ret);
    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 16
Source File: WhileTests.java    From Despector with MIT License 5 votes vote down vote up
@Test
public void testWhileDirectBreak() {
    // if the break is the only thing in a condition it will sometimes be
    // optimized so that the inverse of the condition targets the outside of
    // the loop rather than having a goto inside the condition
    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();
    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(IFNE, end);
    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 17
Source File: CompareExpression.java    From yql-plus with Apache License 2.0 4 votes vote down vote up
@Override
public void generate(CodeEmitter code) {
    // a bit of a hack; should not need to go to dynamic invocation for this unless one arg is ANY
    Label done = new Label();
    MethodVisitor mv = code.getMethodVisitor();
    Label leftNull = new Label();
    Label rightNull = new Label();
    Label bothNull = new Label();
    CodeEmitter.Unification unified = code.unifiedEmit(leftExpr, rightExpr, leftNull, rightNull, bothNull);
    if (unified.type.isPrimitive()) {
        emitPrimitiveCompare(code, unified.type);
    } else {
        // TODO: statically determine if the unified type is Comparable -- for now treat them all like "any"
        CodeEmitter scope = code.createScope();
        MethodVisitor mv2 = scope.getMethodVisitor();
        AssignableValue right = scope.allocate(unified.type);
        AssignableValue left = scope.allocate(unified.type);
        scope.exec(right.write(unified.type));
        scope.exec(left.write(unified.type));
        scope.exec(left.read());
        Label leftIsNotComparable = new Label();
        scope.emitInstanceCheck(unified.type, Comparable.class, leftIsNotComparable);
        scope.exec(right.read());
        mv2.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Comparable.class), "compareTo",
                Type.getMethodDescriptor(Type.INT_TYPE, Type.getType(Object.class)), true);
        scope.gotoExitScope();
        mv2.visitLabel(leftIsNotComparable);
        scope.exec(scope.getLocal("$program").read());
        scope.exec(left.read());
        scope.emitIntConstant((loc != null) ? loc.getLineNumber() : -1);
        scope.emitIntConstant((loc != null) ? loc.getCharacterOffset() : 0);
        mv2.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(ProgramInvocation.class), "notComparable", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class), Type.INT_TYPE, Type.INT_TYPE), false);
        // this bit is not reachable, notComparable throws
        mv2.visitInsn(Opcodes.ICONST_0);
        mv2.visitJumpInsn(Opcodes.GOTO, done);
        scope.endScope();
    }
    if (unified.nullPossible) {
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(leftNull);
        mv.visitInsn(Opcodes.ICONST_M1);
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(rightNull);
        mv.visitInsn(Opcodes.ICONST_1);
        mv.visitJumpInsn(Opcodes.GOTO, done);
        mv.visitLabel(bothNull);
        mv.visitInsn(Opcodes.ICONST_0);
    }
    mv.visitLabel(done);
}
 
Example 18
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 19
Source File: PlatformProviderGenerator.java    From flogger with Apache License 2.0 4 votes vote down vote up
private static void tryBlockForPlatform(MethodVisitor methodVisitor, String platformType) {
  methodVisitor.visitCode();

  // Generate the enveloping try/catch block:
  //
  //   try {
  //     ...
  //   } catch (NoClassDefFoundError | IllegalAccessException | InstantiationException
  //       | InvocationTargetException | NoSuchMethodException e) {
  //     ...
  //   }
  //
  // Note that the exception types need to be listed explicitly (rather than using
  // java.lang.ReflectiveOperationException) because that parent exception type isn't available
  // on Android until API level 19.
  Label startLabel = new Label();
  Label endLabel = new Label();
  Label handlerLabel = new Label();
  methodVisitor.visitTryCatchBlock(
      startLabel, endLabel, handlerLabel, "java/lang/NoClassDefFoundError");
  methodVisitor.visitTryCatchBlock(
      startLabel, endLabel, handlerLabel, "java/lang/IllegalAccessException");
  methodVisitor.visitTryCatchBlock(
      startLabel, endLabel, handlerLabel, "java/lang/InstantiationException");
  methodVisitor.visitTryCatchBlock(
      startLabel, endLabel, handlerLabel, "java/lang/reflect/InvocationTargetException");
  methodVisitor.visitTryCatchBlock(
      startLabel, endLabel, handlerLabel, "java/lang/NoSuchMethodException");
  methodVisitor.visitLabel(startLabel);

  // Generate the actual reflective constructor call inside the try block:
  //
  //   return (Platform) PlatformClass.class.getDeclaredConstructor().newInstance();
  //
  // Note that the constructor call happens reflectively to make sure that the platform class
  // isn't loaded until actually executing this instruction. That is important because an
  // earlier class load could happen outside of the try/catch block where we are explicitly
  // handling the case of the class not being present.
  methodVisitor.visitLdcInsn(Type.getType(platformType));
  methodVisitor.visitInsn(ICONST_0);
  methodVisitor.visitTypeInsn(ANEWARRAY, "java/lang/Class");
  methodVisitor.visitMethodInsn(
      INVOKEVIRTUAL,
      "java/lang/Class",
      "getDeclaredConstructor",
      "([Ljava/lang/Class;)Ljava/lang/reflect/Constructor;",
      false);
  methodVisitor.visitInsn(ICONST_0);
  methodVisitor.visitTypeInsn(ANEWARRAY, "java/lang/Object");
  methodVisitor.visitMethodInsn(
      INVOKEVIRTUAL,
      "java/lang/reflect/Constructor",
      "newInstance",
      "([Ljava/lang/Object;)Ljava/lang/Object;",
      false);
  methodVisitor.visitTypeInsn(CHECKCAST, "com/google/common/flogger/backend/Platform");
  methodVisitor.visitLabel(endLabel);
  methodVisitor.visitInsn(ARETURN);

  // Generate the catch block of the overall try/catch. The catch block is actually just empty,
  // but Java does require the catch handler to have at least a frame in it to declare the
  // exception variable that is available within the catch block scope:
  // https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.12
  methodVisitor.visitLabel(handlerLabel);
  methodVisitor.visitFrame(F_SAME1, 0, null, 1, new Object[] {"java/lang/Throwable"});
  methodVisitor.visitVarInsn(ASTORE, 0);
}
 
Example 20
Source File: LabelNode.java    From JReFrameworker with MIT License 4 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  methodVisitor.visitLabel(getLabel());
}