Path: blob/master/src/java.base/share/classes/sun/reflect/annotation/AnnotationParser.java
41159 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.reflect.annotation;2627import java.lang.annotation.*;28import java.lang.reflect.*;29import java.nio.BufferUnderflowException;30import java.nio.ByteBuffer;31import java.util.*;32import java.util.function.Supplier;33import java.security.AccessController;34import java.security.PrivilegedAction;35import jdk.internal.reflect.ConstantPool;3637import sun.reflect.generics.parser.SignatureParser;38import sun.reflect.generics.tree.TypeSignature;39import sun.reflect.generics.factory.GenericsFactory;40import sun.reflect.generics.factory.CoreReflectionFactory;41import sun.reflect.generics.visitor.Reifier;42import sun.reflect.generics.scope.ClassScope;4344/**45* Parser for Java programming language annotations. Translates46* annotation byte streams emitted by compiler into annotation objects.47*48* @author Josh Bloch49* @since 1.550*/51public class AnnotationParser {52/**53* Parses the annotations described by the specified byte array.54* resolving constant references in the specified constant pool.55* The array must contain an array of annotations as described56* in the RuntimeVisibleAnnotations_attribute:57*58* u2 num_annotations;59* annotation annotations[num_annotations];60*61* @throws AnnotationFormatError if an annotation is found to be62* malformed.63*/64public static Map<Class<? extends Annotation>, Annotation> parseAnnotations(65byte[] rawAnnotations,66ConstantPool constPool,67Class<?> container) {68if (rawAnnotations == null)69return Collections.emptyMap();7071try {72return parseAnnotations2(rawAnnotations, constPool, container, null);73} catch(BufferUnderflowException e) {74throw new AnnotationFormatError("Unexpected end of annotations.");75} catch(IllegalArgumentException e) {76// Type mismatch in constant pool77throw new AnnotationFormatError(e);78}79}8081/**82* Like {@link #parseAnnotations(byte[], sun.reflect.ConstantPool, Class)}83* with an additional parameter {@code selectAnnotationClasses} which selects the84* annotation types to parse (other than selected are quickly skipped).<p>85* This method is only used to parse select meta annotations in the construction86* phase of {@link AnnotationType} instances to prevent infinite recursion.87*88* @param selectAnnotationClasses an array of annotation types to select when parsing89*/90@SafeVarargs91@SuppressWarnings("varargs") // selectAnnotationClasses is used safely92static Map<Class<? extends Annotation>, Annotation> parseSelectAnnotations(93byte[] rawAnnotations,94ConstantPool constPool,95Class<?> container,96Class<? extends Annotation> ... selectAnnotationClasses) {97if (rawAnnotations == null)98return Collections.emptyMap();99100try {101return parseAnnotations2(rawAnnotations, constPool, container, selectAnnotationClasses);102} catch(BufferUnderflowException e) {103throw new AnnotationFormatError("Unexpected end of annotations.");104} catch(IllegalArgumentException e) {105// Type mismatch in constant pool106throw new AnnotationFormatError(e);107}108}109110private static Map<Class<? extends Annotation>, Annotation> parseAnnotations2(111byte[] rawAnnotations,112ConstantPool constPool,113Class<?> container,114Class<? extends Annotation>[] selectAnnotationClasses) {115Map<Class<? extends Annotation>, Annotation> result =116new LinkedHashMap<Class<? extends Annotation>, Annotation>();117ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);118int numAnnotations = buf.getShort() & 0xFFFF;119for (int i = 0; i < numAnnotations; i++) {120Annotation a = parseAnnotation2(buf, constPool, container, false, selectAnnotationClasses);121if (a != null) {122Class<? extends Annotation> klass = a.annotationType();123if (AnnotationType.getInstance(klass).retention() == RetentionPolicy.RUNTIME &&124result.put(klass, a) != null) {125throw new AnnotationFormatError(126"Duplicate annotation for class: "+klass+": " + a);127}128}129}130return result;131}132133/**134* Parses the parameter annotations described by the specified byte array.135* resolving constant references in the specified constant pool.136* The array must contain an array of annotations as described137* in the RuntimeVisibleParameterAnnotations_attribute:138*139* u1 num_parameters;140* {141* u2 num_annotations;142* annotation annotations[num_annotations];143* } parameter_annotations[num_parameters];144*145* Unlike parseAnnotations, rawAnnotations must not be null!146* A null value must be handled by the caller. This is so because147* we cannot determine the number of parameters if rawAnnotations148* is null. Also, the caller should check that the number149* of parameters indicated by the return value of this method150* matches the actual number of method parameters. A mismatch151* indicates that an AnnotationFormatError should be thrown.152*153* @throws AnnotationFormatError if an annotation is found to be154* malformed.155*/156public static Annotation[][] parseParameterAnnotations(157byte[] rawAnnotations,158ConstantPool constPool,159Class<?> container) {160try {161return parseParameterAnnotations2(rawAnnotations, constPool, container);162} catch(BufferUnderflowException e) {163throw new AnnotationFormatError(164"Unexpected end of parameter annotations.");165} catch(IllegalArgumentException e) {166// Type mismatch in constant pool167throw new AnnotationFormatError(e);168}169}170171private static Annotation[][] parseParameterAnnotations2(172byte[] rawAnnotations,173ConstantPool constPool,174Class<?> container) {175ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);176int numParameters = buf.get() & 0xFF;177Annotation[][] result = new Annotation[numParameters][];178179for (int i = 0; i < numParameters; i++) {180int numAnnotations = buf.getShort() & 0xFFFF;181List<Annotation> annotations =182new ArrayList<Annotation>(numAnnotations);183for (int j = 0; j < numAnnotations; j++) {184Annotation a = parseAnnotation(buf, constPool, container, false);185if (a != null) {186AnnotationType type = AnnotationType.getInstance(187a.annotationType());188if (type.retention() == RetentionPolicy.RUNTIME)189annotations.add(a);190}191}192result[i] = annotations.toArray(EMPTY_ANNOTATIONS_ARRAY);193}194return result;195}196197private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY =198new Annotation[0];199200/**201* Parses the annotation at the current position in the specified202* byte buffer, resolving constant references in the specified constant203* pool. The cursor of the byte buffer must point to an "annotation204* structure" as described in the RuntimeVisibleAnnotations_attribute:205*206* annotation {207* u2 type_index;208* u2 num_member_value_pairs;209* { u2 member_name_index;210* member_value value;211* } member_value_pairs[num_member_value_pairs];212* }213* }214*215* Returns the annotation, or null if the annotation's type cannot216* be found by the VM, or is not a valid annotation type.217*218* @param exceptionOnMissingAnnotationClass if true, throw219* TypeNotPresentException if a referenced annotation type is not220* available at runtime221*/222static Annotation parseAnnotation(ByteBuffer buf,223ConstantPool constPool,224Class<?> container,225boolean exceptionOnMissingAnnotationClass) {226return parseAnnotation2(buf, constPool, container, exceptionOnMissingAnnotationClass, null);227}228229@SuppressWarnings("unchecked")230private static Annotation parseAnnotation2(ByteBuffer buf,231ConstantPool constPool,232Class<?> container,233boolean exceptionOnMissingAnnotationClass,234Class<? extends Annotation>[] selectAnnotationClasses) {235int typeIndex = buf.getShort() & 0xFFFF;236Class<? extends Annotation> annotationClass = null;237String sig = "[unknown]";238try {239sig = constPool.getUTF8At(typeIndex);240annotationClass = (Class<? extends Annotation>)parseSig(sig, container);241} catch (NoClassDefFoundError e) {242if (exceptionOnMissingAnnotationClass)243// note: at this point sig is "[unknown]" or VM-style244// name instead of a binary name245throw new TypeNotPresentException(sig, e);246skipAnnotation(buf, false);247return null;248}249catch (TypeNotPresentException e) {250if (exceptionOnMissingAnnotationClass)251throw e;252skipAnnotation(buf, false);253return null;254}255if (selectAnnotationClasses != null && !contains(selectAnnotationClasses, annotationClass)) {256skipAnnotation(buf, false);257return null;258}259AnnotationType type = null;260try {261type = AnnotationType.getInstance(annotationClass);262} catch (IllegalArgumentException e) {263skipAnnotation(buf, false);264return null;265}266267Map<String, Class<?>> memberTypes = type.memberTypes();268Map<String, Object> memberValues =269new LinkedHashMap<String, Object>(type.memberDefaults());270271int numMembers = buf.getShort() & 0xFFFF;272for (int i = 0; i < numMembers; i++) {273int memberNameIndex = buf.getShort() & 0xFFFF;274String memberName = constPool.getUTF8At(memberNameIndex);275Class<?> memberType = memberTypes.get(memberName);276277if (memberType == null) {278// Member is no longer present in annotation type; ignore it279skipMemberValue(buf);280} else {281Object value = parseMemberValue(memberType, buf, constPool, container);282if (value instanceof AnnotationTypeMismatchExceptionProxy)283((AnnotationTypeMismatchExceptionProxy) value).284setMember(type.members().get(memberName));285memberValues.put(memberName, value);286}287}288return annotationForMap(annotationClass, memberValues);289}290291/**292* Returns an annotation of the given type backed by the given293* member {@literal ->} value map.294*/295@SuppressWarnings("removal")296public static Annotation annotationForMap(final Class<? extends Annotation> type,297final Map<String, Object> memberValues)298{299return AccessController.doPrivileged(new PrivilegedAction<Annotation>() {300public Annotation run() {301return (Annotation) Proxy.newProxyInstance(302type.getClassLoader(), new Class<?>[] { type },303new AnnotationInvocationHandler(type, memberValues));304}});305}306307/**308* Parses the annotation member value at the current position in the309* specified byte buffer, resolving constant references in the specified310* constant pool. The cursor of the byte buffer must point to a311* "member_value structure" as described in the312* RuntimeVisibleAnnotations_attribute:313*314* member_value {315* u1 tag;316* union {317* u2 const_value_index;318* {319* u2 type_name_index;320* u2 const_name_index;321* } enum_const_value;322* u2 class_info_index;323* annotation annotation_value;324* {325* u2 num_values;326* member_value values[num_values];327* } array_value;328* } value;329* }330*331* The member must be of the indicated type. If it is not, this332* method returns an AnnotationTypeMismatchExceptionProxy.333*/334@SuppressWarnings("unchecked")335public static Object parseMemberValue(Class<?> memberType,336ByteBuffer buf,337ConstantPool constPool,338Class<?> container) {339Object result = null;340int tag = buf.get();341switch(tag) {342case 'e':343return parseEnumValue((Class<? extends Enum<?>>)memberType, buf, constPool, container);344case 'c':345result = parseClassValue(buf, constPool, container);346break;347case '@':348result = parseAnnotation(buf, constPool, container, true);349break;350case '[':351return parseArray(memberType, buf, constPool, container);352default:353result = parseConst(tag, buf, constPool);354}355356if (result == null) {357result = new AnnotationTypeMismatchExceptionProxy(358memberType.getClass().getName());359} else if (!(result instanceof ExceptionProxy) &&360!memberType.isInstance(result)) {361result = new AnnotationTypeMismatchExceptionProxy(362result.getClass() + "[" + result + "]");363}364return result;365}366367/**368* Parses the primitive or String annotation member value indicated by369* the specified tag byte at the current position in the specified byte370* buffer, resolving constant reference in the specified constant pool.371* The cursor of the byte buffer must point to an annotation member value372* of the type indicated by the specified tag, as described in the373* RuntimeVisibleAnnotations_attribute:374*375* u2 const_value_index;376*/377private static Object parseConst(int tag,378ByteBuffer buf, ConstantPool constPool) {379int constIndex = buf.getShort() & 0xFFFF;380switch(tag) {381case 'B':382return Byte.valueOf((byte) constPool.getIntAt(constIndex));383case 'C':384return Character.valueOf((char) constPool.getIntAt(constIndex));385case 'D':386return Double.valueOf(constPool.getDoubleAt(constIndex));387case 'F':388return Float.valueOf(constPool.getFloatAt(constIndex));389case 'I':390return Integer.valueOf(constPool.getIntAt(constIndex));391case 'J':392return Long.valueOf(constPool.getLongAt(constIndex));393case 'S':394return Short.valueOf((short) constPool.getIntAt(constIndex));395case 'Z':396return Boolean.valueOf(constPool.getIntAt(constIndex) != 0);397case 's':398return constPool.getUTF8At(constIndex);399default:400throw new AnnotationFormatError(401"Invalid member-value tag in annotation: " + tag);402}403}404405/**406* Parses the Class member value at the current position in the407* specified byte buffer, resolving constant references in the specified408* constant pool. The cursor of the byte buffer must point to a "class409* info index" as described in the RuntimeVisibleAnnotations_attribute:410*411* u2 class_info_index;412*/413private static Object parseClassValue(ByteBuffer buf,414ConstantPool constPool,415Class<?> container) {416int classIndex = buf.getShort() & 0xFFFF;417try {418String sig = constPool.getUTF8At(classIndex);419return parseSig(sig, container);420} catch (NoClassDefFoundError e) {421return new TypeNotPresentExceptionProxy("[unknown]", e);422} catch (TypeNotPresentException e) {423return new TypeNotPresentExceptionProxy(e.typeName(), e.getCause());424}425}426427private static Class<?> parseSig(String sig, Class<?> container) {428if (sig.equals("V")) return void.class;429SignatureParser parser = SignatureParser.make();430TypeSignature typeSig = parser.parseTypeSig(sig);431GenericsFactory factory = CoreReflectionFactory.make(container, ClassScope.make(container));432Reifier reify = Reifier.make(factory);433typeSig.accept(reify);434Type result = reify.getResult();435return toClass(result);436}437static Class<?> toClass(Type o) {438if (o instanceof GenericArrayType)439return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),4400)441.getClass();442return (Class)o;443}444445/**446* Parses the enum constant member value at the current position in the447* specified byte buffer, resolving constant references in the specified448* constant pool. The cursor of the byte buffer must point to a449* "enum_const_value structure" as described in the450* RuntimeVisibleAnnotations_attribute:451*452* {453* u2 type_name_index;454* u2 const_name_index;455* } enum_const_value;456*/457@SuppressWarnings({"rawtypes", "unchecked"})458private static Object parseEnumValue(Class<? extends Enum> enumType, ByteBuffer buf,459ConstantPool constPool,460Class<?> container) {461int typeNameIndex = buf.getShort() & 0xFFFF;462String typeName = constPool.getUTF8At(typeNameIndex);463int constNameIndex = buf.getShort() & 0xFFFF;464String constName = constPool.getUTF8At(constNameIndex);465if (!enumType.isEnum()) {466return new AnnotationTypeMismatchExceptionProxy(467typeName + "." + constName);468} else if (enumType != parseSig(typeName, container)) {469return new AnnotationTypeMismatchExceptionProxy(470typeName + "." + constName);471}472473try {474return Enum.valueOf(enumType, constName);475} catch(IllegalArgumentException e) {476return new EnumConstantNotPresentExceptionProxy(477(Class<? extends Enum<?>>)enumType, constName);478}479}480481/**482* Parses the array value at the current position in the specified byte483* buffer, resolving constant references in the specified constant pool.484* The cursor of the byte buffer must point to an array value struct485* as specified in the RuntimeVisibleAnnotations_attribute:486*487* {488* u2 num_values;489* member_value values[num_values];490* } array_value;491*492* If the array values do not match arrayType, an493* AnnotationTypeMismatchExceptionProxy will be returned.494*/495@SuppressWarnings("unchecked")496private static Object parseArray(Class<?> arrayType,497ByteBuffer buf,498ConstantPool constPool,499Class<?> container) {500int length = buf.getShort() & 0xFFFF; // Number of array components501Class<?> componentType = arrayType.getComponentType();502503if (componentType == byte.class) {504return parseByteArray(length, buf, constPool);505} else if (componentType == char.class) {506return parseCharArray(length, buf, constPool);507} else if (componentType == double.class) {508return parseDoubleArray(length, buf, constPool);509} else if (componentType == float.class) {510return parseFloatArray(length, buf, constPool);511} else if (componentType == int.class) {512return parseIntArray(length, buf, constPool);513} else if (componentType == long.class) {514return parseLongArray(length, buf, constPool);515} else if (componentType == short.class) {516return parseShortArray(length, buf, constPool);517} else if (componentType == boolean.class) {518return parseBooleanArray(length, buf, constPool);519} else if (componentType == String.class) {520return parseStringArray(length, buf, constPool);521} else if (componentType == Class.class) {522return parseClassArray(length, buf, constPool, container);523} else if (componentType.isEnum()) {524return parseEnumArray(length, (Class<? extends Enum<?>>)componentType, buf,525constPool, container);526} else {527assert componentType.isAnnotation();528return parseAnnotationArray(length, (Class <? extends Annotation>)componentType, buf,529constPool, container);530}531}532533private static Object parseByteArray(int length,534ByteBuffer buf, ConstantPool constPool) {535byte[] result = new byte[length];536boolean typeMismatch = false;537int tag = 0;538539for (int i = 0; i < length; i++) {540tag = buf.get();541if (tag == 'B') {542int index = buf.getShort() & 0xFFFF;543result[i] = (byte) constPool.getIntAt(index);544} else {545skipMemberValue(tag, buf);546typeMismatch = true;547}548}549return typeMismatch ? exceptionProxy(tag) : result;550}551552private static Object parseCharArray(int length,553ByteBuffer buf, ConstantPool constPool) {554char[] result = new char[length];555boolean typeMismatch = false;556byte tag = 0;557558for (int i = 0; i < length; i++) {559tag = buf.get();560if (tag == 'C') {561int index = buf.getShort() & 0xFFFF;562result[i] = (char) constPool.getIntAt(index);563} else {564skipMemberValue(tag, buf);565typeMismatch = true;566}567}568return typeMismatch ? exceptionProxy(tag) : result;569}570571private static Object parseDoubleArray(int length,572ByteBuffer buf, ConstantPool constPool) {573double[] result = new double[length];574boolean typeMismatch = false;575int tag = 0;576577for (int i = 0; i < length; i++) {578tag = buf.get();579if (tag == 'D') {580int index = buf.getShort() & 0xFFFF;581result[i] = constPool.getDoubleAt(index);582} else {583skipMemberValue(tag, buf);584typeMismatch = true;585}586}587return typeMismatch ? exceptionProxy(tag) : result;588}589590private static Object parseFloatArray(int length,591ByteBuffer buf, ConstantPool constPool) {592float[] result = new float[length];593boolean typeMismatch = false;594int tag = 0;595596for (int i = 0; i < length; i++) {597tag = buf.get();598if (tag == 'F') {599int index = buf.getShort() & 0xFFFF;600result[i] = constPool.getFloatAt(index);601} else {602skipMemberValue(tag, buf);603typeMismatch = true;604}605}606return typeMismatch ? exceptionProxy(tag) : result;607}608609private static Object parseIntArray(int length,610ByteBuffer buf, ConstantPool constPool) {611int[] result = new int[length];612boolean typeMismatch = false;613int tag = 0;614615for (int i = 0; i < length; i++) {616tag = buf.get();617if (tag == 'I') {618int index = buf.getShort() & 0xFFFF;619result[i] = constPool.getIntAt(index);620} else {621skipMemberValue(tag, buf);622typeMismatch = true;623}624}625return typeMismatch ? exceptionProxy(tag) : result;626}627628private static Object parseLongArray(int length,629ByteBuffer buf, ConstantPool constPool) {630long[] result = new long[length];631boolean typeMismatch = false;632int tag = 0;633634for (int i = 0; i < length; i++) {635tag = buf.get();636if (tag == 'J') {637int index = buf.getShort() & 0xFFFF;638result[i] = constPool.getLongAt(index);639} else {640skipMemberValue(tag, buf);641typeMismatch = true;642}643}644return typeMismatch ? exceptionProxy(tag) : result;645}646647private static Object parseShortArray(int length,648ByteBuffer buf, ConstantPool constPool) {649short[] result = new short[length];650boolean typeMismatch = false;651int tag = 0;652653for (int i = 0; i < length; i++) {654tag = buf.get();655if (tag == 'S') {656int index = buf.getShort() & 0xFFFF;657result[i] = (short) constPool.getIntAt(index);658} else {659skipMemberValue(tag, buf);660typeMismatch = true;661}662}663return typeMismatch ? exceptionProxy(tag) : result;664}665666private static Object parseBooleanArray(int length,667ByteBuffer buf, ConstantPool constPool) {668boolean[] result = new boolean[length];669boolean typeMismatch = false;670int tag = 0;671672for (int i = 0; i < length; i++) {673tag = buf.get();674if (tag == 'Z') {675int index = buf.getShort() & 0xFFFF;676result[i] = (constPool.getIntAt(index) != 0);677} else {678skipMemberValue(tag, buf);679typeMismatch = true;680}681}682return typeMismatch ? exceptionProxy(tag) : result;683}684685private static Object parseStringArray(int length,686ByteBuffer buf, ConstantPool constPool) {687String[] result = new String[length];688boolean typeMismatch = false;689int tag = 0;690691for (int i = 0; i < length; i++) {692tag = buf.get();693if (tag == 's') {694int index = buf.getShort() & 0xFFFF;695result[i] = constPool.getUTF8At(index);696} else {697skipMemberValue(tag, buf);698typeMismatch = true;699}700}701return typeMismatch ? exceptionProxy(tag) : result;702}703704private static Object parseClassArray(int length,705ByteBuffer buf,706ConstantPool constPool,707Class<?> container) {708return parseArrayElements(new Class<?>[length],709buf, 'c', () -> parseClassValue(buf, constPool, container));710}711712private static Object parseEnumArray(int length, Class<? extends Enum<?>> enumType,713ByteBuffer buf,714ConstantPool constPool,715Class<?> container) {716return parseArrayElements((Object[]) Array.newInstance(enumType, length),717buf, 'e', () -> parseEnumValue(enumType, buf, constPool, container));718}719720private static Object parseAnnotationArray(int length,721Class<? extends Annotation> annotationType,722ByteBuffer buf,723ConstantPool constPool,724Class<?> container) {725return parseArrayElements((Object[]) Array.newInstance(annotationType, length),726buf, '@', () -> parseAnnotation(buf, constPool, container, true));727}728729private static Object parseArrayElements(Object[] result,730ByteBuffer buf,731int expectedTag,732Supplier<Object> parseElement) {733Object exceptionProxy = null;734for (int i = 0; i < result.length; i++) {735int tag = buf.get();736if (tag == expectedTag) {737Object value = parseElement.get();738if (value instanceof ExceptionProxy) {739if (exceptionProxy == null) exceptionProxy = (ExceptionProxy) value;740} else {741result[i] = value;742}743} else {744skipMemberValue(tag, buf);745if (exceptionProxy == null) exceptionProxy = exceptionProxy(tag);746}747}748return (exceptionProxy != null) ? exceptionProxy : result;749}750751/**752* Returns an appropriate exception proxy for a mismatching array753* annotation where the erroneous array has the specified tag.754*/755private static ExceptionProxy exceptionProxy(int tag) {756return new AnnotationTypeMismatchExceptionProxy(757"Array with component tag: " + tag);758}759760/**761* Skips the annotation at the current position in the specified762* byte buffer. The cursor of the byte buffer must point to763* an "annotation structure" OR two bytes into an annotation764* structure (i.e., after the type index).765*766* @param complete true if the byte buffer points to the beginning767* of an annotation structure (rather than two bytes in).768*/769private static void skipAnnotation(ByteBuffer buf, boolean complete) {770if (complete)771buf.getShort(); // Skip type index772int numMembers = buf.getShort() & 0xFFFF;773for (int i = 0; i < numMembers; i++) {774buf.getShort(); // Skip memberNameIndex775skipMemberValue(buf);776}777}778779/**780* Skips the annotation member value at the current position in the781* specified byte buffer. The cursor of the byte buffer must point to a782* "member_value structure."783*/784private static void skipMemberValue(ByteBuffer buf) {785int tag = buf.get();786skipMemberValue(tag, buf);787}788789/**790* Skips the annotation member value at the current position in the791* specified byte buffer. The cursor of the byte buffer must point792* immediately after the tag in a "member_value structure."793*/794private static void skipMemberValue(int tag, ByteBuffer buf) {795switch(tag) {796case 'e': // Enum value797buf.getInt(); // (Two shorts, actually.)798break;799case '@':800skipAnnotation(buf, true);801break;802case '[':803skipArray(buf);804break;805default:806// Class, primitive, or String807buf.getShort();808}809}810811/**812* Skips the array value at the current position in the specified byte813* buffer. The cursor of the byte buffer must point to an array value814* struct.815*/816private static void skipArray(ByteBuffer buf) {817int length = buf.getShort() & 0xFFFF;818for (int i = 0; i < length; i++)819skipMemberValue(buf);820}821822/**823* Searches for given {@code element} in given {@code array} by identity.824* Returns {@code true} if found {@code false} if not.825*/826private static boolean contains(Object[] array, Object element) {827for (Object e : array)828if (e == element)829return true;830return false;831}832833/*834* This method converts the annotation map returned by the parseAnnotations()835* method to an array. It is called by Field.getDeclaredAnnotations(),836* Method.getDeclaredAnnotations(), and Constructor.getDeclaredAnnotations().837* This avoids the reflection classes to load the Annotation class until838* it is needed.839*/840private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];841public static Annotation[] toArray(Map<Class<? extends Annotation>, Annotation> annotations) {842return annotations.values().toArray(EMPTY_ANNOTATION_ARRAY);843}844845static Annotation[] getEmptyAnnotationArray() { return EMPTY_ANNOTATION_ARRAY; }846}847848849