Package org.apache.xbean.finder
Class AnnotationFinder
- java.lang.Object
-
- org.apache.xbean.finder.AnnotationFinder
-
- All Implemented Interfaces:
IAnnotationFinder
public class AnnotationFinder extends java.lang.Object implements IAnnotationFinder
ClassFinder searches the classpath of the specified classloader for packages, classes, constructors, methods, or fields with specific annotations. For security reasons ASM is used to find the annotations. Classes are not loaded unless they match the requirements of a called findAnnotated* method. Once loaded, these classes are cached.- Version:
- $Rev: 1881759 $ $Date: 2020-09-16 10:29:43 +0200 (Wed, 16 Sep 2020) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
AnnotationFinder.Annotatable
class
AnnotationFinder.AnnotationInfo
class
AnnotationFinder.ClassInfo
class
AnnotationFinder.FieldInfo
static interface
AnnotationFinder.Info
class
AnnotationFinder.InfoBuildingVisitor
class
AnnotationFinder.MethodInfo
class
AnnotationFinder.PackageInfo
class
AnnotationFinder.ParameterInfo
class
AnnotationFinder.SubArchive
-
Field Summary
Fields Modifier and Type Field Description private static boolean
ALLOW_LAZY_LINKING
protected java.util.Map<java.lang.String,java.util.List<AnnotationFinder.Info>>
annotated
private Archive
archive
private static int
ASM_FLAGS
private boolean
checkRuntimeAnnotation
private java.util.List<java.lang.String>
classesNotLoaded
protected java.util.Map<java.lang.String,AnnotationFinder.ClassInfo>
classInfos
private boolean
linking
private java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>>
metaroots
protected java.util.Map<java.lang.String,AnnotationFinder.ClassInfo>
originalInfos
-
Constructor Summary
Constructors Modifier Constructor Description private
AnnotationFinder(AnnotationFinder parent, java.lang.Iterable<java.lang.String> classNames)
AnnotationFinder(Archive archive)
AnnotationFinder(Archive archive, boolean checkRuntimeAnnotation)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private <T> java.util.List<java.lang.Class<? extends T>>
_findSubclasses(java.lang.Class<T> clazz)
protected boolean
cleanOnNaked()
private java.util.List<AnnotationFinder.ClassInfo>
collectImplementations(java.lang.String interfaceName)
AnnotationFinder
enableFindImplementations()
AnnotationFinder
enableFindSubclasses()
AnnotationFinder
enableMetaAnnotations()
java.util.List<java.lang.Class<?>>
findAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<Parameter<java.lang.reflect.Constructor<?>>>
findAnnotatedConstructorParameters(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<java.lang.reflect.Constructor>
findAnnotatedConstructors(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<java.lang.reflect.Field>
findAnnotatedFields(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<Parameter<java.lang.reflect.Method>>
findAnnotatedMethodParameters(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<java.lang.reflect.Method>
findAnnotatedMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<java.lang.Package>
findAnnotatedPackages(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
java.util.List<java.lang.Class<?>>
findClassesInPackage(java.lang.String packageName, boolean recursive)
<T> java.util.List<java.lang.Class<? extends T>>
findImplementations(java.lang.Class<T> clazz)
java.util.List<java.lang.Class<?>>
findInheritedAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Naive implementation - works extremelly slow O(n^3)java.util.List<Annotated<java.lang.Class<?>>>
findMetaAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
private java.util.Set<java.lang.Class<?>>
findMetaAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.util.Set<java.lang.Class<?>> classes)
java.util.List<Annotated<java.lang.reflect.Field>>
findMetaAnnotatedFields(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
private java.util.Set<java.lang.reflect.Field>
findMetaAnnotatedFields(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.util.Set<java.lang.reflect.Field> fields, java.util.Set<java.lang.String> seen)
java.util.List<Annotated<java.lang.reflect.Method>>
findMetaAnnotatedMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
private java.util.Set<java.lang.reflect.Method>
findMetaAnnotatedMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.util.Set<java.lang.reflect.Method> methods, java.util.Set<java.lang.String> seen)
<T> java.util.List<java.lang.Class<? extends T>>
findSubclasses(java.lang.Class<T> clazz)
private <T> void
findSubclasses(AnnotationFinder.ClassInfo classInfo, java.util.List<java.lang.Class<? extends T>> found, java.lang.Class<T> clazz)
java.util.List<java.lang.String>
getAnnotatedClassNames()
protected java.util.List<AnnotationFinder.Info>
getAnnotationInfos(java.lang.String name)
Archive
getArchive()
java.util.List<java.lang.String>
getClassesNotLoaded()
Returns a list of classes that could not be loaded in last invoked findAnnotated* method.boolean
hasMetaAnnotations()
private boolean
hasName(java.lang.String className, java.lang.String simpleName)
private void
index(java.util.List<? extends AnnotationFinder.Info> infos)
private void
index(AnnotationFinder.AnnotationInfo annotationInfo, AnnotationFinder.Info info)
private void
index(AnnotationFinder.Info i)
protected java.util.List<AnnotationFinder.Info>
initAnnotationInfos(java.lang.String name)
boolean
isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
protected boolean
isJvm(java.lang.String name)
private static boolean
isMetaAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
private boolean
isMetaRoot(AnnotationFinder.ClassInfo classInfo)
private static boolean
isMetatypeAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> type)
private static boolean
isSelfAnnotated(java.lang.Class<? extends java.lang.annotation.Annotation> type, java.lang.String name)
private boolean
isSelfAnnotated(AnnotationFinder.ClassInfo classInfo, java.lang.String metatype)
protected boolean
isTracked(java.lang.String annotationType)
AnnotationFinder
link()
The link() method must be called to successfully use the findSubclasses and findImplementations methodsprotected void
linkInterfaces(AnnotationFinder.ClassInfo classInfo)
private void
linkMetaAnnotations()
protected void
linkParent(AnnotationFinder.ClassInfo classInfo)
protected java.util.Map<java.lang.String,java.util.List<AnnotationFinder.Info>>
newAnnotatedMap()
protected java.util.Map<java.lang.String,AnnotationFinder.ClassInfo>
newClassInfoMap()
protected void
readClassDef(java.lang.Class clazz)
protected void
readClassDef(java.lang.String className)
protected void
readClassDef(java.lang.String className, java.io.InputStream in)
private void
readClassDef(AnnotationFinder.ClassInfo info)
private void
resolveAnnotations(java.util.List<java.lang.String> scanned)
Used to support meta annotationsprivate void
resolveAnnotations(AnnotationFinder parent, java.util.List<java.lang.String> scanned)
AnnotationFinder
select(java.lang.Class<?>... clazz)
AnnotationFinder
select(java.lang.Iterable<java.lang.String> clazz)
AnnotationFinder
select(java.lang.String... clazz)
private static boolean
validTarget(java.lang.Class<? extends java.lang.annotation.Annotation> type)
-
-
-
Field Detail
-
ASM_FLAGS
private static final int ASM_FLAGS
- See Also:
- Constant Field Values
-
ALLOW_LAZY_LINKING
private static final boolean ALLOW_LAZY_LINKING
-
metaroots
private final java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> metaroots
-
annotated
protected final java.util.Map<java.lang.String,java.util.List<AnnotationFinder.Info>> annotated
-
classInfos
protected final java.util.Map<java.lang.String,AnnotationFinder.ClassInfo> classInfos
-
originalInfos
protected final java.util.Map<java.lang.String,AnnotationFinder.ClassInfo> originalInfos
-
classesNotLoaded
private final java.util.List<java.lang.String> classesNotLoaded
-
archive
private final Archive archive
-
checkRuntimeAnnotation
private final boolean checkRuntimeAnnotation
-
linking
private volatile boolean linking
-
-
Constructor Detail
-
AnnotationFinder
private AnnotationFinder(AnnotationFinder parent, java.lang.Iterable<java.lang.String> classNames)
-
AnnotationFinder
public AnnotationFinder(Archive archive, boolean checkRuntimeAnnotation)
- Parameters:
archive
-checkRuntimeAnnotation
- Has no effect on findMetaAnnotated* methods
-
AnnotationFinder
public AnnotationFinder(Archive archive)
-
-
Method Detail
-
newAnnotatedMap
protected java.util.Map<java.lang.String,java.util.List<AnnotationFinder.Info>> newAnnotatedMap()
-
newClassInfoMap
protected java.util.Map<java.lang.String,AnnotationFinder.ClassInfo> newClassInfoMap()
-
cleanOnNaked
protected boolean cleanOnNaked()
-
isTracked
protected boolean isTracked(java.lang.String annotationType)
-
hasMetaAnnotations
public boolean hasMetaAnnotations()
-
readClassDef
private void readClassDef(AnnotationFinder.ClassInfo info)
-
resolveAnnotations
private void resolveAnnotations(AnnotationFinder parent, java.util.List<java.lang.String> scanned)
-
index
private void index(java.util.List<? extends AnnotationFinder.Info> infos)
-
index
private void index(AnnotationFinder.Info i)
-
getAnnotatedClassNames
public java.util.List<java.lang.String> getAnnotatedClassNames()
- Specified by:
getAnnotatedClassNames
in interfaceIAnnotationFinder
-
getArchive
public Archive getArchive()
-
link
public AnnotationFinder link()
The link() method must be called to successfully use the findSubclasses and findImplementations methods- Returns:
- Throws:
java.io.IOException
-
enableMetaAnnotations
public AnnotationFinder enableMetaAnnotations()
-
enableFindImplementations
public AnnotationFinder enableFindImplementations()
-
enableFindSubclasses
public AnnotationFinder enableFindSubclasses()
-
resolveAnnotations
private void resolveAnnotations(java.util.List<java.lang.String> scanned)
Used to support meta annotations Once the list of classes has been read from the Archive, we iterate over all the annotations that are used by those classes and recursively resolve any annotations those annotations use.- Parameters:
scanned
-- Throws:
java.lang.ClassNotFoundException
java.io.IOException
-
linkMetaAnnotations
private void linkMetaAnnotations()
-
isMetaRoot
private boolean isMetaRoot(AnnotationFinder.ClassInfo classInfo)
-
isSelfAnnotated
private boolean isSelfAnnotated(AnnotationFinder.ClassInfo classInfo, java.lang.String metatype)
-
hasName
private boolean hasName(java.lang.String className, java.lang.String simpleName)
-
linkParent
protected void linkParent(AnnotationFinder.ClassInfo classInfo)
-
isJvm
protected boolean isJvm(java.lang.String name)
-
linkInterfaces
protected void linkInterfaces(AnnotationFinder.ClassInfo classInfo)
-
isAnnotationPresent
public boolean isAnnotationPresent(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
isAnnotationPresent
in interfaceIAnnotationFinder
-
getClassesNotLoaded
public java.util.List<java.lang.String> getClassesNotLoaded()
Returns a list of classes that could not be loaded in last invoked findAnnotated* method. The list will only contain entries of classes whose byte code matched the requirements of last invoked find* method, but were unable to be loaded and included in the results. The list returned is unmodifiable. Once obtained, the returned list will be a live view of the results from the last findAnnotated* method call. This method is not thread safe.- Specified by:
getClassesNotLoaded
in interfaceIAnnotationFinder
- Returns:
- an unmodifiable live view of classes that could not be loaded in previous findAnnotated* call.
-
findAnnotatedPackages
public java.util.List<java.lang.Package> findAnnotatedPackages(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findAnnotatedPackages
in interfaceIAnnotationFinder
-
findAnnotatedClasses
public java.util.List<java.lang.Class<?>> findAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findAnnotatedClasses
in interfaceIAnnotationFinder
-
findMetaAnnotatedClasses
public java.util.List<Annotated<java.lang.Class<?>>> findMetaAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findMetaAnnotatedClasses
in interfaceIAnnotationFinder
-
isMetaAnnotation
private static boolean isMetaAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> clazz)
-
isMetatypeAnnotation
private static boolean isMetatypeAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> type)
-
isSelfAnnotated
private static boolean isSelfAnnotated(java.lang.Class<? extends java.lang.annotation.Annotation> type, java.lang.String name)
-
validTarget
private static boolean validTarget(java.lang.Class<? extends java.lang.annotation.Annotation> type)
-
findMetaAnnotatedClasses
private java.util.Set<java.lang.Class<?>> findMetaAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.util.Set<java.lang.Class<?>> classes)
-
findInheritedAnnotatedClasses
public java.util.List<java.lang.Class<?>> findInheritedAnnotatedClasses(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Naive implementation - works extremelly slow O(n^3)- Specified by:
findInheritedAnnotatedClasses
in interfaceIAnnotationFinder
- Parameters:
annotation
-- Returns:
- list of directly or indirectly (inherited) annotated classes
-
findAnnotatedMethods
public java.util.List<java.lang.reflect.Method> findAnnotatedMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findAnnotatedMethods
in interfaceIAnnotationFinder
-
findAnnotatedMethodParameters
public java.util.List<Parameter<java.lang.reflect.Method>> findAnnotatedMethodParameters(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
-
findMetaAnnotatedMethods
public java.util.List<Annotated<java.lang.reflect.Method>> findMetaAnnotatedMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findMetaAnnotatedMethods
in interfaceIAnnotationFinder
-
findMetaAnnotatedMethods
private java.util.Set<java.lang.reflect.Method> findMetaAnnotatedMethods(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.util.Set<java.lang.reflect.Method> methods, java.util.Set<java.lang.String> seen)
-
findMetaAnnotatedFields
public java.util.List<Annotated<java.lang.reflect.Field>> findMetaAnnotatedFields(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findMetaAnnotatedFields
in interfaceIAnnotationFinder
-
findMetaAnnotatedFields
private java.util.Set<java.lang.reflect.Field> findMetaAnnotatedFields(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.util.Set<java.lang.reflect.Field> fields, java.util.Set<java.lang.String> seen)
-
findAnnotatedConstructors
public java.util.List<java.lang.reflect.Constructor> findAnnotatedConstructors(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findAnnotatedConstructors
in interfaceIAnnotationFinder
-
findAnnotatedConstructorParameters
public java.util.List<Parameter<java.lang.reflect.Constructor<?>>> findAnnotatedConstructorParameters(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
-
findAnnotatedFields
public java.util.List<java.lang.reflect.Field> findAnnotatedFields(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
- Specified by:
findAnnotatedFields
in interfaceIAnnotationFinder
-
findClassesInPackage
public java.util.List<java.lang.Class<?>> findClassesInPackage(java.lang.String packageName, boolean recursive)
- Specified by:
findClassesInPackage
in interfaceIAnnotationFinder
-
findSubclasses
public <T> java.util.List<java.lang.Class<? extends T>> findSubclasses(java.lang.Class<T> clazz)
- Specified by:
findSubclasses
in interfaceIAnnotationFinder
-
findSubclasses
private <T> void findSubclasses(AnnotationFinder.ClassInfo classInfo, java.util.List<java.lang.Class<? extends T>> found, java.lang.Class<T> clazz)
-
_findSubclasses
private <T> java.util.List<java.lang.Class<? extends T>> _findSubclasses(java.lang.Class<T> clazz)
-
findImplementations
public <T> java.util.List<java.lang.Class<? extends T>> findImplementations(java.lang.Class<T> clazz)
- Specified by:
findImplementations
in interfaceIAnnotationFinder
-
collectImplementations
private java.util.List<AnnotationFinder.ClassInfo> collectImplementations(java.lang.String interfaceName)
-
getAnnotationInfos
protected java.util.List<AnnotationFinder.Info> getAnnotationInfos(java.lang.String name)
-
initAnnotationInfos
protected java.util.List<AnnotationFinder.Info> initAnnotationInfos(java.lang.String name)
-
readClassDef
protected void readClassDef(java.lang.String className)
-
readClassDef
protected void readClassDef(java.lang.String className, java.io.InputStream in) throws java.io.IOException
- Throws:
java.io.IOException
-
readClassDef
protected void readClassDef(java.lang.Class clazz)
-
select
public AnnotationFinder select(java.lang.Class<?>... clazz)
-
select
public AnnotationFinder select(java.lang.String... clazz)
-
select
public AnnotationFinder select(java.lang.Iterable<java.lang.String> clazz)
-
index
private void index(AnnotationFinder.AnnotationInfo annotationInfo, AnnotationFinder.Info info)
-
-