Package org.jboss.jandex
Class AnnotationValue
- java.lang.Object
-
- org.jboss.jandex.AnnotationValue
-
public abstract class AnnotationValue extends Object
An annotation value represents a specific name and value combination in the parameter list of an annotation instance. It also can represent a nested array element in the case of an array value.An annotation value can be any Java primitive:
- byte
- short
- int
- char
- float
- double
- long
- boolean
As well as any the following specialty types:
- String
- Class
- Enum
- Nested annotation
In addition a value can be a single-dimension array of any of the above types
To access a value, the proper typed method must be used that matches the expected type of the annotation parameter. In addition, some methods will allow conversion of different types. For example, a byte can be returned as an integer using
asInt()
. Also all value types support a String representation.To determine the underlying type,
kind()
can be used.Thread-Safety
This class is immutable and can be shared between threads without safe publication.- Author:
- Jason T. Greene
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AnnotationValue.Kind
Specifies the kind of annotation value, which can be used to determine the underlying Java type.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
asBoolean()
Returns the underlying boolean value as Java primitive boolean.boolean[]
asBooleanArray()
Returns the underlying boolean array.byte
asByte()
Converts the underlying numerical type to a byte as if it was casted in Java.byte[]
asByteArray()
Converts an underlying numerical array to a Java primitive byte array.char
asChar()
Returns the underlying character value as Java primitive char.char[]
asCharArray()
Returns the underlying character array.Type
asClass()
Returns the class name, inType
form, that represents a Java Class used by this value.Type[]
asClassArray()
Returns an array of class types representing the underlying class array value.double
asDouble()
Converts the underlying numerical type to a double as if it was casted in Java.double[]
asDoubleArray()
Converts an underlying numerical array to a Java primitive double array.String
asEnum()
Returns the constant name, in string form, that represents the Java enumeration of this value.String[]
asEnumArray()
Returns an array of the constant name, in string form, that represents the Java enumeration of each array element The individual element values are the same as the one returned byEnum.name()
.DotName
asEnumType()
Returns the type name, in DotName form, that represents the Java enumeration of this value.DotName[]
asEnumTypeArray()
Returns an array of the type name, in DotName form, that represents the Java enumeration of each array element.float
asFloat()
Converts the underlying numerical type to a float as if it was casted in Java.float[]
asFloatArray()
Converts an underlying numerical array to a Java primitive float array.int
asInt()
Converts the underlying numerical type to an integer as if it was casted in Java.int[]
asIntArray()
Converts an underlying numerical array to a Java primitive integer array.long
asLong()
Converts the underlying numerical type to an long as if it was casted in Java.long[]
asLongArray()
Converts an underlying numerical array to a Java primitive long array.AnnotationInstance
asNested()
Returns a nested annotation represented by this value.AnnotationInstance[]
asNestedArray()
Returns an array of nested annotations representing the underlying annotation array value.short
asShort()
Converts the underlying numerical type to a short as if it was casted in Java.short[]
asShortArray()
Converts an underlying numerical array to a Java primitive short array.String
asString()
Returns the string representation of the underlying value type.String[]
asStringArray()
Returns a string array representation of the underlying array value.AnnotationValue.Kind
componentKind()
Returns the kind of value that represents the component type of this array if it can be determined.static AnnotationValue
createArrayValue(String name, AnnotationValue[] values)
static AnnotationValue
createBooleanValue(String name, boolean bool)
static AnnotationValue
createByteValue(String name, byte b)
static AnnotationValue
createCharacterValue(String name, char c)
static AnnotationValue
createClassValue(String name, Type type)
static AnnotationValue
createDoubleValue(String name, double d)
static AnnotationValue
createDouleValue(String name, double d)
Deprecated.UsecreateDoubleValue(String, double)
instead.static AnnotationValue
createEnumValue(String name, DotName typeName, String value)
static AnnotationValue
createFloatValue(String name, float f)
static AnnotationValue
createIntegerValue(String name, int i)
static AnnotationValue
createLongalue(String name, long l)
Deprecated.UsecreateLongValue(String, long)
instead.static AnnotationValue
createLongValue(String name, long l)
static AnnotationValue
createNestedAnnotationValue(String name, AnnotationInstance instance)
static AnnotationValue
createShortValue(String name, short s)
static AnnotationValue
createStringValue(String name, String string)
boolean
equals(Object o)
Compares this annotation value to another annotation value, and returns true if equal.int
hashCode()
Computes a hash code for this annotation value.abstract AnnotationValue.Kind
kind()
Returns the kind of this value.String
name()
Returns the name of this value, which is typically the parameter name in the annotation declaration.String
toString()
abstract Object
value()
Returns a detyped value that represents the underlying annotation value.
-
-
-
Method Detail
-
createByteValue
public static AnnotationValue createByteValue(String name, byte b)
-
createShortValue
public static AnnotationValue createShortValue(String name, short s)
-
createIntegerValue
public static AnnotationValue createIntegerValue(String name, int i)
-
createCharacterValue
public static AnnotationValue createCharacterValue(String name, char c)
-
createFloatValue
public static AnnotationValue createFloatValue(String name, float f)
-
createDouleValue
@Deprecated public static AnnotationValue createDouleValue(String name, double d)
Deprecated.UsecreateDoubleValue(String, double)
instead.
-
createDoubleValue
public static AnnotationValue createDoubleValue(String name, double d)
-
createLongalue
@Deprecated public static AnnotationValue createLongalue(String name, long l)
Deprecated.UsecreateLongValue(String, long)
instead.
-
createLongValue
public static AnnotationValue createLongValue(String name, long l)
-
createBooleanValue
public static AnnotationValue createBooleanValue(String name, boolean bool)
-
createStringValue
public static AnnotationValue createStringValue(String name, String string)
-
createClassValue
public static AnnotationValue createClassValue(String name, Type type)
-
createEnumValue
public static AnnotationValue createEnumValue(String name, DotName typeName, String value)
-
createArrayValue
public static AnnotationValue createArrayValue(String name, AnnotationValue[] values)
-
createNestedAnnotationValue
public static AnnotationValue createNestedAnnotationValue(String name, AnnotationInstance instance)
-
name
public final String name()
Returns the name of this value, which is typically the parameter name in the annotation declaration. The value may not represent a parameter (e.g an array element member), in which case name will simply return an empty string ("")- Returns:
- the name of this value
-
value
public abstract Object value()
Returns a detyped value that represents the underlying annotation value. It is recommended that the type specific methods be used instead.- Returns:
- the underlying value
-
kind
public abstract AnnotationValue.Kind kind()
Returns the kind of this value. The kind includes all Java primitives, String and Enum types, nested values, and finally arrays of the above. Since the return type is itself an enumeration, it can be used with Java switch statements.A special
AnnotationValue.Kind.UNKNOWN
kind is used to refer to components of zero-length arrays, as the underlying type is not known.- Returns:
- the kind of value
- Since:
- 2.0
-
componentKind
public AnnotationValue.Kind componentKind()
Returns the kind of value that represents the component type of this array if it can be determined. Otherwise, this method returnsAnnotationValue.Kind.UNKNOWN
. This happens when the annotation value represents an empty array. If the underlying value is not an array an exception will be thrown.- Returns:
- the kind of component, or
AnnotationValue.Kind.UNKNOWN
if it can not be determined. - Throws:
IllegalArgumentException
- if not an array- Since:
- 2.0
-
asInt
public int asInt()
Converts the underlying numerical type to an integer as if it was casted in Java.- Returns:
- an integer representing the numerical parameter
- Throws:
IllegalArgumentException
- if the value is not numerical
-
asLong
public long asLong()
Converts the underlying numerical type to an long as if it was casted in Java.- Returns:
- a long representing the numerical parameter
- Throws:
IllegalArgumentException
- if the value is not numerical
-
asShort
public short asShort()
Converts the underlying numerical type to a short as if it was casted in Java.- Returns:
- a short representing the numerical parameter
- Throws:
IllegalArgumentException
- if the value is not numerical
-
asByte
public byte asByte()
Converts the underlying numerical type to a byte as if it was casted in Java.- Returns:
- a byte representing the numerical parameter
- Throws:
IllegalArgumentException
- if the value is not numerical
-
asFloat
public float asFloat()
Converts the underlying numerical type to a float as if it was casted in Java.- Returns:
- a float representing the numerical parameter
- Throws:
IllegalArgumentException
- if the value is not numerical
-
asDouble
public double asDouble()
Converts the underlying numerical type to a double as if it was casted in Java.- Returns:
- a double representing the numerical parameter
- Throws:
IllegalArgumentException
- if the value is not numerical
-
asChar
public char asChar()
Returns the underlying character value as Java primitive char.- Returns:
- a char representing the character parameter
- Throws:
IllegalArgumentException
- if the value is not a character
-
asBoolean
public boolean asBoolean()
Returns the underlying boolean value as Java primitive boolean.- Returns:
- a boolean representing the character parameter
- Throws:
IllegalArgumentException
- if the value is not a boolean
-
asString
public String asString()
Returns the string representation of the underlying value type. The representation may or may not be convertible to the type it represents. This is best used on String types, but can also provide a useful way to quickly convert a value to a String.- Returns:
- a string representing the value parameter
-
asEnum
public String asEnum()
Returns the constant name, in string form, that represents the Java enumeration of this value. The value is the same as the one returned byEnum.name()
.- Returns:
- the string name of a Java enumeration
- Throws:
IllegalArgumentException
- if the value is not an enum
-
asEnumType
public DotName asEnumType()
Returns the type name, in DotName form, that represents the Java enumeration of this value. The value is the same as the one returned byObject.getClass()
.- Returns:
- the type name of a Java enumeration
- Throws:
IllegalArgumentException
- if the value is not an enum
-
asClass
public Type asClass()
Returns the class name, inType
form, that represents a Java Class used by this value. In addition to standard class name, it can also refer to specialty types, such asVoid
and primitive types (e.g. int.class). More specifically, any erased type that a method can return is a valid annotation Class type.- Returns:
- the Java type of this value
- Throws:
IllegalArgumentException
- if the value is not a Class
-
asNested
public AnnotationInstance asNested()
Returns a nested annotation represented by this value. The nested annotation will have a null target, but may contain an arbitrary amount of nested values- Returns:
- the underlying nested annotation instance
- Throws:
IllegalArgumentException
- if the value is not a nested annotation
-
asIntArray
public int[] asIntArray()
Converts an underlying numerical array to a Java primitive integer array.- Returns:
- an int array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a numerical array.
-
asLongArray
public long[] asLongArray()
Converts an underlying numerical array to a Java primitive long array.- Returns:
- a long array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a numerical array.
-
asShortArray
public short[] asShortArray()
Converts an underlying numerical array to a Java primitive short array.- Returns:
- a short array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a numerical array.
-
asByteArray
public byte[] asByteArray()
Converts an underlying numerical array to a Java primitive byte array.- Returns:
- a byte array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a numerical array.
-
asFloatArray
public float[] asFloatArray()
Converts an underlying numerical array to a Java primitive float array.- Returns:
- a float array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a numerical array.
-
asDoubleArray
public double[] asDoubleArray()
Converts an underlying numerical array to a Java primitive double array.- Returns:
- a double array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a numerical array.
-
asCharArray
public char[] asCharArray()
Returns the underlying character array.- Returns:
- a character array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a character array.
-
asBooleanArray
public boolean[] asBooleanArray()
Returns the underlying boolean array.- Returns:
- a boolean array that represents this value
- Throws:
IllegalArgumentException
- if this value is not a boolean array.
-
asStringArray
public String[] asStringArray()
Returns a string array representation of the underlying array value. The behavior is identical toasString()
as if it were applied to every array element.- Returns:
- a string array representing the underlying array value
- Throws:
IllegalArgumentException
- if this value is not an array
-
asEnumArray
public String[] asEnumArray()
Returns an array of the constant name, in string form, that represents the Java enumeration of each array element The individual element values are the same as the one returned byEnum.name()
.- Returns:
- an array of string names of a Java enums
- Throws:
IllegalArgumentException
- if the value is not an enum array
-
asEnumTypeArray
public DotName[] asEnumTypeArray()
Returns an array of the type name, in DotName form, that represents the Java enumeration of each array element. The individual element values are the same as the one returned byObject.getClass()
. Note that JLS restricts an enum array parameter to the same type. Also, when an empty array is specified in a value, it's types can not be determined.- Returns:
- an array of string type names of Java enum array elements
- Throws:
IllegalArgumentException
- if the value is not an enum array
-
asClassArray
public Type[] asClassArray()
Returns an array of class types representing the underlying class array value. Each element has the same behavior asasClass()
- Returns:
- a class array representing this class array value
- Throws:
IllegalArgumentException
- if the value is not a class array
-
asNestedArray
public AnnotationInstance[] asNestedArray()
Returns an array of nested annotations representing the underlying annotation array value. Each element has the same behavior asasNested()
- Returns:
- an annotation array representing this annotation array value
- Throws:
IllegalArgumentException
- if the value is not an annotation array
-
equals
public boolean equals(Object o)
Compares this annotation value to another annotation value, and returns true if equal.- Overrides:
equals
in classObject
- Parameters:
o
- the annotation value to compare to.- Returns:
- true if equal, false if not
- See Also:
Object.equals(Object)
-
hashCode
public int hashCode()
Computes a hash code for this annotation value.- Overrides:
hashCode
in classObject
- Returns:
- the hash code for this annotation value
- See Also:
Object.hashCode()
-
-