You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/12/08 16:27:13 UTC
[shardingsphere] branch master updated: Refactor ReflectiveUtil (#22749)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 a6d9e3a4876 Refactor ReflectiveUtil (#22749)
a6d9e3a4876 is described below
commit a6d9e3a48768eca37c48fade881aba34567b8e93
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Dec 9 00:27:05 2022 +0800
Refactor ReflectiveUtil (#22749)
---
.../infra/util/reflect/ReflectiveUtil.java | 26 +++++++---------
.../infra/util/reflect/ReflectiveUtilTest.java | 23 +++-----------
.../util/reflect/fixture/ReflectiveFixture.java | 35 ++++++++++++++++++++++
3 files changed, 50 insertions(+), 34 deletions(-)
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java
index 1a6d86c2a1b..18a3747f24c 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java
@@ -23,7 +23,6 @@ import lombok.SneakyThrows;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
-import java.util.Objects;
/**
* Reflective utility.
@@ -38,23 +37,23 @@ public final class ReflectiveUtil {
* @param fieldName field name
* @return field value
*/
- @SneakyThrows(IllegalAccessException.class)
+ @SneakyThrows(ReflectiveOperationException.class)
public static Object getFieldValue(final Object target, final String fieldName) {
- Field field = getField(target.getClass(), fieldName);
- Objects.requireNonNull(field).setAccessible(true);
- return field.get(target);
+ return getField(target.getClass(), fieldName).get(target);
}
- private static Field getField(final Class<?> target, final String fieldName) {
+ private static Field getField(final Class<?> target, final String fieldName) throws NoSuchFieldException {
Class<?> clazz = target;
while (null != clazz) {
try {
- return clazz.getDeclaredField(fieldName);
+ Field result = clazz.getDeclaredField(fieldName);
+ result.setAccessible(true);
+ return result;
} catch (final NoSuchFieldException ignored) {
}
clazz = clazz.getSuperclass();
}
- return null;
+ throw new NoSuchFieldException(String.format("Can not find field name `%s` in class %s.", fieldName, target));
}
/**
@@ -64,11 +63,9 @@ public final class ReflectiveUtil {
* @param fieldName field name
* @param value value
*/
- @SneakyThrows(IllegalAccessException.class)
+ @SneakyThrows(ReflectiveOperationException.class)
public static void setField(final Object target, final String fieldName, final Object value) {
- Field field = getField(target.getClass(), fieldName);
- Objects.requireNonNull(field).setAccessible(true);
- field.set(target, value);
+ getField(target.getClass(), fieldName).set(target, value);
}
/**
@@ -78,11 +75,10 @@ public final class ReflectiveUtil {
* @param fieldName field name
* @param value value
*/
- @SneakyThrows(IllegalAccessException.class)
+ @SneakyThrows(ReflectiveOperationException.class)
public static void setStaticField(final Class<?> target, final String fieldName, final Object value) {
Field field = getField(target, fieldName);
- if (Modifier.isStatic(Objects.requireNonNull(field).getModifiers())) {
- field.setAccessible(true);
+ if (Modifier.isStatic(field.getModifiers())) {
field.set(null, value);
}
}
diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtilTest.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtilTest.java
index 32fc8dd207a..fe044171cfb 100644
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtilTest.java
+++ b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtilTest.java
@@ -17,11 +17,7 @@
package org.apache.shardingsphere.infra.util.reflect;
-import lombok.Getter;
-import lombok.Setter;
-import lombok.AllArgsConstructor;
-import lombok.NoArgsConstructor;
-import lombok.AccessLevel;
+import org.apache.shardingsphere.infra.util.reflect.fixture.ReflectiveFixture;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
@@ -30,33 +26,22 @@ import static org.hamcrest.MatcherAssert.assertThat;
public final class ReflectiveUtilTest {
@Test
- public void assertGetFieldValue() throws IllegalAccessError {
+ public void assertGetFieldValue() {
ReflectiveFixture reflectiveFixture = new ReflectiveFixture("bar");
assertThat(ReflectiveUtil.getFieldValue(reflectiveFixture, "value"), is("bar"));
}
@Test
- public void assertSetField() throws IllegalAccessError {
+ public void assertSetField() {
ReflectiveFixture reflectiveFixture = new ReflectiveFixture();
ReflectiveUtil.setField(reflectiveFixture, "value", "foo");
assertThat(ReflectiveUtil.getFieldValue(reflectiveFixture, "value"), is("foo"));
}
@Test
- public void assertSetStaticField() throws IllegalAccessError {
+ public void assertSetStaticField() {
ReflectiveFixture reflectiveFixture = new ReflectiveFixture();
ReflectiveUtil.setStaticField(reflectiveFixture.getClass(), "staticValue", "foo");
assertThat(ReflectiveUtil.getFieldValue(reflectiveFixture, "staticValue"), is("foo"));
}
-
- @AllArgsConstructor
- @NoArgsConstructor
- @Getter
- @Setter(AccessLevel.PRIVATE)
- private static final class ReflectiveFixture {
-
- private static String staticValue;
-
- private String value;
- }
}
diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/fixture/ReflectiveFixture.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/fixture/ReflectiveFixture.java
new file mode 100644
index 00000000000..060207dd71e
--- /dev/null
+++ b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/fixture/ReflectiveFixture.java
@@ -0,0 +1,35 @@
+/*
+ * 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.shardingsphere.infra.util.reflect.fixture;
+
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+@AllArgsConstructor
+@NoArgsConstructor
+@Getter
+@Setter(AccessLevel.PRIVATE)
+public final class ReflectiveFixture {
+
+ private static String staticValue;
+
+ private String value;
+}