Search in sources :

Example 1 with TypePathEntry

use of com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry in project checker-framework by typetools.

the class ElementAnnotationUtil method getLocationTypeADT.

/**
 * Given a TypePath into a declared type, return the component type that is located at the end
 * of the TypePath
 *
 * @param type a type containing the type specified by location
 * @param location a type path into type
 * @return the type specified by location
 */
private static AnnotatedTypeMirror getLocationTypeADT(AnnotatedDeclaredType type, List<TypeAnnotationPosition.TypePathEntry> location) {
    // List order by outer most type to inner most type.
    ArrayDeque<AnnotatedDeclaredType> outerToInner = new ArrayDeque<>();
    AnnotatedDeclaredType enclosing = type;
    while (enclosing != null) {
        outerToInner.addFirst(enclosing);
        enclosing = enclosing.getEnclosingType();
    }
    // Create a linked list of the location, so removing the first element is easier.
    // Also, the `tail` operation wouldn't work with a Deque.
    @SuppressWarnings("JdkObsolete") LinkedList<TypePathEntry> tailOfLocations = new LinkedList<>(location);
    boolean error = false;
    while (!tailOfLocations.isEmpty()) {
        TypePathEntry currentLocation = tailOfLocations.removeFirst();
        switch(currentLocation.tag) {
            case INNER_TYPE:
                outerToInner.removeFirst();
                break;
            case TYPE_ARGUMENT:
                AnnotatedDeclaredType innerType = outerToInner.getFirst();
                if (currentLocation.arg < innerType.getTypeArguments().size()) {
                    AnnotatedTypeMirror typeArg = innerType.getTypeArguments().get(currentLocation.arg);
                    return getTypeAtLocation(typeArg, tailOfLocations);
                } else {
                    error = true;
                    break;
                }
            default:
                error = true;
        }
        if (error) {
            break;
        }
    }
    if (outerToInner.isEmpty() || error) {
        ErrorReporter.errorAbort("ElementAnnotationUtil.getLocationTypeADT: invalid location %s for type: %s", location, type);
    }
    return outerToInner.getFirst();
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) ArrayDeque(java.util.ArrayDeque) LinkedList(java.util.LinkedList) TypePathEntry(com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry)

Aggregations

TypePathEntry (com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry)1 ArrayDeque (java.util.ArrayDeque)1 LinkedList (java.util.LinkedList)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)1