1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.util.reflection;
19
20 import com.google.common.base.CaseFormat;
21 import lombok.AccessLevel;
22 import lombok.NoArgsConstructor;
23 import lombok.SneakyThrows;
24
25 import java.lang.reflect.Field;
26 import java.lang.reflect.Method;
27 import java.util.Optional;
28
29
30
31
32 @NoArgsConstructor(access = AccessLevel.PRIVATE)
33 public final class ReflectionUtils {
34
35 private static final String GETTER_PREFIX = "get";
36
37
38
39
40
41
42
43
44
45 public static <T> Optional<T> getFieldValue(final Object target, final String fieldName) {
46 return findField(fieldName, target.getClass()).map(optional -> getFieldValue(target, optional));
47 }
48
49 @SuppressWarnings("unchecked")
50 @SneakyThrows(IllegalAccessException.class)
51 private static <T> T getFieldValue(final Object target, final Field field) {
52 boolean accessible = field.isAccessible();
53 if (!accessible) {
54 field.setAccessible(true);
55 }
56 T result = (T) field.get(target);
57 if (!accessible) {
58 field.setAccessible(false);
59 }
60 return result;
61 }
62
63 private static Optional<Field> findField(final String fieldName, final Class<?> targetClass) {
64 Class<?> currentTargetClass = targetClass;
65 while (Object.class != currentTargetClass) {
66 try {
67 return Optional.of(currentTargetClass.getDeclaredField(fieldName));
68 } catch (final NoSuchFieldException ignored) {
69 currentTargetClass = currentTargetClass.getSuperclass();
70 }
71 }
72 return Optional.empty();
73 }
74
75
76
77
78
79
80
81
82
83 @SuppressWarnings("unchecked")
84 @SneakyThrows(ReflectiveOperationException.class)
85 public static <T> T getStaticFieldValue(final Class<?> target, final String fieldName) {
86 Field field = target.getDeclaredField(fieldName);
87 boolean accessible = field.isAccessible();
88 if (!accessible) {
89 field.setAccessible(true);
90 }
91 T result = (T) field.get(target);
92 if (!accessible) {
93 field.setAccessible(false);
94 }
95 return result;
96 }
97
98
99
100
101
102
103
104
105 @SneakyThrows(ReflectiveOperationException.class)
106 public static void setStaticFieldValue(final Class<?> target, final String fieldName, final Object value) {
107 Field field = target.getDeclaredField(fieldName);
108 boolean accessible = field.isAccessible();
109 if (!accessible) {
110 field.setAccessible(true);
111 }
112 field.set(target, value);
113 if (!accessible) {
114 field.setAccessible(false);
115 }
116 }
117
118
119
120
121
122
123
124
125
126
127 @SuppressWarnings("unchecked")
128 @SneakyThrows(ReflectiveOperationException.class)
129 public static <T> T invokeMethod(final Method method, final Object target, final Object... args) {
130 boolean accessible = method.isAccessible();
131 if (!accessible) {
132 method.setAccessible(true);
133 }
134 T result = (T) method.invoke(target, args);
135 if (!accessible) {
136 method.setAccessible(false);
137 }
138 return result;
139 }
140
141
142
143
144
145
146
147
148
149 public static <T> Optional<T> getFieldValueByGetMethod(final Object target, final String fieldName) {
150 String getterName = GETTER_PREFIX + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
151 final Optional<Method> method = findMethod(target.getClass(), getterName);
152 if (method.isPresent()) {
153 T value = invokeMethod(method.get(), target);
154 return Optional.ofNullable(value);
155 } else {
156 return Optional.empty();
157 }
158 }
159
160 private static Optional<Method> findMethod(final Class<?> clazz, final String methodName, final Class<?>... parameterTypes) {
161 try {
162 return Optional.of(clazz.getMethod(methodName, parameterTypes));
163 } catch (final NoSuchMethodException ex) {
164 Class<?> superclass = clazz.getSuperclass();
165 if (null != superclass && Object.class != superclass) {
166 return findMethod(superclass, methodName, parameterTypes);
167 }
168 }
169 return Optional.empty();
170 }
171 }