public abstract class AnnotationValue extends Object
An annotation value can be any Java primitive:
As well as any the following specialty types:
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.Modifier and Type | Class and Description |
---|---|
static class |
AnnotationValue.Kind
Specifies the kind of annotation value, which can be used to determine the underlying Java type.
|
Modifier and Type | Method and 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, in
Type 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 by
Enum.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 |
createDouleValue(String name,
double d) |
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) |
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.
|
public static AnnotationValue createByteValue(String name, byte b)
public static AnnotationValue createShortValue(String name, short s)
public static AnnotationValue createIntegerValue(String name, int i)
public static AnnotationValue createCharacterValue(String name, char c)
public static AnnotationValue createFloatValue(String name, float f)
public static AnnotationValue createDouleValue(String name, double d)
public static AnnotationValue createLongalue(String name, long l)
public static AnnotationValue createBooleanValue(String name, boolean bool)
public static AnnotationValue createStringValue(String name, String string)
public static AnnotationValue createClassValue(String name, Type type)
public static AnnotationValue createEnumValue(String name, DotName typeName, String value)
public static AnnotationValue createArrayValue(String name, AnnotationValue[] values)
public static AnnotationValue createNestedAnnotationValue(String name, AnnotationInstance instance)
public final String name()
public abstract Object value()
public abstract AnnotationValue.Kind kind()
A special AnnotationValue.Kind.UNKNOWN
kind is used to refer to components
of zero-length arrays, as the underlying type is not known.
public AnnotationValue.Kind componentKind()
AnnotationValue.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.AnnotationValue.Kind.UNKNOWN
if it can not be determined.IllegalArgumentException
- if not an arraypublic int asInt()
IllegalArgumentException
- if the value is not numericalpublic long asLong()
IllegalArgumentException
- if the value is not numericalpublic short asShort()
IllegalArgumentException
- if the value is not numericalpublic byte asByte()
IllegalArgumentException
- if the value is not numericalpublic float asFloat()
IllegalArgumentException
- if the value is not numericalpublic double asDouble()
IllegalArgumentException
- if the value is not numericalpublic char asChar()
IllegalArgumentException
- if the value is not a characterpublic boolean asBoolean()
IllegalArgumentException
- if the value is not a booleanpublic String asString()
public String asEnum()
Enum.name()
.IllegalArgumentException
- if the value is not an enumpublic DotName asEnumType()
Object.getClass()
.IllegalArgumentException
- if the value is not an enumpublic Type asClass()
Type
form, that represents a Java
Class used by this value. In addition to standard class name, it can also
refer to specialty types, such as Void
and primitive types (e.g.
int.class). More specifically, any erased type that a method can return
is a valid annotation Class type.IllegalArgumentException
- if the value is not a Classpublic AnnotationInstance asNested()
IllegalArgumentException
- if the value is not a nested annotationpublic int[] asIntArray()
IllegalArgumentException
- if this value is not a numerical array.public long[] asLongArray()
IllegalArgumentException
- if this value is not a numerical array.public short[] asShortArray()
IllegalArgumentException
- if this value is not a numerical array.public byte[] asByteArray()
IllegalArgumentException
- if this value is not a numerical array.public float[] asFloatArray()
IllegalArgumentException
- if this value is not a numerical array.public double[] asDoubleArray()
IllegalArgumentException
- if this value is not a numerical array.public char[] asCharArray()
IllegalArgumentException
- if this value is not a character array.public boolean[] asBooleanArray()
IllegalArgumentException
- if this value is not a boolean array.public String[] asStringArray()
asString()
as if it were applied
to every array element.IllegalArgumentException
- if this value is not an arraypublic String[] asEnumArray()
Enum.name()
.IllegalArgumentException
- if the value is not an enum arraypublic DotName[] asEnumTypeArray()
Object.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.IllegalArgumentException
- if the value is not an enum arraypublic Type[] asClassArray()
asClass()
IllegalArgumentException
- if the value is not a class arraypublic AnnotationInstance[] asNestedArray()
asNested()
IllegalArgumentException
- if the value is not an annotation arraypublic boolean equals(Object o)
equals
in class Object
o
- the annotation value to compare to.Object.equals(Object)
public int hashCode()
hashCode
in class Object
Object.hashCode()
Copyright © 2018 JBoss by Red Hat. All rights reserved.