Class ClassIndex


  • public class ClassIndex
    extends java.lang.Object
    Access to the compile-time generated index of classes.

    Use @IndexAnnotated and @IndexSubclasses annotations to force the classes to be indexed.

    Keep in mind that the class is indexed only when it is compiled with classindex.jar file in classpath.

    Also to preserve class-index data when creating shaded jar you should use the following Maven configuration:

     
     <build>
       <plugins>
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <version>1.4</version>
           <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>shade</goal>
               </goals>
               <configuration>
                 <transformers>
                   <transformer implementation="org.atteo.classindex.ClassIndexTransformer"/>
                 </transformers>
               </configuration>
             </execution>
           </executions>
           <dependencies>
             <groupId>org.atteo.classindex</groupId>
             <artifactId>classindex-transformer</artifactId>
           </dependencies>
         </plugin>
       </plugins>
     </build>
     
     

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ClassIndex()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void findClasses​(java.lang.ClassLoader classLoader, java.util.Set<java.lang.Class<?>> classes, java.lang.Iterable<java.lang.String> entries)  
      private static void findClassesInPackage​(java.lang.ClassLoader classLoader, java.lang.String packageName, java.util.Set<java.lang.Class<?>> classes, java.lang.Iterable<java.lang.String> entries)  
      static java.lang.Iterable<java.lang.Class<?>> getAnnotated​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
      Retrieves a list of classes annotated by given annotation.
      static java.lang.Iterable<java.lang.Class<?>> getAnnotated​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.ClassLoader classLoader)
      Retrieves a list of classes annotated by given annotation.
      static java.lang.Iterable<java.lang.String> getAnnotatedNames​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
      Retrieves names of classes annotated by given annotation.
      static java.lang.Iterable<java.lang.String> getAnnotatedNames​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, java.lang.ClassLoader classLoader)
      Retrieves names of classes annotated by given annotation.
      static java.lang.String getClassSummary​(java.lang.Class<?> klass)
      Returns the Javadoc summary for given class.
      static java.lang.String getClassSummary​(java.lang.Class<?> klass, java.lang.ClassLoader classLoader)
      Returns the Javadoc summary for given class.
      static java.lang.Iterable<java.lang.Class<?>> getPackageClasses​(java.lang.String packageName)
      Retrieves a list of classes from given package.
      static java.lang.Iterable<java.lang.Class<?>> getPackageClasses​(java.lang.String packageName, java.lang.ClassLoader classLoader)
      Retrieves a list of classes from given package.
      static java.lang.Iterable<java.lang.String> getPackageClassesNames​(java.lang.String packageName)
      Retrieves names of classes from given package.
      static java.lang.Iterable<java.lang.String> getPackageClassesNames​(java.lang.String packageName, java.lang.ClassLoader classLoader)
      Retrieves names of classes from given package.
      static <T> java.lang.Iterable<java.lang.Class<? extends T>> getSubclasses​(java.lang.Class<T> superClass)
      Retrieves a list of subclasses of the given class.
      static <T> java.lang.Iterable<java.lang.Class<? extends T>> getSubclasses​(java.lang.Class<T> superClass, java.lang.ClassLoader classLoader)
      Retrieves a list of subclasses of the given class.
      static <T> java.lang.Iterable<java.lang.String> getSubclassesNames​(java.lang.Class<T> superClass)
      Retrieves names of subclasses of the given class.
      static <T> java.lang.Iterable<java.lang.String> getSubclassesNames​(java.lang.Class<T> superClass, java.lang.ClassLoader classLoader)
      Retrieves names of subclasses of the given class.
      private static java.lang.Iterable<java.lang.String> readIndexFile​(java.lang.ClassLoader classLoader, java.lang.String resourceFile)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SUBCLASS_INDEX_PREFIX

        public static final java.lang.String SUBCLASS_INDEX_PREFIX
        See Also:
        Constant Field Values
      • ANNOTATED_INDEX_PREFIX

        public static final java.lang.String ANNOTATED_INDEX_PREFIX
        See Also:
        Constant Field Values
      • PACKAGE_INDEX_NAME

        public static final java.lang.String PACKAGE_INDEX_NAME
        See Also:
        Constant Field Values
    • Constructor Detail

      • ClassIndex

        private ClassIndex()
    • Method Detail

      • getSubclasses

        public static <T> java.lang.Iterable<java.lang.Class<? extends T>> getSubclasses​(java.lang.Class<T> superClass)
        Retrieves a list of subclasses of the given class.

        The class must be annotated with IndexSubclasses for it's subclasses to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        superClass - class to find subclasses for
        Returns:
        list of subclasses
      • getSubclasses

        public static <T> java.lang.Iterable<java.lang.Class<? extends T>> getSubclasses​(java.lang.Class<T> superClass,
                                                                                         java.lang.ClassLoader classLoader)
        Retrieves a list of subclasses of the given class.

        The class must be annotated with IndexSubclasses for it's subclasses to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        superClass - class to find subclasses for
        classLoader - classloader for loading classes
        Returns:
        list of subclasses
      • getSubclassesNames

        public static <T> java.lang.Iterable<java.lang.String> getSubclassesNames​(java.lang.Class<T> superClass)
        Retrieves names of subclasses of the given class.

        The class must be annotated with IndexSubclasses for it's subclasses to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        superClass - class to find subclasses for
        Returns:
        names of subclasses
      • getSubclassesNames

        public static <T> java.lang.Iterable<java.lang.String> getSubclassesNames​(java.lang.Class<T> superClass,
                                                                                  java.lang.ClassLoader classLoader)
        Retrieves names of subclasses of the given class.

        The class must be annotated with IndexSubclasses for it's subclasses to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        superClass - class to find subclasses for
        classLoader - classloader for loading index file
        Returns:
        names of subclasses
      • getPackageClasses

        public static java.lang.Iterable<java.lang.Class<?>> getPackageClasses​(java.lang.String packageName)
        Retrieves a list of classes from given package.

        The package must be annotated with IndexSubclasses for the classes inside to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        packageName - name of the package to search classes for
        Returns:
        list of classes from package
      • getPackageClasses

        public static java.lang.Iterable<java.lang.Class<?>> getPackageClasses​(java.lang.String packageName,
                                                                               java.lang.ClassLoader classLoader)
        Retrieves a list of classes from given package.

        The package must be annotated with IndexSubclasses for the classes inside to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        packageName - name of the package to search classes for
        classLoader - classloader for loading classes
        Returns:
        list of classes from package
      • getPackageClassesNames

        public static java.lang.Iterable<java.lang.String> getPackageClassesNames​(java.lang.String packageName)
        Retrieves names of classes from given package.

        The package must be annotated with IndexSubclasses for the classes inside to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        packageName - name of the package to search classes for
        Returns:
        names of classes from package
      • getPackageClassesNames

        public static java.lang.Iterable<java.lang.String> getPackageClassesNames​(java.lang.String packageName,
                                                                                  java.lang.ClassLoader classLoader)
        Retrieves names of classes from given package.

        The package must be annotated with IndexSubclasses for the classes inside to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        packageName - name of the package to search classes for
        classLoader - classloader for loading index file
        Returns:
        names of classes from package
      • getAnnotated

        public static java.lang.Iterable<java.lang.Class<?>> getAnnotated​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Retrieves a list of classes annotated by given annotation.

        The annotation must be annotated with IndexAnnotated for annotated classes to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        annotation - annotation to search class for
        Returns:
        list of annotated classes
      • getAnnotated

        public static java.lang.Iterable<java.lang.Class<?>> getAnnotated​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
                                                                          java.lang.ClassLoader classLoader)
        Retrieves a list of classes annotated by given annotation.

        The annotation must be annotated with IndexAnnotated for annotated classes to be indexed at compile-time by ClassIndexProcessor.

        Parameters:
        annotation - annotation to search class for
        classLoader - classloader for loading classes
        Returns:
        list of annotated classes
      • getAnnotatedNames

        public static java.lang.Iterable<java.lang.String> getAnnotatedNames​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Retrieves names of classes annotated by given annotation.

        The annotation must be annotated with IndexAnnotated for annotated classes to be indexed at compile-time by ClassIndexProcessor.

        Please note there is no verification if the class really exists. It can be missing when incremental compilation is used. Use getAnnotated(Class) if you need the verification.

        Parameters:
        annotation - annotation to search class for
        Returns:
        names of annotated classes
      • getAnnotatedNames

        public static java.lang.Iterable<java.lang.String> getAnnotatedNames​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
                                                                             java.lang.ClassLoader classLoader)
        Retrieves names of classes annotated by given annotation.

        The annotation must be annotated with IndexAnnotated for annotated classes to be indexed at compile-time by ClassIndexProcessor.

        Please note there is no verification if the class really exists. It can be missing when incremental compilation is used. Use getAnnotated(Class, ClassLoader) if you need the verification.

        Parameters:
        annotation - annotation to search class for
        classLoader - classloader for loading the index file
        Returns:
        names of annotated classes
      • getClassSummary

        public static java.lang.String getClassSummary​(java.lang.Class<?> klass)
        Returns the Javadoc summary for given class.

        Javadoc summary is the first sentence of a Javadoc.

        You need to use IndexSubclasses or IndexAnnotated with IndexAnnotated.storeJavadoc() set to true.

        Parameters:
        klass - class to retrieve summary for
        Returns:
        summary for given class, or null if it does not exists
        See Also:
        Writing doc comments
      • getClassSummary

        public static java.lang.String getClassSummary​(java.lang.Class<?> klass,
                                                       java.lang.ClassLoader classLoader)
        Returns the Javadoc summary for given class.

        Javadoc summary is the first sentence of a Javadoc.

        You need to use IndexSubclasses or IndexAnnotated with IndexAnnotated.storeJavadoc() set to true.

        Parameters:
        klass - class to retrieve summary for
        classLoader - classloader for loading classes
        Returns:
        summary for given class, or null if it does not exists
        See Also:
        Writing doc comments
      • readIndexFile

        private static java.lang.Iterable<java.lang.String> readIndexFile​(java.lang.ClassLoader classLoader,
                                                                          java.lang.String resourceFile)
      • findClasses

        private static void findClasses​(java.lang.ClassLoader classLoader,
                                        java.util.Set<java.lang.Class<?>> classes,
                                        java.lang.Iterable<java.lang.String> entries)
      • findClassesInPackage

        private static void findClassesInPackage​(java.lang.ClassLoader classLoader,
                                                 java.lang.String packageName,
                                                 java.util.Set<java.lang.Class<?>> classes,
                                                 java.lang.Iterable<java.lang.String> entries)