Top Description Inners Fields Constructors Methods
org.jboss.jandex

public final Class AnnotationInstance

extends Object
Class Inheritance
Imports
java.lang.annotation.Annotation, .Retention, .RetentionPolicy, java.util.ArrayList, .Arrays, .Collections, .Comparator, .List

An annotation instance represents a specific usage of an annotation on a target. It contains a set of members, as well as a reference to the target itself (e.g. class, field, method, etc).

Thread-Safety

This class is immutable and can be shared between threads without safe publication.
Author
Jason T. Greene

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final AnnotationInstance[]
private final DotName
pack-priv static final AnnotationInstance.NameComparator
pack-priv static final DotName
private final boolean
private final AnnotationTarget
private final AnnotationValue[]

Constructor Summary

AccessConstructor and Description
private
AnnotationInstance(DotName name, AnnotationTarget target, AnnotationValue[] values, boolean runtimeVisible)

Method Summary

Modifier and TypeMethod and Description
pack-priv static AnnotationInstance
public static AnnotationInstanceBuilder

Returns:

a builder instance, never null
builder
(Class<? extends Annotation>
the type of annotation whose instance will be created, must not be null
annotationType
)

Creates a builder of annotation instances of given annotationType.

public static AnnotationInstanceBuilder

Returns:

a builder instance, never null
builder
(ClassInfo
the type of annotation whose instance will be created, must not be null
annotationType
)

Creates a builder of annotation instances of given annotationType.

public static AnnotationInstanceBuilder

Returns:

a builder instance, never null
builder
(DotName
the type of annotation whose instance will be created, must not be null
annotationType
)

Creates a builder of annotation instances of given annotationType.

public static AnnotationInstanceBuilder

Returns:

a builder instance, never null
builder
(DotName
the type of annotation whose instance will be created, must not be null
annotationType
,
boolean
whether the annotation type has the runtime retention
runtimeVisible
)

Creates a builder of annotation instances of given annotationType.

pack-priv static AnnotationInstance
public static AnnotationInstance

Returns:

the new mock Annotation Instance
create
(DotName
the name of the annotation instance
name
,
AnnotationTarget
the thing the annotation is declared on
target
,
AnnotationValue[]
the members of this annotation instance
values
)

Construct a new mock annotation instance.

public static AnnotationInstance

Returns:

the new mock Annotation Instance
create
(DotName
the name of the annotation instance
name
,
boolean
whether the annotation is visible at runtime via the reflection API
visible
,
AnnotationTarget
the thing the annotation is declared on
target
,
AnnotationValue[]
the members of this annotation instance
values
)

Construct a new mock annotation instance.

public static AnnotationInstance

Returns:

the new mock Annotation Instance
create
(DotName
the name of the annotation instance
name
,
AnnotationTarget
the thing the annotation is declared on
target
,
List<AnnotationValue>
the members of this annotation instance
values
)

Construct a new mock annotation instance.

public static AnnotationInstance

Returns:

the new mock Annotation Instance
create
(DotName
the name of the annotation instance
name
,
boolean
whether the annotation is visible at runtime via the reflection API
visible
,
AnnotationTarget
the thing the annotation is declared on
target
,
List<AnnotationValue>
the members of this annotation instance
values
)

Construct a new mock annotation instance.

public AnnotationInstanceEquivalenceProxy
createEquivalenceProxy()

Returns an equivalence proxy for this annotation instance.

public boolean

Returns:

true if equal, false if not
equals
(Object
the annotation instance to compare to
o
)

Overrides java.lang.Object.equals.

Returns whether this annotation instance is equal to another instance.
public int

Returns:

hash code consistent with annotation equivalence
equivalenceHashCode
()

Returns an equivalence hash code.

public boolean

Returns:

true if equivalent, false if not
equivalentTo
(AnnotationInstance
the annotation instance to compare to
other
)

Returns whether this annotation instance is equivalent to the other annotation instance.

public int

Returns:

the hash code of this object
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hash code representing this object.
public DotName

Returns:

the name of this annotation
name
()

The name of this annotation in DotName form.

public boolean
runtimeVisible()

Returns true if this annotation uses RetentionPolicy.RUNTIME and is visible to runtime reflection.

public AnnotationTarget

Returns:

the target this annotation instance refers to
target
()

The program element that this annotation was declared on.

public String

Returns:

a string representation for this object
toString
(boolean
whether to provide a simpler string representation
simple
)

Returns an optionally simplified string that represents this annotation instance.

public String

Returns:

a simple string representation for this annotation instance
toString
()

Overrides java.lang.Object.toString.

Returns a string representation for this annotation instance.
public AnnotationValue

Returns:

the annotation member with specified name, or null
value
(final String
the name of the annotation member
name
)

Returns the member of this annotation that has the specified name.

public AnnotationValue

Returns:

the value member
value
()

Returns the member that has the special default name value.

pack-priv AnnotationValue[]
public List<AnnotationValue>

Returns:

immutable list of this annotation's members
values
()

Returns an immutable list of all members of this annotation instance.

public List<AnnotationValue>

Returns:

immutable list of this annotation's members
valuesWithDefaults
(IndexView index)

Returns a list of all members of this annotation instance, including default values if defined.

public AnnotationValue

Returns:

the annotation member with specified name, or null
valueWithDefault
(IndexView
the index containing the defining annotation class
index
,
String
the name of the annotation member
name
)

Returns the member of this annotation that has the specified name, accounting for its default value.

public AnnotationValue

Returns:

the value annotation member, or null
valueWithDefault
(IndexView
the index containing the defining annotation class
index
)

Returns the member of this annotation that has special name value, accounting for its default value.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

EMPTY_ARRAYback to summary
pack-priv static final AnnotationInstance[] EMPTY_ARRAY
nameback to summary
private final DotName name
NAME_COMPARATORback to summary
pack-priv static final AnnotationInstance.NameComparator NAME_COMPARATOR
RETENTIONback to summary
pack-priv static final DotName RETENTION
runtimeVisibleback to summary
private final boolean runtimeVisible
targetback to summary
private final AnnotationTarget target
valuesback to summary
private final AnnotationValue[] values

Constructor Detail

AnnotationInstanceback to summary
private AnnotationInstance(DotName name, AnnotationTarget target, AnnotationValue[] values, boolean runtimeVisible)

Method Detail

binarySearchback to summary
pack-priv static AnnotationInstance binarySearch(AnnotationInstance[] annotations, DotName name)
builderback to summary
public static AnnotationInstanceBuilder builder(Class<? extends Annotation> annotationType)

Creates a builder of annotation instances of given annotationType. This allows creating an AnnotationInstance in a nicer way than using any of the create() methods.

Parameters
annotationType:Class<? extends Annotation>

the type of annotation whose instance will be created, must not be null

Returns:AnnotationInstanceBuilder

a builder instance, never null

builderback to summary
public static AnnotationInstanceBuilder builder(ClassInfo annotationType)

Creates a builder of annotation instances of given annotationType. This allows creating an AnnotationInstance in a nicer way than using any of the create() methods.

Parameters
annotationType:ClassInfo

the type of annotation whose instance will be created, must not be null

Returns:AnnotationInstanceBuilder

a builder instance, never null

builderback to summary
public static AnnotationInstanceBuilder builder(DotName annotationType)

Creates a builder of annotation instances of given annotationType. This allows creating an AnnotationInstance in a nicer way than using any of the create() methods.

The given annotationType is assumed to have the runtime retention.

Parameters
annotationType:DotName

the type of annotation whose instance will be created, must not be null

Returns:AnnotationInstanceBuilder

a builder instance, never null

builderback to summary
public static AnnotationInstanceBuilder builder(DotName annotationType, boolean runtimeVisible)

Creates a builder of annotation instances of given annotationType. This allows creating an AnnotationInstance in a nicer way than using any of the create() methods.

Parameters
annotationType:DotName

the type of annotation whose instance will be created, must not be null

runtimeVisible:boolean

whether the annotation type has the runtime retention

Returns:AnnotationInstanceBuilder

a builder instance, never null

createback to summary
pack-priv static AnnotationInstance create(AnnotationInstance instance, AnnotationTarget target)
createback to summary
public static AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] values)

Construct a new mock annotation instance. The passed values array will be defensively copied. It is assumed that the annotation is runtimeVisible().

Parameters
name:DotName

the name of the annotation instance

target:AnnotationTarget

the thing the annotation is declared on

values:AnnotationValue[]

the members of this annotation instance

Returns:AnnotationInstance

the new mock Annotation Instance

createback to summary
public static AnnotationInstance create(DotName name, boolean visible, AnnotationTarget target, AnnotationValue[] values)

Construct a new mock annotation instance. The passed values array will be defensively copied.

Parameters
name:DotName

the name of the annotation instance

visible:boolean

whether the annotation is visible at runtime via the reflection API

target:AnnotationTarget

the thing the annotation is declared on

values:AnnotationValue[]

the members of this annotation instance

Returns:AnnotationInstance

the new mock Annotation Instance

createback to summary
public static AnnotationInstance create(DotName name, AnnotationTarget target, List<AnnotationValue> values)

Construct a new mock annotation instance. The passed values list will be defensively copied. It is assumed that the annotation is runtimeVisible().

Parameters
name:DotName

the name of the annotation instance

target:AnnotationTarget

the thing the annotation is declared on

values:List<AnnotationValue>

the members of this annotation instance

Returns:AnnotationInstance

the new mock Annotation Instance

createback to summary
public static AnnotationInstance create(DotName name, boolean visible, AnnotationTarget target, List<AnnotationValue> values)

Construct a new mock annotation instance. The passed values list will be defensively copied.

Parameters
name:DotName

the name of the annotation instance

visible:boolean

whether the annotation is visible at runtime via the reflection API

target:AnnotationTarget

the thing the annotation is declared on

values:List<AnnotationValue>

the members of this annotation instance

Returns:AnnotationInstance

the new mock Annotation Instance

createEquivalenceProxyback to summary
public AnnotationInstanceEquivalenceProxy createEquivalenceProxy()

Returns an equivalence proxy for this annotation instance.

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Returns whether this annotation instance is equal to another instance. Two annotation instances are equal if their names and values of their members are equal, and they share the exact same AnnotationTarget instance.

Parameters
o:Object

the annotation instance to compare to

Returns:boolean

true if equal, false if not

Annotations
@Override
See Also
Object#equals(Object)
equivalenceHashCodeback to summary
public int equivalenceHashCode()

Returns an equivalence hash code. This is consistent with equivalentTo(AnnotationInstance), therefore no attention is paid to the annotation target.

Returns:int

hash code consistent with annotation equivalence

equivalentToback to summary
public boolean equivalentTo(AnnotationInstance other)

Returns whether this annotation instance is equivalent to the other annotation instance. Two annotation instances are equivalent if their names and members are equal. No attention is paid to the annotation target.

Parameters
other:AnnotationInstance

the annotation instance to compare to

Returns:boolean

true if equivalent, false if not

See Also
equals(Object)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code representing this object.

Returns:int

the hash code of this object

Annotations
@Override
See Also
Object#hashCode()
nameback to summary
public DotName name()

The name of this annotation in DotName form.

Returns:DotName

the name of this annotation

runtimeVisibleback to summary
public boolean runtimeVisible()

Returns true if this annotation uses RetentionPolicy.RUNTIME and is visible to runtime reflection.

Since
3.0
targetback to summary
public AnnotationTarget target()

The program element that this annotation was declared on. This can be a class, a field, a method, a method parameter, or a type in case of type annotations. In addition, it may be null if this instance is a nested annotation, in which case there is no target.

Returns:AnnotationTarget

the target this annotation instance refers to

toStringback to summary
public String toString(boolean simple)

Returns an optionally simplified string that represents this annotation instance. If simplified the output is smaller but missing information, such as the package qualifier.

Parameters
simple:boolean

whether to provide a simpler string representation

Returns:String

a string representation for this object

Since
2.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation for this annotation instance. This method is equivalent to calling toString(boolean) with a value of true.

Returns:String

a simple string representation for this annotation instance

valueback to summary
public AnnotationValue value(final String name)

Returns the member of this annotation that has the specified name. If the member was not specified by this instance, null is returned. Note that this also applies to a defaulted member, whose value is not recorded in the target class file.

Parameters
name:String

the name of the annotation member

Returns:AnnotationValue

the annotation member with specified name, or null

valueback to summary
public AnnotationValue value()

Returns the member that has the special default name value.

Returns:AnnotationValue

the value member

valueArrayback to summary
pack-priv AnnotationValue[] valueArray()
valuesback to summary
public List<AnnotationValue> values()

Returns an immutable list of all members of this annotation instance. While random access is allowed, the ordering algorithm of the list should not be relied upon. Although it will be consistent for the life of this instance.

Returns:List<AnnotationValue>

immutable list of this annotation's members

valuesWithDefaultsback to summary
public List<AnnotationValue> valuesWithDefaults(IndexView index)

Returns a list of all members of this annotation instance, including default values if defined. Since an annotation's defaults are only stored on the annotation's defining class, and not usages of the annotation, an index containing the annotation class must be provided as a parameter. If the index does not contain the defining annotation class, then an IllegalArgumentException will be thrown to prevent non-deterministic results.

The order of this list is undefined.

Returns:List<AnnotationValue>

immutable list of this annotation's members

Exceptions
IllegalArgumentException:
if index does not contain the defining annotation class
Since
2.1
valueWithDefaultback to summary
public AnnotationValue valueWithDefault(IndexView index, String name)

Returns the member of this annotation that has the specified name, accounting for its default value. Since an annotation's defaults are only stored on the annotation's defining class, and not usages of the annotation, an index containing the annotation class must be provided as a parameter. If the index does not contain the defining annotation class, then an IllegalArgumentException will be thrown to prevent non-deterministic results.

If the member was not specified by this annotation instance, then the annotation's ClassInfo is checked for a default value. If there is a default, that value is returned. Otherwise null is returned.

Parameters
index:IndexView

the index containing the defining annotation class

name:String

the name of the annotation member

Returns:AnnotationValue

the annotation member with specified name, or null

Exceptions
IllegalArgumentException:
if index does not contain the defining annotation class
Since
2.1
valueWithDefaultback to summary
public AnnotationValue valueWithDefault(IndexView index)

Returns the member of this annotation that has special name value, accounting for its default value. Since an annotation's defaults are only stored on the annotation's defining class, and not usages of the annotation, an index containing the annotation class must be provided as a parameter. If the index does not contain the defining annotation class, then an IllegalArgumentException will be thrown to prevent non-deterministic results.

If the value member was not specified by this instance, then the annotation's ClassInfo is checked for a default value. If there is a default, that value is returned. Otherwise null is returned.

Parameters
index:IndexView

the index containing the defining annotation class

Returns:AnnotationValue

the value annotation member, or null

Exceptions
IllegalArgumentException:
if index does not contain the defining annotation class
Since
2.1
org.jboss.jandex back to summary

pack-priv Class AnnotationInstance.NameComparator

extends Object
implements Comparator<AnnotationInstance>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int
compare(AnnotationInstance
the first object to be compared.
instance1
,
AnnotationInstance
the second object to be compared.
instance2
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

NameComparatorback to summary
pack-priv NameComparator()

Method Detail

compareback to summary
public int compare(AnnotationInstance instance1, AnnotationInstance instance2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
instance1:AnnotationInstance

the first object to be compared.

instance2:AnnotationInstance

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.