Class Type
- java.lang.Object
-
- org.jboss.jandex.Type
-
- All Implemented Interfaces:
Descriptor
- Direct Known Subclasses:
ArrayType
,ClassType
,ParameterizedType
,PrimitiveType
,TypeVariable
,TypeVariableReference
,UnresolvedTypeVariable
,VoidType
,WildcardType
public abstract class Type extends java.lang.Object implements Descriptor
Represents a Java type usage that is specified on methods, fields, classes, annotations, or other types. A type can be any class based type (interface, class, annotation), any primitive, any array, any generic type declaration, or void.A type usage may have annotations associated with its declaration. A type is equal to another type if, and only if, it represents the same exact definition including the annotations specific to its usage.
To reduce memory overhead, type instances are often shared between their enclosing classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
Type.Builder<THIS extends Type.Builder<THIS>>
Base class for type builders.static class
Type.Kind
Represents a "kind" of Type.
-
Field Summary
Fields Modifier and Type Field Description private AnnotationInstance[]
annotations
private static AnnotationInstance[]
EMPTY_ANNOTATIONS
static Type[]
EMPTY_ARRAY
private DotName
name
-
Fields inherited from interface org.jboss.jandex.Descriptor
NO_SUBSTITUTION
-
-
Constructor Summary
Constructors Constructor Description Type(DotName name, AnnotationInstance[] annotations)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description (package private) Type
addAnnotation(AnnotationInstance annotation)
AnnotationInstance
annotation(DotName name)
Returns the annotation instance with given name declared on this type usage.(package private) AnnotationInstance[]
annotationArray()
java.util.List<AnnotationInstance>
annotations()
Returns the annotation instances declared on this type usage.java.util.List<AnnotationInstance>
annotationsWithRepeatable(DotName name, IndexView index)
Returns the annotation instances with given name declared on this type usage.(package private) void
appendAnnotations(java.lang.StringBuilder builder)
ArrayType
asArrayType()
Casts this type to anArrayType
and returns it if the kind isType.Kind.ARRAY
.ClassType
asClassType()
Casts this type to aClassType
and returns it if the kind isType.Kind.CLASS
.ParameterizedType
asParameterizedType()
Casts this type to aParameterizedType
and returns it if the kind isType.Kind.PARAMETERIZED_TYPE
.PrimitiveType
asPrimitiveType()
Casts this type to aPrimitiveType
and returns it if the kind isType.Kind.PRIMITIVE
.TypeVariable
asTypeVariable()
Casts this type to aTypeVariable
and returns it if the kind isType.Kind.TYPE_VARIABLE
.TypeVariableReference
asTypeVariableReference()
Casts this type to aTypeVariableReference
and returns it if the kind isType.Kind.TYPE_VARIABLE_REFERENCE
.UnresolvedTypeVariable
asUnresolvedTypeVariable()
Casts this type to anUnresolvedTypeVariable
and returns it if the kind isType.Kind.UNRESOLVED_TYPE_VARIABLE
.VoidType
asVoidType()
Casts this type to aVoidType
and returns it if the kind isType.Kind.VOID
.WildcardType
asWildcardType()
Casts this type to aWildcardType
and returns it if the kind isType.Kind.WILDCARD_TYPE
.(package private) abstract Type
copyType(AnnotationInstance[] newAnnotations)
static Type
create(java.lang.Class<?> clazz)
Creates a type that corresponds to the givenclazz
.static Type
create(DotName name, Type.Kind kind)
Creates a type of the specified kind andname
in theClass.getName()
format.static Type
createWithAnnotations(DotName name, Type.Kind kind, AnnotationInstance[] annotations)
Creates an instance of specified type with given typeannotations
.java.lang.String
descriptor(java.util.function.Function<java.lang.String,Type> typeVariableSubstitution)
Returns the bytecode descriptor of this type (or its erasure in case of generic types).boolean
equals(java.lang.Object o)
Compares thisType
with another type.boolean
hasAnnotation(DotName name)
Returns whether an annotation instance with given name is declared on this type usage.int
hashCode()
Computes a hash code representing this type.(package private) boolean
internEquals(java.lang.Object o)
(package private) int
internHashCode()
abstract Type.Kind
kind()
Returns the kind of Type this is.DotName
name()
Returns the name of this type (or its erasure in case of generic types) as aDotName
, using theClass.getName()
format.static Type
parse(java.lang.String type)
Creates aType
by parsing the given string according to the following grammar:java.lang.String
toString()
Returns a string representation for this type.(package private) java.lang.String
toString(boolean simple)
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jboss.jandex.Descriptor
descriptor
-
-
-
-
Field Detail
-
EMPTY_ARRAY
public static final Type[] EMPTY_ARRAY
-
EMPTY_ANNOTATIONS
private static final AnnotationInstance[] EMPTY_ANNOTATIONS
-
name
private final DotName name
-
annotations
private final AnnotationInstance[] annotations
-
-
Constructor Detail
-
Type
Type(DotName name, AnnotationInstance[] annotations)
-
-
Method Detail
-
create
public static Type create(DotName name, Type.Kind kind)
Creates a type of the specified kind andname
in theClass.getName()
format. Specifically:- if
kind
isVOID
, thename
is ignored; - if
kind
isPRIMITIVE
, the name must be the corresponding Java keyword (boolean
,byte
,short
,int
,long
,float
,double
,char
); - if
kind
isCLASS
, thename
must be a binary name of the class; - if
kind
isARRAY
, thename
must consists of one or more[
characters corresponding to the number of dimensions of the array type, followed by the element type as a single-character code for primitive types orLbinary.name.of.TheClass;
for class types (for example,[I
forint[]
or[[Ljava.lang.String;
forString[][]
); - all other kinds cause an exception.
- Parameters:
name
- the name of type to use or parse; must not benull
kind
- the kind of type to create; must not benull
- Returns:
- the type
- Throws:
java.lang.IllegalArgumentException
- if thekind
is not supported
- if
-
create
public static Type create(java.lang.Class<?> clazz)
Creates a type that corresponds to the givenclazz
. The resulting type may be:VoidType
PrimitiveType
ClassType
ArrayType
whose the element type isPrimitiveType
orClassType
- Parameters:
clazz
- aClass
- Returns:
- a
Type
corresponding to the givenclazz
-
createWithAnnotations
public static Type createWithAnnotations(DotName name, Type.Kind kind, AnnotationInstance[] annotations)
Creates an instance of specified type with given typeannotations
. To create the type instance, this method delegates tocreate(DotName, Kind)
.- Parameters:
name
- the name of type to use or parse; must not benull
kind
- the kind of type to create; must not benull
annotations
- the type annotations that should be present on the type instance; may benull
- Returns:
- the annotated type
- Throws:
java.lang.IllegalArgumentException
- if thekind
is not supported
-
parse
public static Type parse(java.lang.String type)
Creates aType
by parsing the given string according to the following grammar:Type -> VoidType | PrimitiveType | ReferenceType VoidType -> 'void' PrimitiveType -> 'boolean' | 'byte' | 'short' | 'int' | 'long' | 'float' | 'double' | 'char' ReferenceType -> PrimitiveType ('[' ']')+ | ClassType ('<' TypeArgument (',' TypeArgument)* '>')? ('[' ']')* ClassType -> FULLY_QUALIFIED_NAME TypeArgument -> ReferenceType | WildcardType WildcardType -> '?' | '?' ('extends' | 'super') ReferenceType
Notice that the resulting type never contains type variables, only "proper" types. Also notice that the grammar above does not support all kinds of nested types; it should be possible to add that later, if there's an actual need.- Parameters:
type
- the string to parse; must not benull
- Returns:
- the parsed type
- Throws:
java.lang.IllegalArgumentException
- if the string does not conform to the grammar given above- Since:
- 3.2.3
-
name
public DotName name()
Returns the name of this type (or its erasure in case of generic types) as aDotName
, using theClass.getName()
format. Specifically:- for primitive types and the void pseudo-type, the corresponding Java keyword
is returned (
void
,boolean
,byte
,short
,int
,long
,float
,double
,char
); - for class types, the binary name of the class is returned;
- for array types, a string is returned that consists of one or more
[
characters corresponding to the number of dimensions of the array type, followed by the element type as a single-character code for primitive types orLbinary.name.of.TheClass;
for class types (for example,[I
forint[]
or[[Ljava.lang.String;
forString[][]
); - for parameterized types, the binary name of the generic class is returned
(for example,
java.util.List
forList<String>
); - for type variables, the name of the first bound of the type variable is returned,
or
java.lang.Object
for type variables that have no bound; - for wildcard types, the name of the upper bound is returned,
or
java.lang.Object
if the wildcard type does not have an upper bound (for example,java.lang.Number
for? extends Number
).
- Returns:
- the name of this type (or its erasure in case of generic types)
- for primitive types and the void pseudo-type, the corresponding Java keyword
is returned (
-
kind
public abstract Type.Kind kind()
Returns the kind of Type this is.- Returns:
- the kind
-
asClassType
public ClassType asClassType()
Casts this type to aClassType
and returns it if the kind isType.Kind.CLASS
. Throws an exception otherwise.- Returns:
- a
ClassType
- Throws:
java.lang.IllegalArgumentException
- if not a class- Since:
- 2.0
-
asParameterizedType
public ParameterizedType asParameterizedType()
Casts this type to aParameterizedType
and returns it if the kind isType.Kind.PARAMETERIZED_TYPE
. Throws an exception otherwise.- Returns:
- a
ParameterizedType
- Throws:
java.lang.IllegalArgumentException
- if not a parameterized type- Since:
- 2.0
-
asTypeVariable
public TypeVariable asTypeVariable()
Casts this type to aTypeVariable
and returns it if the kind isType.Kind.TYPE_VARIABLE
. Throws an exception otherwise.- Returns:
- a
TypeVariable
- Throws:
java.lang.IllegalArgumentException
- if not a type variable- Since:
- 2.0
-
asTypeVariableReference
public TypeVariableReference asTypeVariableReference()
Casts this type to aTypeVariableReference
and returns it if the kind isType.Kind.TYPE_VARIABLE_REFERENCE
. Throws an exception otherwise.- Returns:
- a
TypeVariableReference
- Throws:
java.lang.IllegalArgumentException
- if not a type variable- Since:
- 2.0
-
asArrayType
public ArrayType asArrayType()
Casts this type to anArrayType
and returns it if the kind isType.Kind.ARRAY
. Throws an exception otherwise.- Returns:
- an
ArrayType
- Throws:
java.lang.IllegalArgumentException
- if not an array type- Since:
- 2.0
-
asWildcardType
public WildcardType asWildcardType()
Casts this type to aWildcardType
and returns it if the kind isType.Kind.WILDCARD_TYPE
. Throws an exception otherwise.- Returns:
- a
WildcardType
- Throws:
java.lang.IllegalArgumentException
- if not a wildcard type- Since:
- 2.0
-
asUnresolvedTypeVariable
public UnresolvedTypeVariable asUnresolvedTypeVariable()
Casts this type to anUnresolvedTypeVariable
and returns it if the kind isType.Kind.UNRESOLVED_TYPE_VARIABLE
. Throws an exception otherwise.- Returns:
- an
UnresolvedTypeVariable
- Throws:
java.lang.IllegalArgumentException
- if not an unresolved type- Since:
- 2.0
-
asPrimitiveType
public PrimitiveType asPrimitiveType()
Casts this type to aPrimitiveType
and returns it if the kind isType.Kind.PRIMITIVE
. Throws an exception otherwise.- Returns:
- a
PrimitiveType
- Throws:
java.lang.IllegalArgumentException
- if not a primitive type- Since:
- 2.0
-
asVoidType
public VoidType asVoidType()
Casts this type to aVoidType
and returns it if the kind isType.Kind.VOID
. Throws an exception otherwise.- Returns:
- a
VoidType
- Throws:
java.lang.IllegalArgumentException
- if not a void type- Since:
- 2.0
-
annotationArray
AnnotationInstance[] annotationArray()
-
hasAnnotation
public final boolean hasAnnotation(DotName name)
Returns whether an annotation instance with given name is declared on this type usage.- Parameters:
name
- name of the annotation type to look for, must not benull
- Returns:
true
if the annotation is present,false
otherwise- See Also:
annotation(DotName)
-
annotation
public final AnnotationInstance annotation(DotName name)
Returns the annotation instance with given name declared on this type usage.To allow for
Type
object reuse, the annotation instances returned by this method have anull
annotation target.- Parameters:
name
- name of the annotation type to look for, must not benull
- Returns:
- the annotation instance, or
null
if not found
-
annotationsWithRepeatable
public final java.util.List<AnnotationInstance> annotationsWithRepeatable(DotName name, IndexView index)
Returns the annotation instances with given name declared on this type usage.If the specified annotation is repeatable, the result also contains all values from the container annotation instance. In this case, the
AnnotationInstance.target()
returns the target of the container annotation instance.To allow for
Type
object reuse, the annotation instances returned by this method have anull
annotation target.- Parameters:
name
- name of the annotation type, must not benull
index
- index used to obtain the annotation type, must not benull
- Returns:
- immutable list of annotation instances, never
null
- Throws:
java.lang.IllegalArgumentException
- if the index isnull
, if the index does not contain the annotation type or ifname
does not identify an annotation type- Since:
- 3.0
- See Also:
annotations()
-
annotations
public java.util.List<AnnotationInstance> annotations()
Returns the annotation instances declared on this type usage.To allow for
Type
object reuse, the annotation instances returned by this method have anull
annotation target.- Returns:
- immutable list of annotation instances, never
null
- Since:
- 2.0
-
addAnnotation
Type addAnnotation(AnnotationInstance annotation)
-
copyType
abstract Type copyType(AnnotationInstance[] newAnnotations)
-
toString
public java.lang.String toString()
Returns a string representation for this type. It is similar, yet not identical to a Java source code representation.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the string representation.
-
toString
java.lang.String toString(boolean simple)
-
appendAnnotations
void appendAnnotations(java.lang.StringBuilder builder)
-
descriptor
public java.lang.String descriptor(java.util.function.Function<java.lang.String,Type> typeVariableSubstitution)
Returns the bytecode descriptor of this type (or its erasure in case of generic types). Specifically:- for primitive types and the void pseudo-type, the single-character descriptor is returned;
- for class types, the
Lbinary/name/of/TheClass;
string is returned; - for array types, a string is returned that consists of one or more
[
characters corresponding to the number of dimensions of the array type, followed by the descriptor of the element type (for example,[I
forint[]
or[[Ljava/lang/String;
forString[][]
); - for parameterized types, the descriptor of the generic class is returned
(for example,
Ljava/util/List;
forList<String>
); - for type variables, the descriptor of the first bound of the type variable
is returned, or the descriptor of the
java.lang.Object
class for type variables that have no bound; - for wildcard types, the descriptor of the upper bound is returned,
or the descriptor of the
java.lang.Object
class if the wildcard type does not have an upper bound (for example,Ljava/lang/Number;
for? extends Number
).
typeVariableSubstitution
. If the substitution function returnsnull
for some type variable identifier, or if it returns the type variable itself, no substitution happens and the type variable descriptor is used unmodified.Note that the return value does not come directly from bytecode. Jandex does not store the descriptor strings. Instead, the return value is reconstructed from the Jandex object model.
- Specified by:
descriptor
in interfaceDescriptor
- Parameters:
typeVariableSubstitution
- a substitution function from type variable identifiers to types- Returns:
- the bytecode descriptor of this type (or its erasure in case of generic types)
-
equals
public boolean equals(java.lang.Object o)
Compares thisType
with another type. A type is equal to another type if it is of the same kind, and all of their fields are equal. This includes annotations, which must be equal as well.- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- the type to compare to- Returns:
- true if equal
- See Also:
Object.equals(Object)
-
hashCode
public int hashCode()
Computes a hash code representing this type.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code
-
internEquals
boolean internEquals(java.lang.Object o)
-
internHashCode
int internHashCode()
-
-