forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPublicMethods.java
274 lines (254 loc) · 10.4 KB
/
PublicMethods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* Copyright (c) 2016, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.lang;
import jdk.internal.reflect.ReflectionFactory;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.AccessController;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* A collection of most specific public methods. Methods are added to it using
* {@link #merge(Method)} method. Only the most specific methods for a
* particular signature are kept.
*/
final class PublicMethods {
/**
* a map of (method name, parameter types) -> linked list of Method(s)
*/
private final Map<Key, MethodList> map = new LinkedHashMap<>();
/**
* keeps track of the number of collected methods
*/
private int methodCount;
/**
* Merges new method with existing methods. New method is either
* ignored (if a more specific method with same signature exists) or added
* to the collection. When it is added to the collection, it may replace one
* or more existing methods with same signature if they are less specific
* than added method.
* See comments in code...
*/
void merge(Method method) {
Key key = new Key(method);
MethodList existing = map.get(key);
int xLen = existing == null ? 0 : existing.length();
MethodList merged = MethodList.merge(existing, method);
methodCount += merged.length() - xLen;
// replace if head of list changed
if (merged != existing) {
map.put(key, merged);
}
}
/**
* Dumps methods to array.
*/
Method[] toArray() {
Method[] array = new Method[methodCount];
int i = 0;
for (MethodList ml : map.values()) {
for (; ml != null; ml = ml.next) {
array[i++] = ml.method;
}
}
return array;
}
/**
* Method (name, parameter types) tuple.
*/
private static final class Key {
private static final ReflectionFactory reflectionFactory =
AccessController.doPrivileged(
new ReflectionFactory.GetReflectionFactoryAction());
private final String name; // must be interned (as from Method.getName())
private final Class<?>[] ptypes;
Key(Method method) {
name = method.getName();
ptypes = reflectionFactory.getExecutableSharedParameterTypes(method);
}
static boolean matches(Method method,
String name, // may not be interned
Class<?>[] ptypes) {
return method.getName().equals(name) &&
Arrays.equals(
reflectionFactory.getExecutableSharedParameterTypes(method),
ptypes
);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key that = (Key) o;
//noinspection StringEquality (guaranteed interned String(s))
return name == that.name &&
Arrays.equals(ptypes, that.ptypes);
}
@Override
public int hashCode() {
return System.identityHashCode(name) + // guaranteed interned String
31 * Arrays.hashCode(ptypes);
}
}
/**
* Node of a inked list containing Method(s) sharing the same
* (name, parameter types) tuple.
*/
static final class MethodList {
Method method;
MethodList next;
private MethodList(Method method) {
this.method = method;
}
/**
* @return the head of a linked list containing given {@code methods}
* filtered by given method {@code name}, parameter types
* {@code ptypes} and including or excluding static methods as
* requested by {@code includeStatic} flag.
*/
static MethodList filter(Method[] methods, String name,
Class<?>[] ptypes, boolean includeStatic) {
MethodList head = null, tail = null;
for (Method method : methods) {
if ((includeStatic || !Modifier.isStatic(method.getModifiers())) &&
Key.matches(method, name, ptypes)) {
if (tail == null) {
head = tail = new MethodList(method);
} else {
tail = tail.next = new MethodList(method);
}
}
}
return head;
}
/**
* This method should only be called with the {@code head} (possibly null)
* of a list of Method(s) that share the same (method name, parameter types)
* and another {@code methodList} that also contains Method(s) with the
* same and equal (method name, parameter types) as the 1st list.
* It modifies the 1st list and returns the head of merged list
* containing only the most specific methods for each signature
* (i.e. return type). The returned head of the merged list may or
* may not be the same as the {@code head} of the given list.
* The given {@code methodList} is not modified.
*/
static MethodList merge(MethodList head, MethodList methodList) {
for (MethodList ml = methodList; ml != null; ml = ml.next) {
head = merge(head, ml.method);
}
return head;
}
private static MethodList merge(MethodList head, Method method) {
Class<?> dclass = method.getDeclaringClass();
Class<?> rtype = method.getReturnType();
MethodList prev = null;
for (MethodList l = head; l != null; l = l.next) {
// eXisting method
Method xmethod = l.method;
// only merge methods with same signature:
// (return type, name, parameter types) tuple
// as we only keep methods with same (name, parameter types)
// tuple together in one list, we only need to check return type
if (rtype == xmethod.getReturnType()) {
Class<?> xdclass = xmethod.getDeclaringClass();
if (dclass.isInterface() == xdclass.isInterface()) {
// both methods are declared by interfaces
// or both by classes
if (dclass.isAssignableFrom(xdclass)) {
// existing method is the same or overrides
// new method - ignore new method
return head;
}
if (xdclass.isAssignableFrom(dclass)) {
// new method overrides existing
// method - knock out existing method
if (prev != null) {
prev.next = l.next;
} else {
head = l.next;
}
// keep iterating
} else {
// unrelated (should only happen for interfaces)
prev = l;
// keep iterating
}
} else if (dclass.isInterface()) {
// new method is declared by interface while
// existing method is declared by class -
// ignore new method
return head;
} else /* xdclass.isInterface() */ {
// new method is declared by class while
// existing method is declared by interface -
// knock out existing method
if (prev != null) {
prev.next = l.next;
} else {
head = l.next;
}
// keep iterating
}
} else {
// distinct signatures
prev = l;
// keep iterating
}
}
// append new method to the list
if (prev == null) {
head = new MethodList(method);
} else {
prev.next = new MethodList(method);
}
return head;
}
private int length() {
int len = 1;
for (MethodList ml = next; ml != null; ml = ml.next) {
len++;
}
return len;
}
/**
* @return 1st method in list with most specific return type
*/
Method getMostSpecific() {
Method m = method;
Class<?> rt = m.getReturnType();
for (MethodList ml = next; ml != null; ml = ml.next) {
Method m2 = ml.method;
Class<?> rt2 = m2.getReturnType();
if (rt2 != rt && rt.isAssignableFrom(rt2)) {
// found more specific return type
m = m2;
rt = rt2;
}
}
return m;
}
}
}