use of com.sun.source.tree.ParameterizedTypeTree in project error-prone by google.
the class IterablePathParameter method matchVariable.
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
Type type = ASTHelpers.getType(tree);
VarSymbol symbol = ASTHelpers.getSymbol(tree);
if (type == null || symbol == null) {
return NO_MATCH;
}
if (symbol.getKind() != ElementKind.PARAMETER) {
return NO_MATCH;
}
if (!isSameType(type, state.getSymtab().iterableType, state)) {
return NO_MATCH;
}
if (type.getTypeArguments().isEmpty()) {
return NO_MATCH;
}
if (!isSameType(wildBound(getOnlyElement(type.getTypeArguments())), state.getTypeFromString(Path.class.getName()), state)) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
if (tree.getType() instanceof ParameterizedTypeTree) {
description.addFix(SuggestedFix.builder().addImport("java.util.Collection").replace(((ParameterizedTypeTree) tree.getType()).getType(), "Collection").build());
}
return description.build();
}
use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.
the class BaseTypeVisitor method warnAboutIrrelevantJavaTypes.
/**
* Warns if a type annotation is written on a Java type that is not listed in
* the @RelevantJavaTypes annotation.
*
* @param annoTrees annotations written before a variable/method declaration, if this type is from
* one; null otherwise. This might contain type annotations that the Java parser attached to
* the declaration rather than to the type.
* @param typeTree the type that any type annotations in annoTrees apply to
*/
public void warnAboutIrrelevantJavaTypes(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) {
if (!shouldWarnAboutIrrelevantJavaTypes()) {
return;
}
Tree t = typeTree;
while (true) {
switch(t.getKind()) {
// Recurse for compound types whose top level is not at the far left.
case ARRAY_TYPE:
t = ((ArrayTypeTree) t).getType();
continue;
case MEMBER_SELECT:
t = ((MemberSelectTree) t).getExpression();
continue;
case PARAMETERIZED_TYPE:
t = ((ParameterizedTypeTree) t).getType();
continue;
// Base cases
case PRIMITIVE_TYPE:
case IDENTIFIER:
List<AnnotationTree> supportedAnnoTrees = supportedAnnoTrees(annoTrees);
if (!supportedAnnoTrees.isEmpty() && !atypeFactory.isRelevant(TreeUtils.typeOf(t))) {
checker.reportError(t, "anno.on.irrelevant", supportedAnnoTrees, t);
}
return;
case ANNOTATED_TYPE:
AnnotatedTypeTree at = (AnnotatedTypeTree) t;
ExpressionTree underlying = at.getUnderlyingType();
List<AnnotationTree> annos = supportedAnnoTrees(at.getAnnotations());
if (!annos.isEmpty() && !atypeFactory.isRelevant(TreeUtils.typeOf(underlying))) {
checker.reportError(t, "anno.on.irrelevant", annos, underlying);
}
return;
default:
return;
}
}
}
use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.
the class NullnessVisitor method visitAnnotatedType.
@Override
public void visitAnnotatedType(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) {
// Look for a MEMBER_SELECT or PRIMITIVE within the type.
Tree t = typeTree;
while (t != null) {
switch(t.getKind()) {
case MEMBER_SELECT:
Tree expr = ((MemberSelectTree) t).getExpression();
if (atypeFactory.containsNullnessAnnotation(annoTrees, expr)) {
checker.reportError(expr, "nullness.on.outer");
}
t = null;
break;
case PRIMITIVE_TYPE:
if (atypeFactory.containsNullnessAnnotation(annoTrees, t)) {
checker.reportError(t, "nullness.on.primitive");
}
t = null;
break;
case ANNOTATED_TYPE:
AnnotatedTypeTree at = ((AnnotatedTypeTree) t);
Tree underlying = at.getUnderlyingType();
if (underlying.getKind() == Tree.Kind.PRIMITIVE_TYPE) {
if (atypeFactory.containsNullnessAnnotation(null, at)) {
checker.reportError(t, "nullness.on.primitive");
}
t = null;
} else {
t = underlying;
}
break;
case ARRAY_TYPE:
t = ((ArrayTypeTree) t).getType();
break;
case PARAMETERIZED_TYPE:
t = ((ParameterizedTypeTree) t).getType();
break;
default:
t = null;
break;
}
}
super.visitAnnotatedType(annoTrees, typeTree);
}
use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.
the class BaseTypeValidator method visitDeclared.
@Override
public Void visitDeclared(AnnotatedDeclaredType type, Tree tree) {
if (visitedNodes.containsKey(type)) {
return visitedNodes.get(type);
}
final boolean skipChecks = checker.shouldSkipUses(type.getUnderlyingType().asElement());
if (checkTopLevelDeclaredOrPrimitiveType && !skipChecks) {
// Ensure that type use is a subtype of the element type
// isValidUse determines the erasure of the types.
Set<AnnotationMirror> bounds = atypeFactory.getTypeDeclarationBounds(type.getUnderlyingType());
AnnotatedDeclaredType elemType = type.deepCopy();
elemType.clearPrimaryAnnotations();
elemType.addAnnotations(bounds);
if (!visitor.isValidUse(elemType, type, tree)) {
reportInvalidAnnotationsOnUse(type, tree);
}
}
// Set checkTopLevelDeclaredType to true, because the next time visitDeclared is called,
// the type isn't the top level, so always do the check.
checkTopLevelDeclaredOrPrimitiveType = true;
if (TreeUtils.isClassTree(tree)) {
visitedNodes.put(type, null);
visitClassTypeParameters(type, (ClassTree) tree);
return null;
}
/*
* Try to reconstruct the ParameterizedTypeTree from the given tree.
* TODO: there has to be a nicer way to do this...
*/
Pair<ParameterizedTypeTree, AnnotatedDeclaredType> p = extractParameterizedTypeTree(tree, type);
ParameterizedTypeTree typeArgTree = p.first;
type = p.second;
if (typeArgTree == null) {
return super.visitDeclared(type, tree);
}
// else
// We put this here because we don't want to put it in visitedNodes before calling
// super (in the else branch) because that would cause the super implementation
// to detect that we've already visited type and to immediately return.
visitedNodes.put(type, null);
// We have a ParameterizedTypeTree -> visit it.
visitParameterizedType(type, typeArgTree);
/*
* Instead of calling super with the unchanged "tree", adapt the
* second argument to be the corresponding type argument tree. This
* ensures that the first and second parameter to this method always
* correspond. visitDeclared is the only method that had this
* problem.
*/
List<? extends AnnotatedTypeMirror> tatypes = type.getTypeArguments();
if (tatypes == null) {
return null;
}
// May be zero for a "diamond" (inferred type args in constructor invocation).
int numTypeArgs = typeArgTree.getTypeArguments().size();
if (numTypeArgs != 0) {
// but I didn't manage to reduce it to a test case.
assert tatypes.size() <= numTypeArgs || skipChecks : "size mismatch for type arguments: " + type + " and " + typeArgTree;
for (int i = 0; i < tatypes.size(); ++i) {
scan(tatypes.get(i), typeArgTree.getTypeArguments().get(i));
}
}
return null;
}
use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.
the class TypeFromTypeTreeVisitor method visitParameterizedType.
@Override
public AnnotatedTypeMirror visitParameterizedType(ParameterizedTypeTree node, AnnotatedTypeFactory f) {
ClassSymbol baseType = (ClassSymbol) TreeUtils.elementFromTree(node.getType());
updateWildcardBounds(node.getTypeArguments(), baseType.getTypeParameters());
List<AnnotatedTypeMirror> args = CollectionsPlume.mapList((Tree t) -> visit(t, f), node.getTypeArguments());
// use creator?
AnnotatedTypeMirror result = f.type(node);
AnnotatedTypeMirror atype = visit(node.getType(), f);
result.addAnnotations(atype.getAnnotations());
// diamond which should be inferred.
if (result instanceof AnnotatedDeclaredType && !args.isEmpty()) {
assert result instanceof AnnotatedDeclaredType : node + " --> " + result;
((AnnotatedDeclaredType) result).setTypeArguments(args);
}
return result;
}
Aggregations