Class 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) $
    • Field Detail

      • 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
      • 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()
      • cleanOnNaked

        protected boolean cleanOnNaked()
      • isTracked

        protected boolean isTracked​(java.lang.String annotationType)
      • hasMetaAnnotations

        public boolean hasMetaAnnotations()
      • resolveAnnotations

        private void resolveAnnotations​(AnnotationFinder parent,
                                        java.util.List<java.lang.String> scanned)
      • 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
      • enableFindImplementations

        public AnnotationFinder enableFindImplementations()
      • 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()
      • hasName

        private boolean hasName​(java.lang.String className,
                                java.lang.String simpleName)
      • isJvm

        protected boolean isJvm​(java.lang.String name)
      • isAnnotationPresent

        public boolean isAnnotationPresent​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Specified by:
        isAnnotationPresent in interface IAnnotationFinder
      • 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 interface IAnnotationFinder
        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 interface IAnnotationFinder
      • findAnnotatedClasses

        public java.util.List<java.lang.Class<?>> findAnnotatedClasses​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Specified by:
        findAnnotatedClasses in interface IAnnotationFinder
      • findMetaAnnotatedClasses

        public java.util.List<Annotated<java.lang.Class<?>>> findMetaAnnotatedClasses​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Specified by:
        findMetaAnnotatedClasses in interface IAnnotationFinder
      • 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 interface IAnnotationFinder
        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 interface IAnnotationFinder
      • 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 interface IAnnotationFinder
      • 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 interface IAnnotationFinder
      • 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 interface IAnnotationFinder
      • 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 interface IAnnotationFinder
      • findClassesInPackage

        public java.util.List<java.lang.Class<?>> findClassesInPackage​(java.lang.String packageName,
                                                                       boolean recursive)
        Specified by:
        findClassesInPackage in interface IAnnotationFinder
      • findSubclasses

        public <T> java.util.List<java.lang.Class<? extends T>> findSubclasses​(java.lang.Class<T> clazz)
        Specified by:
        findSubclasses in interface IAnnotationFinder
      • 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 interface IAnnotationFinder
      • 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.Iterable<java.lang.String> clazz)