1 /* |
1 /* |
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
24 */ |
24 */ |
25 |
25 |
26 package java.lang.reflect; |
26 package java.lang.reflect; |
27 |
27 |
28 import java.lang.annotation.*; |
28 import java.lang.annotation.*; |
|
29 import java.util.Arrays; |
29 import java.util.Map; |
30 import java.util.Map; |
30 import java.util.Objects; |
31 import java.util.Objects; |
31 import java.util.StringJoiner; |
32 import java.util.StringJoiner; |
|
33 import java.util.stream.Stream; |
|
34 import java.util.stream.Collectors; |
32 |
35 |
33 import jdk.internal.misc.SharedSecrets; |
36 import jdk.internal.misc.SharedSecrets; |
34 import sun.reflect.annotation.AnnotationParser; |
37 import sun.reflect.annotation.AnnotationParser; |
35 import sun.reflect.annotation.AnnotationSupport; |
38 import sun.reflect.annotation.AnnotationSupport; |
36 import sun.reflect.annotation.TypeAnnotationParser; |
39 import sun.reflect.annotation.TypeAnnotationParser; |
107 StringBuilder sb = new StringBuilder(); |
110 StringBuilder sb = new StringBuilder(); |
108 |
111 |
109 printModifiersIfNonzero(sb, modifierMask, isDefault); |
112 printModifiersIfNonzero(sb, modifierMask, isDefault); |
110 specificToStringHeader(sb); |
113 specificToStringHeader(sb); |
111 sb.append('('); |
114 sb.append('('); |
112 StringJoiner sj = new StringJoiner(","); |
115 |
113 for (Class<?> parameterType : parameterTypes) { |
116 sb.append(Stream.of(parameterTypes).map(Type::getTypeName). |
114 sj.add(parameterType.getTypeName()); |
117 collect(Collectors.joining(","))); |
115 } |
118 |
116 sb.append(sj.toString()); |
|
117 sb.append(')'); |
119 sb.append(')'); |
118 |
120 |
119 if (exceptionTypes.length > 0) { |
121 if (exceptionTypes.length > 0) { |
120 StringJoiner joiner = new StringJoiner(",", " throws ", ""); |
122 sb.append(Stream.of(exceptionTypes).map(Type::getTypeName). |
121 for (Class<?> exceptionType : exceptionTypes) { |
123 collect(Collectors.joining(",", " throws ", ""))); |
122 joiner.add(exceptionType.getTypeName()); |
|
123 } |
|
124 sb.append(joiner.toString()); |
|
125 } |
124 } |
126 return sb.toString(); |
125 return sb.toString(); |
127 } catch (Exception e) { |
126 } catch (Exception e) { |
128 return "<" + e + ">"; |
127 return "<" + e + ">"; |
129 } |
128 } |
133 * Generate toString header information specific to a method or |
132 * Generate toString header information specific to a method or |
134 * constructor. |
133 * constructor. |
135 */ |
134 */ |
136 abstract void specificToStringHeader(StringBuilder sb); |
135 abstract void specificToStringHeader(StringBuilder sb); |
137 |
136 |
|
137 static String typeVarBounds(TypeVariable<?> typeVar) { |
|
138 Type[] bounds = typeVar.getBounds(); |
|
139 if (bounds.length == 1 && bounds[0].equals(Object.class)) { |
|
140 return typeVar.getName(); |
|
141 } else { |
|
142 return typeVar.getName() + " extends " + |
|
143 Stream.of(bounds).map(Type::getTypeName). |
|
144 collect(Collectors.joining(" & ")); |
|
145 } |
|
146 } |
|
147 |
138 String sharedToGenericString(int modifierMask, boolean isDefault) { |
148 String sharedToGenericString(int modifierMask, boolean isDefault) { |
139 try { |
149 try { |
140 StringBuilder sb = new StringBuilder(); |
150 StringBuilder sb = new StringBuilder(); |
141 |
151 |
142 printModifiersIfNonzero(sb, modifierMask, isDefault); |
152 printModifiersIfNonzero(sb, modifierMask, isDefault); |
143 |
153 |
144 TypeVariable<?>[] typeparms = getTypeParameters(); |
154 TypeVariable<?>[] typeparms = getTypeParameters(); |
145 if (typeparms.length > 0) { |
155 if (typeparms.length > 0) { |
146 StringJoiner sj = new StringJoiner(",", "<", "> "); |
156 sb.append(Stream.of(typeparms).map(Executable::typeVarBounds). |
147 for(TypeVariable<?> typeparm: typeparms) { |
157 collect(Collectors.joining(",", "<", "> "))); |
148 sj.add(typeparm.getTypeName()); |
|
149 } |
|
150 sb.append(sj.toString()); |
|
151 } |
158 } |
152 |
159 |
153 specificToGenericStringHeader(sb); |
160 specificToGenericStringHeader(sb); |
154 |
161 |
155 sb.append('('); |
162 sb.append('('); |
164 sb.append(sj.toString()); |
171 sb.append(sj.toString()); |
165 sb.append(')'); |
172 sb.append(')'); |
166 |
173 |
167 Type[] exceptionTypes = getGenericExceptionTypes(); |
174 Type[] exceptionTypes = getGenericExceptionTypes(); |
168 if (exceptionTypes.length > 0) { |
175 if (exceptionTypes.length > 0) { |
169 StringJoiner joiner = new StringJoiner(",", " throws ", ""); |
176 sb.append(Stream.of(exceptionTypes).map(Type::getTypeName). |
170 for (Type exceptionType : exceptionTypes) { |
177 collect(Collectors.joining(",", " throws ", ""))); |
171 joiner.add(exceptionType.getTypeName()); |
|
172 } |
|
173 sb.append(joiner.toString()); |
|
174 } |
178 } |
175 return sb.toString(); |
179 return sb.toString(); |
176 } catch (Exception e) { |
180 } catch (Exception e) { |
177 return "<" + e + ">"; |
181 return "<" + e + ">"; |
178 } |
182 } |