See: Description
Interface | Description |
---|---|
AnnotationTarget |
Represents an object that can be a target of an annotation.
|
IndexView |
The basic contract for accessing Jandex indexed information.
|
Class | Description |
---|---|
AnnotationInstance |
An annotation instance represents a specific usage of an annotation on a
target.
|
AnnotationValue |
An annotation value represents a specific name and value combination in the
parameter list of an annotation instance.
|
ArrayType |
Represents a Java array type declaration.
|
ClassExtendsTypeTarget |
Represents a type annotation target which occurs in the extends or implements clause of an enclosing class.
|
ClassInfo |
Represents a class entry in an index.
|
ClassInfo.EnclosingMethodInfo |
Provides information on the enclosing method or constructor for a local or anonymous class,
if available.
|
ClassType |
Represents a standard raw class name.
|
CompositeIndex |
Composite annotation index.
|
DotName |
A DotName represents a dot separated name, typically a Java package or a Java class.
|
EmptyTypeTarget |
Represents a type annotation target which occurs directly on a field type, a method return type, or a method receiver
type.
|
FieldInfo |
Represents a field.
|
Index |
An index useful for quickly processing annotations.
|
Indexer |
Analyzes and indexes the annotation and key structural information of a set
of classes.
|
IndexReader |
Reads a Jandex index file and returns the saved index.
|
IndexWriter |
Writes a Jandex index file to a stream.
|
JandexAntTask |
Ant Task that indexes jars, and saves the resulting index
|
JarIndexer |
Class which contains utility methods to create an index for a jar file
|
Main |
Responsible for launching the indexing tool on a java command line.
|
MethodInfo |
Represents a Java method, constructor, or static initializer.
|
MethodParameterInfo |
Represents an individual Java method parameter that was annotated.
|
MethodParameterTypeTarget |
Represents a type annotation target which occurs within a method parameter type.
|
ParameterizedType |
Represents a generic parameterized type.
|
PositionBasedTypeTarget |
A common parent for type targets which provide a position.
|
PrimitiveType |
Represents a primitive Java type.
|
Result |
The result from a jar indexing operation.
|
ThrowsTypeTarget |
Represents a type annotation target which occurs within a throwable type on a method.
|
Type |
Represents a Java type declaration usage that is specified on methods, fields, classes,
annotations, or other types.
|
TypeParameterBoundTypeTarget |
Represents a type annotation target which occurs within a bound of type parameter type.
|
TypeParameterTypeTarget |
Represents a type annotation target which occurs within a type parameter type.
|
TypeTarget |
Represents a type that is the target of a type annotation.
|
TypeVariable |
Represents a resolved type parameter or type argument.
|
UnresolvedTypeVariable |
Represents a type variable that could not be resolved during indexing.
|
VoidType |
Specifies "void" in a method signature.
|
WildcardType |
Represents a generic wildcard.
|
Enum | Description |
---|---|
AnnotationTarget.Kind |
Specifies the kind of object a target represents.
|
AnnotationValue.Kind |
Specifies the kind of annotation value, which can be used to determine the underlying Java type.
|
ClassInfo.NestingType |
Describes the form of nesting used by a class
|
PrimitiveType.Primitive |
Specifies the underlying Java primitive type for a
PrimitiveType |
Type.Kind |
Represents a "kind" of Type.
|
TypeTarget.Usage |
Specifies a form of usage of a type annotation
|
Exception | Description |
---|---|
UnsupportedVersion |
The version encountered is not supported.
|
It supports the following capabilities:
The starting point for most usages is to use Indexer
on a set of class file streams
to ultimately produce an Index
. Alternatively, if a persisted Jandex index file is
available, IndexReader
can be used to load the Index
directly
from disk. The index files can be produced by using an IndexWriter
, or by using
the command line tool, which is available when using "-jar" on the Jandex jar, or by using the
JandexAntTask
with ant.
The following example demonstrates indexing a class and browsing its methods:
// Index java.util.Map Indexer indexer = new Indexer(); // Normally a direct file is opened, but class-loader backed streams work as well. InputStream stream = getClass().getClassLoader().getResourceAsStream("java/util/Map.class"); indexer.index(stream); Index index = indexer.complete(); // Retrieve Map from the index and print its declared methods ClassInfo clazz = index.getClassByName(DotName.createSimple("java.util.Map")); for (MethodInfo method : clazz.methods()) { System.out.println(method); }
The following example demonstrates indexing a class and persisting the index to disk. The resulting file can later be loaded scanning Java classes:
// Index java.util.Map Indexer indexer = new Indexer(); // Normally a direct file is opened, but class-loader backed streams work as well. InputStream stream = getClass().getClassLoader().getResourceAsStream("java/util/Map.class"); indexer.index(stream); Index index = indexer.complete(); FileOutputStream out = new FileOutputStream("/tmp/index.idx"); IndexWriter writer = new IndexWriter(out); try { writer.write(index); } finally { safeClose(out); }
The following example demonstrates indexing hibernate core, followed by the entire Java JDK using Jandex on the CLI:
$ java -jar target/jandex-2.0.0.Alpha1.jar hibernate-core-4.0.0.Final.jar Wrote /Users/jason/devel/jandex/hibernate-core-4.0.0.Final-jar.idx in 0.9020 seconds (2722 classes, 20 annotations, 1696 instances, 621565 bytes) $ java -jar target/jandex-2.0.0.Alpha1.jar rt.jar Wrote /Users/jason/devel/jandex/rt-jar.idx in 4.2870 seconds (19831 classes, 41 annotations, 1699 instances, 4413790 bytes)
The above summary output tells us that this version of hibernate has 2,722 classes, and those classes contained 1,696 annotation declarations, using 20 different annotation types. The resulting index is 606KB uncompressed, which is only 14% of the 4.1MB compressed jar size, or 4% of the uncompressed class file data. If the index is stored in the jar (using the -m option) it can be compressed an additional 47%, leading to a jar growth of only 8%
The following example demonstrates loading the index from the previous example and using that
index to print all methods on java.util.Map
:
FileInputStream input = new FileInputStream("/tmp/index.idx"); IndexReader reader = new IndexReader(input); try { index = reader.read(); } finally { safeClose(input); } // Retrieve Map from the index and print its declared methods ClassInfo clazz = index.getClassByName(DotName.createSimple("java.util.Map")); for (MethodInfo method : clazz.methods()) { System.out.println(method); }
The following example demonstrates indexing the Thread and String classes, and searching for methods that have been marked with @Deprecated:
Indexer indexer = new Indexer(); InputStream stream = getClass().getClassLoader().getResourceAsStream("java/lang/Thread.class"); InputStream stream = getClass().getClassLoader().getResourceAsStream("java/lang/String.class"); indexer.index(stream); Index index = indexer.complete(); DotName deprecated = DotName.createSimple("java.lang.Deprecated"); List<AnnotationInstance> annotations = index.getAnnotations(deprecated); for (AnnotationInstance annotation : annotations) { switch (annotation.target().kind()) { case METHOD: System.out.println(annotation.target()); break; } }
The following example demonstrates indexing the Collections class and printing the resolved bound
on the List<T>
method parameter, which resolves to Comparable from the method type parameter.
The sort() method analyzed by the example is defined in source as:
public static <T extends Comparable<? super T>> void sort(List<T> list)
The example code, which prints "Comparable<? super T>"
, followed by "T"
is:
Indexer indexer = new Indexer(); InputStream stream = getClass().getClassLoader().getResourceAsStream("java/util/Collections.class"); indexer.index(stream); Index index = indexer.complete(); // Find method ClassInfo clazz = index.getClassByName(DotName.createSimple("java.util.Collections")); Type listType = Type.create(DotName.createSimple("java.util.List"), Type.Kind.CLASS); MethodInfo sort = clazz.method("sort", listType); Type t = sort.parameters().get(0).asParameterizedType() // List<T extends Comparable<? super T>> .arguments().get(0) // T extends Comparable<? super T> .asTypeVariable().bounds().get(0); // Comparable<? super T> System.out.println(t); System.out.println(t.asWildcardType().superBound()); // T
Consider a complex nested generic structure which contains a @Label
annotation
Map<Integer, List<@Label("Name") String>> names
The following code will print "Name"
, the annotation value associated with the type:
Indexer indexer = new Indexer(); InputStream stream = new FileInputStream("/tmp/Test.class"); indexer.index(stream); stream = new FileInputStream("/tmp/Test$Label.class"); indexer.index(stream); Index index = indexer.complete(); DotName test = DotName.createSimple("Test"); FieldInfo field = index.getClassByName(test).field("names"); System.out.println( field.type().asParameterizedType().arguments().get(1) .asParameterizedType().arguments().get(0) .annotations().get(0).value().asString() );
A type annotation can also be located by searching for the annotation. The target for a found type annotation
is represented as a TypeTarget
. The TypeTarget
provides a reference to the annotated
type, as well as the enclosing target that contains the type. The target itself can be a method, a class, or a field.
The usage on that target can be a number of places, including parameters, return types, type parameters,
type arguments, class extends values, type bounds and receiver types. Subclasses of TypeTarget
provide
the necessary information to locate the starting point of the usage.
Since the particular type use can occur at any depth, the relevant branch of the type tree constrained by the above starting point must be traversed to understand the context of the use.
Consider a complex nested generic structure which contains a @Label
annotation
Map<Integer, List<@Label("Name") String>> names
The following code locates a type annotation using a hardcoded path:
Indexer indexer = new Indexer(); InputStream stream = new FileInputStream("/tmp/Test.class"); indexer.index(stream); stream = new FileInputStream("/tmp/Test$Label.class"); indexer.index(stream); Index index = indexer.complete(); List<AnnotationInstance> annotations = index.getAnnotations(DotName.createSimple("Test$Label")); for (AnnotationInstance annotation : annotations) { if (annotation.target().kind() == AnnotationTarget.Kind.TYPE) { TypeTarget typeTarget = annotation.target().asType(); System.out.println("Type usage is located within: " + typeTarget.enclosingTarget()); System.out.println("Usage type: " + typeTarget.usage()); System.out.println("Target type:" + typeTarget.target()); System.out.println("Equivalent? " + (typeTarget.enclosingTarget().asField().type() .asParameterizedType().arguments().get(1) .asParameterizedType().arguments().get(0) == typeTarget.target())); }
The above code prints the following output:
Type usage is located within: java.util.Map<java.lang.Integer, java.util.List<@Label(value = "Name") java.lang.String>> Test.names Usage type: EMPTY Target type:@Label(value = "Name") java.lang.String Equivalent? true
Copyright © 2018 JBoss by Red Hat. All rights reserved.