forked from apache/dubbo
/
MethodUtils.java
394 lines (350 loc) · 14.6 KB
/
MethodUtils.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dubbo.common.utils;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static org.apache.dubbo.common.function.Streams.filterAll;
import static org.apache.dubbo.common.utils.ClassUtils.getAllInheritedTypes;
import static org.apache.dubbo.common.utils.MemberUtils.isPrivate;
import static org.apache.dubbo.common.utils.MemberUtils.isStatic;
import static org.apache.dubbo.common.utils.ReflectUtils.EMPTY_CLASS_ARRAY;
import static org.apache.dubbo.common.utils.ReflectUtils.resolveTypes;
import static org.apache.dubbo.common.utils.StringUtils.isNotEmpty;
/**
* Miscellaneous method utility methods.
* Mainly for internal use within the framework.
*
* @since 2.7.2
*/
public interface MethodUtils {
/**
* Return {@code true} if the provided method is a set method.
* Otherwise, return {@code false}.
*
* @param method the method to check
* @return whether the given method is setter method
*/
public static boolean isSetter(Method method) {
return method.getName().startsWith("set")
&& !"set".equals(method.getName())
&& Modifier.isPublic(method.getModifiers())
&& method.getParameterCount() == 1
&& ClassUtils.isPrimitive(method.getParameterTypes()[0]);
}
/**
* Return {@code true} if the provided method is a get method.
* Otherwise, return {@code false}.
*
* @param method the method to check
* @return whether the given method is getter method
*/
public static boolean isGetter(Method method) {
String name = method.getName();
return (name.startsWith("get") || name.startsWith("is"))
&& !"get".equals(name) && !"is".equals(name)
&& !"getClass".equals(name) && !"getObject".equals(name)
&& Modifier.isPublic(method.getModifiers())
&& method.getParameterTypes().length == 0
&& ClassUtils.isPrimitive(method.getReturnType());
}
/**
* Return {@code true} If this method is a meta method.
* Otherwise, return {@code false}.
*
* @param method the method to check
* @return whether the given method is meta method
*/
public static boolean isMetaMethod(Method method) {
String name = method.getName();
if (!(name.startsWith("get") || name.startsWith("is"))) {
return false;
}
if ("get".equals(name)) {
return false;
}
if ("getClass".equals(name)) {
return false;
}
if (!Modifier.isPublic(method.getModifiers())) {
return false;
}
if (method.getParameterTypes().length != 0) {
return false;
}
if (!ClassUtils.isPrimitive(method.getReturnType())) {
return false;
}
return true;
}
/**
* Check if the method is a deprecated method. The standard is whether the {@link java.lang.Deprecated} annotation is declared on the class.
* Return {@code true} if this annotation is present.
* Otherwise, return {@code false}.
*
* @param method the method to check
* @return whether the given method is deprecated method
*/
public static boolean isDeprecated(Method method) {
return method.getAnnotation(Deprecated.class) != null;
}
/**
* Create an instance of {@link Predicate} for {@link Method} to exclude the specified declared class
*
* @param declaredClass the declared class to exclude
* @return non-null
* @since 2.7.6
*/
static Predicate<Method> excludedDeclaredClass(Class<?> declaredClass) {
return method -> !Objects.equals(declaredClass, method.getDeclaringClass());
}
/**
* Get all {@link Method methods} of the declared class
*
* @param declaringClass the declared class
* @param includeInheritedTypes include the inherited types, e,g. super classes or interfaces
* @param publicOnly only public method
* @param methodsToFilter (optional) the methods to be filtered
* @return non-null read-only {@link List}
* @since 2.7.6
*/
static List<Method> getMethods(Class<?> declaringClass, boolean includeInheritedTypes, boolean publicOnly,
Predicate<Method>... methodsToFilter) {
if (declaringClass == null || declaringClass.isPrimitive()) {
return emptyList();
}
// All declared classes
List<Class<?>> declaredClasses = new LinkedList<>();
// Add the top declaring class
declaredClasses.add(declaringClass);
// If the super classes are resolved, all them into declaredClasses
if (includeInheritedTypes) {
declaredClasses.addAll(getAllInheritedTypes(declaringClass));
}
// All methods
List<Method> allMethods = new LinkedList<>();
for (Class<?> classToSearch : declaredClasses) {
Method[] methods = publicOnly ? classToSearch.getMethods() : classToSearch.getDeclaredMethods();
// Add the declared methods or public methods
for (Method method : methods) {
allMethods.add(method);
}
}
return unmodifiableList(filterAll(allMethods, methodsToFilter));
}
/**
* Get all declared {@link Method methods} of the declared class, excluding the inherited methods
*
* @param declaringClass the declared class
* @param methodsToFilter (optional) the methods to be filtered
* @return non-null read-only {@link List}
* @see #getMethods(Class, boolean, boolean, Predicate[])
* @since 2.7.6
*/
static List<Method> getDeclaredMethods(Class<?> declaringClass, Predicate<Method>... methodsToFilter) {
return getMethods(declaringClass, false, false, methodsToFilter);
}
/**
* Get all public {@link Method methods} of the declared class, including the inherited methods.
*
* @param declaringClass the declared class
* @param methodsToFilter (optional) the methods to be filtered
* @return non-null read-only {@link List}
* @see #getMethods(Class, boolean, boolean, Predicate[])
* @since 2.7.6
*/
static List<Method> getMethods(Class<?> declaringClass, Predicate<Method>... methodsToFilter) {
return getMethods(declaringClass, false, true, methodsToFilter);
}
/**
* Get all declared {@link Method methods} of the declared class, including the inherited methods.
*
* @param declaringClass the declared class
* @param methodsToFilter (optional) the methods to be filtered
* @return non-null read-only {@link List}
* @see #getMethods(Class, boolean, boolean, Predicate[])
* @since 2.7.6
*/
static List<Method> getAllDeclaredMethods(Class<?> declaringClass, Predicate<Method>... methodsToFilter) {
return getMethods(declaringClass, true, false, methodsToFilter);
}
/**
* Get all public {@link Method methods} of the declared class, including the inherited methods.
*
* @param declaringClass the declared class
* @param methodsToFilter (optional) the methods to be filtered
* @return non-null read-only {@link List}
* @see #getMethods(Class, boolean, boolean, Predicate[])
* @since 2.7.6
*/
static List<Method> getAllMethods(Class<?> declaringClass, Predicate<Method>... methodsToFilter) {
return getMethods(declaringClass, true, true, methodsToFilter);
}
// static List<Method> getOverriderMethods(Class<?> implementationClass, Class<?>... superTypes) {
//
// }
/**
* Find the {@link Method} by the the specified type and method name without the parameter types
*
* @param type the target type
* @param methodName the specified method name
* @return if not found, return <code>null</code>
* @since 2.7.6
*/
static Method findMethod(Class type, String methodName) {
return findMethod(type, methodName, EMPTY_CLASS_ARRAY);
}
/**
* Find the {@link Method} by the the specified type, method name and parameter types
*
* @param type the target type
* @param methodName the method name
* @param parameterTypes the parameter types
* @return if not found, return <code>null</code>
* @since 2.7.6
*/
static Method findMethod(Class type, String methodName, Class<?>... parameterTypes) {
Method method = null;
try {
if (type != null && isNotEmpty(methodName)) {
method = type.getDeclaredMethod(methodName, parameterTypes);
}
} catch (NoSuchMethodException e) {
}
return method;
}
/**
* Invoke the target object and method
*
* @param object the target object
* @param methodName the method name
* @param methodParameters the method parameters
* @param <T> the return type
* @return the target method's execution result
* @since 2.7.6
*/
static <T> T invokeMethod(Object object, String methodName, Object... methodParameters) {
Class type = object.getClass();
Class[] parameterTypes = resolveTypes(methodParameters);
Method method = findMethod(type, methodName, parameterTypes);
T value = null;
try {
final boolean isAccessible = method.isAccessible();
if (!isAccessible) {
method.setAccessible(true);
}
value = (T) method.invoke(object, methodParameters);
method.setAccessible(isAccessible);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return value;
}
/**
* Tests whether one method, as a member of a given type,
* overrides another method.
*
* @param overrider the first method, possible overrider
* @param overridden the second method, possibly being overridden
* @return {@code true} if and only if the first method overrides
* the second
* @jls 8.4.8 Inheritance, Overriding, and Hiding
* @jls 9.4.1 Inheritance and Overriding
* @see Elements#overrides(ExecutableElement, ExecutableElement, TypeElement)
*/
static boolean overrides(Method overrider, Method overridden) {
if (overrider == null || overridden == null) {
return false;
}
// equality comparison: If two methods are same
if (Objects.equals(overrider, overridden)) {
return false;
}
// Modifiers comparison: Any method must be non-static method
if (isStatic(overrider) || isStatic(overridden)) { //
return false;
}
// Modifiers comparison: the accessibility of any method must not be private
if (isPrivate(overrider) || isPrivate(overridden)) {
return false;
}
// Inheritance comparison: The declaring class of overrider must be inherit from the overridden's
if (!overridden.getDeclaringClass().isAssignableFrom(overrider.getDeclaringClass())) {
return false;
}
// Method comparison: must not be "default" method
if (overrider.isDefault()) {
return false;
}
// Method comparison: The method name must be equal
if (!Objects.equals(overrider.getName(), overridden.getName())) {
return false;
}
// Method comparison: The count of method parameters must be equal
if (!Objects.equals(overrider.getParameterCount(), overridden.getParameterCount())) {
return false;
}
// Method comparison: Any parameter type of overrider must equal the overridden's
for (int i = 0; i < overrider.getParameterCount(); i++) {
if (!Objects.equals(overridden.getParameterTypes()[i], overrider.getParameterTypes()[i])) {
return false;
}
}
// Method comparison: The return type of overrider must be inherit from the overridden's
if (!overridden.getReturnType().isAssignableFrom(overrider.getReturnType())) {
return false;
}
// Throwable comparison: "throws" Throwable list will be ignored, trust the compiler verify
return true;
}
/**
* Find the nearest overridden {@link Method method} from the inherited class
*
* @param overrider the overrider {@link Method method}
* @return if found, the overrider <code>method</code>, or <code>null</code>
*/
static Method findNearestOverriddenMethod(Method overrider) {
Class<?> declaringClass = overrider.getDeclaringClass();
Method overriddenMethod = null;
for (Class<?> inheritedType : getAllInheritedTypes(declaringClass)) {
overriddenMethod = findOverriddenMethod(overrider, inheritedType);
if (overriddenMethod != null) {
break;
}
}
return overriddenMethod;
}
/**
* Find the overridden {@link Method method} from the declaring class
*
* @param overrider the overrider {@link Method method}
* @param declaringClass the class that is declaring the overridden {@link Method method}
* @return if found, the overrider <code>method</code>, or <code>null</code>
*/
static Method findOverriddenMethod(Method overrider, Class<?> declaringClass) {
List<Method> matchedMethods = getAllMethods(declaringClass, method -> overrides(overrider, method));
return matchedMethods.isEmpty() ? null : matchedMethods.get(0);
}
}