Example usage for org.objectweb.asm TypeReference THROWS

List of usage examples for org.objectweb.asm TypeReference THROWS

Introduction

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

Prototype

int THROWS

To view the source code for org.objectweb.asm TypeReference THROWS.

Click Source Link

Document

The sort of type references that target the type of an exception declared in the throws clause of a method.

Usage

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public AnnotationVisitor visitTypeAnnotation(final int typeRef, @Nullable final TypePath typePath,
        final String desc, final boolean visible) {
    final A a = this.annotationVisitor.init(desc, visible ? RetentionPolicy.RUNTIME : RetentionPolicy.CLASS);
    if (a == null) {
        log.warn(getM() + ": Cannot read annotation for descriptor '" + desc + "'!");
        return null;
    }/*from   www .j  av a 2 s . c  om*/
    final TypeReference typeReference = new TypeReference(typeRef);
    switch (typeReference.getSort()) {
    case TypeReference.METHOD_FORMAL_PARAMETER: {
        final int formalParameterIndex = typeReference.getFormalParameterIndex();
        final T[] paramTs = getM().getParamTs();
        if (paramTs.length <= formalParameterIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a
                    + "' to missing method parameter at index '" + formalParameterIndex + "'!");
            break;
        }
        final T paramT = paramTs[formalParameterIndex];
        assert paramT != null;
        paramTs[formalParameterIndex] = annotateT(paramT, a, typePath);
        break;
    }
    case TypeReference.METHOD_RECEIVER: {
        // for type annotations like: void test(@Annots This this, ...) for none-static methods
        // TODO receiver needs full signature, test-method DU#getQualifiedT(T) does't work,
        // because we would have to read outer classes first
        T receiverT = getM().getReceiverT();
        if (receiverT == null) {
            receiverT = getM().getT();
        }
        if (receiverT == null) {
            assert getM().isDynamic();
            log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing method receiver!");
            break;
        }
        getM().setReceiverT(annotateT(receiverT, a, typePath));
        break;
    }
    case TypeReference.METHOD_RETURN:
        getM().setReturnT(annotateT(getM().getReturnT(), a, typePath));
        break;
    case TypeReference.METHOD_TYPE_PARAMETER: {
        final int typeParameterIndex = typeReference.getTypeParameterIndex();
        final T[] typeParams = getM().getTypeParams();
        if (typeParams.length <= typeParameterIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a
                    + "' to missing method type parameter at index '" + typeParameterIndex + "'!");
            break;
        }
        final T typeParam = typeParams[typeParameterIndex];
        assert typeParam != null;
        typeParams[typeParameterIndex] = annotateT(typeParam, a, typePath);
        break;
    }
    case TypeReference.METHOD_TYPE_PARAMETER_BOUND: {
        final int typeParameterIndex = typeReference.getTypeParameterIndex();
        final int typeParameterBoundIndex = typeReference.getTypeParameterBoundIndex();
        final T t = getM().getTypeParams()[typeParameterIndex];
        if (typeParameterBoundIndex == 0) {
            // 0: annotation targets extends type
            final T superT = t.getSuperT();
            if (superT == null) {
                log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing super type!");
                break;
            }
            t.setSuperT(annotateT(superT, a, typePath));
            break;
        }
        // 1-based interface index
        final T[] interfaceTs = t.getInterfaceTs();
        if (interfaceTs.length < typeParameterBoundIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing interface type!");
            break;
        }
        final T interfaceT = interfaceTs[typeParameterBoundIndex - 1];
        assert interfaceT != null;
        interfaceTs[typeParameterBoundIndex - 1] = annotateT(interfaceT, a, typePath);
        break;
    }
    case TypeReference.THROWS: {
        final int exceptionIndex = typeReference.getExceptionIndex();
        final T[] throwsTs = getM().getThrowsTs();
        if (throwsTs.length < exceptionIndex) {
            log.warn(getM() + ": Cannot apply type annotation '" + a + "' to missing throws type!");
            break;
        }
        final T throwsT = throwsTs[exceptionIndex];
        assert throwsT != null;
        throwsTs[exceptionIndex] = annotateT(throwsT, a, typePath);
        break;
    }
    default:
        log.warn(
                getM() + ": Unknown type annotation ref sort '0x" + Integer.toHexString(typeReference.getSort())
                        + "' : " + typeRef + " : " + typePath + " : " + desc + " : " + visible);
    }
    return this.annotationVisitor;
}