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 componentizedDotName
values.It contains the following information:
- All annotations and a collection of targets they refer to
- All classes (including interfaces) scanned during the indexing process (typically all classes in a jar)
- All modules scanned during the indexing process (typically one module in a jar)
- All subclasses for each class known to this index
- All subinterfaces for each interface known to this index
- All implementors for each interface known to this index
- All users of each class known to this index
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<DotName,AnnotationInstance[]>
annotations
(package private) java.util.Map<DotName,ClassInfo>
classes
(package private) java.util.Map<DotName,java.util.Collection<ClassInfo>>
classesInPackage
private static java.util.List<AnnotationInstance>
EMPTY_ANNOTATION_LIST
private static java.util.List<ClassInfo>
EMPTY_CLASSINFO_LIST
(package private) java.util.Map<DotName,ClassInfo[]>
implementors
(package private) java.util.Map<DotName,ModuleInfo>
modules
(package private) java.util.Map<DotName,ClassInfo[]>
subclasses
(package private) java.util.Map<DotName,ClassInfo[]>
subinterfaces
(package private) java.util.Map<DotName,java.util.Set<DotName>>
subpackages
(package private) java.util.Map<DotName,ClassInfo[]>
users
-
Constructor Summary
Constructors Constructor Description Index(java.util.Map<DotName,AnnotationInstance[]> annotations, java.util.Map<DotName,ClassInfo[]> subclasses, java.util.Map<DotName,ClassInfo[]> subinterfaces, java.util.Map<DotName,ClassInfo[]> implementors, java.util.Map<DotName,ClassInfo> classes, java.util.Map<DotName,ModuleInfo> modules, java.util.Map<DotName,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>> subinterfaces, 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.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>> subinterfaces, 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)
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)
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.Collection<ClassInfo>
getAllKnownImplementations(DotName interfaceName)
Returns all known classes that implement the given interface, directly and indirectly.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 subclasses of the given class, direct and indirect.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.Collection<ClassInfo>
getAllKnownSubinterfaces(DotName interfaceName)
Returns all known subinterfaces of the given interface, direct and indirect.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)
Returns the class (or enum, record, interface, annotation) with given name.java.util.Collection<ClassInfo>
getClassesInPackage(DotName packageName)
Returns all classes known to this index that are present in given package.java.util.Collection<ClassInfo>
getKnownClasses()
Gets all known classes by this index (those which were scanned).java.util.Collection<ClassInfo>
getKnownDirectImplementations(DotName interfaceName)
Returns all known classes that directly implement the given interface.java.util.List<ClassInfo>
getKnownDirectImplementors(DotName interfaceName)
Gets all known direct implementors of the specified interface.java.util.List<ClassInfo>
getKnownDirectSubclasses(DotName className)
Returns all known direct subclasses of the given class.java.util.List<ClassInfo>
getKnownDirectSubinterfaces(DotName interfaceName)
Returns all known direct subinterfaces of the given interface.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)
Returns a list of classes in this index 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)
java.util.Set<DotName>
getSubpackages(DotName packageName)
Returns a set of packages known to this index that are direct subpackages of given package.static Index
of(java.io.File... files)
Constructs an Index of the passed files and 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 subclassesstatic ClassInfo
singleClass(byte[] classData)
static ClassInfo
singleClass(java.io.InputStream classData)
static ClassInfo
singleClass(java.lang.Class<?> clazz)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jboss.jandex.IndexView
getAllKnownImplementations, getAllKnownImplementations, getAllKnownImplementors, getAllKnownImplementors, getAllKnownSubclasses, getAllKnownSubclasses, getAllKnownSubinterfaces, getAllKnownSubinterfaces, getAnnotations, getAnnotations, getAnnotationsWithRepeatable, getAnnotationsWithRepeatable, getClassByName, getClassByName, getClassesInPackage, getKnownDirectImplementations, getKnownDirectImplementations, getKnownDirectImplementors, getKnownDirectImplementors, getKnownDirectSubclasses, getKnownDirectSubclasses, getKnownDirectSubinterfaces, getKnownDirectSubinterfaces, getKnownUsers, getKnownUsers, getModuleByName, getSubpackages
-
-
-
-
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
-
annotations
final java.util.Map<DotName,AnnotationInstance[]> annotations
-
modules
final java.util.Map<DotName,ModuleInfo> modules
-
-
Constructor Detail
-
Index
Index(java.util.Map<DotName,AnnotationInstance[]> annotations, java.util.Map<DotName,ClassInfo[]> subclasses, java.util.Map<DotName,ClassInfo[]> subinterfaces, java.util.Map<DotName,ClassInfo[]> implementors, java.util.Map<DotName,ClassInfo> classes, java.util.Map<DotName,ModuleInfo> modules, java.util.Map<DotName,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
-
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>> subinterfaces, 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 namesubinterfaces
- A map to lookup subinterfaces by super interface 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
-
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>> subinterfaces, 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)
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 namesubinterfaces
- A map to lookup subinterfaces by super interface nameimplementors
- A map to lookup implementing classes by interface nameclasses
- A map to lookup classes by class namemodules
- A map to lookup modules by 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... files) throws java.io.IOException
Constructs an Index of the passed files and directories. Files may be class files or JAR files. Directories are scanned for class files, but not recursively.- Parameters:
files
- class files, JAR files or directories containing class files to index- Returns:
- the index
- Throws:
java.lang.IllegalArgumentException
- if any passedFile
is null or not a class file, JAR file or directoryjava.io.IOException
-
singleClass
public static ClassInfo singleClass(java.lang.Class<?> clazz) throws java.io.IOException
- Parameters:
clazz
- the class to index, must not benull
- Returns:
- the corresponding
ClassInfo
- Throws:
java.io.IOException
-
singleClass
public static ClassInfo singleClass(byte[] classData) throws java.io.IOException
- Parameters:
classData
- the class bytecode to index, must not benull
- Returns:
- the corresponding
ClassInfo
- Throws:
java.io.IOException
-
singleClass
public static ClassInfo singleClass(java.io.InputStream classData) throws java.io.IOException
Creates a temporaryIndexer
, indexes givenclassData
, and returns the correspondingClassInfo
. Closing the input stream is the caller's responsibility.- Parameters:
classData
- the class bytecode to index, must not benull
- Returns:
- the corresponding
ClassInfo
- Throws:
java.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:
- immutable collection of annotation instances, never
null
-
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:
- immutable collection of annotation instances, never
null
-
getRepeatableAnnotations
private java.util.Collection<AnnotationInstance> getRepeatableAnnotations(DotName annotationName, DotName containingAnnotationName)
-
getKnownDirectSubclasses
public java.util.List<ClassInfo> getKnownDirectSubclasses(DotName className)
Returns all known direct subclasses of the given class. Indirect subclasses are not returned.Note that interfaces are considered direct subclasses of
java.lang.Object
.- Specified by:
getKnownDirectSubclasses
in interfaceIndexView
- Parameters:
className
- the class- Returns:
- an immutable collection of known direct subclasses of given class, never
null
-
getAllKnownSubclasses
public java.util.Collection<ClassInfo> getAllKnownSubclasses(DotName className)
Description copied from interface:IndexView
Returns all known subclasses of the given class, direct and indirect. In other words, all classes that are assignable to the given class.- Specified by:
getAllKnownSubclasses
in interfaceIndexView
- Parameters:
className
- the class- Returns:
- immutable collection of all known subclasses of given class, never
null
-
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)
-
getKnownDirectSubinterfaces
public java.util.List<ClassInfo> getKnownDirectSubinterfaces(DotName interfaceName)
Returns all known direct subinterfaces of the given interface. Indirect subinterfaces are not returned.- Specified by:
getKnownDirectSubinterfaces
in interfaceIndexView
- Parameters:
interfaceName
- the interface- Returns:
- immutable collection of all known subinterfaces of given interface, never
null
-
getAllKnownSubinterfaces
public java.util.Collection<ClassInfo> getAllKnownSubinterfaces(DotName interfaceName)
Returns all known subinterfaces of the given interface, direct and indirect. In other words, all interfaces that are assignable to the given interface.- Specified by:
getAllKnownSubinterfaces
in interfaceIndexView
- Parameters:
interfaceName
- the interface- Returns:
- immutable collection of all known subinterfaces of given interface, never
null
-
getKnownDirectImplementations
public java.util.Collection<ClassInfo> getKnownDirectImplementations(DotName interfaceName)
Description copied from interface:IndexView
Returns all known classes that directly implement the given interface. Classes that do not directly implement the given interface but do directly implement subinterfaces are not returned. Subclasses of classes that directly implement the given interface are not returned either.Note that unlike
IndexView.getKnownDirectImplementors(DotName)
, this method does NOT return direct subinterfaces of the given interface, which is typically what you expect when you call this method.- Specified by:
getKnownDirectImplementations
in interfaceIndexView
- Parameters:
interfaceName
- the interface- Returns:
- immutable collection of all known direct implementations of the interface, never
null
-
getAllKnownImplementations
public java.util.Collection<ClassInfo> getAllKnownImplementations(DotName interfaceName)
Description copied from interface:IndexView
Returns all known classes that implement the given interface, directly and indirectly. That is, all classes that implement the interface and its subinterfaces, as well as all their subclasses. In other words, all classes that are assignable to the interface.- Specified by:
getAllKnownImplementations
in interfaceIndexView
- Parameters:
interfaceName
- the interface- Returns:
- immutable collection of all known implementations of the interface, never
null
-
getKnownDirectImplementors
public java.util.List<ClassInfo> getKnownDirectImplementors(DotName interfaceName)
Gets all known direct implementors of the specified interface. 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 also includes direct subinterfaces. This is inconsistent with
IndexView.getAllKnownImplementors(DotName)
, which doesn't return subinterfaces.Note that this will only pick up classes that directly implement given interface. It will not pick up classes implementing subinterfaces.
- Specified by:
getKnownDirectImplementors
in interfaceIndexView
- Parameters:
interfaceName
- The interface- Returns:
- All known direct implementors of the interface
-
getAllKnownImplementors
public java.util.Set<ClassInfo> getAllKnownImplementors(DotName interfaceName)
Returns all known classes that implement the given interface, directly and indirectly. This will return all classes that implement the interface and its subinterfaces, as well as subclasses of classes that implement the interface and its subinterfaces. (In short, it will return every class in the index that is assignable to the interface.)Note that this method only returns classes. Unlike
IndexView.getKnownDirectImplementors(DotName)
, this method does not return subinterfaces of given interface.- 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)
Returns the class (or enum, record, interface, annotation) with given name. Returnsnull
if class with given name is not present in the index.- 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:
- immutable collection of known classes, never
null
-
getKnownModules
public java.util.Collection<ModuleInfo> getKnownModules()
Gets all known modules by this index (those which were scanned).- Specified by:
getKnownModules
in interfaceIndexView
- Returns:
- immutable collection of known modules, never
null
-
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
-
getKnownUsers
public java.util.List<ClassInfo> getKnownUsers(DotName className)
Returns a list of classes in this index that use the specified class. For one class to use another class, the other class has to:- occur in the signature of the class (that is, in the superclass type, in the superinterface types, in the type parameters, or in the list of permitted subclasses), or
- occur in the signature of any of the class's methods (that is, in the return type, in the parameter types, in the exception types, or in the type parameters), or
- occur in the type of any of the class's fields or record components, or
- occur in the list of class references in the constant pool, as described by the JLS and JVMS.
- Specified by:
getKnownUsers
in interfaceIndexView
- Parameters:
className
- the name of the class to look for- Returns:
- immutable collection of classes that use the specified class, never
null
-
getClassesInPackage
public java.util.Collection<ClassInfo> getClassesInPackage(DotName packageName)
Returns all classes known to this index that are present in given package. Classes present in subpackages of given package are not returned. Classes present in the unnamed package may be looked up usingnull
as the package name. If this index does not contain any class in given package, returns an empty collection.In the default
Index
implementation, this information is not stored in the index initially. Instead, an index of classes by package name is constructed on demand (on the first invocation of this method).- Specified by:
getClassesInPackage
in interfaceIndexView
- Parameters:
packageName
- package name in the common, dot-separated form (e.g.com.example.foobar
);null
means the unnamed package- Returns:
- immutable collection of classes present in given package, never
null
-
getSubpackages
public java.util.Set<DotName> getSubpackages(DotName packageName)
Returns a set of packages known to this index that are direct subpackages of given package. Indirect subpackages of given package (subpackages of subpackages) are not returned. If this index does not contain any class in a direct or indirect subpackage of given package, returns an empty collection.Given that the unnamed package may not contain subpackages, passing
null
as the package name is permitted, but always results in an empty set.In the default
Index
implementation, this information is not stored in the index initially. Instead, an index of packages is constructed on demand (on the first invocation of this method).- Specified by:
getSubpackages
in interfaceIndexView
- Parameters:
packageName
- package name in the common, dot-separated form (e.g.com.example.foobar
);null
means the unnamed package- Returns:
- immutable set of subpackages of given package, never
null
-
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
-
-