Search in sources :

Example 1 with DotSeparatedIdentifiers

use of org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers in project checker-framework by typetools.

the class UnitsAnnotatedTypeFactory method addUnitToExternalQualMap.

/**
 * Adds the annotation class to the external qualifier map if it is not an alias annotation.
 */
private void addUnitToExternalQualMap(final Class<? extends Annotation> annoClass) {
    AnnotationMirror mirror = UnitsRelationsTools.buildAnnoMirrorWithNoPrefix(processingEnv, annoClass.getCanonicalName());
    // if it is not an aliased annotation, add to external quals map if it isn't already in map
    if (!isAliasedAnnotation(mirror)) {
        String unitClassName = annoClass.getCanonicalName();
        if (!externalQualsMap.containsKey(unitClassName)) {
            externalQualsMap.put(unitClassName, annoClass);
        }
    } else // if it is an aliased annotation
    {
        // ensure it has a base unit
        @CanonicalName Name baseUnitClass = getBaseUnitAnno(mirror);
        if (baseUnitClass != null) {
            // if the base unit isn't already added, add that first
            // https://tinyurl.com/cfissue/658
            @SuppressWarnings("signature") @DotSeparatedIdentifiers String baseUnitClassName = baseUnitClass.toString();
            if (!externalQualsMap.containsKey(baseUnitClassName)) {
                loadExternalUnit(baseUnitClassName);
            }
            // then add the aliased annotation to the alias map
            // TODO: refactor so we can directly add to alias map, skipping the assert check in
            // canonicalAnnotation.
            canonicalAnnotation(mirror);
        } else {
        // error: somehow the aliased annotation has @UnitsMultiple meta annotation, but no
        // base class defined in that meta annotation
        // TODO: error abort
        }
    }
    // process the units annotation and add its corresponding units relations class
    addUnitsRelations(annoClass);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) CanonicalName(org.checkerframework.checker.signature.qual.CanonicalName) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) CanonicalName(org.checkerframework.checker.signature.qual.CanonicalName) Name(javax.lang.model.element.Name) DotSeparatedIdentifiers(org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers)

Example 2 with DotSeparatedIdentifiers

use of org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers in project checker-framework by typetools.

the class AnnotationFileParser method getImportedAnnotations.

/**
 * Returns all annotations imported by the annotation file, as a value for {@link
 * #allAnnotations}. Note that this also modifies {@link #importedConstants} and {@link
 * #importedTypes}.
 *
 * <p>This method misses annotations that are not imported. The {@link #getAnnotation} method
 * compensates for this deficiency by adding any fully-qualified annotation that it encounters.
 *
 * @return a map from names to TypeElement, for all annotations imported by the annotation file.
 *     Two entries for each annotation: one for the simple name and another for the
 *     fully-qualified name, with the same value.
 * @see #allAnnotations
 */
private Map<String, TypeElement> getImportedAnnotations() {
    Map<String, TypeElement> result = new HashMap<>();
    // TODO: The size can be greater than 1, but this ignores all but the first element.
    assert !stubUnit.getCompilationUnits().isEmpty();
    CompilationUnit cu = stubUnit.getCompilationUnits().get(0);
    if (cu.getImports() == null) {
        return result;
    }
    for (ImportDeclaration importDecl : cu.getImports()) {
        try {
            if (importDecl.isAsterisk()) {
                @// https://tinyurl.com/cfissue/3094:
                SuppressWarnings(// https://tinyurl.com/cfissue/3094:
                "signature") @DotSeparatedIdentifiers String imported = importDecl.getName().toString();
                if (importDecl.isStatic()) {
                    // Wildcard import of members of a type (class or interface)
                    TypeElement element = getTypeElement(imported, "Imported type not found", importDecl);
                    if (element != null) {
                        // Find nested annotations
                        // Find compile time constant fields, or values of an enum
                        putAllNew(result, annosInType(element));
                        importedConstants.addAll(getImportableMembers(element));
                        addEnclosingTypesToImportedTypes(element);
                    }
                } else {
                    // Wildcard import of members of a package
                    PackageElement element = findPackage(imported, importDecl);
                    if (element != null) {
                        putAllNew(result, annosInPackage(element));
                        addEnclosingTypesToImportedTypes(element);
                    }
                }
            } else {
                // A single (non-wildcard) import.
                @// importDecl is non-wildcard, so its name is
                SuppressWarnings(// importDecl is non-wildcard, so its name is
                "signature") @FullyQualifiedName String imported = importDecl.getNameAsString();
                final TypeElement importType = elements.getTypeElement(imported);
                if (importType == null && !importDecl.isStatic()) {
                    // Class or nested class (according to JSL), but we can't resolve
                    stubWarnNotFound(importDecl, "Imported type not found: " + imported);
                } else if (importType == null) {
                    // static import of field or method.
                    Pair<@FullyQualifiedName String, String> typeParts = AnnotationFileUtil.partitionQualifiedName(imported);
                    String type = typeParts.first;
                    String fieldName = typeParts.second;
                    TypeElement enclType = getTypeElement(type, String.format("Enclosing type of static field %s not found", fieldName), importDecl);
                    if (enclType != null) {
                        // want a warning, imported might be a method.
                        for (VariableElement field : ElementUtils.getAllFieldsIn(enclType, elements)) {
                            // field.getSimpleName() is a CharSequence, not a String
                            if (fieldName.equals(field.getSimpleName().toString())) {
                                importedConstants.add(imported);
                            }
                        }
                    }
                } else if (importType.getKind() == ElementKind.ANNOTATION_TYPE) {
                    // Single annotation or nested annotation
                    TypeElement annoElt = elements.getTypeElement(imported);
                    if (annoElt != null) {
                        putIfAbsent(result, annoElt.getSimpleName().toString(), annoElt);
                        importedTypes.put(annoElt.getSimpleName().toString(), annoElt);
                    } else {
                        stubWarnNotFound(importDecl, "Could not load import: " + imported);
                    }
                } else {
                    // Class or nested class
                    // TODO: Is this needed?
                    importedConstants.add(imported);
                    TypeElement element = getTypeElement(imported, "Imported type not found", importDecl);
                    importedTypes.put(element.getSimpleName().toString(), element);
                }
            }
        } catch (AssertionError error) {
            stubWarnNotFound(importDecl, error.toString());
        }
    }
    return result;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) VariableElement(javax.lang.model.element.VariableElement) DotSeparatedIdentifiers(org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) PackageElement(javax.lang.model.element.PackageElement) Pair(org.checkerframework.javacutil.Pair) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair)

Example 3 with DotSeparatedIdentifiers

use of org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers in project checker-framework by typetools.

the class InsertAjavaAnnotations method getImportedAnnotations.

/**
 * Returns all annotations imported by the annotation file as a mapping from simple and qualified
 * names to TypeElements.
 *
 * @param cu compilation unit to extract imports from
 * @return a map from names to TypeElement, for all annotations imported by the annotation file.
 *     Two entries for each annotation: one for the simple name and another for the
 *     fully-qualified name, with the same value.
 */
private Map<String, TypeElement> getImportedAnnotations(CompilationUnit cu) {
    if (cu.getImports() == null) {
        return Collections.emptyMap();
    }
    Map<String, TypeElement> result = new HashMap<>();
    for (ImportDeclaration importDecl : cu.getImports()) {
        if (importDecl.isAsterisk()) {
            @// https://tinyurl.com/cfissue/3094:
            SuppressWarnings(// https://tinyurl.com/cfissue/3094:
            "signature") @DotSeparatedIdentifiers String imported = importDecl.getName().toString();
            if (importDecl.isStatic()) {
                // Wildcard import of members of a type (class or interface)
                TypeElement element = elements.getTypeElement(imported);
                if (element != null) {
                    // Find nested annotations
                    result.putAll(AnnotationFileParser.annosInType(element));
                }
            } else {
                // Wildcard import of members of a package
                PackageElement element = elements.getPackageElement(imported);
                if (element != null) {
                    result.putAll(AnnotationFileParser.annosInPackage(element));
                }
            }
        } else {
            @// importDecl is non-wildcard, so its name is
            SuppressWarnings(// importDecl is non-wildcard, so its name is
            "signature") @FullyQualifiedName String imported = importDecl.getNameAsString();
            TypeElement importType = elements.getTypeElement(imported);
            if (importType != null && importType.getKind() == ElementKind.ANNOTATION_TYPE) {
                TypeElement annoElt = elements.getTypeElement(imported);
                if (annoElt != null) {
                    result.put(annoElt.getSimpleName().toString(), annoElt);
                }
            }
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) PackageElement(javax.lang.model.element.PackageElement) DotSeparatedIdentifiers(org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers)

Aggregations

DotSeparatedIdentifiers (org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers)3 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)2 HashMap (java.util.HashMap)2 PackageElement (javax.lang.model.element.PackageElement)2 TypeElement (javax.lang.model.element.TypeElement)2 FullyQualifiedName (org.checkerframework.checker.signature.qual.FullyQualifiedName)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)1 LinkedHashMap (java.util.LinkedHashMap)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Name (javax.lang.model.element.Name)1 VariableElement (javax.lang.model.element.VariableElement)1 BinaryName (org.checkerframework.checker.signature.qual.BinaryName)1 CanonicalName (org.checkerframework.checker.signature.qual.CanonicalName)1 Pair (org.checkerframework.javacutil.Pair)1