Search in sources :

Example 46 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class TypeFromTree method fromExpression.

/**
 * Returns an AnnotatedTypeMirror representing the input expression tree.
 *
 * @param tree must be an ExpressionTree
 * @return an AnnotatedTypeMirror representing the input expression tree
 */
public static AnnotatedTypeMirror fromExpression(final AnnotatedTypeFactory typeFactory, final ExpressionTree tree) {
    abortIfTreeIsNull(typeFactory, tree);
    final AnnotatedTypeMirror type;
    try {
        type = expressionVisitor.visit(tree, typeFactory);
    } catch (Throwable t) {
        throw new BugInCF(t, "Error in AnnotatedTypeMirror.fromExpression(%s, %s): %s", typeFactory.getClass().getSimpleName(), tree, t.getMessage());
    }
    ifExecutableCheckElement(typeFactory, tree, type);
    return type;
}
Also used : BugInCF(org.checkerframework.javacutil.BugInCF)

Example 47 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class TypeFromTypeTreeVisitor method visitAnnotatedType.

@Override
public AnnotatedTypeMirror visitAnnotatedType(AnnotatedTypeTree node, AnnotatedTypeFactory f) {
    AnnotatedTypeMirror type = visit(node.getUnderlyingType(), f);
    if (type == null) {
        // e.g., for receiver type
        type = f.toAnnotatedType(f.types.getNoType(TypeKind.NONE), false);
    }
    assert AnnotatedTypeFactory.validAnnotatedType(type);
    List<? extends AnnotationMirror> annos = TreeUtils.annotationsFromTree(node);
    if (type.getKind() == TypeKind.WILDCARD) {
        // Work-around for https://github.com/eisop/checker-framework/issues/17
        // For an annotated wildcard tree node, the type attached to the
        // node is a WildcardType with a correct bound (set to the type
        // variable which the wildcard instantiates). The underlying type is
        // also a WildcardType but with a bound of null. Here we update the
        // bound of the underlying WildcardType to be consistent.
        WildcardType wildcardAttachedToNode = (WildcardType) TreeUtils.typeOf(node);
        WildcardType underlyingWildcard = (WildcardType) type.getUnderlyingType();
        underlyingWildcard.withTypeVar(wildcardAttachedToNode.bound);
        // End of work-around
        final AnnotatedWildcardType wctype = ((AnnotatedWildcardType) type);
        final ExpressionTree underlyingTree = node.getUnderlyingType();
        if (underlyingTree.getKind() == Tree.Kind.UNBOUNDED_WILDCARD) {
            // primary annotations on unbounded wildcard types apply to both bounds
            wctype.getExtendsBound().addAnnotations(annos);
            wctype.getSuperBound().addAnnotations(annos);
        } else if (underlyingTree.getKind() == Tree.Kind.EXTENDS_WILDCARD) {
            wctype.getSuperBound().addAnnotations(annos);
        } else if (underlyingTree.getKind() == Tree.Kind.SUPER_WILDCARD) {
            wctype.getExtendsBound().addAnnotations(annos);
        } else {
            throw new BugInCF("Unexpected kind for type.  node=" + node + " type=" + type + " kind=" + underlyingTree.getKind());
        }
    } else {
        type.addAnnotations(annos);
    }
    return type;
}
Also used : AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 48 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class NoElementQualifierHierarchy method getQualifierKind.

/**
 * Returns the {@link QualifierKind} for the given annotation.
 *
 * @param anno an annotation that is a qualifier in this
 * @return the {@code QualifierKind} for the given annotation
 */
protected QualifierKind getQualifierKind(AnnotationMirror anno) {
    String name = AnnotationUtils.annotationName(anno);
    QualifierKind kind = qualifierKindHierarchy.getQualifierKind(name);
    if (kind == null) {
        throw new BugInCF("Annotation not in hierarchy: %s", anno);
    }
    return kind;
}
Also used : QualifierKind(org.checkerframework.framework.util.QualifierKind) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 49 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class LiteralTreeAnnotator method visitLiteral.

/**
 * Go through the string patterns and add the greatest lower bound of all matching patterns.
 */
@Override
public Void visitLiteral(LiteralTree tree, AnnotatedTypeMirror type) {
    if (!stringPatterns.isEmpty() && tree.getKind() == Tree.Kind.STRING_LITERAL) {
        List<Set<? extends AnnotationMirror>> matches = new ArrayList<>();
        List<Set<? extends AnnotationMirror>> nonMatches = new ArrayList<>();
        String string = (String) tree.getValue();
        for (Pattern pattern : stringPatterns.keySet()) {
            Set<AnnotationMirror> sam = stringPatterns.get(pattern);
            if (pattern.matcher(string).matches()) {
                matches.add(sam);
            } else {
                nonMatches.add(sam);
            }
        }
        if (!matches.isEmpty()) {
            Set<? extends AnnotationMirror> res = matches.get(0);
            for (Set<? extends AnnotationMirror> sam : matches) {
                res = qualHierarchy.greatestLowerBounds(res, sam);
            }
            // Verify that res is not a subtype of any type in nonMatches
            for (Set<? extends AnnotationMirror> sam : nonMatches) {
                if (qualHierarchy.isSubtype(res, sam)) {
                    String matchesOnePerLine = "";
                    for (Set<? extends AnnotationMirror> match : matches) {
                        matchesOnePerLine += System.lineSeparator() + "     " + match;
                    }
                    throw new BugInCF(StringsPlume.joinLines("Bug in @QualifierForLiterals(stringpatterns=...) in type hierarchy" + " definition:", " the glb of `matches` for \"" + string + "\" is " + res, " which is a subtype of " + sam, " whose pattern does not match \"" + string + "\".", "  matches = " + matchesOnePerLine, "  nonMatches = " + nonMatches));
                }
            }
            type.addAnnotations(res);
        }
    }
    return super.visitLiteral(tree, type);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Pattern(java.util.regex.Pattern) Set(java.util.Set) ArrayList(java.util.ArrayList) BugInCF(org.checkerframework.javacutil.BugInCF)

Example 50 with BugInCF

use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.

the class WholeProgramInferenceJavaParserStorage method addSourceFile.

/**
 * Reads in the file at {@code path} and creates a wrapper around its compilation unit. Stores the
 * wrapper in {@link #sourceToAnnos}, but doesn't create wrappers around any classes in the file.
 *
 * @param path path to source file to read
 */
private void addSourceFile(String path) {
    if (sourceToAnnos.containsKey(path)) {
        return;
    }
    CompilationUnit root;
    try {
        root = JavaParserUtil.parseCompilationUnit(new File(path));
    } catch (FileNotFoundException e) {
        throw new BugInCF("Failed to read Java file " + path, e);
    }
    JavaParserUtil.concatenateAddedStringLiterals(root);
    CompilationUnitAnnos sourceAnnos = new CompilationUnitAnnos(root);
    sourceToAnnos.put(path, sourceAnnos);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) FileNotFoundException(java.io.FileNotFoundException) BugInCF(org.checkerframework.javacutil.BugInCF) File(java.io.File)

Aggregations

BugInCF (org.checkerframework.javacutil.BugInCF)127 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)29 ArrayList (java.util.ArrayList)28 AnnotationMirror (javax.lang.model.element.AnnotationMirror)26 TypeElement (javax.lang.model.element.TypeElement)26 TypeMirror (javax.lang.model.type.TypeMirror)25 ExecutableElement (javax.lang.model.element.ExecutableElement)24 MethodTree (com.sun.source.tree.MethodTree)20 ExpressionTree (com.sun.source.tree.ExpressionTree)18 VariableTree (com.sun.source.tree.VariableTree)18 Element (javax.lang.model.element.Element)18 ClassTree (com.sun.source.tree.ClassTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 NewClassTree (com.sun.source.tree.NewClassTree)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 IOException (java.io.IOException)16 Tree (com.sun.source.tree.Tree)15 Map (java.util.Map)15 List (java.util.List)14 VariableElement (javax.lang.model.element.VariableElement)14