Example usage for org.objectweb.asm Label Label

List of usage examples for org.objectweb.asm Label Label

Introduction

In this page you can find the example usage for org.objectweb.asm Label Label.

Prototype

public Label() 

Source Link

Document

Constructs a new label.

Usage

From source file:com.google.devtools.build.android.desugar.BytecodeTypeInferenceTest.java

License:Open Source License

@Test
public void testUninitializedInferType() {
    Label label = new Label();
    InferredType type = InferredType.createUninitializedType(label);
    assertThat(type.descriptor()).isEqualTo(InferredType.UNINITIALIZED_PREFIX);
    assertThat(type.uninitializationLabel()).isEqualTo(label);
}

From source file:com.google.devtools.build.android.desugar.CoreLibrarySupport.java

License:Open Source License

private void makeDispatchHelperMethod(ClassVisitor helper, EmulatedMethod method,
        ImmutableList<Class<?>> typechecks) {
    checkArgument(method.owner().isInterface());
    String owner = method.owner().getName().replace('.', '/');
    Type methodType = Type.getMethodType(method.descriptor());
    String companionDesc = InterfaceDesugaring.companionDefaultMethodDescriptor(owner, method.descriptor());
    MethodVisitor dispatchMethod = helper.visitMethod(method.access() | Opcodes.ACC_STATIC, method.name(),
            companionDesc, /*signature=*/ null, // signature is invalid due to extra "receiver" argument
            method.exceptions().toArray(EMPTY_LIST));

    dispatchMethod.visitCode();//from w w w .  ja  v  a 2  s .c  o m
    {
        // See if the receiver might come with its own implementation of the method, and call it.
        // We do this by testing for the interface type created by EmulatedInterfaceRewriter
        Label fallthrough = new Label();
        String emulationInterface = renameCoreLibrary(owner);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, emulationInterface);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, emulationInterface);

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKEINTERFACE, emulationInterface, method.name(),
                method.descriptor(), /*itf=*/ true);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Next, check for subtypes with specialized implementations and call them
    for (Class<?> tested : typechecks) {
        Label fallthrough = new Label();
        String testedName = tested.getName().replace('.', '/');
        // In case of a class this must be a member move; for interfaces use the companion.
        String target = tested.isInterface() ? InterfaceDesugaring.getCompanionClassName(testedName)
                : checkNotNull(memberMoves.get(rewriter.unprefix(testedName) + '#' + method.name()));
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.INSTANCEOF, testedName);
        dispatchMethod.visitJumpInsn(Opcodes.IFEQ, fallthrough);
        dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
        dispatchMethod.visitTypeInsn(Opcodes.CHECKCAST, testedName); // make verifier happy

        visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
        dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, target, method.name(),
                InterfaceDesugaring.companionDefaultMethodDescriptor(testedName, method.descriptor()),
                /*itf=*/ false);
        dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

        dispatchMethod.visitLabel(fallthrough);
        // Trivial frame for the branch target: same empty stack as before
        dispatchMethod.visitFrame(Opcodes.F_SAME, 0, EMPTY_FRAME, 0, EMPTY_FRAME);
    }

    // Call static type's default implementation in companion class
    dispatchMethod.visitVarInsn(Opcodes.ALOAD, 0); // load "receiver"
    visitLoadArgs(dispatchMethod, methodType, 1 /* receiver already loaded above */);
    dispatchMethod.visitMethodInsn(Opcodes.INVOKESTATIC, InterfaceDesugaring.getCompanionClassName(owner),
            method.name(), companionDesc, /*itf=*/ false);
    dispatchMethod.visitInsn(methodType.getReturnType().getOpcode(Opcodes.IRETURN));

    dispatchMethod.visitMaxs(0, 0);
    dispatchMethod.visitEnd();
}

From source file:com.google.devtools.build.lib.syntax.compiler.LabelAdder.java

License:Open Source License

public LabelAdder() {
    this.label = new Label();
}

From source file:com.google.devtools.build.wireless.testing.java.injector.coverage.CodeCoverageClassAdapterTest.java

License:Apache License

public void testVisitMethod_lineCoverageInstrumentationAdded() {
    final String methodName = "method";
    final String className = "com/google/common/Timer";
    final String sourceFile = "Timer.java";
    final String methodDesc = "()V";
    final String method = className + "." + methodName + methodDesc;

    String[] inclusion = new String[] { "+com" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.LINE);/* w  w  w .  j av  a 2s  . co  m*/

    classAdapter.visit(1, Opcodes.ACC_PUBLIC, className, null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource(sourceFile, null);

    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, methodName, "()V", null, null);
    final int lineNumber = 20;
    mv.visitLineNumber(20, new Label());

    assertEquals(1, statisticContainer.getInstrumentedLines(className).size());
    assertTrue(statisticContainer.getInstrumentedLines(className).get(0).equals(lineNumber));
}

From source file:com.google.devtools.build.wireless.testing.java.injector.coverage.CodeCoverageClassAdapterTest.java

License:Apache License

public void testVisitMethod_lineCoverageInstrumentationNotAdded() {
    final String methodName = "method";
    final String className = "com/google/common/Timer";
    final String sourceFile = "Timer.java";
    final String methodDesc = "()V";
    final String method = className + "." + methodName + methodDesc;

    String[] inclusion = new String[] { "+com" };
    classAdapter = new CodeCoverageClassAdapter(new EmptyVisitor(), statisticContainer, inclusion,
            CoverageMode.SUMMARY);/*from  w  w w.ja  va 2s  .c  o m*/

    classAdapter.visit(1, Opcodes.ACC_PUBLIC, className, null, ClassNames.JAVA_LANG_OBJECT, null);
    classAdapter.visitSource(sourceFile, null);

    MethodVisitor mv = classAdapter.visitMethod(Opcodes.ACC_PUBLIC, methodName, "()V", null, null);
    final int lineNumber = 20;
    mv.visitLineNumber(20, new Label());

    assertEquals(0, statisticContainer.getInstrumentedLines(method).size());
}

From source file:com.google.gag.agent.ThisHadBetterGenerator.java

License:Apache License

private static void visitThisHadBetter(MethodVisitor mv, LocalVarInfo param, AnnoInfo anno, boolean be) {

    Label okay = new Label();

    Property property = Property.valueOf((String) anno.getValue("value"));
    switch (property) {
    case NEGATIVE:
    case POSITIVE:
    case ZERO://from   w  ww . ja v a2s .  co m
        visitComparable(mv, be, property, param, okay);
        break;
    case NULL:
        visitNullCheck(mv, be, param, okay);
        break;
    case THE_BLUE_PILL:
    case THE_RED_PILL:
        visitPill(mv, be, property, param, okay);
        break;
    case THE_STOLEN_DEATH_STAR_PLANS:
        visitDeathStarPlans(mv, be, param, okay);
        break;
    default:
        throw new AnnotationStateError("Unsupported Property: " + property);
    }

    visitException(mv, param, " is" + (be ? " not " : " ") + format(property));
    mv.visitLabel(okay);
}

From source file:com.google.gag.agent.ThisHadBetterGenerator.java

License:Apache License

/**
 * The given Property needs to be either {@link Property#THE_BLUE_PILL} or
 * {@link Property#THE_RED_PILL}./* w w  w.j  a  v a2s .co  m*/
 */
private static void visitPill(MethodVisitor mv, boolean be, Property property, LocalVarInfo param, Label okay) {

    if (param.getType().getSort() != Type.OBJECT) {
        throw new AnnotationStateError("Unsupported type: " + param.getType());
    }

    // TODO: Also handle if parameter is null.

    Label notOkay = new Label();

    // See if the param type matches a Pill type
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitMethodInsn(INVOKEVIRTUAL, param.getType().getInternalName(), "getClass", "()Ljava/lang/Class;");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;");
    mv.visitLdcInsn("Pill|ThePill|.*[\\.$]Pill|.*[\\.$]ThePill");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "matches", "(Ljava/lang/String;)Z");

    if (be) {
        // If the param type does not match a Pill type, that's not okay, go to exception.
        mv.visitJumpInsn(FALSE, notOkay);
    } else {
        // If the param type does not match a Pill type, then that's okay, skip exception.
        mv.visitJumpInsn(FALSE, okay);
    }

    // At this point, the param type matches a Pill type.
    // So check if the param type is an enum type.
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitTypeInsn(INSTANCEOF, "java/lang/Enum");

    if (be) {
        // If the param type is not an enum, that's not okay, go to exception.
        mv.visitJumpInsn(FALSE, notOkay);
    } else {
        // If the param type is not an enum, that's okay, skip exception.
        mv.visitJumpInsn(FALSE, okay);
    }

    // Check that the Pill type has the property specified in the annotation.
    // First try to match on "BLUE" (or "RED").
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitTypeInsn(CHECKCAST, "java/lang/Enum");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Enum", "name", "()Ljava/lang/String;");
    mv.visitLdcInsn(property == Property.THE_BLUE_PILL ? "BLUE" : "RED");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z");
    mv.visitJumpInsn(TRUE, be ? okay : notOkay);

    // Then try to see if the value ends with "BLUE_PILL" (or "RED_PILL").
    mv.visitVarInsn(ALOAD, param.getIndex());
    mv.visitTypeInsn(CHECKCAST, "java/lang/Enum");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Enum", "name", "()Ljava/lang/String;");
    mv.visitLdcInsn(property == Property.THE_BLUE_PILL ? "BLUE_PILL" : "RED_PILL");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "endsWith", "(Ljava/lang/String;)Z");
    mv.visitJumpInsn(be ? TRUE : FALSE, okay);

    mv.visitLabel(notOkay);
}

From source file:com.google.gwt.jvm.asm.NativeMethodDelegatingVisitor.java

License:Apache License

private void assertThisNotNull() {
    delegate.visitVarInsn(Opcodes.ALOAD, 0);
    Label notNull = new Label();
    delegate.visitJumpInsn(Opcodes.IFNONNULL, notNull);
    delegate.visitTypeInsn(Opcodes.NEW, NULL_POINTER_EXCEPTION);
    delegate.visitInsn(Opcodes.DUP);//from w w  w.  j a  v a  2  s .  c om
    delegate.visitMethodInsn(Opcodes.INVOKESPECIAL, NULL_POINTER_EXCEPTION, "<init>", "()V");
    delegate.visitInsn(Opcodes.ATHROW);
    delegate.visitLabel(notNull);
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void doBindOne(final MethodVisitor mv, final CodeGenSupport cgs, final ColumnModel field) {
    if (field.isNested() && field.isNotNull()) {
        for (final ColumnModel c : field.getAllLeafColumns()) {
            doBindOne(mv, cgs, c);//from  ww w.  j a  v  a  2 s  . c o  m
        }

    } else if (field.isNested()) {
        final int colIdx = cgs.getColumnIndex();
        final Label isnull = new Label();
        final Label end = new Label();

        cgs.setFieldReference(field);
        cgs.pushFieldValue();
        mv.visitJumpInsn(IFNULL, isnull);
        cgs.resetColumnIndex(colIdx);
        for (final ColumnModel c : field.getNestedColumns()) {
            doBindOne(mv, cgs, c);
        }
        mv.visitJumpInsn(GOTO, end);

        mv.visitLabel(isnull);
        cgs.resetColumnIndex(colIdx);
        for (final ColumnModel c : field.getAllLeafColumns()) {
            cgs.setFieldReference(c);
            dialect.getSqlTypeInfo(c).generatePreparedStatementNull(cgs);
        }

        mv.visitLabel(end);
    } else {
        cgs.setFieldReference(field);
        dialect.getSqlTypeInfo(field).generatePreparedStatementSet(cgs);
    }
}

From source file:com.google.gwtorm.jdbc.AccessGen.java

License:Apache License

private void doFetchOne(final MethodVisitor mv, final CodeGenSupport cgs, final ColumnModel field,
        final int reportLiveInto) {
    if (field.isNested()) {
        int oldIdx = cgs.getColumnIndex();
        final Type vType = CodeGenSupport.toType(field);
        final int livecnt;

        if (field.isNotNull()) {
            livecnt = -1;/*from w  ww .  j a  v  a  2  s .co  m*/
        } else {
            livecnt = cgs.newLocal();
            cgs.push(0);
            mv.visitVarInsn(ISTORE, livecnt);
        }

        cgs.setFieldReference(field);
        cgs.fieldSetBegin();
        mv.visitTypeInsn(NEW, vType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, vType.getInternalName(), "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
        cgs.fieldSetEnd();

        cgs.resetColumnIndex(oldIdx);
        for (final ColumnModel c : field.getNestedColumns()) {
            doFetchOne(mv, cgs, c, livecnt);
        }

        if (livecnt >= 0) {
            oldIdx = cgs.getColumnIndex();

            final Label islive = new Label();
            mv.visitVarInsn(ILOAD, livecnt);
            mv.visitJumpInsn(IFNE, islive);
            cgs.setFieldReference(field);
            cgs.fieldSetBegin();
            mv.visitInsn(ACONST_NULL);
            cgs.fieldSetEnd();

            if (reportLiveInto >= 0) {
                final Label end = new Label();
                mv.visitJumpInsn(GOTO, end);
                mv.visitLabel(islive);
                mv.visitIincInsn(reportLiveInto, 1);
                mv.visitLabel(end);
            } else {
                mv.visitLabel(islive);
            }

            cgs.resetColumnIndex(oldIdx);
            cgs.freeLocal(livecnt);
        }

    } else {
        final int dupTo;
        if (reportLiveInto >= 0 && CodeGenSupport.toType(field).getSort() == Type.OBJECT) {
            dupTo = cgs.newLocal();
        } else {
            dupTo = -1;
        }

        cgs.setFieldReference(field);
        cgs.setDupOnFieldSetEnd(dupTo);
        dialect.getSqlTypeInfo(field).generateResultSetGet(cgs);

        if (reportLiveInto >= 0) {
            final Label wasnull = new Label();
            if (dupTo >= 0) {
                mv.visitVarInsn(ALOAD, dupTo);
                mv.visitJumpInsn(IFNULL, wasnull);
                cgs.freeLocal(dupTo);
            } else {
                cgs.pushSqlHandle();
                mv.visitMethodInsn(INVOKEINTERFACE, Type.getType(ResultSet.class).getInternalName(), "wasNull",
                        Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
                mv.visitJumpInsn(IFNE, wasnull);
            }
            mv.visitIincInsn(reportLiveInto, 1);
            mv.visitLabel(wasnull);
        }
    }
}