Class QualifierDefaults
DefaultQualifier annotation.
Type variable uses have two possible defaults. If flow sensitive type refinement is enabled,
unannotated top-level type variable uses receive the same default as local variables. All other
type variable uses are defaulted using the TYPE_VARIABLE_USE default.
<T> void method(USE T tIn) {
LOCAL T t = tIn;
}
The parameter tIn will be defaulted using the TYPE_VARIABLE_USE default. The
local variable t will be defaulted using the LOCAL_VARIABLE default, in order to
allow dataflow to refine T.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static enumSpecifies whether the type variable or wildcard has an explicit upper bound (UPPER), an explicit lower bound (LOWER), or no explicit bounds (UNBOUNDED).protected classA default applier element.protected classThe implementation of default application as an annotated type scanner. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final List<TypeUseLocation> Conservative unchecked default locations that should be bottom.static final List<TypeUseLocation> Conservative unchecked default locations that should be top.protected final @Nullable ExecutableElementThe applyToSubpackages() element/field of a @DefaultQualifier annotation.protected final ExecutableElementThe locations() element/field of a @DefaultQualifier annotation.protected final ExecutableElementThe value() element/field of a @DefaultQualifier annotation.protected final IdentityHashMap<Element, QualifierDefaults.BoundType> Mapping from an Element to the bound type.static final List<TypeUseLocation> Permissive unchecked default locations that should be bottom.static final List<TypeUseLocation> Permissive unchecked default locations that should be top.static final List<TypeUseLocation> CLIMB locations whose standard default is bottom for a given type system.static final List<TypeUseLocation> CLIMB locations whose standard default is top for a given type system. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCheckedCodeDefault(AnnotationMirror absoluteDefaultAnno, TypeUseLocation location) Adds a default annotation for checked code.voidaddCheckedCodeDefault(AnnotationMirror absoluteDefaultAnno, TypeUseLocation location, boolean applyToSubpackages) Deprecated.voidaddCheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Adds a default annotation, at each of the given locations.voidAdd standard CLIMB defaults that do not conflict with previously added defaults.voidAdds the conservative defaults for unchecked code, which-AuseConservativeDefaultsForUncheckedCode(e.g., withsourceorbytecode) enables: the top qualifier atCONSERVATIVE_UNCHECKED_DEFAULTS_TOPand the bottom qualifier atCONSERVATIVE_UNCHECKED_DEFAULTS_BOTTOM, at each location the checker has not already given a conservative default.voidaddConservativeUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location) Add a conservative default annotation for unchecked elements.voidaddConservativeUncheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Adds a conservative default annotation for unchecked elements, at each of the given locations.voidaddElementDefault(Element elem, AnnotationMirror elementDefaultAnno, TypeUseLocation location) Sets the default annotations for a certain Element.voidaddPackageDefault(PackageElement pkg, AnnotationMirror packageDefaultAnno, TypeUseLocation location, boolean applyToSubpackages) Sets the default annotations for a package, likeaddElementDefault(javax.lang.model.element.Element, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation), and says whether the default also applies to the package's subpackages, like theapplyToSubpackageselement of@DefaultQualifier.voidAdds the permissive defaults for unchecked code, which-AusePermissiveDefaultsForUncheckedCode(e.g., withsourceorbytecode) enables: the top qualifier atPERMISSIVE_UNCHECKED_DEFAULTS_TOPand the bottom qualifier atPERMISSIVE_UNCHECKED_DEFAULTS_BOTTOM, at each location the checker has not already given a permissive default.voidaddPermissiveUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location) Add a permissive default annotation for unchecked elements.voidaddPermissiveUncheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Adds a permissive default annotation for unchecked elements, at each of the given locations.voidaddUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location) voidaddUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location, boolean applyToSubpackages) voidaddUncheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) voidAdds the defaults for unchecked code of each mode the command-line options enable:addConservativeDefaultsForUncheckedCode()andaddPermissiveDefaultsForUncheckedCode().voidannotate(Tree tree, AnnotatedTypeMirror type) Applies default annotations to a type given aTree.voidannotate(Element elt, AnnotatedTypeMirror type) Applies default annotations to a type obtained from anElement.booleanapplyConservativeDefaults(Element annotationScope) Given an element, returns true if the conservative default should be applied for it.booleanapplyPermissiveDefaults(Element annotationScope) Given an element, returns whether the permissive default should be applied for it.voidcheckConflictingDefaults(Element elt, Tree tree) Reports each pair ofelt's own written@DefaultQualifierannotations that set the sameTypeUseLocationin the same qualifier hierarchy to different qualifiers.voidcheckDefaultQualifiers(Element elt, Tree tree) Checks @DefaultQualifier annotations oneltfor conflicting defaults and for locations prohibited by the qualifier'sTargetLocationsmeta-annotation.voidcheckTargetLocations(Element elt, Tree tree) Reports an error for each written @DefaultQualifier annotation oneltthat specifies aTypeUseLocationprohibited by the qualifier'sTargetLocationsmeta-annotation.protected QualifierDefaults.DefaultApplierElementcreateDefaultApplierElement(AnnotatedTypeFactory atypeFactory, Element annotationScope, AnnotatedTypeMirror type, boolean fromTree) Create the default applier element.booleanCheck that a default with TypeUseLocation OTHERWISE or ALL is specified.booleanpermittedAtLocation(AnnotationMirror anno, TypeUseLocation location) booleanpermittedAtProgrammaticLocation(AnnotationMirror anno, TypeUseLocation location) Returns true ifannomay be applied atlocationas a programmatic default (e.g., viaaddCheckedCodeDefault(javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation),addUncheckedCodeDefault(org.checkerframework.framework.util.defaults.QualifierDefaults.UncheckedDefaultsMode, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation),addElementDefault(javax.lang.model.element.Element, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation), oraddPackageDefault(javax.lang.model.element.PackageElement, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation, boolean)).toString()static List<TypeUseLocation> Returns an array of locations that are valid for the unchecked value defaults.
-
Field Details
-
defaultQualifierValueElement
The value() element/field of a @DefaultQualifier annotation. -
defaultQualifierLocationsElement
The locations() element/field of a @DefaultQualifier annotation. -
defaultQualifierApplyToSubpackagesElement
The applyToSubpackages() element/field of a @DefaultQualifier annotation. Null if the version of@DefaultQualifieron the classpath predates this element. -
elementToBoundType
Mapping from an Element to the bound type. -
STANDARD_CLIMB_DEFAULTS_TOP
CLIMB locations whose standard default is top for a given type system. -
STANDARD_CLIMB_DEFAULTS_BOTTOM
CLIMB locations whose standard default is bottom for a given type system. -
CONSERVATIVE_UNCHECKED_DEFAULTS_TOP
Conservative unchecked default locations that should be top. -
CONSERVATIVE_UNCHECKED_DEFAULTS_BOTTOM
Conservative unchecked default locations that should be bottom. -
PERMISSIVE_UNCHECKED_DEFAULTS_TOP
Permissive unchecked default locations that should be top. These are the mirror image ofCONSERVATIVE_UNCHECKED_DEFAULTS_TOP: a call into unchecked code may pass anything, so its parameters are assumed to accept anything. -
PERMISSIVE_UNCHECKED_DEFAULTS_BOTTOM
Permissive unchecked default locations that should be bottom. A value obtained from unchecked code is assumed to satisfy any requirement, so no error is issued at its use.
-
-
Constructor Details
-
QualifierDefaults
- Parameters:
elements- interface to Element data in the current processing environmentatypeFactory- an annotation factory, used to get annotations by name
-
-
Method Details
-
validLocationsForUncheckedCodeDefaults
Returns an array of locations that are valid for the unchecked value defaults. These are simply by syntax, since an entire file is typechecked, it is not possible for local variables to be unchecked. -
toString
-
hasDefaultsForCheckedCode
public boolean hasDefaultsForCheckedCode()Check that a default with TypeUseLocation OTHERWISE or ALL is specified.- Returns:
- true if we found a Default with location OTHERWISE or ALL
-
addUncheckedStandardDefaults
public void addUncheckedStandardDefaults()Adds the defaults for unchecked code of each mode the command-line options enable:addConservativeDefaultsForUncheckedCode()andaddPermissiveDefaultsForUncheckedCode(). -
addConservativeDefaultsForUncheckedCode
public void addConservativeDefaultsForUncheckedCode()Adds the conservative defaults for unchecked code, which-AuseConservativeDefaultsForUncheckedCode(e.g., withsourceorbytecode) enables: the top qualifier atCONSERVATIVE_UNCHECKED_DEFAULTS_TOPand the bottom qualifier atCONSERVATIVE_UNCHECKED_DEFAULTS_BOTTOM, at each location the checker has not already given a conservative default. To add a checker-specific default instead, useaddConservativeUncheckedCodeDefault(javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation). -
addPermissiveDefaultsForUncheckedCode
public void addPermissiveDefaultsForUncheckedCode()Adds the permissive defaults for unchecked code, which-AusePermissiveDefaultsForUncheckedCode(e.g., withsourceorbytecode) enables: the top qualifier atPERMISSIVE_UNCHECKED_DEFAULTS_TOPand the bottom qualifier atPERMISSIVE_UNCHECKED_DEFAULTS_BOTTOM, at each location the checker has not already given a permissive default. To add a checker-specific default instead, useaddPermissiveUncheckedCodeDefault(javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation). -
permittedAtLocation
Doesanno'sTargetLocationsmeta-annotation permit it atlocation?A qualifier can restrict where it may be written via
TargetLocations. Defaulting a qualifier onto a prohibited location causes type validation errors on code the user did not write.- Parameters:
anno- a qualifierlocation- a type use location- Returns:
- true if
annomay be applied atlocation
-
permittedAtProgrammaticLocation
Returns true ifannomay be applied atlocationas a programmatic default (e.g., viaaddCheckedCodeDefault(javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation),addUncheckedCodeDefault(org.checkerframework.framework.util.defaults.QualifierDefaults.UncheckedDefaultsMode, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation),addElementDefault(javax.lang.model.element.Element, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation), oraddPackageDefault(javax.lang.model.element.PackageElement, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation, boolean)).If
annocarries aProgrammaticDefaultLocationsmeta-annotation, its locations are checked. Otherwise, falls back topermittedAtLocation(AnnotationMirror, TypeUseLocation).A qualifier may explicitly specify its allowed programmatic default locations via
ProgrammaticDefaultLocations. If omitted, top and bottom qualifiers in the qualifier hierarchy are permitted at all locations for programmatic defaults (as the canonical bounds of the lattice), while other qualifiers fall back topermittedAtLocation(javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation).- Parameters:
anno- the annotation mirror to checklocation- the location- Returns:
- true if
annomay be used as a programmatic default atlocation
-
addClimbStandardDefaults
public void addClimbStandardDefaults()Add standard CLIMB defaults that do not conflict with previously added defaults. -
addCheckedCodeDefault
Adds a default annotation for checked code. A programmer may override this by writing the@DefaultQualifierannotation on an element.A checked-code default applies to all checked code; it is not scoped to a package.
- Parameters:
absoluteDefaultAnno- the default annotation mirrorlocation- the type use location
-
addCheckedCodeDefault
@Deprecated public void addCheckedCodeDefault(AnnotationMirror absoluteDefaultAnno, TypeUseLocation location, boolean applyToSubpackages) Deprecated.applyToSubpackageshas no effect, because a checked-code default is not scoped to a package. UseaddCheckedCodeDefault(AnnotationMirror, TypeUseLocation), oraddPackageDefault(javax.lang.model.element.PackageElement, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation, boolean)for a default of one package that need not apply to its subpackages.Adds a default annotation for checked code.- Parameters:
absoluteDefaultAnno- the default annotation mirrorlocation- the type use locationapplyToSubpackages- ignored
-
addConservativeUncheckedCodeDefault
public void addConservativeUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location) Add a conservative default annotation for unchecked elements.- Parameters:
uncheckedDefaultAnno- the default annotation mirrorlocation- the type use location
-
addPermissiveUncheckedCodeDefault
public void addPermissiveUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location) Add a permissive default annotation for unchecked elements.- Parameters:
uncheckedDefaultAnno- the default annotation mirrorlocation- the type use location
-
addConservativeUncheckedCodeDefaults
public void addConservativeUncheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Adds a conservative default annotation for unchecked elements, at each of the given locations.- Parameters:
absoluteDefaultAnno- the default annotation mirrorlocations- the type use locations to apply the default to
-
addPermissiveUncheckedCodeDefaults
public void addPermissiveUncheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Adds a permissive default annotation for unchecked elements, at each of the given locations.- Parameters:
absoluteDefaultAnno- the default annotation mirrorlocations- the type use locations to apply the default to
-
addUncheckedCodeDefault
@Deprecated public void addUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location, boolean applyToSubpackages) Deprecated.Add an unchecked-code default annotation for unchecked elements.- Parameters:
uncheckedDefaultAnno- the default annotation mirrorlocation- the type use locationapplyToSubpackages- ignored, because an unchecked-code default is not scoped to a package
-
addUncheckedCodeDefault
@Deprecated public void addUncheckedCodeDefault(AnnotationMirror uncheckedDefaultAnno, TypeUseLocation location) Deprecated.Add an unchecked-code default annotation for unchecked elements.- Parameters:
uncheckedDefaultAnno- the default annotation mirrorlocation- the type use location
-
addUncheckedCodeDefaults
@Deprecated public void addUncheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Deprecated.Adds an unchecked-code default annotation for unchecked elements, at each of the given locations.- Parameters:
absoluteDefaultAnno- the default annotation mirrorlocations- the type use locations to apply the default to
-
addCheckedCodeDefaults
public void addCheckedCodeDefaults(AnnotationMirror absoluteDefaultAnno, TypeUseLocation[] locations) Adds a default annotation, at each of the given locations. A programmer may override it by writing the @DefaultQualifier annotation on an element.- Parameters:
absoluteDefaultAnno- the default annotation mirrorlocations- the type use locations to apply the default to
-
addElementDefault
public void addElementDefault(Element elem, AnnotationMirror elementDefaultAnno, TypeUseLocation location) Sets the default annotations for a certain Element.This default is combined with any written
@DefaultQualifierannotations on the element and inherits the defaults of enclosing elements, no matter in which order the defaults ofelem, of its enclosing elements, or of its members were queried while the type factory was being initialized.This is an initialization-time API: it must be called while the type factory is being created, such as from
GenericAnnotatedTypeFactory.createQualifierDefaults()orGenericAnnotatedTypeFactory.addCheckedCodeDefaults(org.checkerframework.framework.util.defaults.QualifierDefaults). Calling it after type checking has begun throws aTypeSystemError, because types that have already been computed and dataflow results that have already been produced are never recomputed, and diagnostics that have already been issued cannot be retracted, so the new default would apply to some of the program and not to the rest of it.If the registered default conflicts with a
@DefaultQualifierwritten onelem-- samelocationand same qualifier hierarchy, but a different qualifier -- then aTypeSystemErroris thrown later, whenelem's defaults are computed. Only one qualifier from a hierarchy can be the default for a location, so a type system must not register one that contradicts what a user is permitted to write.A default on a package also applies to its subpackages; use
addPackageDefault(javax.lang.model.element.PackageElement, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation, boolean)for one that does not.- Parameters:
elem- the scope to set the default withinelementDefaultAnno- the default to setlocation- the location to apply the default to- Throws:
TypeSystemError- if called after type checking has begun
-
addPackageDefault
public void addPackageDefault(PackageElement pkg, AnnotationMirror packageDefaultAnno, TypeUseLocation location, boolean applyToSubpackages) Sets the default annotations for a package, likeaddElementDefault(javax.lang.model.element.Element, javax.lang.model.element.AnnotationMirror, org.checkerframework.framework.qual.TypeUseLocation), and says whether the default also applies to the package's subpackages, like theapplyToSubpackageselement of@DefaultQualifier.- Parameters:
pkg- the package to set the default withinpackageDefaultAnno- the default to setlocation- the location to apply the default toapplyToSubpackages- whether the default also applies to the subpackages ofpkg- Throws:
TypeSystemError- if called after type checking has begun
-
checkDefaultQualifiers
Checks @DefaultQualifier annotations oneltfor conflicting defaults and for locations prohibited by the qualifier'sTargetLocationsmeta-annotation.- Parameters:
elt- a declaration in sourcetree- the declaration ofelt, on which the diagnostics are reported
-
checkTargetLocations
Reports an error for each written @DefaultQualifier annotation oneltthat specifies aTypeUseLocationprohibited by the qualifier'sTargetLocationsmeta-annotation.- Parameters:
elt- an element that may carry @DefaultQualifier annotationstree- the declaration ofelt, on which the errors are reported
-
checkConflictingDefaults
Reports each pair ofelt's own written@DefaultQualifierannotations that set the sameTypeUseLocationin the same qualifier hierarchy to different qualifiers.Called by the visitor for a declaration in source, so that the diagnostic does not depend on whether anything happened to ask for
elt's defaults:defaultsAtDirect(javax.lang.model.element.Element)runs on a cache miss, and for a package whosepackage-info.javais the only file compiled it never runs at all. The winner is decided by source order, as it is there.- Parameters:
elt- a declaration in sourcetree- the declaration ofelt, on which the errors are reported
-
annotate
Applies default annotations to a type obtained from anElement.- Parameters:
elt- the element from which the type was obtainedtype- the type to annotate
-
annotate
Applies default annotations to a type given aTree.- Parameters:
tree- the tree from which the type was obtainedtype- the type to annotate
-
applyPermissiveDefaults
Given an element, returns whether the permissive default should be applied for it. Handles elements from bytecode or source code.At most one of this and
applyConservativeDefaults(javax.lang.model.element.Element)returns true for an element: a kind of code cannot be defaulted both permissively and conservatively, whichSourceCheckerrejects at startup.- Parameters:
annotationScope- the element that the permissive default might apply to- Returns:
- whether the permissive default applies to the given element
-
applyConservativeDefaults
Given an element, returns true if the conservative default should be applied for it. Handles elements from bytecode or source code.- Parameters:
annotationScope- the element that the conservative default might apply to- Returns:
- true if the conservative default applies to the given element
-
createDefaultApplierElement
protected QualifierDefaults.DefaultApplierElement createDefaultApplierElement(AnnotatedTypeFactory atypeFactory, Element annotationScope, AnnotatedTypeMirror type, boolean fromTree) Create the default applier element.- Parameters:
atypeFactory- the annotated type factoryannotationScope- the scope of the defaulttype- the type to which to apply the defaultfromTree- whether the element came from a tree- Returns:
- the default applier element
-
applyToSubpackageshas no effect, because a checked-code default is not scoped to a package.