Class BaseTypeValidator
- All Implemented Interfaces:
TypeValidator,AnnotatedTypeVisitor<Void,Tree>
- Direct Known Subclasses:
ReportVisitor.ReportTypeValidator
The validator is called on the type of every expression, such as on the right-hand side of
x = Optional.of(Optional.of("baz"));. However, note that the type of the right-hand side
is Optional<? extends Object>, not Optional<Optional<String>>.
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).
-
Nested Class Summary
Nested classes/interfaces inherited from class org.checkerframework.framework.type.visitor.AnnotatedTypeScanner
AnnotatedTypeScanner.Reduce<R> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final AnnotatedTypeFactoryAnnotatedTypeFactory.protected final BaseTypeCheckerBaseTypeChecker.protected booleanShould the primary annotation on the top level type be checked?protected final booleanTrue if "-AignoreTargetLocations" was passed on the command line.protected booleanIs the type valid? This is side-effected by the visitor, and read at the end of visiting.protected final booleanTrue if no supported qualifier in this type system declaresTargetLocations.protected final Map<@CanonicalName String, List<TypeUseLocation>> Mapping from qualifier canonical names to their declared type-use locations, as specified by theTargetLocationsmeta-annotation.protected final QualifierHierarchyThe qualifer hierarchy.protected final BaseTypeVisitor<?> BaseTypeVisitor.Fields inherited from class org.checkerframework.framework.type.visitor.AnnotatedTypeScanner
defaultResult, reduceFunction, VISITED_NODES_INITIAL_CAPACITY -
Constructor Summary
ConstructorsConstructorDescriptionBaseTypeValidator(BaseTypeChecker checker, BaseTypeVisitor<?> visitor, AnnotatedTypeFactory atypeFactory) Creates a new BaseTypeValidator. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanareBoundsValid(AnnotatedTypeMirror upperBound, AnnotatedTypeMirror lowerBound) Returns true if the effective annotations on the upperBound are above (or equal to) those on the lowerBound.protected voidcheckCapturedWildcardBounds(AnnotatedTypeMirror.AnnotatedDeclaredType type, AnnotatedTypeMirror.AnnotatedDeclaredType capturedType, ParameterizedTypeTree tree) Rechecks, for every wildcard type argument whose capture is a captured type variable, that the upper bound of the captured type variable is a subtype of the extends bound of the wildcard, and issues the "type.argument.type.incompatible" error if it is not.protected List<DiagMessage> Checks every property listed inisValidStructurally(org.checkerframework.framework.type.AnnotatedTypeMirror), but only for the top level type.booleanisValid(AnnotatedTypeMirror type, Tree tree) Validate the type against the given tree.protected List<DiagMessage> Performs some well-formedness checks on the givenAnnotatedTypeMirror.protected voidReport an "annotations.on.use" error for the given type and tree.protected voidreportInvalidBounds(AnnotatedTypeMirror type, Tree tree) Most errors reported by this class are of the form type.invalid.protected voidreportInvalidType(AnnotatedTypeMirror type, Tree p) protected voidreportValidityResult(@CompilerMessageKey String errorType, AnnotatedTypeMirror type, Tree p) protected voidreportValidityResultOnUnannotatedType(@CompilerMessageKey String errorType, AnnotatedTypeMirror type, Tree p) LikereportValidityResult(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 booleanShould the top-level declared or primitive type be checked?voidvalidateTargetLocation(AnnotatedTypeMirror type, Tree tree, TypeUseLocation required) Validates whether the qualifiers on the tree are at the correct type-use locations, as specified by the meta-annotationTargetLocations.protected voidValidates whether the qualifiers on the type parameter bounds are at the correct type-use locations, as specified by the meta-annotationTargetLocations.voidvalidateVariableTargetLocation(AnnotatedTypeMirror type, Tree tree) Validates whether the qualifiers on the tree are at the correct type-use locations, as specified by the meta-annotationTargetLocations.protected voidValidates whether the qualifiers on the wildcard tree are at the correct type-use locations, as specified by the meta-annotationTargetLocations.visitArray(AnnotatedTypeMirror.AnnotatedArrayType type, Tree tree) Visits an array type.protected voidVisits the type parameters of a class tree.Visits a declared type.protected VoidChecks 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.type.incompatible" error if they are not.Visits a primitive type.protected voidvisitTypeParameterBounds(AnnotatedTypeMirror.AnnotatedTypeVariable typeParameter, TypeParameterTree typeParameterTree) Visits type parameter bounds.Visits a type variable.Visits a wildcard type.Methods inherited from class org.checkerframework.framework.type.visitor.AnnotatedTypeScanner
getVisited, hasVisited, markVisited, reduce, reset, scan, scan, scanAndReduce, scanAndReduce, visit, visit, visitExecutable, visitIntersection, visitNoType, visitNull, visitUnion
-
Field Details
-
isValid
protected boolean isValidIs the type valid? This is side-effected by the visitor, and read at the end of visiting. -
checkTopLevelDeclaredOrPrimitiveType
protected boolean checkTopLevelDeclaredOrPrimitiveTypeShould the primary annotation on the top level type be checked? -
checker
BaseTypeChecker. -
visitor
BaseTypeVisitor. -
atypeFactory
AnnotatedTypeFactory. -
qualHierarchy
The qualifer hierarchy. -
ignoreTargetLocations
protected final boolean ignoreTargetLocationsTrue if "-AignoreTargetLocations" was passed on the command line. -
qualAllowedLocations
Mapping from qualifier canonical names to their declared type-use locations, as specified by theTargetLocationsmeta-annotation. -
noQualHasTargetLocations
protected final boolean noQualHasTargetLocationsTrue if no supported qualifier in this type system declaresTargetLocations. Used to short-circuit target-location checks.
-
-
Constructor Details
-
BaseTypeValidator
public BaseTypeValidator(BaseTypeChecker checker, BaseTypeVisitor<?> visitor, AnnotatedTypeFactory atypeFactory) Creates a new BaseTypeValidator.- Parameters:
checker- the checkervisitor- the visitoratypeFactory- the type factory
-
-
Method Details
-
isValid
Validate the type against the given tree. This method both issues error messages and also returns a boolean value.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.
- Specified by:
isValidin interfaceTypeValidator- Parameters:
type- the type to validatetree- the tree from which the type originated. If the tree is a method tree,typeis its return type. If the tree is a variable tree,typeis the variable's type.- Returns:
- true if the type is valid
-
shouldCheckTopLevelDeclaredOrPrimitiveType
Should the top-level declared or primitive type be checked?If
typeis 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.
- Parameters:
type- the AnnotatedTypeMirror being validatedtree- a Tree whose type istype- Returns:
- true if the top-level type should be checked, if
typeis a declared or primitive type
-
isValidStructurally
Performs some well-formedness checks on the givenAnnotatedTypeMirror. 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):
- There should not be multiple annotations from the same qualifier hierarchy.
- There should not be more annotations than the width of the QualifierHierarchy.
- If the type is not a type variable, then the number of annotations should be the same as the width of the QualifierHierarchy.
- These properties should also hold recursively for component types of arrays and for bounds of type variables and wildcards.
- Parameters:
type- the type to test- Returns:
- list of reasons the type is invalid, or empty list if the type is valid
-
isTopLevelValidType
Checks every property listed inisValidStructurally(org.checkerframework.framework.type.AnnotatedTypeMirror), but only for the top level type. If successful, returns an empty list. If not successful, returns diagnostics.- Parameters:
type- the type to be checked- Returns:
- the diagnostics indicating failure, or an empty list if successful
-
reportValidityResult
protected void reportValidityResult(@CompilerMessageKey String errorType, AnnotatedTypeMirror type, Tree p) -
reportValidityResultOnUnannotatedType
protected void reportValidityResultOnUnannotatedType(@CompilerMessageKey String errorType, AnnotatedTypeMirror type, Tree p) LikereportValidityResult(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", whereasreportValidityResult(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>". -
reportInvalidBounds
Most errors reported by this class are of the form type.invalid. This method reports when the bounds of a wildcard or type variable don't make sense. Bounds make sense when the effective annotations on the upper bound are supertypes of those on the lower bounds for all hierarchies. To ensure that this subtlety is not lost on users, we report "bound.type.incompatible" and print the bounds along with the invalid type rather than a "type.invalid".- Parameters:
type- the type with invalid boundstree- where to report the error
-
reportInvalidType
-
reportInvalidAnnotationsOnUse
Report an "annotations.on.use" error for the given type and tree.- Parameters:
type- the type with invalid annotationsp- the tree where to report the error
-
visitDeclared
Description copied from interface:AnnotatedTypeVisitorVisits a declared type.- Specified by:
visitDeclaredin interfaceAnnotatedTypeVisitor<Void,Tree> - Overrides:
visitDeclaredin classAnnotatedTypeScanner<Void,Tree> - Parameters:
type- the type to visittree- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitClassTypeParameters
protected void visitClassTypeParameters(AnnotatedTypeMirror.AnnotatedDeclaredType type, ClassTree tree) Visits the type parameters of a class tree.- Parameters:
type- type oftreetree- a class tree
-
visitTypeParameterBounds
protected void visitTypeParameterBounds(AnnotatedTypeMirror.AnnotatedTypeVariable typeParameter, TypeParameterTree typeParameterTree) Visits type parameter bounds.- Parameters:
typeParameter- type oftypeParameterTreetypeParameterTree- a type parameter tree
-
visitPrimitive
Description copied from interface:AnnotatedTypeVisitorVisits a primitive type.- Specified by:
visitPrimitivein interfaceAnnotatedTypeVisitor<Void,Tree> - Overrides:
visitPrimitivein classAnnotatedTypeScanner<Void,Tree> - Parameters:
type- the type to visittree- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitArray
Description copied from interface:AnnotatedTypeVisitorVisits an array type.- Specified by:
visitArrayin interfaceAnnotatedTypeVisitor<Void,Tree> - Overrides:
visitArrayin classAnnotatedTypeScanner<Void,Tree> - Parameters:
type- the type to visittree- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitParameterizedType
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.type.incompatible" error if they are not.- Parameters:
type- the type to checktree- the type's tree
-
checkCapturedWildcardBounds
protected void checkCapturedWildcardBounds(AnnotatedTypeMirror.AnnotatedDeclaredType type, AnnotatedTypeMirror.AnnotatedDeclaredType capturedType, ParameterizedTypeTree tree) Rechecks, for every wildcard type argument whose capture is a captured type variable, that the upper bound of the captured type variable is a subtype of the extends bound of the wildcard, and issues the "type.argument.type.incompatible" error if it is not.For most captured type variables, this will trivially hold, as capturing incorporated the extends bound of the wildcard into the upper bound of the type variable. This will fail if the bound and the wildcard have generic types and there is no appropriate glb, in which case the two bounds have contradictory requirements and no type can satisfy both.
Checkers with a nonstandard subtyping relationship (where this recheck can spuriously fail even though capture conversion itself succeeded) may override this method to do nothing.
- Parameters:
type- the (possibly unconverted) parameterized type being validatedcapturedType-typeafter capture conversiontree- the tree fortype
-
visitTypeVariable
Description copied from interface:AnnotatedTypeVisitorVisits a type variable.- Specified by:
visitTypeVariablein interfaceAnnotatedTypeVisitor<Void,Tree> - Overrides:
visitTypeVariablein classAnnotatedTypeScanner<Void,Tree> - Parameters:
type- the type to visittree- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitWildcard
Description copied from interface:AnnotatedTypeVisitorVisits a wildcard type.- Specified by:
visitWildcardin interfaceAnnotatedTypeVisitor<Void,Tree> - Overrides:
visitWildcardin classAnnotatedTypeScanner<Void,Tree> - Parameters:
type- the type to visittree- a visitor-specified parameter- Returns:
- a visitor-specified result
-
validateVariableTargetLocation
Validates whether the qualifiers on the tree are at the correct type-use locations, as specified by the meta-annotationTargetLocations.More specifically, this method only checks qualifiers on a VariableTree and thus checks for the following type-use locations: FIELD, LOCAL_VARIABLE, RESOURCE_VARIABLE, EXCEPTION_PARAMETER, PARAMETER, RECEIVER and CONSTRUCTOR_RESULT.
The other two validate methods achieve the same goal but perform checks on different trees and different type-use locations. This separation exists because variables can automatically infer their type-use location from their
ElementKind. By contrast, other constructs (like method returns or type bounds) have context-dependent locations that must be explicitly provided by the caller, and wildcards do not have an element. SeevalidateTargetLocation(AnnotatedTypeMirror, Tree, TypeUseLocation)andvalidateWildcardTargetLocations(AnnotatedWildcardType, Tree).- Specified by:
validateVariableTargetLocationin interfaceTypeValidator- Parameters:
type- the type of the treetree- the tree whose qualifiers are to be validated- See Also:
-
validateTypeParameterTargetLocations
protected void validateTypeParameterTargetLocations(AnnotatedTypeMirror.AnnotatedTypeVariable type, Tree tree) Validates whether the qualifiers on the type parameter bounds are at the correct type-use locations, as specified by the meta-annotationTargetLocations.More specifically, this method checks qualifiers on a TypeParameterTree for the
TypeUseLocation.UPPER_BOUNDandTypeUseLocation.LOWER_BOUNDlocations.- Parameters:
type- the type variable declaration whose bounds are to be validatedtree- the tree of this type parameter- See Also:
-
validateTargetLocation
Validates whether the qualifiers on the tree are at the correct type-use locations, as specified by the meta-annotationTargetLocations.This method checks qualifiers for context-dependent locations such as
TypeUseLocation.CONSTRUCTOR_RESULTandTypeUseLocation.RETURNon Method trees, or other caller-specified locations.The other validate methods achieve the same goal but perform checks on specific trees and their associated type-use locations:
validateVariableTargetLocation(AnnotatedTypeMirror, Tree),validateWildcardTargetLocations(AnnotatedWildcardType, Tree), andvalidateTypeParameterTargetLocations(AnnotatedTypeVariable, Tree).- Specified by:
validateTargetLocationin interfaceTypeValidator- Parameters:
type- the type of the treetree- the tree whose qualifiers are to be validatedrequired- the required TypeUseLocation. If it is not present in the specification of the meta-annotation (TargetLocations), issue an error.- See Also:
-
areBoundsValid
Returns true if the effective annotations on the upperBound are above (or equal to) those on the lowerBound.- Parameters:
upperBound- the upper bound to checklowerBound- the lower bound to check- Returns:
- true if the effective annotations on the upperBound are above (or equal to) those on the lowerBound
-
validateWildcardTargetLocations
protected void validateWildcardTargetLocations(AnnotatedTypeMirror.AnnotatedWildcardType type, Tree tree) Validates whether the qualifiers on the wildcard tree are at the correct type-use locations, as specified by the meta-annotationTargetLocations.More specifically, this method only checks qualifiers on a WildcardTree and thus checks for the following type-use locations: (EXPLICIT/IMPLICIT) LOWER_BOUND and (EXPLICIT/IMPLICIT) UPPER_BOUND.
The other two validate methods achieve the same goal but perform checks on different trees and different type-use locations. This separation exists because wildcards do not have an element and determine their locations based on their bounds. By contrast, variables can automatically infer their type-use location from their ElementKind, and other constructs have context-dependent locations that must be explicitly provided by the caller. See
validateVariableTargetLocation(AnnotatedTypeMirror, Tree)andvalidateTargetLocation(AnnotatedTypeMirror, Tree, TypeUseLocation).- Parameters:
type- the type to checktree- the tree of this type- See Also:
-