You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/12/24 14:37:23 UTC
[shardingsphere] branch master updated: Refactor AgentReflectionUtil (#23083)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 0967b62e0ca Refactor AgentReflectionUtil (#23083)
0967b62e0ca is described below
commit 0967b62e0ca3408f1e666020b830641e912440ae
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Dec 24 22:37:17 2022 +0800
Refactor AgentReflectionUtil (#23083)
---
.../agent/core/util/AgentReflectionUtil.java | 43 ++++++++++++++--------
1 file changed, 28 insertions(+), 15 deletions(-)
diff --git a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentReflectionUtil.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentReflectionUtil.java
index 9643202215d..9e183ec4d9b 100644
--- a/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentReflectionUtil.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/AgentReflectionUtil.java
@@ -23,6 +23,7 @@ import lombok.SneakyThrows;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.Optional;
/**
* Agent reflection utility.
@@ -38,27 +39,39 @@ public final class AgentReflectionUtil {
* @param <T> type of field value
* @return field value
*/
- @SuppressWarnings("unchecked")
@SneakyThrows(ReflectiveOperationException.class)
public static <T> T getFieldValue(final Object target, final String fieldName) {
- Class<?> clazz = target.getClass();
- while (null != clazz) {
+ Optional<Field> field = findField(fieldName, target.getClass());
+ if (field.isPresent()) {
+ return getFieldValue(target, field.get());
+ }
+ throw new NoSuchFieldException(String.format("Can not find field name `%s` in class %s.", fieldName, target.getClass()));
+ }
+
+ @SuppressWarnings("unchecked")
+ @SneakyThrows(IllegalAccessException.class)
+ private static <T> T getFieldValue(final Object target, final Field field) {
+ boolean accessible = field.isAccessible();
+ if (!accessible) {
+ field.setAccessible(true);
+ }
+ T result = (T) field.get(target);
+ if (!accessible) {
+ field.setAccessible(false);
+ }
+ return result;
+ }
+
+ private static Optional<Field> findField(final String fieldName, final Class<?> targetClass) {
+ Class<?> currentTargetClass = targetClass;
+ while (Object.class != currentTargetClass) {
try {
- Field field = clazz.getDeclaredField(fieldName);
- boolean accessible = field.isAccessible();
- if (!accessible) {
- field.setAccessible(true);
- }
- T result = (T) field.get(target);
- if (!accessible) {
- field.setAccessible(false);
- }
- return result;
+ return Optional.of(currentTargetClass.getDeclaredField(fieldName));
} catch (final NoSuchFieldException ignored) {
+ currentTargetClass = currentTargetClass.getSuperclass();
}
- clazz = clazz.getSuperclass();
}
- throw new NoSuchFieldException(String.format("Can not find field name `%s` in class %s.", fieldName, target.getClass()));
+ return Optional.empty();
}
/**