public class AnnotationClassLoader extends Object
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.
Modifier and Type | Field and Description |
---|---|
protected BaseTypeChecker |
checker
For issuing errors to the user.
|
protected URLClassLoader |
classLoader
The class loader used to load annotation classes.
|
protected ProcessingEnvironment |
processingEnv
Processing Env used to create an
AnnotationBuilder , which is in turn used to build
the annotation mirror from the loaded class. |
Constructor and Description |
---|
AnnotationClassLoader(BaseTypeChecker checker)
Constructor for loading annotations defined for a checker.
|
Modifier and Type | Method and Description |
---|---|
Set<Class<? extends Annotation>> |
getBundledAnnotationClasses()
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 the
Target meta-annotation,
and has the required ElementType values. |
protected boolean |
isSupportedAnnotationClass(Class<? extends Annotation> annoClass)
Checks to see whether a particular annotation class is supported.
|
protected @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 Set<Class<? extends Annotation>> |
loadAnnotationClasses(@Nullable Set<String> annoNames)
Loads a set of annotations indicated by their names.
|
@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.
|
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.
|
protected void |
printPaths()
Debug Use: Displays all classpaths examined by the class loader.
|
protected final BaseTypeChecker checker
protected final ProcessingEnvironment processingEnv
AnnotationBuilder
, which is in turn used to build
the annotation mirror from the loaded class.protected final URLClassLoader classLoader
public AnnotationClassLoader(BaseTypeChecker checker)
checker
- a BaseTypeChecker
or its subclassprotected final void printPaths()
public final Set<Class<? extends Annotation>> getBundledAnnotationClasses()
createSupportedTypeQualifiers()
(or its helper methods) to help define the set of supported
qualifiers.AnnotatedTypeFactory.createSupportedTypeQualifiers()
public final @Nullable Class<? extends Annotation> loadExternalAnnotationClass(@BinaryName String annoName)
annoName
- canonical name of an external annotation class, e.g.
"myproject.qual.myannotation"isSupportedAnnotationClass(Class)
public final Set<Class<? extends Annotation>> loadExternalAnnotationClassesFromDirectory(String dirName)
dirName
- absolute path to a directory containing annotation classesprotected final @Nullable Class<? extends Annotation> loadAnnotationClass(@BinaryName String className, boolean issueError)
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.@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.protected final Set<Class<? extends Annotation>> loadAnnotationClasses(@Nullable Set<String> annoNames)
annoNames
- a set of binary names for annotation classesloadAnnotationClass(String, boolean)
protected boolean hasWellDefinedTargetMetaAnnotation(Class<? extends Annotation> annoClass)
Target
meta-annotation,
and has the required ElementType
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 load Unqualified
.
annoClass
- an annotation classprotected boolean isSupportedAnnotationClass(Class<? extends Annotation> annoClass)
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.
annoClass
- an annotation class