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/20 16:02:13 UTC
[shardingsphere] branch master updated: Refactor ReflectionUtil (#23001)
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 1e2d663b93f Refactor ReflectionUtil (#23001)
1e2d663b93f is described below
commit 1e2d663b93fe87708c7359827acd1d547f18035e
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Dec 21 00:02:06 2022 +0800
Refactor ReflectionUtil (#23001)
---
.../agent/core/util/ReflectionUtil.java | 6 +--
.../loader/PluginConfigurationLoaderTest.java | 4 +-
.../wrapper/type/CounterWrapperTest.java | 4 +-
.../prometheus/wrapper/type/GaugeWrapperTest.java | 4 +-
.../wrapper/type/HistogramWrapperTest.java | 4 +-
.../wrapper/type/SummaryWrapperTest.java | 4 +-
.../AbstractJDBCExecutorCallbackAdviceTest.java | 4 +-
.../jaeger/advice/JDBCExecutorCallbackAdvice.java | 4 +-
.../advice/JDBCExecutorCallbackAdvice.java | 4 +-
.../zipkin/advice/JDBCExecutorCallbackAdvice.java | 4 +-
.../{ReflectiveUtil.java => ReflectionUtil.java} | 42 ++++---------------
.../infra/util/reflect/ReflectiveUtilTest.java | 47 ----------------------
.../util/reflect/fixture/ReflectiveFixture.java | 35 ----------------
.../AtomikosTransactionManagerProviderTest.java | 6 +--
.../BitronixXATransactionManagerProviderTest.java | 4 +-
.../DataSourceXAResourceRecoveryHelperTest.java | 4 +-
.../NarayanaXATransactionManagerProviderTest.java | 8 ++--
.../cluster/coordinator/util/ReflectionUtil.java | 15 ++-----
18 files changed, 42 insertions(+), 161 deletions(-)
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/ReflectionUtil.java
similarity index 96%
copy from infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java
copy to agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/ReflectionUtil.java
index 18a3747f24c..53c33b909e9 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/ReflectionUtil.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.util.reflect;
+package org.apache.shardingsphere.agent.core.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@@ -25,10 +25,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
- * Reflective utility.
+ * Reflection utility.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ReflectiveUtil {
+public final class ReflectionUtil {
/**
* Get field value.
diff --git a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/PluginConfigurationLoaderTest.java b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/PluginConfigurationLoaderTest.java
index 96211da34d0..89bc6d6daa0 100644
--- a/agent/core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/PluginConfigurationLoaderTest.java
+++ b/agent/core/src/test/java/org/apache/shardingsphere/agent/core/config/loader/PluginConfigurationLoaderTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.agent.core.config.loader;
import org.apache.shardingsphere.agent.core.path.AgentPathBuilder;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import org.junit.Test;
import java.io.File;
@@ -35,7 +35,7 @@ public final class PluginConfigurationLoaderTest {
@Test
public void assertLoad() throws IOException {
- ReflectiveUtil.setStaticField(AgentPathBuilder.class, "agentPath", new File(getResourceUrl()));
+ ReflectionUtil.setStaticField(AgentPathBuilder.class, "agentPath", new File(getResourceUrl()));
assertNotNull(PluginConfigurationLoader.load());
}
diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java
index 18c10f7a327..1f402d8a135 100644
--- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java
+++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/CounterWrapperTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type;
import io.prometheus.client.Counter;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
@@ -32,7 +32,7 @@ public final class CounterWrapperTest {
CounterWrapper counterWrapper = new CounterWrapper(counter);
counterWrapper.inc();
counterWrapper.inc(1);
- counter = (Counter) ReflectiveUtil.getFieldValue(counterWrapper, "counter");
+ counter = (Counter) ReflectionUtil.getFieldValue(counterWrapper, "counter");
assertThat(counter.get(), is(2.0));
}
}
diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java
index 294475af9f0..3583cbc1919 100644
--- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java
+++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/GaugeWrapperTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type;
import io.prometheus.client.Gauge;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
@@ -32,7 +32,7 @@ public final class GaugeWrapperTest {
GaugeWrapper gaugeWrapper = new GaugeWrapper(gauge);
gaugeWrapper.inc();
gaugeWrapper.inc(1);
- gauge = (Gauge) ReflectiveUtil.getFieldValue(gaugeWrapper, "gauge");
+ gauge = (Gauge) ReflectionUtil.getFieldValue(gaugeWrapper, "gauge");
assertThat(gauge.get(), is(2.0));
}
}
diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java
index 8252c3b10e8..6e56358a982 100644
--- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java
+++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/HistogramWrapperTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type;
import io.prometheus.client.Histogram;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
@@ -31,7 +31,7 @@ public final class HistogramWrapperTest {
Histogram histogram = Histogram.build().name("a").help("help").create();
HistogramWrapper histogramWrapper = new HistogramWrapper(histogram);
histogramWrapper.observe(1);
- histogram = (Histogram) ReflectiveUtil.getFieldValue(histogramWrapper, "histogram");
+ histogram = (Histogram) ReflectionUtil.getFieldValue(histogramWrapper, "histogram");
assertThat(histogram.collect().size(), is(1));
}
}
diff --git a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java
index 1dfeabace67..65408c97c13 100644
--- a/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java
+++ b/agent/plugins/metrics/type/prometheus/src/test/java/org/apache/shardingsphere/agent/metrics/prometheus/wrapper/type/SummaryWrapperTest.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.agent.metrics.prometheus.wrapper.type;
import io.prometheus.client.Summary;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
@@ -30,7 +30,7 @@ public final class SummaryWrapperTest {
public void assertCreate() {
SummaryWrapper summaryWrapper = new SummaryWrapper(Summary.build().name("a").help("help").create());
summaryWrapper.observe(1);
- Summary summary = (Summary) ReflectiveUtil.getFieldValue(summaryWrapper, "summary");
+ Summary summary = (Summary) ReflectionUtil.getFieldValue(summaryWrapper, "summary");
assertThat(summary.collect().size(), is(1));
}
}
diff --git a/agent/plugins/tracing/test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractJDBCExecutorCallbackAdviceTest.java b/agent/plugins/tracing/test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractJDBCExecutorCallbackAdviceTest.java
index d69868695d4..f3b50d23dc0 100644
--- a/agent/plugins/tracing/test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractJDBCExecutorCallbackAdviceTest.java
+++ b/agent/plugins/tracing/test/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/advice/AbstractJDBCExecutorCallbackAdviceTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.agent.plugin.tracing.advice;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.agent.advice.TargetAdviceObject;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import org.apache.shardingsphere.agent.plugin.tracing.AgentRunner;
import org.apache.shardingsphere.agent.plugin.tracing.MockDataSourceMetaData;
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
@@ -29,7 +30,6 @@ import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.context.SQLUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
import org.junit.runner.RunWith;
import org.mockito.internal.util.reflection.FieldReader;
@@ -87,7 +87,7 @@ public abstract class AbstractJDBCExecutorCallbackAdviceTest implements AdviceTe
cachedDatasourceMetaData.put("mock_url", new MockDataSourceMetaData());
Map<String, DatabaseType> storageTypes = new LinkedHashMap<>(1, 1);
storageTypes.put("mock.db", new MySQLDatabaseType());
- ReflectiveUtil.setField(mock, "storageTypes", storageTypes);
+ ReflectionUtil.setField(mock, "storageTypes", storageTypes);
targetObject = (TargetAdviceObject) mock;
}
}
diff --git a/agent/plugins/tracing/type/jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/advice/JDBCExecutorCallbackAdvice.java b/agent/plugins/tracing/type/jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/advice/JDBCExecutorCallbackAdvice.java
index 49cfeb19c9a..1e0229f65d2 100644
--- a/agent/plugins/tracing/type/jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/advice/JDBCExecutorCallbackAdvice.java
+++ b/agent/plugins/tracing/type/jaeger/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/jaeger/advice/JDBCExecutorCallbackAdvice.java
@@ -32,7 +32,7 @@ import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import java.lang.reflect.Method;
import java.sql.DatabaseMetaData;
@@ -56,7 +56,7 @@ public final class JDBCExecutorCallbackAdvice implements InstanceMethodAdvice {
builder = builder.asChildOf(root);
}
JDBCExecutionUnit executionUnit = (JDBCExecutionUnit) args[0];
- Map<String, DatabaseType> storageTypes = (Map<String, DatabaseType>) ReflectiveUtil.getFieldValue(target, "storageTypes");
+ Map<String, DatabaseType> storageTypes = (Map<String, DatabaseType>) ReflectionUtil.getFieldValue(target, "storageTypes");
Method getMetaDataMethod = JDBCExecutorCallback.class.getDeclaredMethod("getDataSourceMetaData", DatabaseMetaData.class, DatabaseType.class);
getMetaDataMethod.setAccessible(true);
DataSourceMetaData metaData = (DataSourceMetaData) getMetaDataMethod.invoke(target,
diff --git a/agent/plugins/tracing/type/opentelemetry/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/JDBCExecutorCallbackAdvice.java b/agent/plugins/tracing/type/opentelemetry/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/JDBCExecutorCallbackAdvice.java
index 06eca6ec07e..76ca576acab 100644
--- a/agent/plugins/tracing/type/opentelemetry/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/JDBCExecutorCallbackAdvice.java
+++ b/agent/plugins/tracing/type/opentelemetry/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/opentelemetry/advice/JDBCExecutorCallbackAdvice.java
@@ -32,7 +32,7 @@ import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import java.lang.reflect.Method;
import java.sql.DatabaseMetaData;
@@ -58,7 +58,7 @@ public class JDBCExecutorCallbackAdvice implements InstanceMethodAdvice {
spanBuilder.setAttribute(OpenTelemetryConstants.COMPONENT, OpenTelemetryConstants.COMPONENT_NAME);
spanBuilder.setAttribute(OpenTelemetryConstants.DB_TYPE, OpenTelemetryConstants.DB_TYPE_VALUE);
JDBCExecutionUnit executionUnit = (JDBCExecutionUnit) args[0];
- Map<String, DatabaseType> storageTypes = (Map<String, DatabaseType>) ReflectiveUtil.getFieldValue(target, "storageTypes");
+ Map<String, DatabaseType> storageTypes = (Map<String, DatabaseType>) ReflectionUtil.getFieldValue(target, "storageTypes");
Method getMetaDataMethod = JDBCExecutorCallback.class.getDeclaredMethod("getDataSourceMetaData", DatabaseMetaData.class, DatabaseType.class);
getMetaDataMethod.setAccessible(true);
DataSourceMetaData metaData = (DataSourceMetaData) getMetaDataMethod.invoke(target,
diff --git a/agent/plugins/tracing/type/zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/advice/JDBCExecutorCallbackAdvice.java b/agent/plugins/tracing/type/zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/advice/JDBCExecutorCallbackAdvice.java
index c20e1f8cba9..60445053563 100644
--- a/agent/plugins/tracing/type/zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/advice/JDBCExecutorCallbackAdvice.java
+++ b/agent/plugins/tracing/type/zipkin/src/main/java/org/apache/shardingsphere/agent/plugin/tracing/zipkin/advice/JDBCExecutorCallbackAdvice.java
@@ -28,7 +28,7 @@ import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.agent.core.util.ReflectionUtil;
import java.lang.reflect.Method;
import java.sql.DatabaseMetaData;
@@ -51,7 +51,7 @@ public final class JDBCExecutorCallbackAdvice implements InstanceMethodAdvice {
span.tag(ZipkinConstants.Tags.COMPONENT, ZipkinConstants.COMPONENT_NAME);
span.tag(ZipkinConstants.Tags.DB_TYPE, ZipkinConstants.DB_TYPE_VALUE);
JDBCExecutionUnit executionUnit = (JDBCExecutionUnit) args[0];
- Map<String, DatabaseType> storageTypes = (Map<String, DatabaseType>) ReflectiveUtil.getFieldValue(target, "storageTypes");
+ Map<String, DatabaseType> storageTypes = (Map<String, DatabaseType>) ReflectionUtil.getFieldValue(target, "storageTypes");
Method getMetaDataMethod = JDBCExecutorCallback.class.getDeclaredMethod("getDataSourceMetaData", DatabaseMetaData.class, DatabaseType.class);
getMetaDataMethod.setAccessible(true);
DataSourceMetaData metaData = (DataSourceMetaData) getMetaDataMethod.invoke(target,
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/ReflectionUtil.java
similarity index 68%
rename from infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java
rename to infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectionUtil.java
index 18a3747f24c..0d26e90c313 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/ReflectionUtil.java
@@ -22,24 +22,23 @@ import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
/**
- * Reflective utility.
+ * Reflection utility.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ReflectiveUtil {
+public final class ReflectionUtil {
/**
- * Get field value.
- *
+ * Set value to specified field.
+ *
* @param target target
* @param fieldName field name
- * @return field value
+ * @param value value
*/
@SneakyThrows(ReflectiveOperationException.class)
- public static Object getFieldValue(final Object target, final String fieldName) {
- return getField(target.getClass(), fieldName).get(target);
+ public static void setField(final Object target, final String fieldName, final Object value) {
+ getField(target.getClass(), fieldName).set(target, value);
}
private static Field getField(final Class<?> target, final String fieldName) throws NoSuchFieldException {
@@ -55,31 +54,4 @@ public final class ReflectiveUtil {
}
throw new NoSuchFieldException(String.format("Can not find field name `%s` in class %s.", fieldName, target));
}
-
- /**
- * Set value to specified field.
- *
- * @param target target
- * @param fieldName field name
- * @param value value
- */
- @SneakyThrows(ReflectiveOperationException.class)
- public static void setField(final Object target, final String fieldName, final Object value) {
- getField(target.getClass(), fieldName).set(target, value);
- }
-
- /**
- * Set value to specified static field.
- *
- * @param target target
- * @param fieldName field name
- * @param value value
- */
- @SneakyThrows(ReflectiveOperationException.class)
- public static void setStaticField(final Class<?> target, final String fieldName, final Object value) {
- Field field = getField(target, fieldName);
- 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
deleted file mode 100644
index fe044171cfb..00000000000
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtilTest.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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;
-
-import org.apache.shardingsphere.infra.util.reflect.fixture.ReflectiveFixture;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-public final class ReflectiveUtilTest {
-
- @Test
- public void assertGetFieldValue() {
- ReflectiveFixture reflectiveFixture = new ReflectiveFixture("bar");
- assertThat(ReflectiveUtil.getFieldValue(reflectiveFixture, "value"), is("bar"));
- }
-
- @Test
- public void assertSetField() {
- ReflectiveFixture reflectiveFixture = new ReflectiveFixture();
- ReflectiveUtil.setField(reflectiveFixture, "value", "foo");
- assertThat(ReflectiveUtil.getFieldValue(reflectiveFixture, "value"), is("foo"));
- }
-
- @Test
- public void assertSetStaticField() {
- ReflectiveFixture reflectiveFixture = new ReflectiveFixture();
- ReflectiveUtil.setStaticField(reflectiveFixture.getClass(), "staticValue", "foo");
- assertThat(ReflectiveUtil.getFieldValue(reflectiveFixture, "staticValue"), is("foo"));
- }
-}
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
deleted file mode 100644
index 060207dd71e..00000000000
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/fixture/ReflectiveFixture.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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;
-}
diff --git a/kernel/transaction/type/xa/provider/atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerProviderTest.java b/kernel/transaction/type/xa/provider/atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerProviderTest.java
index 7e0f6cfbe5a..a6b414f929c 100644
--- a/kernel/transaction/type/xa/provider/atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerProviderTest.java
+++ b/kernel/transaction/type/xa/provider/atomikos/src/test/java/org/apache/shardingsphere/transaction/xa/atomikos/manager/AtomikosTransactionManagerProviderTest.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.transaction.xa.atomikos.manager;
import com.atomikos.icatch.config.UserTransactionService;
import com.atomikos.icatch.jta.UserTransactionManager;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.infra.util.reflect.ReflectionUtil;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.junit.Before;
import org.junit.Test;
@@ -58,8 +58,8 @@ public final class AtomikosTransactionManagerProviderTest {
@Before
public void setUp() {
- ReflectiveUtil.setField(transactionManagerProvider, "transactionManager", userTransactionManager);
- ReflectiveUtil.setField(transactionManagerProvider, "userTransactionService", userTransactionService);
+ ReflectionUtil.setField(transactionManagerProvider, "transactionManager", userTransactionManager);
+ ReflectionUtil.setField(transactionManagerProvider, "userTransactionService", userTransactionService);
}
@Test
diff --git a/kernel/transaction/type/xa/provider/bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerProviderTest.java b/kernel/transaction/type/xa/provider/bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerProviderTest.java
index 6888356a791..9b96188f6af 100644
--- a/kernel/transaction/type/xa/provider/bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerProviderTest.java
+++ b/kernel/transaction/type/xa/provider/bitronix/src/test/java/org/apache/shardingsphere/transaction/xa/bitronix/manager/BitronixXATransactionManagerProviderTest.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.transaction.xa.bitronix.manager;
import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.resource.ResourceRegistrar;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.infra.util.reflect.ReflectionUtil;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.junit.Before;
import org.junit.Test;
@@ -53,7 +53,7 @@ public final class BitronixXATransactionManagerProviderTest {
@Before
public void setUp() {
- ReflectiveUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
+ ReflectionUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
}
@Test
diff --git a/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/DataSourceXAResourceRecoveryHelperTest.java b/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/DataSourceXAResourceRecoveryHelperTest.java
index 5e253cffc69..1118427dbc1 100644
--- a/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/DataSourceXAResourceRecoveryHelperTest.java
+++ b/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/DataSourceXAResourceRecoveryHelperTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.transaction.xa.narayana.manager;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.infra.util.reflect.ReflectionUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -70,7 +70,7 @@ public final class DataSourceXAResourceRecoveryHelperTest {
@Test
public void assertGetXAResourcesWithoutConnecting() throws SQLException {
- ReflectiveUtil.setField(recoveryHelper, "delegate", xaResource);
+ ReflectionUtil.setField(recoveryHelper, "delegate", xaResource);
recoveryHelper.getXAResources();
XAResource[] xaResources = recoveryHelper.getXAResources();
assertThat(xaResources.length, is(1));
diff --git a/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerProviderTest.java b/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerProviderTest.java
index 5f488f510e1..48f52bc1aae 100644
--- a/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerProviderTest.java
+++ b/kernel/transaction/type/xa/provider/narayana/src/test/java/org/apache/shardingsphere/transaction/xa/narayana/manager/NarayanaXATransactionManagerProviderTest.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.transaction.xa.narayana.manager;
import com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule;
import com.arjuna.ats.jbossatx.jta.RecoveryManagerService;
-import org.apache.shardingsphere.infra.util.reflect.ReflectiveUtil;
+import org.apache.shardingsphere.infra.util.reflect.ReflectionUtil;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.junit.Before;
import org.junit.Test;
@@ -59,9 +59,9 @@ public final class NarayanaXATransactionManagerProviderTest {
@Before
public void setUp() {
- ReflectiveUtil.setField(transactionManagerProvider, "xaRecoveryModule", xaRecoveryModule);
- ReflectiveUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
- ReflectiveUtil.setField(transactionManagerProvider, "recoveryManagerService", recoveryManagerService);
+ ReflectionUtil.setField(transactionManagerProvider, "xaRecoveryModule", xaRecoveryModule);
+ ReflectionUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
+ ReflectionUtil.setField(transactionManagerProvider, "recoveryManagerService", recoveryManagerService);
}
@Test
diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/util/ReflectionUtil.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/util/ReflectionUtil.java
index e1ef0956850..1f576de8a22 100644
--- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/util/ReflectionUtil.java
+++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/util/ReflectionUtil.java
@@ -42,7 +42,7 @@ public final class ReflectionUtil {
*/
@SuppressWarnings("unchecked")
public static <T> T getFieldValue(final Object target, final String fieldName, final Class<T> valueClass) throws NoSuchFieldException, IllegalAccessException {
- Field field = getField(target.getClass(), fieldName, true);
+ Field field = getField(target.getClass(), fieldName);
Object value = field.get(target);
Preconditions.checkNotNull(value);
if (valueClass.isAssignableFrom(value.getClass())) {
@@ -51,17 +51,8 @@ public final class ReflectionUtil {
throw new ClassCastException("field " + fieldName + " is " + value.getClass().getName() + " can cast to " + valueClass.getName());
}
- /**
- * Get field from class.
- *
- * @param targetClass target class
- * @param fieldName field name
- * @param isDeclared is declared
- * @return {@link Field}
- * @throws NoSuchFieldException no such field exception
- */
- public static Field getField(final Class<?> targetClass, final String fieldName, final boolean isDeclared) throws NoSuchFieldException {
- Field result = isDeclared ? targetClass.getDeclaredField(fieldName) : targetClass.getField(fieldName);
+ private static Field getField(final Class<?> targetClass, final String fieldName) throws NoSuchFieldException {
+ Field result = targetClass.getDeclaredField(fieldName);
result.setAccessible(true);
return result;
}