mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
6304578: (reflect) toGenericString fails to print bounds of type variables on generic methods
Reviewed-by: vromero, plevart, briangoetz, mcimadamore
This commit is contained in:
parent
72bfdd96f1
commit
7a350b9474
7 changed files with 88 additions and 49 deletions
|
@ -59,6 +59,8 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jdk.internal.HotSpotIntrinsicCandidate;
|
||||
import jdk.internal.loader.BootLoader;
|
||||
|
@ -200,7 +202,8 @@ public final class Class<T> implements java.io.Serializable,
|
|||
* and {@code class}, {@code enum}, {@code interface}, or
|
||||
* <code>@</code>{@code interface}, as appropriate), followed
|
||||
* by the type's name, followed by an angle-bracketed
|
||||
* comma-separated list of the type's type parameters, if any.
|
||||
* comma-separated list of the type's type parameters, if any,
|
||||
* including informative bounds on the type parameters, if any.
|
||||
*
|
||||
* A space is used to separate modifiers from one another and to
|
||||
* separate any modifiers from the kind of type. The modifiers
|
||||
|
@ -262,11 +265,8 @@ public final class Class<T> implements java.io.Serializable,
|
|||
|
||||
TypeVariable<?>[] typeparms = component.getTypeParameters();
|
||||
if (typeparms.length > 0) {
|
||||
StringJoiner sj = new StringJoiner(",", "<", ">");
|
||||
for(TypeVariable<?> typeparm: typeparms) {
|
||||
sj.add(typeparm.getTypeName());
|
||||
}
|
||||
sb.append(sj.toString());
|
||||
sb.append(Stream.of(typeparms).map(Class::typeVarBounds).
|
||||
collect(Collectors.joining(",", "<", ">")));
|
||||
}
|
||||
|
||||
for (int i = 0; i < arrayDepth; i++)
|
||||
|
@ -276,6 +276,17 @@ public final class Class<T> implements java.io.Serializable,
|
|||
}
|
||||
}
|
||||
|
||||
static String typeVarBounds(TypeVariable<?> typeVar) {
|
||||
Type[] bounds = typeVar.getBounds();
|
||||
if (bounds.length == 1 && bounds[0].equals(Object.class)) {
|
||||
return typeVar.getName();
|
||||
} else {
|
||||
return typeVar.getName() + " extends " +
|
||||
Stream.of(bounds).map(Type::getTypeName).
|
||||
collect(Collectors.joining(" & "));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@code Class} object associated with the class or
|
||||
* interface with the given string name. Invoking this method is
|
||||
|
@ -3399,14 +3410,14 @@ public final class Class<T> implements java.io.Serializable,
|
|||
* Helper method to get the method name from arguments.
|
||||
*/
|
||||
private String methodToString(String name, Class<?>[] argTypes) {
|
||||
StringJoiner sj = new StringJoiner(", ", getName() + "." + name + "(", ")");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(getName() + "." + name + "(");
|
||||
if (argTypes != null) {
|
||||
for (int i = 0; i < argTypes.length; i++) {
|
||||
Class<?> c = argTypes[i];
|
||||
sj.add((c == null) ? "null" : c.getName());
|
||||
}
|
||||
Stream.of(argTypes).map(c -> {return (c == null) ? "null" : c.getName();}).
|
||||
collect(Collectors.joining(","));
|
||||
}
|
||||
return sj.toString();
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** use serialVersionUID from JDK 1.1 for interoperability */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue