APIdia Launch Partner
Top Description Fields Constructors Methods
sun.reflect.annotation

public Class AnnotationParser

extends Object
Class Inheritance
Imports
java.lang.annotation.*, java.lang.reflect.*, java.nio.BufferUnderflowException, .ByteBuffer, java.util.*, java.util.function.Supplier, jdk.internal.reflect.ConstantPool, sun.invoke.util.BytecodeDescriptor

Parser for Java programming language annotations. Translates annotation byte streams emitted by compiler into annotation objects.
Author
Josh Bloch
Since
1.5

Field Summary

Modifier and TypeField and Description
private static final Annotation[]
private static final Annotation[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Annotation
annotationForMap(final Class<? extends Annotation> type, final Map<String, Object> memberValues)

Returns an annotation of the given type backed by the given member -> value map.

private static boolean
contains(Object[] array, Object element)

Searches for given element in given array by identity.

private static ExceptionProxy
exceptionProxy(int tag)

Returns an appropriate exception proxy for a mismatching array annotation where the erroneous array has the specified tag.

pack-priv static Annotation[]
pack-priv static Annotation
parseAnnotation(ByteBuffer buf, ConstantPool constPool, Class<?> container, boolean
if true, throw TypeNotPresentException if a referenced annotation type is not available at runtime
exceptionOnMissingAnnotationClass
)

Parses the annotation at the current position in the specified byte buffer, resolving constant references in the specified constant pool.

private static Annotation
parseAnnotation2(ByteBuffer buf, ConstantPool constPool, Class<?> container, boolean exceptionOnMissingAnnotationClass, Class<? extends Annotation>[] selectAnnotationClasses)

private static Object
parseAnnotationArray(int length, Class<? extends Annotation> annotationType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

public static Map<Class<? extends Annotation>, Annotation>
parseAnnotations(byte[] rawAnnotations, ConstantPool constPool, Class<?> container)

Parses the annotations described by the specified byte array.

private static Map<Class<? extends Annotation>, Annotation>
parseAnnotations2(byte[] rawAnnotations, ConstantPool constPool, Class<?> container, Class<? extends Annotation>[] selectAnnotationClasses)

private static Object
parseArray(Class<?> arrayType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

Parses the array value at the current position in the specified byte buffer, resolving constant references in the specified constant pool.

private static Object
parseArrayElements(Object[] result, ByteBuffer buf, int expectedTag, Supplier<Object> parseElement)

private static Object
parseBooleanArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseByteArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseCharArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseClassArray(int length, ByteBuffer buf, ConstantPool constPool, Class<?> container)

private static Object
parseClassValue(ByteBuffer buf, ConstantPool constPool, Class<?> container)

Parses the Class member value at the current position in the specified byte buffer, resolving constant references in the specified constant pool.

private static Object
parseConst(int tag, ByteBuffer buf, ConstantPool constPool)

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.

private static Object
parseDoubleArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseEnumArray(int length, Class<? extends Enum<?>> enumType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

private static Object
parseEnumValue(Class<? extends Enum<E extends Enum<E>>> enumType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

Parses the enum constant member value at the current position in the specified byte buffer, resolving constant references in the specified constant pool.

private static Object
parseFloatArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseIntArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseLongArray(int length, ByteBuffer buf, ConstantPool constPool)

public static Object
parseMemberValue(Class<?> memberType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

Parses the annotation member value at the current position in the specified byte buffer, resolving constant references in the specified constant pool.

public static Annotation[][]
parseParameterAnnotations(byte[] rawAnnotations, ConstantPool constPool, Class<?> container)

Parses the parameter annotations described by the specified byte array.

private static Annotation[][]
parseParameterAnnotations2(byte[] rawAnnotations, ConstantPool constPool, Class<?> container)

public static Map<Class<? extends Annotation>, Annotation>
parseSelectAnnotations(byte[] rawAnnotations, ConstantPool constPool, Class<?> container, Class<? extends Annotation>...
an array of annotation types to select when parsing
selectAnnotationClasses
)

Like parseAnnotations(byte[], sun.reflect.ConstantPool, Class) with an additional parameter selectAnnotationClasses which selects the annotation types to parse (other than selected are quickly skipped).

private static Object
parseShortArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Class<?>
parseSig(String sig, Class<?> container)

private static Object
parseStringArray(int length, ByteBuffer buf, ConstantPool constPool)

private static Object
parseUnknownArray(int length, ByteBuffer buf)

private static void
skipAnnotation(ByteBuffer buf, boolean
true if the byte buffer points to the beginning of an annotation structure (rather than two bytes in).
complete
)

Skips the annotation at the current position in the specified byte buffer.

private static void
skipArray(ByteBuffer buf)

Skips the array value at the current position in the specified byte buffer.

private static void
skipMemberValue(ByteBuffer buf)

Skips the annotation member value at the current position in the specified byte buffer.

private static void
skipMemberValue(int tag, ByteBuffer buf)

Skips the annotation member value at the current position in the specified byte buffer.

public static Annotation[]
toArray(Map<Class<? extends Annotation>, Annotation> annotations)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_ANNOTATION_ARRAYback to summary
private static final Annotation[] EMPTY_ANNOTATION_ARRAY
EMPTY_ANNOTATIONS_ARRAYback to summary
private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY

Constructor Detail

AnnotationParserback to summary
public AnnotationParser()

Method Detail

annotationForMapback to summary
public static Annotation annotationForMap(final Class<? extends Annotation> type, final Map<String, Object> memberValues)

Returns an annotation of the given type backed by the given member -> value map.

containsback to summary
private static boolean contains(Object[] array, Object element)

Searches for given element in given array by identity. Returns true if found false if not.

exceptionProxyback to summary
private static ExceptionProxy exceptionProxy(int tag)

Returns an appropriate exception proxy for a mismatching array annotation where the erroneous array has the specified tag.

getEmptyAnnotationArrayback to summary
pack-priv static Annotation[] getEmptyAnnotationArray()
parseAnnotationback to summary
pack-priv static Annotation parseAnnotation(ByteBuffer buf, ConstantPool constPool, Class<?> container, boolean exceptionOnMissingAnnotationClass)

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:boolean

if true, throw TypeNotPresentException if a referenced annotation type is not available at runtime

parseAnnotation2back to summary
private static Annotation parseAnnotation2(ByteBuffer buf, ConstantPool constPool, Class<?> container, boolean exceptionOnMissingAnnotationClass, Class<? extends Annotation>[] selectAnnotationClasses)
Annotations
@SuppressWarnings:unchecked
parseAnnotationArrayback to summary
private static Object parseAnnotationArray(int length, Class<? extends Annotation> annotationType, ByteBuffer buf, ConstantPool constPool, Class<?> container)
parseAnnotationsback to summary
public static Map<Class<? extends Annotation>, Annotation> parseAnnotations(byte[] rawAnnotations, ConstantPool constPool, Class<?> container)

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];

Exceptions
AnnotationFormatError:
if an annotation is found to be malformed.
parseAnnotations2back to summary
private static Map<Class<? extends Annotation>, Annotation> parseAnnotations2(byte[] rawAnnotations, ConstantPool constPool, Class<?> container, Class<? extends Annotation>[] selectAnnotationClasses)
parseArrayback to summary
private static Object parseArray(Class<?> arrayType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

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.

Annotations
@SuppressWarnings:unchecked
parseArrayElementsback to summary
private static Object parseArrayElements(Object[] result, ByteBuffer buf, int expectedTag, Supplier<Object> parseElement)
parseBooleanArrayback to summary
private static Object parseBooleanArray(int length, ByteBuffer buf, ConstantPool constPool)
parseByteArrayback to summary
private static Object parseByteArray(int length, ByteBuffer buf, ConstantPool constPool)
parseCharArrayback to summary
private static Object parseCharArray(int length, ByteBuffer buf, ConstantPool constPool)
parseClassArrayback to summary
private static Object parseClassArray(int length, ByteBuffer buf, ConstantPool constPool, Class<?> container)
parseClassValueback to summary
private static Object parseClassValue(ByteBuffer buf, ConstantPool constPool, Class<?> container)

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;

parseConstback to summary
private static Object parseConst(int tag, ByteBuffer buf, ConstantPool constPool)

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;

parseDoubleArrayback to summary
private static Object parseDoubleArray(int length, ByteBuffer buf, ConstantPool constPool)
parseEnumArrayback to summary
private static Object parseEnumArray(int length, Class<? extends Enum<?>> enumType, ByteBuffer buf, ConstantPool constPool, Class<?> container)
parseEnumValueback to summary
private static Object parseEnumValue(Class<? extends Enum<E extends Enum<E>>> enumType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

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;

Annotations
@SuppressWarnings:rawtypes, unchecked
parseFloatArrayback to summary
private static Object parseFloatArray(int length, ByteBuffer buf, ConstantPool constPool)
parseIntArrayback to summary
private static Object parseIntArray(int length, ByteBuffer buf, ConstantPool constPool)
parseLongArrayback to summary
private static Object parseLongArray(int length, ByteBuffer buf, ConstantPool constPool)
parseMemberValueback to summary
public static Object parseMemberValue(Class<?> memberType, ByteBuffer buf, ConstantPool constPool, Class<?> container)

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.

Annotations
@SuppressWarnings:unchecked
parseParameterAnnotationsback to summary
public static Annotation[][] parseParameterAnnotations(byte[] rawAnnotations, ConstantPool constPool, Class<?> container)

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.

Exceptions
AnnotationFormatError:
if an annotation is found to be malformed.
parseParameterAnnotations2back to summary
private static Annotation[][] parseParameterAnnotations2(byte[] rawAnnotations, ConstantPool constPool, Class<?> container)
parseSelectAnnotationsback to summary
public static Map<Class<? extends Annotation>, Annotation> parseSelectAnnotations(byte[] rawAnnotations, ConstantPool constPool, Class<?> container, Class<? extends Annotation>... selectAnnotationClasses)

Like parseAnnotations(byte[], sun.reflect.ConstantPool, Class) with an additional parameter selectAnnotationClasses which selects the annotation types to parse (other than selected are quickly skipped).

This method is used to parse select meta annotations in the construction phase of AnnotationType instances to prevent infinite recursion.

Parameters
selectAnnotationClasses:Class<? extends Annotation>[]

an array of annotation types to select when parsing

Annotations
@SafeVarargs
@SuppressWarnings:varargs
parseShortArrayback to summary
private static Object parseShortArray(int length, ByteBuffer buf, ConstantPool constPool)
parseSigback to summary
private static Class<?> parseSig(String sig, Class<?> container)
parseStringArrayback to summary
private static Object parseStringArray(int length, ByteBuffer buf, ConstantPool constPool)
parseUnknownArrayback to summary
private static Object parseUnknownArray(int length, ByteBuffer buf)
skipAnnotationback to summary
private static void skipAnnotation(ByteBuffer buf, boolean complete)

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).

Parameters
complete:boolean

true if the byte buffer points to the beginning of an annotation structure (rather than two bytes in).

skipArrayback to summary
private static void skipArray(ByteBuffer buf)

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.

skipMemberValueback to summary
private static void skipMemberValue(ByteBuffer buf)

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."

skipMemberValueback to summary
private static void skipMemberValue(int tag, ByteBuffer buf)

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."

toArrayback to summary
public static Annotation[] toArray(Map<Class<? extends Annotation>, Annotation> annotations)