Search in sources :

Example 6 with FullyQualifiedName

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

the class AnnotationFileParser method getAnnotation.

/**
 * Convert {@code annotation} into an AnnotationMirror. Returns null if the annotation isn't
 * supported by the checker or if some error occurred while converting it.
 *
 * @param annotation syntax tree for an annotation
 * @param allAnnotations map from simple name to annotation definition; side-effected by this
 *     method
 * @return the AnnotationMirror for the annotation, or null if it cannot be built
 */
@Nullable
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, TypeElement> allAnnotations) {
    // https://tinyurl.com/cfissue/3094
    @SuppressWarnings("signature") @FullyQualifiedName String annoNameFq = annotation.getNameAsString();
    TypeElement annoTypeElt = allAnnotations.get(annoNameFq);
    if (annoTypeElt == null) {
        // If the annotation was not imported, then #getImportedAnnotations did not add it to the
        // allAnnotations field. This code adds the annotation when it is encountered (i.e. here).
        // Note that this does not call AnnotationFileParser#getTypeElement to avoid a spurious
        // diagnostic if the annotation is actually unknown.
        annoTypeElt = elements.getTypeElement(annoNameFq);
        if (annoTypeElt == null) {
            // Not a supported annotation -> ignore
            return null;
        }
        putAllNew(allAnnotations, createNameToAnnotationMap(Collections.singletonList(annoTypeElt)));
    }
    // not anonymous, so name is not empty
    @SuppressWarnings("signature") @CanonicalName String annoName = annoTypeElt.getQualifiedName().toString();
    if (annotation instanceof MarkerAnnotationExpr) {
        return AnnotationBuilder.fromName(elements, annoName);
    } else if (annotation instanceof NormalAnnotationExpr) {
        NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
        List<MemberValuePair> pairs = nrmanno.getPairs();
        if (pairs != null) {
            for (MemberValuePair mvp : pairs) {
                String member = mvp.getNameAsString();
                Expression exp = mvp.getValue();
                try {
                    builderAddElement(builder, member, exp);
                } catch (AnnotationFileParserException e) {
                    warn(exp, "For annotation %s, could not add  %s=%s  because %s", annotation, member, exp, e.getMessage());
                    return null;
                }
            }
        }
        return builder.build();
    } else if (annotation instanceof SingleMemberAnnotationExpr) {
        SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
        AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoName);
        Expression valExpr = sglanno.getMemberValue();
        try {
            builderAddElement(builder, "value", valExpr);
        } catch (AnnotationFileParserException e) {
            warn(valExpr, "For annotation %s, could not add  value=%s  because %s", annotation, valExpr, e.getMessage());
            return null;
        }
        return builder.build();
    } else {
        throw new BugInCF("AnnotationFileParser: unknown annotation type: " + annotation);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) MarkerAnnotationExpr(com.github.javaparser.ast.expr.MarkerAnnotationExpr) BugInCF(org.checkerframework.javacutil.BugInCF) SingleMemberAnnotationExpr(com.github.javaparser.ast.expr.SingleMemberAnnotationExpr) MemberValuePair(com.github.javaparser.ast.expr.MemberValuePair) AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) Expression(com.github.javaparser.ast.expr.Expression) ArrayList(java.util.ArrayList) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) NormalAnnotationExpr(com.github.javaparser.ast.expr.NormalAnnotationExpr) CanonicalName(org.checkerframework.checker.signature.qual.CanonicalName) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 7 with FullyQualifiedName

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

the class AnnotationFileParser method getValueOfExpressionInAnnotation.

/**
 * Returns the value of {@code expr}.
 *
 * @param name the name of an annotation element/argument, used for diagnostic messages
 * @param expr the expression to determine the value of
 * @param valueKind the type of the result
 * @return the value of {@code expr}
 * @throws AnnotationFileParserException if a problem occurred getting the value
 */
private Object getValueOfExpressionInAnnotation(String name, Expression expr, TypeKind valueKind) throws AnnotationFileParserException {
    if (expr instanceof FieldAccessExpr || expr instanceof NameExpr) {
        VariableElement elem;
        if (expr instanceof NameExpr) {
            elem = findVariableElement((NameExpr) expr);
        } else {
            elem = findVariableElement((FieldAccessExpr) expr);
        }
        if (elem == null) {
            throw new AnnotationFileParserException(String.format("variable %s not found", expr));
        }
        Object value = elem.getConstantValue() != null ? elem.getConstantValue() : elem;
        if (value instanceof Number) {
            return convert((Number) value, valueKind);
        } else {
            return value;
        }
    } else if (expr instanceof StringLiteralExpr) {
        return ((StringLiteralExpr) expr).asString();
    } else if (expr instanceof BooleanLiteralExpr) {
        return ((BooleanLiteralExpr) expr).getValue();
    } else if (expr instanceof CharLiteralExpr) {
        return convert((int) ((CharLiteralExpr) expr).asChar(), valueKind);
    } else if (expr instanceof DoubleLiteralExpr) {
        // double, too.
        return ((DoubleLiteralExpr) expr).asDouble();
    } else if (expr instanceof IntegerLiteralExpr) {
        return convert(((IntegerLiteralExpr) expr).asNumber(), valueKind);
    } else if (expr instanceof LongLiteralExpr) {
        return convert(((LongLiteralExpr) expr).asNumber(), valueKind);
    } else if (expr instanceof UnaryExpr) {
        switch(expr.toString()) {
            // the smallest member of an integral type is larger than the largest value.
            case "-9223372036854775808L":
            case "-9223372036854775808l":
                return convert(Long.MIN_VALUE, valueKind, false);
            case "-2147483648":
                return convert(Integer.MIN_VALUE, valueKind, false);
            default:
                if (((UnaryExpr) expr).getOperator() == UnaryExpr.Operator.MINUS) {
                    Object value = getValueOfExpressionInAnnotation(name, ((UnaryExpr) expr).getExpression(), valueKind);
                    if (value instanceof Number) {
                        return convert((Number) value, valueKind, true);
                    }
                }
                throw new AnnotationFileParserException("unexpected Unary annotation expression: " + expr);
        }
    } else if (expr instanceof ClassExpr) {
        ClassExpr classExpr = (ClassExpr) expr;
        // Type.toString(): @FullyQualifiedName
        @SuppressWarnings("signature") @FullyQualifiedName String className = classExpr.getType().toString();
        if (importedTypes.containsKey(className)) {
            return importedTypes.get(className).asType();
        }
        TypeElement typeElement = findTypeOfName(className);
        if (typeElement == null) {
            throw new AnnotationFileParserException("unknown class name " + className);
        }
        return typeElement.asType();
    } else if (expr instanceof NullLiteralExpr) {
        throw new AnnotationFileParserException("Illegal annotation value null, for " + name);
    } else {
        throw new AnnotationFileParserException("Unexpected annotation expression: " + expr);
    }
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) TypeElement(javax.lang.model.element.TypeElement) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) VariableElement(javax.lang.model.element.VariableElement) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr)

Example 8 with FullyQualifiedName

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

the class SourceChecker method shouldSkipUses.

// /////////////////////////////////////////////////////////////////////////
// / Skipping uses and defs
// /
/**
 * Tests whether the class owner of the passed element is an unannotated class and matches the
 * pattern specified in the {@code checker.skipUses} property.
 *
 * @param element an element
 * @return true iff the enclosing class of element should be skipped
 */
public final boolean shouldSkipUses(Element element) {
    if (element == null) {
        return false;
    }
    TypeElement typeElement = ElementUtils.enclosingTypeElement(element);
    if (typeElement == null) {
        throw new BugInCF("enclosingTypeElement(%s [%s]) => null%n", element, element.getClass());
    }
    // TypeElement.toString(): @FullyQualifiedName
    @SuppressWarnings("signature:assignment") @FullyQualifiedName String name = typeElement.toString();
    return shouldSkipUses(name);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) FullyQualifiedName(org.checkerframework.checker.signature.qual.FullyQualifiedName) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 9 with FullyQualifiedName

use of org.checkerframework.checker.signature.qual.FullyQualifiedName 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

FullyQualifiedName (org.checkerframework.checker.signature.qual.FullyQualifiedName)9 TypeElement (javax.lang.model.element.TypeElement)7 ArrayList (java.util.ArrayList)4 VariableElement (javax.lang.model.element.VariableElement)4 HashMap (java.util.HashMap)3 PackageElement (javax.lang.model.element.PackageElement)3 BugInCF (org.checkerframework.javacutil.BugInCF)3 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)2 NodeList (com.github.javaparser.ast.NodeList)2 MemberValuePair (com.github.javaparser.ast.expr.MemberValuePair)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 DotSeparatedIdentifiers (org.checkerframework.checker.signature.qual.DotSeparatedIdentifiers)2 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 AnnotationDeclaration (com.github.javaparser.ast.body.AnnotationDeclaration)1 BodyDeclaration (com.github.javaparser.ast.body.BodyDeclaration)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 EnumConstantDeclaration (com.github.javaparser.ast.body.EnumConstantDeclaration)1 EnumDeclaration (com.github.javaparser.ast.body.EnumDeclaration)1 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1