8281462: Annotation toString output for enum not reusable for source input

Reviewed-by: mchung
This commit is contained in:
Joe Darcy 2022-02-11 23:24:08 +00:00
parent 4032fe76dc
commit c3179a8760
8 changed files with 74 additions and 55 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -145,7 +145,9 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
private String toStringImpl() {
StringBuilder result = new StringBuilder(128);
result.append('@');
result.append(type.getName());
// Guard against null canonical name; shouldn't happen
result.append(Objects.toString(type.getCanonicalName(),
"<no canonical name>"));
result.append('(');
boolean firstMember = true;
Set<Map.Entry<String, Object>> entries = memberValues.entrySet();
@ -189,6 +191,10 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
return toSourceString((long) value);
else if (type == Byte.class)
return toSourceString((byte) value);
else if (value instanceof Enum<?> v)
// Predicate above covers enum constants, including
// those with specialized class bodies.
return toSourceString(v);
else
return value.toString();
} else {
@ -219,6 +225,10 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
stringStream =
Arrays.stream((String[])value).
map(AnnotationInvocationHandler::toSourceString);
else if (type.getComponentType().isEnum())
stringStream =
Arrays.stream((Enum<?>[])value).
map(AnnotationInvocationHandler::toSourceString);
else
stringStream = Arrays.stream((Object[])value).map(Objects::toString);
@ -231,15 +241,9 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
* string representation of an annotation.
*/
private static String toSourceString(Class<?> clazz) {
Class<?> finalComponent = clazz;
StringBuilder arrayBrackets = new StringBuilder();
while(finalComponent.isArray()) {
finalComponent = finalComponent.getComponentType();
arrayBrackets.append("[]");
}
return finalComponent.getName() + arrayBrackets.toString() + ".class";
// Guard against null canonical name; shouldn't happen
return Objects.toString(clazz.getCanonicalName(),
"<no canonical name>") + ".class";
}
private static String toSourceString(float f) {
@ -307,6 +311,10 @@ class AnnotationInvocationHandler implements InvocationHandler, Serializable {
return String.valueOf(ell) + "L";
}
private static String toSourceString(Enum<?> enumConstant) {
return enumConstant.name();
}
/**
* Return a string suitable for use in the string representation
* of an annotation.