Package org.jboss.jandex
Class Index
- java.lang.Object
-
- org.jboss.jandex.Index
-
- All Implemented Interfaces:
IndexView
public final class Index extends java.lang.Object implements IndexView
An index useful for quickly processing annotations. The index is read-only and supports concurrent access. Also the index is optimized for memory efficiency by using componentized DotName values.It contains the following information:
- All annotations and a collection of targets they refer to
- All classes (including methodParameters) scanned during the indexing process (typical all classes in a jar)
- All subclasses indexed by super class known to this index
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<DotName,java.util.List<AnnotationInstance>>
annotations
(package private) java.util.Map<DotName,ClassInfo>
classes
private static java.util.List<AnnotationInstance>
EMPTY_ANNOTATION_LIST
private static java.util.List<ClassInfo>
EMPTY_CLASSINFO_LIST
(package private) java.util.Map<DotName,java.util.List<ClassInfo>>
implementors
(package private) java.util.Map<DotName,ModuleInfo>
modules
(package private) static DotName
REPEATABLE
(package private) java.util.Map<DotName,java.util.List<ClassInfo>>
subclasses
(package private) java.util.Map<DotName,java.util.List<ClassInfo>>
users
-
Constructor Summary
Constructors Constructor Description Index(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, java.util.Map<DotName,java.util.List<ClassInfo>> subclasses, java.util.Map<DotName,java.util.List<ClassInfo>> implementors, java.util.Map<DotName,ClassInfo> classes, java.util.Map<DotName,ModuleInfo> modules, java.util.Map<DotName,java.util.List<ClassInfo>> users)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Index
create(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, java.util.Map<DotName,java.util.List<ClassInfo>> subclasses, java.util.Map<DotName,java.util.List<ClassInfo>> implementors, java.util.Map<DotName,ClassInfo> classes)
Constructs a "mock" Index using the passed values.static Index
create(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, java.util.Map<DotName,java.util.List<ClassInfo>> subclasses, java.util.Map<DotName,java.util.List<ClassInfo>> implementors, java.util.Map<DotName,ClassInfo> classes, java.util.Map<DotName,java.util.List<ClassInfo>> users)
Constructs a "mock" Index using the passed values.java.util.Set<ClassInfo>
getAllKnownImplementors(DotName interfaceName)
Returns all known classes that implement the given interface, directly and indirectly.java.util.Collection<ClassInfo>
getAllKnownSubclasses(DotName className)
Returns all known (including non-direct) sub classes of the given class.private void
getAllKnownSubClasses(DotName className, java.util.Set<ClassInfo> allKnown, java.util.Set<DotName> processedClasses)
private void
getAllKnownSubClasses(DotName name, java.util.Set<ClassInfo> allKnown, java.util.Set<DotName> subClassesToProcess, java.util.Set<DotName> processedClasses)
java.util.List<AnnotationInstance>
getAnnotations(DotName annotationName)
Obtains a list of instances for the specified annotation.java.util.Collection<AnnotationInstance>
getAnnotationsWithRepeatable(DotName annotationName, IndexView index)
Obtains a list of instances for the specified annotation.ClassInfo
getClassByName(DotName className)
Gets the class (or interface, or annotation) that was scanned during the indexing phase.java.util.Collection<ClassInfo>
getKnownClasses()
Gets all known classes by this index (those which were scanned).java.util.List<ClassInfo>
getKnownDirectImplementors(DotName className)
Gets all known direct implementors of the specified interface name.java.util.List<ClassInfo>
getKnownDirectSubclasses(DotName className)
Gets all known direct subclasses of the specified class name.private void
getKnownImplementors(DotName name, java.util.Set<ClassInfo> allKnown, java.util.Set<DotName> subInterfacesToProcess, java.util.Set<DotName> processedClasses)
java.util.Collection<ModuleInfo>
getKnownModules()
Gets all known modules by this index (those which were scanned).java.util.List<ClassInfo>
getKnownUsers(DotName className)
Obtains a list of classes that use the specified class.ModuleInfo
getModuleByName(DotName moduleName)
Gets the module that was scanned during the indexing phase.private java.util.Collection<AnnotationInstance>
getRepeatableAnnotations(DotName annotationName, DotName containingAnnotationName)
static Index
of(java.io.File... directories)
Constructs an Index of class files found in the passed directories.static Index
of(java.lang.Class<?>... classes)
Constructs an Index of the passed classes.static Index
of(java.lang.Iterable<java.lang.Class<?>> classes)
Constructs an Index of the passed classes.void
printAnnotations()
Print all annotations known by this index to stdout.void
printSubclasses()
Print all classes that have known subclasses, and all their subclasses
-
-
-
Field Detail
-
EMPTY_ANNOTATION_LIST
private static final java.util.List<AnnotationInstance> EMPTY_ANNOTATION_LIST
-
EMPTY_CLASSINFO_LIST
private static final java.util.List<ClassInfo> EMPTY_CLASSINFO_LIST
-
REPEATABLE
static final DotName REPEATABLE
-
annotations
final java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations
-
modules
final java.util.Map<DotName,ModuleInfo> modules
-
-
Constructor Detail
-
Index
Index(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, java.util.Map<DotName,java.util.List<ClassInfo>> subclasses, java.util.Map<DotName,java.util.List<ClassInfo>> implementors, java.util.Map<DotName,ClassInfo> classes, java.util.Map<DotName,ModuleInfo> modules, java.util.Map<DotName,java.util.List<ClassInfo>> users)
-
-
Method Detail
-
create
public static Index create(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, java.util.Map<DotName,java.util.List<ClassInfo>> subclasses, java.util.Map<DotName,java.util.List<ClassInfo>> implementors, java.util.Map<DotName,ClassInfo> classes)
Constructs a "mock" Index using the passed values. All passed values MUST NOT BE MODIFIED AFTER THIS CALL. Otherwise the resulting object would not conform to the contract outlined above. Also, to conform to the memory efficiency contract this method should be passed componentized DotNames, which all share common root instances. Of course for testing code this doesn't really matter.- Parameters:
annotations
- A map to lookup annotation instances by class namesubclasses
- A map to lookup subclasses by super class nameimplementors
- A map to lookup implementing classes by interface nameclasses
- A map to lookup classes by class name- Returns:
- the index
-
create
public static Index create(java.util.Map<DotName,java.util.List<AnnotationInstance>> annotations, java.util.Map<DotName,java.util.List<ClassInfo>> subclasses, java.util.Map<DotName,java.util.List<ClassInfo>> implementors, java.util.Map<DotName,ClassInfo> classes, java.util.Map<DotName,java.util.List<ClassInfo>> users)
Constructs a "mock" Index using the passed values. All passed values MUST NOT BE MODIFIED AFTER THIS CALL. Otherwise the resulting object would not conform to the contract outlined above. Also, to conform to the memory efficiency contract this method should be passed componentized DotNames, which all share common root instances. Of course for testing code this doesn't really matter.- Parameters:
annotations
- A map to lookup annotation instances by class namesubclasses
- A map to lookup subclasses by super class nameimplementors
- A map to lookup implementing classes by interface nameclasses
- A map to lookup classes by class nameusers
- A map to lookup class users- Returns:
- the index
-
of
public static Index of(java.lang.Iterable<java.lang.Class<?>> classes) throws java.io.IOException
Constructs an Index of the passed classes.- Parameters:
classes
- Classes to index- Returns:
- the index
- Throws:
java.io.IOException
-
of
public static Index of(java.lang.Class<?>... classes) throws java.io.IOException
Constructs an Index of the passed classes.- Parameters:
classes
- Classes to index- Returns:
- the index
- Throws:
java.io.IOException
-
of
public static Index of(java.io.File... directories) throws java.io.IOException
Constructs an Index of class files found in the passed directories. The directories are not scanned recursively.- Parameters:
directories
- Directories containing class files to index- Returns:
- the index
- Throws:
java.lang.IllegalArgumentException
- if any passedFile
is null or not a directoryjava.io.IOException
-
getAnnotations
public java.util.List<AnnotationInstance> getAnnotations(DotName annotationName)
Obtains a list of instances for the specified annotation. This is done using an O(1) lookup. Valid instance targets include field, method, parameter, and class.- Specified by:
getAnnotations
in interfaceIndexView
- Parameters:
annotationName
- the name of the annotation to look for- Returns:
- a non-null list of annotation instances
-
getAnnotationsWithRepeatable
public java.util.Collection<AnnotationInstance> getAnnotationsWithRepeatable(DotName annotationName, IndexView index)
Obtains a list of instances for the specified annotation. If the specified annotation is repeatable (JLS 9.6), the result also contains all values from all instances of the container annotation. In this case, theAnnotationInstance.target()
returns the target of the container annotation instance.- Specified by:
getAnnotationsWithRepeatable
in interfaceIndexView
- Parameters:
annotationName
- the name of the repeatable annotationindex
- the index containing the annotation class- Returns:
- a non-null list of annotation instances
-
getRepeatableAnnotations
private java.util.Collection<AnnotationInstance> getRepeatableAnnotations(DotName annotationName, DotName containingAnnotationName)
-
getKnownDirectSubclasses
public java.util.List<ClassInfo> getKnownDirectSubclasses(DotName className)
Gets all known direct subclasses of the specified class name. A known direct subclass is one which was found during the scanning process; however, this is often not the complete universe of subclasses, since typically indexes are constructed per jar. It is expected that several indexes will need to be searched when analyzing a jar that is a part of a complex multi-module/classloader environment (like an EE application server).Note that this will only pick up direct subclasses of the class. It will not pick up subclasses of subclasses.
- Specified by:
getKnownDirectSubclasses
in interfaceIndexView
- Parameters:
className
- the super class of the desired subclasses- Returns:
- a non-null list of all known subclasses of className
-
getAllKnownSubclasses
public java.util.Collection<ClassInfo> getAllKnownSubclasses(DotName className)
Description copied from interface:IndexView
Returns all known (including non-direct) sub classes of the given class. I.e., returns all known classes that are assignable to the given class.- Specified by:
getAllKnownSubclasses
in interfaceIndexView
- Parameters:
className
- The class- Returns:
- All known subclasses
-
getAllKnownSubClasses
private void getAllKnownSubClasses(DotName className, java.util.Set<ClassInfo> allKnown, java.util.Set<DotName> processedClasses)
-
getAllKnownSubClasses
private void getAllKnownSubClasses(DotName name, java.util.Set<ClassInfo> allKnown, java.util.Set<DotName> subClassesToProcess, java.util.Set<DotName> processedClasses)
-
getKnownDirectImplementors
public java.util.List<ClassInfo> getKnownDirectImplementors(DotName className)
Gets all known direct implementors of the specified interface name. A known direct implementor is one which was found during the scanning process; however, this is often not the complete universe of implementors, since typically indexes are constructed per jar. It is expected that several indexes will need to be searched when analyzing a jar that is a part of a complex multi-module/classloader environment (like an EE application server).The list of implementors may also include other methodParameters, in order to get a complete list of all classes that are assignable to a given interface it is necessary to recursively call
IndexView.getKnownDirectImplementors(DotName)
for every implementing interface found.- Specified by:
getKnownDirectImplementors
in interfaceIndexView
- Parameters:
className
- the super class of the desired subclasses- Returns:
- a non-null list of all known subclasses of className
-
getAllKnownImplementors
public java.util.Set<ClassInfo> getAllKnownImplementors(DotName interfaceName)
Description copied from interface:IndexView
Returns all known classes that implement the given interface, directly and indirectly. This will all return classes that implement sub methodParameters of the interface, and sub-classes of classes that implement the interface. (In short, it will return every class that is assignable to the interface that is found in the index)This will only return classes, not methodParameters.
- Specified by:
getAllKnownImplementors
in interfaceIndexView
- Parameters:
interfaceName
- The interface- Returns:
- All known implementors of the interface
-
getKnownImplementors
private void getKnownImplementors(DotName name, java.util.Set<ClassInfo> allKnown, java.util.Set<DotName> subInterfacesToProcess, java.util.Set<DotName> processedClasses)
-
getClassByName
public ClassInfo getClassByName(DotName className)
Gets the class (or interface, or annotation) that was scanned during the indexing phase.- Specified by:
getClassByName
in interfaceIndexView
- Parameters:
className
- the name of the class- Returns:
- information about the class or null if it is not known
-
getKnownClasses
public java.util.Collection<ClassInfo> getKnownClasses()
Gets all known classes by this index (those which were scanned).- Specified by:
getKnownClasses
in interfaceIndexView
- Returns:
- a collection of known classes
-
getKnownModules
public java.util.Collection<ModuleInfo> getKnownModules()
Gets all known modules by this index (those which were scanned).- Specified by:
getKnownModules
in interfaceIndexView
- Returns:
- a collection of known modules
-
getModuleByName
public ModuleInfo getModuleByName(DotName moduleName)
Gets the module that was scanned during the indexing phase.- Specified by:
getModuleByName
in interfaceIndexView
- Parameters:
moduleName
- the name of the module- Returns:
- information about the module or null if it is not known
-
printAnnotations
public void printAnnotations()
Print all annotations known by this index to stdout.
-
printSubclasses
public void printSubclasses()
Print all classes that have known subclasses, and all their subclasses
-
getKnownUsers
public java.util.List<ClassInfo> getKnownUsers(DotName className)
Obtains a list of classes that use the specified class. In other words, a list of classes that include a reference to the specified class in their constant pool.- Specified by:
getKnownUsers
in interfaceIndexView
- Parameters:
className
- the name of the class to look for- Returns:
- a non-null list of classes that use the specified class
-
-