author | darcy |
Thu, 29 Aug 2019 16:31:34 -0700 | |
changeset 57956 | e0b8b019d2f5 |
parent 55387 | 761b86d5563d |
child 58520 | e036ee8bae56 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.reflect.annotation; |
|
27 |
||
35295 | 28 |
import java.io.ObjectInputStream; |
2 | 29 |
import java.lang.annotation.*; |
30 |
import java.lang.reflect.*; |
|
31 |
import java.io.Serializable; |
|
32 |
import java.util.*; |
|
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
33 |
import java.util.stream.*; |
2 | 34 |
import java.security.AccessController; |
35 |
import java.security.PrivilegedAction; |
|
36 |
||
37 |
/** |
|
38 |
* InvocationHandler for dynamic proxy implementation of Annotation. |
|
39 |
* |
|
40 |
* @author Josh Bloch |
|
41 |
* @since 1.5 |
|
42 |
*/ |
|
43 |
class AnnotationInvocationHandler implements InvocationHandler, Serializable { |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55387
diff
changeset
|
44 |
@java.io.Serial |
10357
c2cde4cd24a1
7080038: (ann) Serializable types in sun.reflect.annotation do not declare serialVersionUIDs
darcy
parents:
5506
diff
changeset
|
45 |
private static final long serialVersionUID = 6182022883658399397L; |
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
46 |
private final Class<? extends Annotation> type; |
2 | 47 |
private final Map<String, Object> memberValues; |
48 |
||
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
49 |
AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) { |
27069 | 50 |
Class<?>[] superInterfaces = type.getInterfaces(); |
51 |
if (!type.isAnnotation() || |
|
52 |
superInterfaces.length != 1 || |
|
53 |
superInterfaces[0] != java.lang.annotation.Annotation.class) |
|
54 |
throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type."); |
|
2 | 55 |
this.type = type; |
56 |
this.memberValues = memberValues; |
|
57 |
} |
|
58 |
||
59 |
public Object invoke(Object proxy, Method method, Object[] args) { |
|
60 |
String member = method.getName(); |
|
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
61 |
int parameterCount = method.getParameterCount(); |
2 | 62 |
|
63 |
// Handle Object and Annotation methods |
|
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
64 |
if (parameterCount == 1 && member == "equals" && |
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
65 |
method.getParameterTypes()[0] == Object.class) { |
39043
815e52744068
8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation
darcy
parents:
38770
diff
changeset
|
66 |
return equalsImpl(proxy, args[0]); |
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
67 |
} |
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
68 |
if (parameterCount != 0) { |
27069 | 69 |
throw new AssertionError("Too many parameters for an annotation method"); |
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
70 |
} |
27069 | 71 |
|
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
72 |
if (member == "toString") { |
2 | 73 |
return toStringImpl(); |
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
74 |
} else if (member == "hashCode") { |
2 | 75 |
return hashCodeImpl(); |
47436
389695e5e8db
8189266: (ann) Optimize AnnotationInvocationHandler.invoke
redestad
parents:
47216
diff
changeset
|
76 |
} else if (member == "annotationType") { |
2 | 77 |
return type; |
27069 | 78 |
} |
2 | 79 |
|
80 |
// Handle annotation member accessors |
|
81 |
Object result = memberValues.get(member); |
|
82 |
||
83 |
if (result == null) |
|
84 |
throw new IncompleteAnnotationException(type, member); |
|
85 |
||
86 |
if (result instanceof ExceptionProxy) |
|
87 |
throw ((ExceptionProxy) result).generateException(); |
|
88 |
||
89 |
if (result.getClass().isArray() && Array.getLength(result) != 0) |
|
90 |
result = cloneArray(result); |
|
91 |
||
92 |
return result; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* This method, which clones its array argument, would not be necessary |
|
97 |
* if Cloneable had a public clone method. |
|
98 |
*/ |
|
99 |
private Object cloneArray(Object array) { |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
100 |
Class<?> type = array.getClass(); |
2 | 101 |
|
102 |
if (type == byte[].class) { |
|
103 |
byte[] byteArray = (byte[])array; |
|
104 |
return byteArray.clone(); |
|
105 |
} |
|
106 |
if (type == char[].class) { |
|
107 |
char[] charArray = (char[])array; |
|
108 |
return charArray.clone(); |
|
109 |
} |
|
110 |
if (type == double[].class) { |
|
111 |
double[] doubleArray = (double[])array; |
|
112 |
return doubleArray.clone(); |
|
113 |
} |
|
114 |
if (type == float[].class) { |
|
115 |
float[] floatArray = (float[])array; |
|
116 |
return floatArray.clone(); |
|
117 |
} |
|
118 |
if (type == int[].class) { |
|
119 |
int[] intArray = (int[])array; |
|
120 |
return intArray.clone(); |
|
121 |
} |
|
122 |
if (type == long[].class) { |
|
123 |
long[] longArray = (long[])array; |
|
124 |
return longArray.clone(); |
|
125 |
} |
|
126 |
if (type == short[].class) { |
|
127 |
short[] shortArray = (short[])array; |
|
128 |
return shortArray.clone(); |
|
129 |
} |
|
130 |
if (type == boolean[].class) { |
|
131 |
boolean[] booleanArray = (boolean[])array; |
|
132 |
return booleanArray.clone(); |
|
133 |
} |
|
134 |
||
135 |
Object[] objectArray = (Object[])array; |
|
136 |
return objectArray.clone(); |
|
137 |
} |
|
138 |
||
139 |
||
140 |
/** |
|
141 |
* Implementation of dynamicProxy.toString() |
|
142 |
*/ |
|
143 |
private String toStringImpl() { |
|
27069 | 144 |
StringBuilder result = new StringBuilder(128); |
2 | 145 |
result.append('@'); |
146 |
result.append(type.getName()); |
|
147 |
result.append('('); |
|
148 |
boolean firstMember = true; |
|
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
149 |
Set<Map.Entry<String, Object>> entries = memberValues.entrySet(); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
150 |
boolean loneValue = entries.size() == 1; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
151 |
for (Map.Entry<String, Object> e : entries) { |
2 | 152 |
if (firstMember) |
153 |
firstMember = false; |
|
154 |
else |
|
155 |
result.append(", "); |
|
156 |
||
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
157 |
String key = e.getKey(); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
158 |
if (!loneValue || !"value".equals(key)) { |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
159 |
result.append(key); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
160 |
result.append('='); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
161 |
} |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
162 |
loneValue = false; |
2 | 163 |
result.append(memberValueToString(e.getValue())); |
164 |
} |
|
165 |
result.append(')'); |
|
166 |
return result.toString(); |
|
167 |
} |
|
168 |
||
169 |
/** |
|
38770
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
170 |
* Translates a member value (in "dynamic proxy return form") into a string. |
2 | 171 |
*/ |
172 |
private static String memberValueToString(Object value) { |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
173 |
Class<?> type = value.getClass(); |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
174 |
if (!type.isArray()) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
175 |
// primitive value, string, class, enum const, or annotation |
38770
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
176 |
if (type == Class.class) |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
177 |
return toSourceString((Class<?>) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
178 |
else if (type == String.class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
179 |
return toSourceString((String) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
180 |
if (type == Character.class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
181 |
return toSourceString((char) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
182 |
else if (type == Double.class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
183 |
return toSourceString((double) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
184 |
else if (type == Float.class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
185 |
return toSourceString((float) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
186 |
else if (type == Long.class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
187 |
return toSourceString((long) value); |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
188 |
else if (type == Byte.class) |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
189 |
return toSourceString((byte) value); |
38770
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
190 |
else |
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
191 |
return value.toString(); |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
192 |
} else { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
193 |
Stream<String> stringStream; |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
194 |
if (type == byte[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
195 |
stringStream = convert((byte[]) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
196 |
else if (type == char[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
197 |
stringStream = convert((char[]) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
198 |
else if (type == double[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
199 |
stringStream = DoubleStream.of((double[]) value) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
200 |
.mapToObj(AnnotationInvocationHandler::toSourceString); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
201 |
else if (type == float[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
202 |
stringStream = convert((float[]) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
203 |
else if (type == int[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
204 |
stringStream = IntStream.of((int[]) value).mapToObj(String::valueOf); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
205 |
else if (type == long[].class) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
206 |
stringStream = LongStream.of((long[]) value) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
207 |
.mapToObj(AnnotationInvocationHandler::toSourceString); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
208 |
} else if (type == short[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
209 |
stringStream = convert((short[]) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
210 |
else if (type == boolean[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
211 |
stringStream = convert((boolean[]) value); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
212 |
else if (type == Class[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
213 |
stringStream = |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
214 |
Arrays.stream((Class<?>[]) value). |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
215 |
map(AnnotationInvocationHandler::toSourceString); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
216 |
else if (type == String[].class) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
217 |
stringStream = |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
218 |
Arrays.stream((String[])value). |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
219 |
map(AnnotationInvocationHandler::toSourceString); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
220 |
else |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
221 |
stringStream = Arrays.stream((Object[])value).map(Objects::toString); |
2 | 222 |
|
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
223 |
return stringStreamToString(stringStream); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
224 |
} |
2 | 225 |
} |
226 |
||
227 |
/** |
|
38770
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
228 |
* Translates a Class value to a form suitable for use in the |
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
229 |
* string representation of an annotation. |
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
230 |
*/ |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
231 |
private static String toSourceString(Class<?> clazz) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
232 |
Class<?> finalComponent = clazz; |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
233 |
StringBuilder arrayBrackets = new StringBuilder(); |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
234 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
235 |
while(finalComponent.isArray()) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
236 |
finalComponent = finalComponent.getComponentType(); |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
237 |
arrayBrackets.append("[]"); |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
238 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
239 |
|
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
240 |
return finalComponent.getName() + arrayBrackets.toString() + ".class"; |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
241 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
242 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
243 |
private static String toSourceString(float f) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
244 |
if (Float.isFinite(f)) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
245 |
return Float.toString(f) + "f" ; |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
246 |
else { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
247 |
if (Float.isInfinite(f)) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
248 |
return (f < 0.0f) ? "-1.0f/0.0f": "1.0f/0.0f"; |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
249 |
} else |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
250 |
return "0.0f/0.0f"; |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
251 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
252 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
253 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
254 |
private static String toSourceString(double d) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
255 |
if (Double.isFinite(d)) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
256 |
return Double.toString(d); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
257 |
else { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
258 |
if (Double.isInfinite(d)) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
259 |
return (d < 0.0f) ? "-1.0/0.0": "1.0/0.0"; |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
260 |
} else |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
261 |
return "0.0/0.0"; |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
262 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
263 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
264 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
265 |
private static String toSourceString(char c) { |
41882 | 266 |
StringBuilder sb = new StringBuilder(4); |
267 |
sb.append('\''); |
|
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
268 |
sb.append(quote(c)); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
269 |
return sb.append('\'') .toString(); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
270 |
} |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
271 |
|
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
272 |
/** |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
273 |
* Escapes a character if it has an escape sequence or is |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
274 |
* non-printable ASCII. Leaves non-ASCII characters alone. |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
275 |
*/ |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
276 |
private static String quote(char ch) { |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
277 |
switch (ch) { |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
278 |
case '\b': return "\\b"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
279 |
case '\f': return "\\f"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
280 |
case '\n': return "\\n"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
281 |
case '\r': return "\\r"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
282 |
case '\t': return "\\t"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
283 |
case '\'': return "\\'"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
284 |
case '\"': return "\\\""; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
285 |
case '\\': return "\\\\"; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
286 |
default: |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
287 |
return (isPrintableAscii(ch)) |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
288 |
? String.valueOf(ch) |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
289 |
: String.format("\\u%04x", (int) ch); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
290 |
} |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
291 |
} |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
292 |
|
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
293 |
/** |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
294 |
* Is a character printable ASCII? |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
295 |
*/ |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
296 |
private static boolean isPrintableAscii(char ch) { |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
297 |
return ch >= ' ' && ch <= '~'; |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
298 |
} |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
299 |
|
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
300 |
private static String toSourceString(byte b) { |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
301 |
return String.format("(byte)0x%02x", b); |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
302 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
303 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
304 |
private static String toSourceString(long ell) { |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
305 |
return String.valueOf(ell) + "L"; |
38770
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
306 |
} |
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
307 |
|
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
308 |
/** |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
309 |
* Return a string suitable for use in the string representation |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
310 |
* of an annotation. |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
311 |
*/ |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
312 |
private static String toSourceString(String s) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
313 |
StringBuilder sb = new StringBuilder(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
314 |
sb.append('"'); |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
315 |
for (int i = 0; i < s.length(); i++) { |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
316 |
sb.append(quote(s.charAt(i))); |
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
317 |
} |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
318 |
sb.append('"'); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
319 |
return sb.toString(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
320 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
321 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
322 |
private static Stream<String> convert(byte[] values) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
323 |
List<String> list = new ArrayList<>(values.length); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
324 |
for (byte b : values) |
55387
761b86d5563d
8164819: Make javac's toString() on annotation objects consistent with core reflection
darcy
parents:
52220
diff
changeset
|
325 |
list.add(toSourceString(b)); |
40116
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
326 |
return list.stream(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
327 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
328 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
329 |
private static Stream<String> convert(char[] values) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
330 |
List<String> list = new ArrayList<>(values.length); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
331 |
for (char c : values) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
332 |
list.add(toSourceString(c)); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
333 |
return list.stream(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
334 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
335 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
336 |
private static Stream<String> convert(float[] values) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
337 |
List<String> list = new ArrayList<>(values.length); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
338 |
for (float f : values) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
339 |
list.add(toSourceString(f)); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
340 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
341 |
return list.stream(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
342 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
343 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
344 |
private static Stream<String> convert(short[] values) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
345 |
List<String> list = new ArrayList<>(values.length); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
346 |
for (short s : values) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
347 |
list.add(Short.toString(s)); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
348 |
return list.stream(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
349 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
350 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
351 |
private static Stream<String> convert(boolean[] values) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
352 |
List<String> list = new ArrayList<>(values.length); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
353 |
for (boolean b : values) |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
354 |
list.add(Boolean.toString(b)); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
355 |
return list.stream(); |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
356 |
} |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
357 |
|
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
358 |
private static String stringStreamToString(Stream<String> stream) { |
9bde91c0d0ef
8162817: Annotation toString output not reusable for source input
darcy
parents:
39043
diff
changeset
|
359 |
return stream.collect(Collectors.joining(", ", "{", "}")); |
38770
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
360 |
} |
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
361 |
|
e8746fa36f1a
5040830: (ann) please improve toString() for annotations containing exception proxies
darcy
parents:
35295
diff
changeset
|
362 |
/** |
2 | 363 |
* Implementation of dynamicProxy.equals(Object o) |
364 |
*/ |
|
39043
815e52744068
8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation
darcy
parents:
38770
diff
changeset
|
365 |
private Boolean equalsImpl(Object proxy, Object o) { |
815e52744068
8071859: AnnotationInvocationHandler.equals(Object) return true when apply to annotation
darcy
parents:
38770
diff
changeset
|
366 |
if (o == proxy) |
2 | 367 |
return true; |
368 |
||
369 |
if (!type.isInstance(o)) |
|
370 |
return false; |
|
371 |
for (Method memberMethod : getMemberMethods()) { |
|
372 |
String member = memberMethod.getName(); |
|
373 |
Object ourValue = memberValues.get(member); |
|
374 |
Object hisValue = null; |
|
375 |
AnnotationInvocationHandler hisHandler = asOneOfUs(o); |
|
376 |
if (hisHandler != null) { |
|
377 |
hisValue = hisHandler.memberValues.get(member); |
|
378 |
} else { |
|
379 |
try { |
|
380 |
hisValue = memberMethod.invoke(o); |
|
381 |
} catch (InvocationTargetException e) { |
|
382 |
return false; |
|
383 |
} catch (IllegalAccessException e) { |
|
384 |
throw new AssertionError(e); |
|
385 |
} |
|
386 |
} |
|
387 |
if (!memberValueEquals(ourValue, hisValue)) |
|
388 |
return false; |
|
389 |
} |
|
390 |
return true; |
|
391 |
} |
|
392 |
||
393 |
/** |
|
394 |
* Returns an object's invocation handler if that object is a dynamic |
|
395 |
* proxy with a handler of type AnnotationInvocationHandler. |
|
396 |
* Returns null otherwise. |
|
397 |
*/ |
|
398 |
private AnnotationInvocationHandler asOneOfUs(Object o) { |
|
399 |
if (Proxy.isProxyClass(o.getClass())) { |
|
400 |
InvocationHandler handler = Proxy.getInvocationHandler(o); |
|
401 |
if (handler instanceof AnnotationInvocationHandler) |
|
402 |
return (AnnotationInvocationHandler) handler; |
|
403 |
} |
|
404 |
return null; |
|
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* Returns true iff the two member values in "dynamic proxy return form" |
|
409 |
* are equal using the appropriate equality function depending on the |
|
410 |
* member type. The two values will be of the same type unless one of |
|
411 |
* the containing annotations is ill-formed. If one of the containing |
|
412 |
* annotations is ill-formed, this method will return false unless the |
|
413 |
* two members are identical object references. |
|
414 |
*/ |
|
415 |
private static boolean memberValueEquals(Object v1, Object v2) { |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
416 |
Class<?> type = v1.getClass(); |
2 | 417 |
|
418 |
// Check for primitive, string, class, enum const, annotation, |
|
419 |
// or ExceptionProxy |
|
420 |
if (!type.isArray()) |
|
421 |
return v1.equals(v2); |
|
422 |
||
423 |
// Check for array of string, class, enum const, annotation, |
|
424 |
// or ExceptionProxy |
|
425 |
if (v1 instanceof Object[] && v2 instanceof Object[]) |
|
426 |
return Arrays.equals((Object[]) v1, (Object[]) v2); |
|
427 |
||
428 |
// Check for ill formed annotation(s) |
|
429 |
if (v2.getClass() != type) |
|
430 |
return false; |
|
431 |
||
432 |
// Deal with array of primitives |
|
433 |
if (type == byte[].class) |
|
434 |
return Arrays.equals((byte[]) v1, (byte[]) v2); |
|
435 |
if (type == char[].class) |
|
436 |
return Arrays.equals((char[]) v1, (char[]) v2); |
|
437 |
if (type == double[].class) |
|
438 |
return Arrays.equals((double[]) v1, (double[]) v2); |
|
439 |
if (type == float[].class) |
|
440 |
return Arrays.equals((float[]) v1, (float[]) v2); |
|
441 |
if (type == int[].class) |
|
442 |
return Arrays.equals((int[]) v1, (int[]) v2); |
|
443 |
if (type == long[].class) |
|
444 |
return Arrays.equals((long[]) v1, (long[]) v2); |
|
445 |
if (type == short[].class) |
|
446 |
return Arrays.equals((short[]) v1, (short[]) v2); |
|
447 |
assert type == boolean[].class; |
|
448 |
return Arrays.equals((boolean[]) v1, (boolean[]) v2); |
|
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* Returns the member methods for our annotation type. These are |
|
453 |
* obtained lazily and cached, as they're expensive to obtain |
|
454 |
* and we only need them if our equals method is invoked (which should |
|
455 |
* be rare). |
|
456 |
*/ |
|
457 |
private Method[] getMemberMethods() { |
|
28056
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
458 |
Method[] value = memberMethods; |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
459 |
if (value == null) { |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
460 |
value = computeMemberMethods(); |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
461 |
memberMethods = value; |
2 | 462 |
} |
28056
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
463 |
return value; |
2 | 464 |
} |
28056
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
465 |
|
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
466 |
private Method[] computeMemberMethods() { |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
467 |
return AccessController.doPrivileged( |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
468 |
new PrivilegedAction<Method[]>() { |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
469 |
public Method[] run() { |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
470 |
final Method[] methods = type.getDeclaredMethods(); |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
471 |
validateAnnotationMethods(methods); |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
472 |
AccessibleObject.setAccessible(methods, true); |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
473 |
return methods; |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
474 |
}}); |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
475 |
} |
0cab6eb92852
8065172: More core reflection final and volatile annotations
martin
parents:
27069
diff
changeset
|
476 |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
28056
diff
changeset
|
477 |
private transient volatile Method[] memberMethods; |
2 | 478 |
|
479 |
/** |
|
27069 | 480 |
* Validates that a method is structurally appropriate for an |
481 |
* annotation type. As of Java SE 8, annotation types cannot |
|
482 |
* contain static methods and the declared methods of an |
|
483 |
* annotation type must take zero arguments and there are |
|
484 |
* restrictions on the return type. |
|
485 |
*/ |
|
486 |
private void validateAnnotationMethods(Method[] memberMethods) { |
|
487 |
/* |
|
488 |
* Specification citations below are from JLS |
|
489 |
* 9.6.1. Annotation Type Elements |
|
490 |
*/ |
|
491 |
boolean valid = true; |
|
492 |
for(Method method : memberMethods) { |
|
493 |
/* |
|
494 |
* "By virtue of the AnnotationTypeElementDeclaration |
|
495 |
* production, a method declaration in an annotation type |
|
496 |
* declaration cannot have formal parameters, type |
|
497 |
* parameters, or a throws clause. |
|
498 |
* |
|
499 |
* "By virtue of the AnnotationTypeElementModifier |
|
500 |
* production, a method declaration in an annotation type |
|
501 |
* declaration cannot be default or static." |
|
502 |
*/ |
|
503 |
if (method.getModifiers() != (Modifier.PUBLIC | Modifier.ABSTRACT) || |
|
504 |
method.isDefault() || |
|
505 |
method.getParameterCount() != 0 || |
|
506 |
method.getExceptionTypes().length != 0) { |
|
507 |
valid = false; |
|
508 |
break; |
|
509 |
} |
|
510 |
||
511 |
/* |
|
512 |
* "It is a compile-time error if the return type of a |
|
513 |
* method declared in an annotation type is not one of the |
|
514 |
* following: a primitive type, String, Class, any |
|
515 |
* parameterized invocation of Class, an enum type |
|
516 |
* (section 8.9), an annotation type, or an array type |
|
517 |
* (chapter 10) whose element type is one of the preceding |
|
518 |
* types." |
|
519 |
*/ |
|
520 |
Class<?> returnType = method.getReturnType(); |
|
521 |
if (returnType.isArray()) { |
|
522 |
returnType = returnType.getComponentType(); |
|
523 |
if (returnType.isArray()) { // Only single dimensional arrays |
|
524 |
valid = false; |
|
525 |
break; |
|
526 |
} |
|
527 |
} |
|
528 |
||
529 |
if (!((returnType.isPrimitive() && returnType != void.class) || |
|
530 |
returnType == java.lang.String.class || |
|
531 |
returnType == java.lang.Class.class || |
|
532 |
returnType.isEnum() || |
|
533 |
returnType.isAnnotation())) { |
|
534 |
valid = false; |
|
535 |
break; |
|
536 |
} |
|
537 |
||
538 |
/* |
|
539 |
* "It is a compile-time error if any method declared in an |
|
540 |
* annotation type has a signature that is |
|
541 |
* override-equivalent to that of any public or protected |
|
542 |
* method declared in class Object or in the interface |
|
543 |
* java.lang.annotation.Annotation." |
|
544 |
* |
|
545 |
* The methods in Object or Annotation meeting the other |
|
546 |
* criteria (no arguments, contrained return type, etc.) |
|
547 |
* above are: |
|
548 |
* |
|
549 |
* String toString() |
|
550 |
* int hashCode() |
|
551 |
* Class<? extends Annotation> annotationType() |
|
552 |
*/ |
|
553 |
String methodName = method.getName(); |
|
554 |
if ((methodName.equals("toString") && returnType == java.lang.String.class) || |
|
555 |
(methodName.equals("hashCode") && returnType == int.class) || |
|
556 |
(methodName.equals("annotationType") && returnType == java.lang.Class.class)) { |
|
557 |
valid = false; |
|
558 |
break; |
|
559 |
} |
|
560 |
} |
|
561 |
if (valid) |
|
562 |
return; |
|
563 |
else |
|
564 |
throw new AnnotationFormatError("Malformed method on an annotation type"); |
|
565 |
} |
|
566 |
||
567 |
/** |
|
2 | 568 |
* Implementation of dynamicProxy.hashCode() |
569 |
*/ |
|
570 |
private int hashCodeImpl() { |
|
571 |
int result = 0; |
|
572 |
for (Map.Entry<String, Object> e : memberValues.entrySet()) { |
|
573 |
result += (127 * e.getKey().hashCode()) ^ |
|
574 |
memberValueHashCode(e.getValue()); |
|
575 |
} |
|
576 |
return result; |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Computes hashCode of a member value (in "dynamic proxy return form") |
|
581 |
*/ |
|
582 |
private static int memberValueHashCode(Object value) { |
|
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
583 |
Class<?> type = value.getClass(); |
2 | 584 |
if (!type.isArray()) // primitive, string, class, enum const, |
585 |
// or annotation |
|
586 |
return value.hashCode(); |
|
587 |
||
588 |
if (type == byte[].class) |
|
589 |
return Arrays.hashCode((byte[]) value); |
|
590 |
if (type == char[].class) |
|
591 |
return Arrays.hashCode((char[]) value); |
|
592 |
if (type == double[].class) |
|
593 |
return Arrays.hashCode((double[]) value); |
|
594 |
if (type == float[].class) |
|
595 |
return Arrays.hashCode((float[]) value); |
|
596 |
if (type == int[].class) |
|
597 |
return Arrays.hashCode((int[]) value); |
|
598 |
if (type == long[].class) |
|
599 |
return Arrays.hashCode((long[]) value); |
|
600 |
if (type == short[].class) |
|
601 |
return Arrays.hashCode((short[]) value); |
|
602 |
if (type == boolean[].class) |
|
603 |
return Arrays.hashCode((boolean[]) value); |
|
604 |
return Arrays.hashCode((Object[]) value); |
|
605 |
} |
|
606 |
||
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55387
diff
changeset
|
607 |
@java.io.Serial |
2 | 608 |
private void readObject(java.io.ObjectInputStream s) |
609 |
throws java.io.IOException, ClassNotFoundException { |
|
35295 | 610 |
ObjectInputStream.GetField fields = s.readFields(); |
611 |
||
612 |
@SuppressWarnings("unchecked") |
|
613 |
Class<? extends Annotation> t = (Class<? extends Annotation>)fields.get("type", null); |
|
614 |
@SuppressWarnings("unchecked") |
|
615 |
Map<String, Object> streamVals = (Map<String, Object>)fields.get("memberValues", null); |
|
2 | 616 |
|
617 |
// Check to make sure that types have not evolved incompatibly |
|
618 |
||
619 |
AnnotationType annotationType = null; |
|
620 |
try { |
|
35295 | 621 |
annotationType = AnnotationType.getInstance(t); |
2 | 622 |
} catch(IllegalArgumentException e) { |
18199 | 623 |
// Class is no longer an annotation type; time to punch out |
624 |
throw new java.io.InvalidObjectException("Non-annotation type in annotation serial stream"); |
|
2 | 625 |
} |
626 |
||
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
627 |
Map<String, Class<?>> memberTypes = annotationType.memberTypes(); |
35295 | 628 |
// consistent with runtime Map type |
629 |
Map<String, Object> mv = new LinkedHashMap<>(); |
|
2 | 630 |
|
18199 | 631 |
// If there are annotation members without values, that |
632 |
// situation is handled by the invoke method. |
|
35295 | 633 |
for (Map.Entry<String, Object> memberValue : streamVals.entrySet()) { |
2 | 634 |
String name = memberValue.getKey(); |
35295 | 635 |
Object value = null; |
3959
05a07c0a273b
5062288: (reflect) Core reflection uses raw types when it could be using wildcards
darcy
parents:
1508
diff
changeset
|
636 |
Class<?> memberType = memberTypes.get(name); |
2 | 637 |
if (memberType != null) { // i.e. member still exists |
35295 | 638 |
value = memberValue.getValue(); |
2 | 639 |
if (!(memberType.isInstance(value) || |
640 |
value instanceof ExceptionProxy)) { |
|
35295 | 641 |
value = new AnnotationTypeMismatchExceptionProxy( |
2 | 642 |
value.getClass() + "[" + value + "]").setMember( |
35295 | 643 |
annotationType.members().get(name)); |
2 | 644 |
} |
645 |
} |
|
35295 | 646 |
mv.put(name, value); |
647 |
} |
|
648 |
||
649 |
UnsafeAccessor.setType(this, t); |
|
650 |
UnsafeAccessor.setMemberValues(this, mv); |
|
651 |
} |
|
652 |
||
653 |
private static class UnsafeAccessor { |
|
46873
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
654 |
private static final jdk.internal.misc.Unsafe unsafe |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
655 |
= jdk.internal.misc.Unsafe.getUnsafe(); |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
656 |
private static final long typeOffset = unsafe.objectFieldOffset |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
657 |
(AnnotationInvocationHandler.class, "type"); |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
658 |
private static final long memberValuesOffset = unsafe.objectFieldOffset |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
659 |
(AnnotationInvocationHandler.class, "memberValues"); |
7ac2f551b0d6
8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents:
41882
diff
changeset
|
660 |
|
35295 | 661 |
static void setType(AnnotationInvocationHandler o, |
662 |
Class<? extends Annotation> type) { |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
47436
diff
changeset
|
663 |
unsafe.putReference(o, typeOffset, type); |
35295 | 664 |
} |
665 |
||
666 |
static void setMemberValues(AnnotationInvocationHandler o, |
|
667 |
Map<String, Object> memberValues) { |
|
52220
9c260a6b6471
8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents:
47436
diff
changeset
|
668 |
unsafe.putReference(o, memberValuesOffset, memberValues); |
2 | 669 |
} |
670 |
} |
|
671 |
} |