6304578: (reflect) toGenericString fails to print bounds of type variables on generic methods

Reviewed-by: vromero, plevart, briangoetz, mcimadamore
This commit is contained in:
Joe Darcy 2018-11-01 20:37:45 -07:00
parent 72bfdd96f1
commit 7a350b9474
7 changed files with 88 additions and 49 deletions

View file

@ -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>&#64;</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 */

View file

@ -382,7 +382,8 @@ public final class Constructor<T> extends Executable {
* including type parameters. The string is formatted as the
* constructor access modifiers, if any, followed by an
* angle-bracketed comma separated list of the constructor's type
* parameters, if any, followed by the fully-qualified name of the
* parameters, if any, including informative bounds of the
* type parameters, if any, followed by the fully-qualified name of the
* declaring class, followed by a parenthesized, comma-separated
* list of the constructor's generic formal parameter types.
*

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2018, 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
@ -26,9 +26,12 @@
package java.lang.reflect;
import java.lang.annotation.*;
import java.util.Arrays;
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.misc.SharedSecrets;
import sun.reflect.annotation.AnnotationParser;
@ -109,19 +112,15 @@ public abstract class Executable extends AccessibleObject
printModifiersIfNonzero(sb, modifierMask, isDefault);
specificToStringHeader(sb);
sb.append('(');
StringJoiner sj = new StringJoiner(",");
for (Class<?> parameterType : parameterTypes) {
sj.add(parameterType.getTypeName());
}
sb.append(sj.toString());
sb.append(Stream.of(parameterTypes).map(Type::getTypeName).
collect(Collectors.joining(",")));
sb.append(')');
if (exceptionTypes.length > 0) {
StringJoiner joiner = new StringJoiner(",", " throws ", "");
for (Class<?> exceptionType : exceptionTypes) {
joiner.add(exceptionType.getTypeName());
}
sb.append(joiner.toString());
sb.append(Stream.of(exceptionTypes).map(Type::getTypeName).
collect(Collectors.joining(",", " throws ", "")));
}
return sb.toString();
} catch (Exception e) {
@ -135,6 +134,17 @@ public abstract class Executable extends AccessibleObject
*/
abstract void specificToStringHeader(StringBuilder sb);
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(" & "));
}
}
String sharedToGenericString(int modifierMask, boolean isDefault) {
try {
StringBuilder sb = new StringBuilder();
@ -143,11 +153,8 @@ public abstract class Executable extends AccessibleObject
TypeVariable<?>[] typeparms = 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(Executable::typeVarBounds).
collect(Collectors.joining(",", "<", "> ")));
}
specificToGenericStringHeader(sb);
@ -166,11 +173,8 @@ public abstract class Executable extends AccessibleObject
Type[] exceptionTypes = getGenericExceptionTypes();
if (exceptionTypes.length > 0) {
StringJoiner joiner = new StringJoiner(",", " throws ", "");
for (Type exceptionType : exceptionTypes) {
joiner.add(exceptionType.getTypeName());
}
sb.append(joiner.toString());
sb.append(Stream.of(exceptionTypes).map(Type::getTypeName).
collect(Collectors.joining(",", " throws ", "")));
}
return sb.toString();
} catch (Exception e) {

View file

@ -436,10 +436,11 @@ public final class Method extends Executable {
}
/**
* Returns a string describing this {@code Method}, including
* type parameters. The string is formatted as the method access
* Returns a string describing this {@code Method}, including type
* parameters. The string is formatted as the method access
* modifiers, if any, followed by an angle-bracketed
* comma-separated list of the method's type parameters, if any,
* including informative bounds of the type parameters, if any,
* followed by the method's generic return type, followed by a
* space, followed by the class declaring the method, followed by
* a period, followed by the method name, followed by a