Class AnnotationClassLoader
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
SubtypingAnnotationClassLoader
,UnitsAnnotationClassLoader
AnnotatedTypeFactory
by reflectively looking up the list of
annotation class names in each checker's qual directory, and then loading and returning it as a
set of annotation classes. It can also look up and load annotation classes from external
directories that are passed as arguments to checkers that have extension capabilities such as the
Subtyping Checker, Fenum Checker, and Units Checker.
To load annotations using this class, their directory structure and package structure must be identical.
Only annotation classes that have the Target
meta-annotation with the value of ElementType.TYPE_USE
(and optionally ElementType.TYPE_PARAMETER
) are loaded. If it has
other ElementType
values, it won't be loaded. Other annotation classes must be manually
listed in a checker's annotated type factory by overriding AnnotatedTypeFactory.createSupportedTypeQualifiers()
.
Checker writers may wish to subclass this class if they wish to implement some custom rules to
filter or process loaded annotation classes, by providing an override implementation of isSupportedAnnotationClass(Class)
. See
org.checkerframework.checker.units.UnitsAnnotationClassLoader
for an example.
-
Field Summary
Modifier and TypeFieldDescriptionprotected final BaseTypeChecker
For issuing errors to the user.protected final URLClassLoader
The class loader used to load annotation classes.protected final ProcessingEnvironment
Processing Env used to create anAnnotationBuilder
, which is in turn used to build the annotation mirror from the loaded class. -
Constructor Summary
ConstructorDescriptionAnnotationClassLoader
(BaseTypeChecker checker) Constructor for loading annotations defined for a checker. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
final Set<Class<? extends Annotation>>
Gets the set of annotation classes in the qual directory of a checker shipped with the Checker Framework.protected boolean
hasWellDefinedTargetMetaAnnotation
(Class<? extends Annotation> annoClass) Checks to see whether a particular annotation class has theTarget
meta-annotation, and has the requiredElementType
values.protected boolean
isSupportedAnnotationClass
(Class<? extends Annotation> annoClass) Checks to see whether a particular annotation class is supported.protected final @Nullable Class<? extends Annotation>
loadAnnotationClass
(@BinaryName String className, boolean issueError) Loads the class indicated by the name, and checks to see if it is an annotation that is supported by a checker.protected final Set<Class<? extends Annotation>>
loadAnnotationClasses
(@Nullable Set<@BinaryName String> annoNames) Loads a set of annotations indicated by their names.final @Nullable Class<? extends Annotation>
loadExternalAnnotationClass
(@BinaryName String annoName) This method takes as input the canonical name of an external annotation class and loads and returns that class via the class loader.final Set<Class<? extends Annotation>>
This method takes as input a fully qualified path to a directory, and loads and returns the set of all supported annotation classes from that directory.protected final void
Debug Use: Displays all classpaths examined by the class loader.
-
Field Details
-
checker
For issuing errors to the user. -
processingEnv
Processing Env used to create anAnnotationBuilder
, which is in turn used to build the annotation mirror from the loaded class. -
classLoader
The class loader used to load annotation classes.
-
-
Constructor Details
-
AnnotationClassLoader
Constructor for loading annotations defined for a checker.- Parameters:
checker
- aBaseTypeChecker
or its subclass
-
-
Method Details
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
printPaths
protected final void printPaths()Debug Use: Displays all classpaths examined by the class loader. -
getBundledAnnotationClasses
Gets the set of annotation classes in the qual directory of a checker shipped with the Checker Framework. Note that the returned set from this method is mutable. This method is intended to be called withincreateSupportedTypeQualifiers()
(or its helper methods) to help define the set of supported qualifiers.- Returns:
- a mutable set of the loaded bundled annotation classes
- See Also:
-
loadExternalAnnotationClass
public final @Nullable Class<? extends Annotation> loadExternalAnnotationClass(@BinaryName String annoName) This method takes as input the canonical name of an external annotation class and loads and returns that class via the class loader. This method returns null if the external annotation class was loaded successfully but was deemed not supported by a checker. Errors are issued if the external class is not an annotation, or if it could not be loaded successfully.- Parameters:
annoName
- canonical name of an external annotation class, e.g. "myproject.qual.myannotation"- Returns:
- the loaded annotation class, or null if it was not a supported annotation as decided by
isSupportedAnnotationClass(Class)
-
loadExternalAnnotationClassesFromDirectory
public final Set<Class<? extends Annotation>> loadExternalAnnotationClassesFromDirectory(String dirName) This method takes as input a fully qualified path to a directory, and loads and returns the set of all supported annotation classes from that directory.- Parameters:
dirName
- absolute path to a directory containing annotation classes- Returns:
- a set of annotation classes
-
loadAnnotationClass
protected final @Nullable Class<? extends Annotation> loadAnnotationClass(@BinaryName String className, boolean issueError) Loads the class indicated by the name, and checks to see if it is an annotation that is supported by a checker.- Parameters:
className
- the name of the class, in binary name formatissueError
- set to true to issue a warning when a loaded annotation is not a type annotation. It is useful to set this to true if a given annotation must be a well-defined type annotation (eg for annotation class names given as command line arguments). It should be set to false if the annotation is a meta-annotation or non-type annotation.- Returns:
- the loaded annotation class if it has a
@Target
meta-annotation with the required ElementType values, and is a supported annotation by a checker. If the annotation is not supported by a checker, null is returned.
-
loadAnnotationClasses
protected final Set<Class<? extends Annotation>> loadAnnotationClasses(@Nullable Set<@BinaryName String> annoNames) Loads a set of annotations indicated by their names.- Parameters:
annoNames
- a set of binary names for annotation classes- Returns:
- a set of loaded annotation classes
- See Also:
-
hasWellDefinedTargetMetaAnnotation
Checks to see whether a particular annotation class has theTarget
meta-annotation, and has the requiredElementType
values.A subclass may override this method to load annotations that are not intended to be annotated in source code. E.g.:
SubtypingChecker
overrides this method to loadUnqualified
.- Parameters:
annoClass
- an annotation class- Returns:
- true if the annotation is well defined, false if it isn't
-
isSupportedAnnotationClass
Checks to see whether a particular annotation class is supported.By default, all loaded annotations that pass the basic checks in
loadAnnotationClass(String, boolean)
are supported.Individual checkers can create a subclass of AnnotationClassLoader and override this method to indicate whether a particular annotation is supported.
- Parameters:
annoClass
- an annotation class- Returns:
- true if the annotation is supported, false if it isn't
-