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

@ -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) {