-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathObjectMethods.java
More file actions
63 lines (53 loc) · 2.82 KB
/
ObjectMethods.java
File metadata and controls
63 lines (53 loc) · 2.82 KB
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
package net.datafaker.providers.base;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.Collections.synchronizedMap;
import static java.util.stream.Collectors.toMap;
public class ObjectMethods {
private static final Map<Class<?>, Map<String, Method>> METHODS_BY_NAME = synchronizedMap(new IdentityHashMap<>());
private static final Map<Class<?>, Map<String, Method>> METHODS_BY_RETURN_TYPE = synchronizedMap(new IdentityHashMap<>());
private static final Set<String> IGNORED_METHODS = Set.of("equals", "hashCode", "toString", "Builder", "stream");
private static synchronized Map<String, Method> scanMethodsByName(Class<?> clazz) {
return Stream.of(clazz.getMethods())
.filter(ObjectMethods::isUseful)
.collect(toMap(Method::getName, method -> method));
}
private static synchronized Map<String, Method> scanMethodsByReturnType(Class<?> clazz) {
return Stream.of(clazz.getMethods())
.filter(ObjectMethods::isUseful)
.collect(toMap(method -> method.getReturnType().getSimpleName(), method -> method, ObjectMethods::chooseFirstByAlphabet));
}
private static Method chooseFirstByAlphabet(Method m1, Method m2) {
return m1.getName().compareTo(m2.getName()) < 0 ? m1 : m2;
}
/**
* Later we could mark all provider methods with some annotation like "@Provider" instead of this shaky logic
*/
private static boolean isUseful(Method method) {
return method.getParameterCount() == 0
&& method.getDeclaringClass() != Object.class
&& method.getReturnType() != void.class
&& !IGNORED_METHODS.contains(method.getName());
}
public static Method getMethodByName(Object object, String methodName) {
return METHODS_BY_NAME.computeIfAbsent(object.getClass(), ObjectMethods::scanMethodsByName).get(methodName);
}
private static Method getMethodByReturnType(Object object, String returnTypeSimpleName) {
return METHODS_BY_RETURN_TYPE.computeIfAbsent(object.getClass(), ObjectMethods::scanMethodsByReturnType).get(returnTypeSimpleName);
}
@SuppressWarnings("unchecked")
public static <T> T executeMethodByReturnType(Object object, String returnTypeSimpleName) {
try {
Method method = getMethodByReturnType(object, returnTypeSimpleName);
if (method == null) return null;
method.setAccessible(true);
return (T) method.invoke(object);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Failed to call method %s.%s()".formatted(object.getClass().getName(), returnTypeSimpleName), e);
}
}
}