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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations