package sun.reflect.annotation;
Parser for Java programming language annotations. Translates
annotation byte streams emitted by compiler into annotation objects.
- Author(s):
- Josh Bloch
- Since:
- 1.5
Parses the annotations described by the specified byte array.
resolving constant references in the specified constant pool.
The array must contain an array of annotations as described
in the RuntimeVisibleAnnotations_attribute:
u2 num_annotations;
annotation annotations[num_annotations];
if (rawAnnotations == null)
int numAnnotations = buf.getShort() & 0xFFFF;
for (int i = 0; i < numAnnotations; i++) { if (result.put(klass, a) != null)
"Duplicate annotation for class: "+klass+": " + a);
Parses the parameter annotations described by the specified byte array.
resolving constant references in the specified constant pool.
The array must contain an array of annotations as described
in the RuntimeVisibleParameterAnnotations_attribute:
u1 num_parameters;
{
u2 num_annotations;
annotation annotations[num_annotations];
} parameter_annotations[num_parameters];
Unlike parseAnnotations, rawAnnotations must not be null!
A null value must be handled by the caller. This is so because
we cannot determine the number of parameters if rawAnnotations
is null. Also, the caller should check that the number
of parameters indicated by the return value of this method
matches the actual number of method parameters. A mismatch
indicates that an AnnotationFormatError should be thrown.
"Unexpected end of parameter annotations.");
int numParameters = buf.get() & 0xFF;
for (int i = 0; i < numParameters; i++) { int numAnnotations = buf.getShort() & 0xFFFF;
for (int j = 0; j < numAnnotations; j++) { Parses the annotation at the current position in the specified
byte buffer, resolving constant references in the specified constant
pool. The cursor of the byte buffer must point to an "annotation
structure" as described in the RuntimeVisibleAnnotations_attribute:
annotation {
u2 type_index;
u2 num_member_value_pairs;
{ u2 member_name_index;
member_value value;
} member_value_pairs[num_member_value_pairs];
}
}
Returns the annotation, or null if the annotation's type cannot
be found by the VM, or is not a valid annotation type.
- Parameters:
exceptionOnMissingAnnotationClass if true, throw
TypeNotPresentException if a referenced annotation type is not
available at runtime
boolean exceptionOnMissingAnnotationClass) { int typeIndex = buf.getShort() & 0xFFFF;
Class annotationClass = null;
annotationClass = parseSig(sig, container);
annotationClass = constPool.getClassAt(typeIndex);
if (exceptionOnMissingAnnotationClass)
if (exceptionOnMissingAnnotationClass)
int numMembers = buf.getShort() & 0xFFFF;
for (int i = 0; i < numMembers; i++) { int memberNameIndex = buf.getShort() & 0xFFFF;
Class memberType = memberTypes.get(memberName);
if (memberType == null) { memberValues.put(memberName, value);
Returns an annotation of the given type backed by the given
member -> value map.
Parses the annotation member value at the current position in the
specified byte buffer, resolving constant references in the specified
constant pool. The cursor of the byte buffer must point to a
"member_value structure" as described in the
RuntimeVisibleAnnotations_attribute:
member_value {
u1 tag;
union {
u2 const_value_index;
{
u2 type_name_index;
u2 const_name_index;
} enum_const_value;
u2 class_info_index;
annotation annotation_value;
{
u2 num_values;
member_value values[num_values];
} array_value;
} value;
}
The member must be of the indicated type. If it is not, this
method returns an AnnotationTypeMismatchExceptionProxy.
return parseArray(memberType, buf, constPool, container);
result.getClass() + "[" + result + "]");
Parses the primitive or String annotation member value indicated by
the specified tag byte at the current position in the specified byte
buffer, resolving constant reference in the specified constant pool.
The cursor of the byte buffer must point to an annotation member value
of the type indicated by the specified tag, as described in the
RuntimeVisibleAnnotations_attribute:
u2 const_value_index;
int constIndex = buf.getShort() & 0xFFFF;
"Invalid member-value tag in annotation: " + tag);
Parses the Class member value at the current position in the
specified byte buffer, resolving constant references in the specified
constant pool. The cursor of the byte buffer must point to a "class
info index" as described in the RuntimeVisibleAnnotations_attribute:
u2 class_info_index;
int classIndex = buf.getShort() & 0xFFFF;
if (sig.equals("V")) return void.class; Parses the enum constant member value at the current position in the
specified byte buffer, resolving constant references in the specified
constant pool. The cursor of the byte buffer must point to a
"enum_const_value structure" as described in the
RuntimeVisibleAnnotations_attribute:
{
u2 type_name_index;
u2 const_name_index;
} enum_const_value;
int typeNameIndex = buf.getShort() & 0xFFFF;
int constNameIndex = buf.getShort() & 0xFFFF;
typeName + "." + constName);
} else if (enumType != parseSig(typeName, container)) { typeName + "." + constName);
return Enum.valueOf(enumType, constName);
Parses the array value at the current position in the specified byte
buffer, resolving constant references in the specified constant pool.
The cursor of the byte buffer must point to an array value struct
as specified in the RuntimeVisibleAnnotations_attribute:
{
u2 num_values;
member_value values[num_values];
} array_value;
If the array values do not match arrayType, an
AnnotationTypeMismatchExceptionProxy will be returned.
if (componentType == byte.class) { } else if (componentType == char.class) { } else if (componentType == double.class) { } else if (componentType == float.class) { } else if (componentType == int.class) { } else if (componentType == long.class) { } else if (componentType == short.class) { } else if (componentType == boolean.class) { } else if (componentType == String.class) { } else if (componentType == Class.class) { } else if (componentType.isEnum()) { byte[] result = new byte[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { result[i] = (byte) constPool.getIntAt(index);
char[] result = new char[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { result[i] = (char) constPool.getIntAt(index);
double[] result = new double[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { float[] result = new float[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { int[] result = new int[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { long[] result = new long[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { short[] result = new short[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { result[i] = (short) constPool.getIntAt(index);
boolean[] result = new boolean[length];
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { result[i] = (constPool.getIntAt(index) != 0);
boolean typeMismatch = false;
for (int i = 0; i < length; i++) { boolean typeMismatch = false;
for (int i = 0; i < length; i++) { boolean typeMismatch = false;
for (int i = 0; i < length; i++) { boolean typeMismatch = false;
for (int i = 0; i < length; i++) { Return an appropriate exception proxy for a mismatching array
annotation where the erroneous array has the specified tag.
"Array with component tag: " + tag);
Skips the annotation at the current position in the specified
byte buffer. The cursor of the byte buffer must point to
an "annotation structure" OR two bytes into an annotation
structure (i.e., after the type index).
- Parameter:
- complete true if the byte buffer points to the beginning
of an annotation structure (rather than two bytes in).
int numMembers = buf.getShort() & 0xFFFF;
for (int i = 0; i < numMembers; i++) { Skips the annotation member value at the current position in the
specified byte buffer. The cursor of the byte buffer must point to a
"member_value structure."
Skips the annotation member value at the current position in the
specified byte buffer. The cursor of the byte buffer must point
immediately after the tag in a "member_value structure."
Skips the array value at the current position in the specified byte
buffer. The cursor of the byte buffer must point to an array value
struct.
for (int i = 0; i < length; i++)