public class BaseTypeValidator extends AnnotatedTypeScanner<Void,Tree> implements TypeValidator
Note: A TypeValidator (this class and its subclasses) cannot tell whether an annotation was
written by a programmer or defaulted/inferred/computed by the Checker Framework, because the
AnnotatedTypeMirror does not make distinctions about which annotations in an AnnotatedTypeMirror
were explicitly written and which were added by a checker. To issue a warning/error only when a
programmer writes an annotation, override BaseTypeVisitor.visitAnnotatedType(com.sun.source.tree.AnnotatedTypeTree, java.lang.Void)
and BaseTypeVisitor.visitVariable(com.sun.source.tree.VariableTree, java.lang.Void)
.
AnnotatedTypeScanner.Reduce<R>
Modifier and Type | Field and Description |
---|---|
protected AnnotatedTypeFactory |
atypeFactory
AnnotatedTypeFactory.
|
protected BaseTypeChecker |
checker
BaseTypeChecker.
|
protected boolean |
checkTopLevelDeclaredOrPrimitiveType
Should the primary annotation on the top level type be checked?
|
protected boolean |
isValid
Is the type valid? This is side-effected by the visitor, and read at the end of visiting.
|
protected BaseTypeVisitor<?> |
visitor
BaseTypeVisitor.
|
defaultResult, reduceFunction, visitedNodes
Constructor and Description |
---|
BaseTypeValidator(BaseTypeChecker checker,
BaseTypeVisitor<?> visitor,
AnnotatedTypeFactory atypeFactory) |
Modifier and Type | Method and Description |
---|---|
boolean |
areBoundsValid(AnnotatedTypeMirror upperBound,
AnnotatedTypeMirror lowerBound)
Returns true if the effective annotations on the upperBound are above those on the lowerBound.
|
protected List<DiagMessage> |
isTopLevelValidType(QualifierHierarchy qualifierHierarchy,
AnnotatedTypeMirror type)
Checks every property listed in
isValidStructurally(org.checkerframework.framework.type.QualifierHierarchy, org.checkerframework.framework.type.AnnotatedTypeMirror) , but only for the top level type. |
boolean |
isValid(AnnotatedTypeMirror type,
Tree tree)
Validate the type against the given tree.
|
protected List<DiagMessage> |
isValidStructurally(QualifierHierarchy qualifierHierarchy,
AnnotatedTypeMirror type)
Performs some well-formedness checks on the given
AnnotatedTypeMirror . |
protected void |
reportInvalidAnnotationsOnUse(AnnotatedTypeMirror type,
Tree p)
Report an "annotations.on.use" error for the given type and tree.
|
protected void |
reportInvalidBounds(AnnotatedTypeMirror type,
Tree tree)
Most errors reported by this class are of the form type.invalid.
|
protected void |
reportInvalidType(AnnotatedTypeMirror type,
Tree p) |
protected void |
reportValidityResult(@CompilerMessageKey String errorType,
AnnotatedTypeMirror type,
Tree p) |
protected void |
reportValidityResultOnUnannotatedType(@CompilerMessageKey String errorType,
AnnotatedTypeMirror type,
Tree p)
Like
reportValidityResult(java.lang.String, org.checkerframework.framework.type.AnnotatedTypeMirror, com.sun.source.tree.Tree) , but the type is printed in the error message without
annotations. |
protected boolean |
shouldCheckTopLevelDeclaredOrPrimitiveType(AnnotatedTypeMirror type,
Tree tree)
Should the top-level declared or primitive type be checked?
|
Void |
visitArray(AnnotatedTypeMirror.AnnotatedArrayType type,
Tree tree)
Visits an array type.
|
protected void |
visitClassTypeParameters(AnnotatedTypeMirror.AnnotatedDeclaredType type,
ClassTree tree)
Visits the type parameters of a class tree.
|
Void |
visitDeclared(AnnotatedTypeMirror.AnnotatedDeclaredType type,
Tree tree)
Visits a declared type.
|
protected Void |
visitParameterizedType(AnnotatedTypeMirror.AnnotatedDeclaredType type,
ParameterizedTypeTree tree)
Checks that the annotations on the type arguments supplied to a type or a method invocation are
within the bounds of the type variables as declared, and issues the "type.argument" error if
they are not.
|
Void |
visitPrimitive(AnnotatedTypeMirror.AnnotatedPrimitiveType type,
Tree tree)
Visits a primitive type.
|
protected void |
visitTypeParameterBounds(AnnotatedTypeMirror.AnnotatedTypeVariable typeParameter,
TypeParameterTree typeParameterTree)
Visits type parameter bounds.
|
Void |
visitTypeVariable(AnnotatedTypeMirror.AnnotatedTypeVariable type,
Tree tree)
Visits a type variable.
|
Void |
visitWildcard(AnnotatedTypeMirror.AnnotatedWildcardType type,
Tree tree)
Visits a wildcard type.
|
reduce, reset, scan, scan, scanAndReduce, scanAndReduce, visit, visit, visitExecutable, visitIntersection, visitNoType, visitNull, visitUnion
protected boolean isValid
protected boolean checkTopLevelDeclaredOrPrimitiveType
protected final BaseTypeChecker checker
protected final BaseTypeVisitor<?> visitor
protected final AnnotatedTypeFactory atypeFactory
public BaseTypeValidator(BaseTypeChecker checker, BaseTypeVisitor<?> visitor, AnnotatedTypeFactory atypeFactory)
public boolean isValid(AnnotatedTypeMirror type, Tree tree)
This is the entry point to the type validator. Neither this method nor visit should be
called directly by a visitor, only use BaseTypeVisitor.validateTypeOf(Tree)
.
This method is only called on top-level types, but it validates the entire type including components of a compound type. Subclasses should override this only if there is special-case behavior that should be performed only on top-level types.
isValid
in interface TypeValidator
type
- the type to validatetree
- the tree from which the type originated. If the tree is a method tree, type
is its return type. If the tree is a variable tree, type
is the variable's type.protected boolean shouldCheckTopLevelDeclaredOrPrimitiveType(AnnotatedTypeMirror type, Tree tree)
If type
is not a declared or primitive type, then this method returns true.
Top-level type is not checked if tree is a local variable or an expression tree.
type
- AnnotatedTypeMirror being validatedtree
- a Tree whose type is type
type
is a declared or
primitive type.protected List<DiagMessage> isValidStructurally(QualifierHierarchy qualifierHierarchy, AnnotatedTypeMirror type)
AnnotatedTypeMirror
. Returns a list
of failures. If successful, returns an empty list. The method will never return failures for a
valid type, but might not catch all invalid types.
This method ensures that the type is structurally or lexically well-formed, but it does not
check whether the annotations are semantically sensible. Subclasses should generally override
visit methods such as visitDeclared(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType, com.sun.source.tree.Tree)
rather than this method.
Currently, this implementation checks the following (subclasses can extend this behavior):
qualifierHierarchy
- the QualifierHierarchytype
- the type to testprotected List<DiagMessage> isTopLevelValidType(QualifierHierarchy qualifierHierarchy, AnnotatedTypeMirror type)
isValidStructurally(org.checkerframework.framework.type.QualifierHierarchy, org.checkerframework.framework.type.AnnotatedTypeMirror)
, but only for the top level type.
If successful, returns an empty list. If not successful, returns diagnostics.qualifierHierarchy
- the QualifierHierarchytype
- the type to be checkedprotected void reportValidityResult(@CompilerMessageKey String errorType, AnnotatedTypeMirror type, Tree p)
protected void reportValidityResultOnUnannotatedType(@CompilerMessageKey String errorType, AnnotatedTypeMirror type, Tree p)
reportValidityResult(java.lang.String, org.checkerframework.framework.type.AnnotatedTypeMirror, com.sun.source.tree.Tree)
, but the type is printed in the error message without
annotations. This method would print "annotation @NonNull is not permitted on type int",
whereas reportValidityResult(java.lang.String, org.checkerframework.framework.type.AnnotatedTypeMirror, com.sun.source.tree.Tree)
would print "annotation @NonNull is not permitted on
type @NonNull int". In addition, when the underlying type is a compound type such as
@Bad List<String>
, the erased type will be used, i.e., "List
" will print
instead of "@Bad List<String>
".protected void reportInvalidBounds(AnnotatedTypeMirror type, Tree tree)
type
- the type with invalid boundstree
- where to report the errorprotected void reportInvalidType(AnnotatedTypeMirror type, Tree p)
protected void reportInvalidAnnotationsOnUse(AnnotatedTypeMirror type, Tree p)
type
- the type with invalid annotationsp
- the tree where to report the errorpublic Void visitDeclared(AnnotatedTypeMirror.AnnotatedDeclaredType type, Tree tree)
AnnotatedTypeVisitor
visitDeclared
in interface AnnotatedTypeVisitor<Void,Tree>
visitDeclared
in class AnnotatedTypeScanner<Void,Tree>
type
- the type to visittree
- a visitor-specified parameterprotected void visitClassTypeParameters(AnnotatedTypeMirror.AnnotatedDeclaredType type, ClassTree tree)
type
- type of tree
tree
- a class treeprotected void visitTypeParameterBounds(AnnotatedTypeMirror.AnnotatedTypeVariable typeParameter, TypeParameterTree typeParameterTree)
typeParameter
- type of typeParameterTree
typeParameterTree
- a type parameter treepublic Void visitPrimitive(AnnotatedTypeMirror.AnnotatedPrimitiveType type, Tree tree)
AnnotatedTypeVisitor
visitPrimitive
in interface AnnotatedTypeVisitor<Void,Tree>
visitPrimitive
in class AnnotatedTypeScanner<Void,Tree>
type
- the type to visittree
- a visitor-specified parameterpublic Void visitArray(AnnotatedTypeMirror.AnnotatedArrayType type, Tree tree)
AnnotatedTypeVisitor
visitArray
in interface AnnotatedTypeVisitor<Void,Tree>
visitArray
in class AnnotatedTypeScanner<Void,Tree>
type
- the type to visittree
- a visitor-specified parameterprotected Void visitParameterizedType(AnnotatedTypeMirror.AnnotatedDeclaredType type, ParameterizedTypeTree tree)
type
- the type to checktree
- the type's treepublic Void visitTypeVariable(AnnotatedTypeMirror.AnnotatedTypeVariable type, Tree tree)
AnnotatedTypeVisitor
visitTypeVariable
in interface AnnotatedTypeVisitor<Void,Tree>
visitTypeVariable
in class AnnotatedTypeScanner<Void,Tree>
type
- the type to visittree
- a visitor-specified parameterpublic Void visitWildcard(AnnotatedTypeMirror.AnnotatedWildcardType type, Tree tree)
AnnotatedTypeVisitor
visitWildcard
in interface AnnotatedTypeVisitor<Void,Tree>
visitWildcard
in class AnnotatedTypeScanner<Void,Tree>
type
- the type to visittree
- a visitor-specified parameterpublic boolean areBoundsValid(AnnotatedTypeMirror upperBound, AnnotatedTypeMirror lowerBound)