From 1e2d663b93fe87708c7359827acd1d547f18035e Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 21 Dec 2022 00:02:06 +0800 Subject: [PATCH] Refactor ReflectionUtil (#23001) --- .../agent/core/util/ReflectionUtil.java | 6 +- .../loader/PluginConfigurationLoaderTest.java | 4 +- .../wrapper/type/CounterWrapperTest.java | 4 +- .../wrapper/type/GaugeWrapperTest.java | 4 +- .../wrapper/type/HistogramWrapperTest.java | 4 +- .../wrapper/type/SummaryWrapperTest.java | 4 +- ...bstractJDBCExecutorCallbackAdviceTest.java | 4 +- .../advice/JDBCExecutorCallbackAdvice.java | 4 +- .../advice/JDBCExecutorCallbackAdvice.java | 4 +- .../advice/JDBCExecutorCallbackAdvice.java | 4 +- .../infra/util/reflect/ReflectionUtil.java | 57 +++++++++++++++++++ .../util/reflect/ReflectiveUtilTest.java | 47 --------------- .../reflect/fixture/ReflectiveFixture.java | 35 ------------ ...tomikosTransactionManagerProviderTest.java | 6 +- ...ronixXATransactionManagerProviderTest.java | 4 +- ...ataSourceXAResourceRecoveryHelperTest.java | 4 +- ...ayanaXATransactionManagerProviderTest.java | 8 +-- .../coordinator/util/ReflectionUtil.java | 15 +---- 18 files changed, 92 insertions(+), 126 deletions(-) rename infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java => agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/ReflectionUtil.java (96%) create mode 100644 infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectionUtil.java delete mode 100644 infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtilTest.java delete mode 100644 infra/util/src/test/java/org/apache/shardingsphere/infra/util/reflect/fixture/ReflectiveFixture.java 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% rename from infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectiveUtil.java rename to agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/ReflectionUtil.java index 18a3747f24c39..53c33b909e9e6 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.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 96211da34d087..89bc6d6daa00f 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 18c10f7a327b7..1f402d8a1359c 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 void assertCreate() { 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 294475af9f0a0..3583cbc19194b 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 void assertCreate() { 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 8252c3b10e8cb..6e56358a982ef 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 void assertCreate() { 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 1dfeabace678d..65408c97c1351 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 d69868695d4b9..f3b50d23dc084 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 @@ 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.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 void prepare() { cachedDatasourceMetaData.put("mock_url", new MockDataSourceMetaData()); Map 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 49cfeb19c9a51..1e0229f65d22e 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.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 void beforeMethod(final TargetAdviceObject target, final Method method, f builder = builder.asChildOf(root); } JDBCExecutionUnit executionUnit = (JDBCExecutionUnit) args[0]; - Map storageTypes = (Map) ReflectiveUtil.getFieldValue(target, "storageTypes"); + Map storageTypes = (Map) 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 06eca6ec07e51..76ca576acab1d 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.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 void beforeMethod(final TargetAdviceObject target, final Method method, f spanBuilder.setAttribute(OpenTelemetryConstants.COMPONENT, OpenTelemetryConstants.COMPONENT_NAME); spanBuilder.setAttribute(OpenTelemetryConstants.DB_TYPE, OpenTelemetryConstants.DB_TYPE_VALUE); JDBCExecutionUnit executionUnit = (JDBCExecutionUnit) args[0]; - Map storageTypes = (Map) ReflectiveUtil.getFieldValue(target, "storageTypes"); + Map storageTypes = (Map) 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 c20e1f8cba9c6..604450535631e 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.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 void beforeMethod(final TargetAdviceObject target, final Method method, f span.tag(ZipkinConstants.Tags.COMPONENT, ZipkinConstants.COMPONENT_NAME); span.tag(ZipkinConstants.Tags.DB_TYPE, ZipkinConstants.DB_TYPE_VALUE); JDBCExecutionUnit executionUnit = (JDBCExecutionUnit) args[0]; - Map storageTypes = (Map) ReflectiveUtil.getFieldValue(target, "storageTypes"); + Map storageTypes = (Map) 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/ReflectionUtil.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectionUtil.java new file mode 100644 index 0000000000000..0d26e90c313cb --- /dev/null +++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectionUtil.java @@ -0,0 +1,57 @@ +/* + * 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 lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; + +import java.lang.reflect.Field; + +/** + * Reflection utility. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ReflectionUtil { + + /** + * 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); + } + + private static Field getField(final Class target, final String fieldName) throws NoSuchFieldException { + Class clazz = target; + while (null != clazz) { + try { + Field result = clazz.getDeclaredField(fieldName); + result.setAccessible(true); + return result; + } catch (final NoSuchFieldException ignored) { + } + clazz = clazz.getSuperclass(); + } + throw new NoSuchFieldException(String.format("Can not find field name `%s` in class %s.", fieldName, target)); + } +} 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 fe044171cfbd5..0000000000000 --- 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 060207dd71e54..0000000000000 --- 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 7e0f6cfbe5aee..a6b414f929ce6 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 @@ 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 6888356a791b2..9b96188f6af0a 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 @@ 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 5e253cffc697a..1118427dbc130 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 void assertGetXAResourcesCreatingConnecting() throws SQLException { @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 5f488f510e120..48f52bc1aae74 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 @@ 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 e1ef095685033..1f576de8a224e 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 getFieldValue(final Object target, final String fieldName, final Class 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 static T getFieldValue(final Object target, final String fieldName, f 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; }