src/share/classes/sun/reflect/annotation/AnnotationParser.java
Print this page
*** 57,70 ****
* annotation annotations[num_annotations];
*
* @throws AnnotationFormatError if an annotation is found to be
* malformed.
*/
! public static Map<Class, Annotation> parseAnnotations(
byte[] rawAnnotations,
ConstantPool constPool,
! Class container) {
if (rawAnnotations == null)
return Collections.emptyMap();
try {
return parseAnnotations2(rawAnnotations, constPool, container);
--- 57,70 ----
* annotation annotations[num_annotations];
*
* @throws AnnotationFormatError if an annotation is found to be
* malformed.
*/
! public static Map<Class<? extends Annotation>, Annotation> parseAnnotations(
byte[] rawAnnotations,
ConstantPool constPool,
! Class<?> container) {
if (rawAnnotations == null)
return Collections.emptyMap();
try {
return parseAnnotations2(rawAnnotations, constPool, container);
*** 74,94 ****
// Type mismatch in constant pool
throw new AnnotationFormatError(e);
}
}
! private static Map<Class, Annotation> parseAnnotations2(
byte[] rawAnnotations,
ConstantPool constPool,
! Class container) {
! Map<Class, Annotation> result = new LinkedHashMap<Class, Annotation>();
ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
int numAnnotations = buf.getShort() & 0xFFFF;
for (int i = 0; i < numAnnotations; i++) {
Annotation a = parseAnnotation(buf, constPool, container, false);
if (a != null) {
! Class klass = a.annotationType();
AnnotationType type = AnnotationType.getInstance(klass);
if (type.retention() == RetentionPolicy.RUNTIME)
if (result.put(klass, a) != null)
throw new AnnotationFormatError(
"Duplicate annotation for class: "+klass+": " + a);
--- 74,95 ----
// Type mismatch in constant pool
throw new AnnotationFormatError(e);
}
}
! private static Map<Class<? extends Annotation>, Annotation> parseAnnotations2(
byte[] rawAnnotations,
ConstantPool constPool,
! Class<?> container) {
! Map<Class<? extends Annotation>, Annotation> result =
! new LinkedHashMap<Class<? extends Annotation>, Annotation>();
ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
int numAnnotations = buf.getShort() & 0xFFFF;
for (int i = 0; i < numAnnotations; i++) {
Annotation a = parseAnnotation(buf, constPool, container, false);
if (a != null) {
! Class<? extends Annotation> klass = a.annotationType();
AnnotationType type = AnnotationType.getInstance(klass);
if (type.retention() == RetentionPolicy.RUNTIME)
if (result.put(klass, a) != null)
throw new AnnotationFormatError(
"Duplicate annotation for class: "+klass+": " + a);
*** 121,131 ****
* malformed.
*/
public static Annotation[][] parseParameterAnnotations(
byte[] rawAnnotations,
ConstantPool constPool,
! Class container) {
try {
return parseParameterAnnotations2(rawAnnotations, constPool, container);
} catch(BufferUnderflowException e) {
throw new AnnotationFormatError(
"Unexpected end of parameter annotations.");
--- 122,132 ----
* malformed.
*/
public static Annotation[][] parseParameterAnnotations(
byte[] rawAnnotations,
ConstantPool constPool,
! Class<?> container) {
try {
return parseParameterAnnotations2(rawAnnotations, constPool, container);
} catch(BufferUnderflowException e) {
throw new AnnotationFormatError(
"Unexpected end of parameter annotations.");
*** 136,146 ****
}
private static Annotation[][] parseParameterAnnotations2(
byte[] rawAnnotations,
ConstantPool constPool,
! Class container) {
ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
int numParameters = buf.get() & 0xFF;
Annotation[][] result = new Annotation[numParameters][];
for (int i = 0; i < numParameters; i++) {
--- 137,147 ----
}
private static Annotation[][] parseParameterAnnotations2(
byte[] rawAnnotations,
ConstantPool constPool,
! Class<?> container) {
ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
int numParameters = buf.get() & 0xFF;
Annotation[][] result = new Annotation[numParameters][];
for (int i = 0; i < numParameters; i++) {
*** 186,204 ****
* TypeNotPresentException if a referenced annotation type is not
* available at runtime
*/
private static Annotation parseAnnotation(ByteBuffer buf,
ConstantPool constPool,
! Class container,
boolean exceptionOnMissingAnnotationClass) {
int typeIndex = buf.getShort() & 0xFFFF;
! Class annotationClass = null;
String sig = "[unknown]";
try {
try {
sig = constPool.getUTF8At(typeIndex);
! annotationClass = parseSig(sig, container);
} catch (IllegalArgumentException ex) {
// support obsolete early jsr175 format class files
annotationClass = constPool.getClassAt(typeIndex);
}
} catch (NoClassDefFoundError e) {
--- 187,205 ----
* TypeNotPresentException if a referenced annotation type is not
* available at runtime
*/
private static Annotation parseAnnotation(ByteBuffer buf,
ConstantPool constPool,
! Class<?> container,
boolean exceptionOnMissingAnnotationClass) {
int typeIndex = buf.getShort() & 0xFFFF;
! Class<? extends Annotation> annotationClass = null;
String sig = "[unknown]";
try {
try {
sig = constPool.getUTF8At(typeIndex);
! annotationClass = (Class<? extends Annotation>)parseSig(sig, container);
} catch (IllegalArgumentException ex) {
// support obsolete early jsr175 format class files
annotationClass = constPool.getClassAt(typeIndex);
}
} catch (NoClassDefFoundError e) {
*** 221,239 ****
} catch (IllegalArgumentException e) {
skipAnnotation(buf, false);
return null;
}
! Map<String, Class> memberTypes = type.memberTypes();
Map<String, Object> memberValues =
new LinkedHashMap<String, Object>(type.memberDefaults());
int numMembers = buf.getShort() & 0xFFFF;
for (int i = 0; i < numMembers; i++) {
int memberNameIndex = buf.getShort() & 0xFFFF;
String memberName = constPool.getUTF8At(memberNameIndex);
! Class memberType = memberTypes.get(memberName);
if (memberType == null) {
// Member is no longer present in annotation type; ignore it
skipMemberValue(buf);
} else {
--- 222,240 ----
} catch (IllegalArgumentException e) {
skipAnnotation(buf, false);
return null;
}
! Map<String, Class<?>> memberTypes = type.memberTypes();
Map<String, Object> memberValues =
new LinkedHashMap<String, Object>(type.memberDefaults());
int numMembers = buf.getShort() & 0xFFFF;
for (int i = 0; i < numMembers; i++) {
int memberNameIndex = buf.getShort() & 0xFFFF;
String memberName = constPool.getUTF8At(memberNameIndex);
! Class<?> memberType = memberTypes.get(memberName);
if (memberType == null) {
// Member is no longer present in annotation type; ignore it
skipMemberValue(buf);
} else {
*** 250,260 ****
/**
* Returns an annotation of the given type backed by the given
* member -> value map.
*/
public static Annotation annotationForMap(
! Class type, Map<String, Object> memberValues)
{
return (Annotation) Proxy.newProxyInstance(
type.getClassLoader(), new Class[] { type },
new AnnotationInvocationHandler(type, memberValues));
}
--- 251,261 ----
/**
* Returns an annotation of the given type backed by the given
* member -> value map.
*/
public static Annotation annotationForMap(
! Class<? extends Annotation> type, Map<String, Object> memberValues)
{
return (Annotation) Proxy.newProxyInstance(
type.getClassLoader(), new Class[] { type },
new AnnotationInvocationHandler(type, memberValues));
}
*** 284,301 ****
* }
*
* The member must be of the indicated type. If it is not, this
* method returns an AnnotationTypeMismatchExceptionProxy.
*/
! public static Object parseMemberValue(Class memberType, ByteBuffer buf,
ConstantPool constPool,
! Class container) {
Object result = null;
int tag = buf.get();
switch(tag) {
case 'e':
! return parseEnumValue(memberType, buf, constPool, container);
case 'c':
result = parseClassValue(buf, constPool, container);
break;
case '@':
result = parseAnnotation(buf, constPool, container, true);
--- 285,303 ----
* }
*
* The member must be of the indicated type. If it is not, this
* method returns an AnnotationTypeMismatchExceptionProxy.
*/
! public static Object parseMemberValue(Class<?> memberType,
! ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
Object result = null;
int tag = buf.get();
switch(tag) {
case 'e':
! return parseEnumValue((Class<? extends Enum<?>>)memberType, buf, constPool, container);
case 'c':
result = parseClassValue(buf, constPool, container);
break;
case '@':
result = parseAnnotation(buf, constPool, container, true);
*** 359,369 ****
*
* u2 class_info_index;
*/
private static Object parseClassValue(ByteBuffer buf,
ConstantPool constPool,
! Class container) {
int classIndex = buf.getShort() & 0xFFFF;
try {
try {
String sig = constPool.getUTF8At(classIndex);
return parseSig(sig, container);
--- 361,371 ----
*
* u2 class_info_index;
*/
private static Object parseClassValue(ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
int classIndex = buf.getShort() & 0xFFFF;
try {
try {
String sig = constPool.getUTF8At(classIndex);
return parseSig(sig, container);
*** 377,397 ****
catch (TypeNotPresentException e) {
return new TypeNotPresentExceptionProxy(e.typeName(), e.getCause());
}
}
! private static Class<?> parseSig(String sig, Class container) {
if (sig.equals("V")) return void.class;
SignatureParser parser = SignatureParser.make();
TypeSignature typeSig = parser.parseTypeSig(sig);
GenericsFactory factory = CoreReflectionFactory.make(container, ClassScope.make(container));
Reifier reify = Reifier.make(factory);
typeSig.accept(reify);
Type result = reify.getResult();
return toClass(result);
}
! static Class toClass(Type o) {
if (o instanceof GenericArrayType)
return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
0)
.getClass();
return (Class)o;
--- 379,399 ----
catch (TypeNotPresentException e) {
return new TypeNotPresentExceptionProxy(e.typeName(), e.getCause());
}
}
! private static Class<?> parseSig(String sig, Class<?> container) {
if (sig.equals("V")) return void.class;
SignatureParser parser = SignatureParser.make();
TypeSignature typeSig = parser.parseTypeSig(sig);
GenericsFactory factory = CoreReflectionFactory.make(container, ClassScope.make(container));
Reifier reify = Reifier.make(factory);
typeSig.accept(reify);
Type result = reify.getResult();
return toClass(result);
}
! static Class<?> toClass(Type o) {
if (o instanceof GenericArrayType)
return Array.newInstance(toClass(((GenericArrayType)o).getGenericComponentType()),
0)
.getClass();
return (Class)o;
*** 407,419 ****
* {
* u2 type_name_index;
* u2 const_name_index;
* } enum_const_value;
*/
! private static Object parseEnumValue(Class enumType, ByteBuffer buf,
ConstantPool constPool,
! Class container) {
int typeNameIndex = buf.getShort() & 0xFFFF;
String typeName = constPool.getUTF8At(typeNameIndex);
int constNameIndex = buf.getShort() & 0xFFFF;
String constName = constPool.getUTF8At(constNameIndex);
--- 409,421 ----
* {
* u2 type_name_index;
* u2 const_name_index;
* } enum_const_value;
*/
! private static Object parseEnumValue(Class<? extends Enum> enumType, ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
int typeNameIndex = buf.getShort() & 0xFFFF;
String typeName = constPool.getUTF8At(typeNameIndex);
int constNameIndex = buf.getShort() & 0xFFFF;
String constName = constPool.getUTF8At(constNameIndex);
*** 447,462 ****
* } array_value;
*
* If the array values do not match arrayType, an
* AnnotationTypeMismatchExceptionProxy will be returned.
*/
! private static Object parseArray(Class arrayType,
ByteBuffer buf,
ConstantPool constPool,
! Class container) {
int length = buf.getShort() & 0xFFFF; // Number of array components
! Class componentType = arrayType.getComponentType();
if (componentType == byte.class) {
return parseByteArray(length, buf, constPool);
} else if (componentType == char.class) {
return parseCharArray(length, buf, constPool);
--- 449,464 ----
* } array_value;
*
* If the array values do not match arrayType, an
* AnnotationTypeMismatchExceptionProxy will be returned.
*/
! private static Object parseArray(Class<?> arrayType,
ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
int length = buf.getShort() & 0xFFFF; // Number of array components
! Class<?> componentType = arrayType.getComponentType();
if (componentType == byte.class) {
return parseByteArray(length, buf, constPool);
} else if (componentType == char.class) {
return parseCharArray(length, buf, constPool);
*** 475,489 ****
} else if (componentType == String.class) {
return parseStringArray(length, buf, constPool);
} else if (componentType == Class.class) {
return parseClassArray(length, buf, constPool, container);
} else if (componentType.isEnum()) {
! return parseEnumArray(length, componentType, buf,
constPool, container);
} else {
assert componentType.isAnnotation();
! return parseAnnotationArray(length, componentType, buf,
constPool, container);
}
}
private static Object parseByteArray(int length,
--- 477,491 ----
} else if (componentType == String.class) {
return parseStringArray(length, buf, constPool);
} else if (componentType == Class.class) {
return parseClassArray(length, buf, constPool, container);
} else if (componentType.isEnum()) {
! return parseEnumArray(length, (Class<? extends Enum>)componentType, buf,
constPool, container);
} else {
assert componentType.isAnnotation();
! return parseAnnotationArray(length, (Class <? extends Annotation>)componentType, buf,
constPool, container);
}
}
private static Object parseByteArray(int length,
*** 658,669 ****
}
private static Object parseClassArray(int length,
ByteBuffer buf,
ConstantPool constPool,
! Class container) {
! Object[] result = new Class[length];
boolean typeMismatch = false;
int tag = 0;
for (int i = 0; i < length; i++) {
tag = buf.get();
--- 660,671 ----
}
private static Object parseClassArray(int length,
ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
! Object[] result = new Class<?>[length];
boolean typeMismatch = false;
int tag = 0;
for (int i = 0; i < length; i++) {
tag = buf.get();
*** 675,688 ****
}
}
return typeMismatch ? exceptionProxy(tag) : result;
}
! private static Object parseEnumArray(int length, Class enumType,
ByteBuffer buf,
ConstantPool constPool,
! Class container) {
Object[] result = (Object[]) Array.newInstance(enumType, length);
boolean typeMismatch = false;
int tag = 0;
for (int i = 0; i < length; i++) {
--- 677,690 ----
}
}
return typeMismatch ? exceptionProxy(tag) : result;
}
! private static Object parseEnumArray(int length, Class<? extends Enum> enumType,
ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
Object[] result = (Object[]) Array.newInstance(enumType, length);
boolean typeMismatch = false;
int tag = 0;
for (int i = 0; i < length; i++) {
*** 696,709 ****
}
return typeMismatch ? exceptionProxy(tag) : result;
}
private static Object parseAnnotationArray(int length,
! Class annotationType,
ByteBuffer buf,
ConstantPool constPool,
! Class container) {
Object[] result = (Object[]) Array.newInstance(annotationType, length);
boolean typeMismatch = false;
int tag = 0;
for (int i = 0; i < length; i++) {
--- 698,711 ----
}
return typeMismatch ? exceptionProxy(tag) : result;
}
private static Object parseAnnotationArray(int length,
! Class<? extends Annotation> annotationType,
ByteBuffer buf,
ConstantPool constPool,
! Class<?> container) {
Object[] result = (Object[]) Array.newInstance(annotationType, length);
boolean typeMismatch = false;
int tag = 0;
for (int i = 0; i < length; i++) {
*** 795,803 ****
* Method.getDeclaredAnnotations(), and Constructor.getDeclaredAnnotations().
* This avoids the reflection classes to load the Annotation class until
* it is needed.
*/
private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
! public static Annotation[] toArray(Map<Class, Annotation> annotations) {
return annotations.values().toArray(EMPTY_ANNOTATION_ARRAY);
}
}
--- 797,805 ----
* Method.getDeclaredAnnotations(), and Constructor.getDeclaredAnnotations().
* This avoids the reflection classes to load the Annotation class until
* it is needed.
*/
private static final Annotation[] EMPTY_ANNOTATION_ARRAY = new Annotation[0];
! public static Annotation[] toArray(Map<Class<? extends Annotation>, Annotation> annotations) {
return annotations.values().toArray(EMPTY_ANNOTATION_ARRAY);
}
}