You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/12/21 08:10:14 UTC
[shardingsphere] branch master updated: Remove useless infra-util and cluster's ReflectionUtil (#23012)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 4bd333bdc25 Remove useless infra-util and cluster's ReflectionUtil (#23012)
4bd333bdc25 is described below
commit 4bd333bdc2541b69644a64a1800f30dc0b7f941d
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Dec 21 16:10:06 2022 +0800
Remove useless infra-util and cluster's ReflectionUtil (#23012)
* Remove useless infra-util's ReflectionUtil
* Remove useless cluster's ReflectionUtil
---
.../infra/util/reflect/ReflectionUtil.java | 57 ---------------------
.../AtomikosTransactionManagerProviderTest.java | 8 +--
.../BitronixXATransactionManagerProviderTest.java | 6 +--
.../DataSourceXAResourceRecoveryHelperTest.java | 6 +--
.../NarayanaXATransactionManagerProviderTest.java | 10 ++--
.../ProcessListChangedSubscriberTest.java | 10 ++--
.../cluster/coordinator/util/ReflectionUtil.java | 59 ----------------------
7 files changed, 20 insertions(+), 136 deletions(-)
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
deleted file mode 100644
index 0d26e90c313..00000000000
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/reflect/ReflectionUtil.java
+++ /dev/null
@@ -1,57 +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 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/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 a6b414f929c..1f3ad716799 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,12 +19,12 @@ 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.ReflectionUtil;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.internal.util.reflection.InstanceField;
import org.mockito.junit.MockitoJUnitRunner;
import javax.sql.XADataSource;
@@ -57,9 +57,9 @@ public final class AtomikosTransactionManagerProviderTest {
private XADataSource xaDataSource;
@Before
- public void setUp() {
- ReflectionUtil.setField(transactionManagerProvider, "transactionManager", userTransactionManager);
- ReflectionUtil.setField(transactionManagerProvider, "userTransactionService", userTransactionService);
+ public void setUp() throws NoSuchFieldException {
+ new InstanceField(AtomikosTransactionManagerProvider.class.getDeclaredField("transactionManager"), transactionManagerProvider).set(userTransactionManager);
+ new InstanceField(AtomikosTransactionManagerProvider.class.getDeclaredField("userTransactionService"), transactionManagerProvider).set(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 9b96188f6af..9561a0d11ec 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,12 +19,12 @@ package org.apache.shardingsphere.transaction.xa.bitronix.manager;
import bitronix.tm.BitronixTransactionManager;
import bitronix.tm.resource.ResourceRegistrar;
-import org.apache.shardingsphere.infra.util.reflect.ReflectionUtil;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.internal.util.reflection.InstanceField;
import org.mockito.junit.MockitoJUnitRunner;
import javax.sql.XADataSource;
@@ -52,8 +52,8 @@ public final class BitronixXATransactionManagerProviderTest {
private XADataSource xaDataSource;
@Before
- public void setUp() {
- ReflectionUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
+ public void setUp() throws NoSuchFieldException {
+ new InstanceField(BitronixXATransactionManagerProvider.class.getDeclaredField("transactionManager"), transactionManagerProvider).set(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 1118427dbc1..d57747f0246 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,11 +17,11 @@
package org.apache.shardingsphere.transaction.xa.narayana.manager;
-import org.apache.shardingsphere.infra.util.reflect.ReflectionUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.internal.util.reflection.InstanceField;
import org.mockito.junit.MockitoJUnitRunner;
import javax.sql.XAConnection;
@@ -69,8 +69,8 @@ public final class DataSourceXAResourceRecoveryHelperTest {
}
@Test
- public void assertGetXAResourcesWithoutConnecting() throws SQLException {
- ReflectionUtil.setField(recoveryHelper, "delegate", xaResource);
+ public void assertGetXAResourcesWithoutConnecting() throws SQLException, NoSuchFieldException {
+ new InstanceField(DataSourceXAResourceRecoveryHelper.class.getDeclaredField("delegate"), recoveryHelper).set(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 48f52bc1aae..a45cd7aff68 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,12 +19,12 @@ 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.ReflectionUtil;
import org.apache.shardingsphere.transaction.xa.spi.SingleXAResource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.internal.util.reflection.InstanceField;
import org.mockito.junit.MockitoJUnitRunner;
import javax.sql.XADataSource;
@@ -58,10 +58,10 @@ public final class NarayanaXATransactionManagerProviderTest {
private XADataSource xaDataSource;
@Before
- public void setUp() {
- ReflectionUtil.setField(transactionManagerProvider, "xaRecoveryModule", xaRecoveryModule);
- ReflectionUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
- ReflectionUtil.setField(transactionManagerProvider, "recoveryManagerService", recoveryManagerService);
+ public void setUp() throws NoSuchFieldException {
+ new InstanceField(NarayanaXATransactionManagerProvider.class.getDeclaredField("xaRecoveryModule"), transactionManagerProvider).set(xaRecoveryModule);
+ new InstanceField(NarayanaXATransactionManagerProvider.class.getDeclaredField("transactionManager"), transactionManagerProvider).set(transactionManager);
+ new InstanceField(NarayanaXATransactionManagerProvider.class.getDeclaredField("recoveryManagerService"), transactionManagerProvider).set(recoveryManagerService);
}
@Test
diff --git a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ProcessListChangedSubscriberTest.java b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ProcessListChangedSubscriberTest.java
index d6876b4cc60..b04f5f699b6 100644
--- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ProcessListChangedSubscriberTest.java
+++ b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/subscriber/ProcessListChangedSubscriberTest.java
@@ -35,7 +35,6 @@ import org.apache.shardingsphere.mode.manager.cluster.coordinator.RegistryCenter
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.KillProcessListIdEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ShowProcessListTriggerEvent;
import org.apache.shardingsphere.mode.manager.cluster.coordinator.registry.status.compute.event.ShowProcessListUnitCompleteEvent;
-import org.apache.shardingsphere.mode.manager.cluster.coordinator.util.ReflectionUtil;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.process.ShowProcessListManager;
import org.apache.shardingsphere.mode.process.lock.ShowProcessListSimpleLock;
@@ -46,6 +45,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
+import org.mockito.internal.util.reflection.FieldReader;
import org.mockito.junit.MockitoJUnitRunner;
import java.sql.SQLException;
@@ -122,23 +122,23 @@ public final class ProcessListChangedSubscriberTest {
}
@Test
- public void assertTriggerShowProcessList() throws NoSuchFieldException, IllegalAccessException {
+ public void assertTriggerShowProcessList() throws NoSuchFieldException {
String instanceId = contextManager.getInstanceContext().getInstance().getMetaData().getId();
ShowProcessListManager.getInstance().putProcessContext("foo_execution_id", mock(ExecuteProcessContext.class));
String processListId = "foo_process_id";
subscriber.triggerShowProcessList(new ShowProcessListTriggerEvent(instanceId, processListId));
- ClusterPersistRepository repository = ReflectionUtil.getFieldValue(subscriber, "registryCenter", RegistryCenter.class).getRepository();
+ ClusterPersistRepository repository = ((RegistryCenter) new FieldReader(subscriber, ProcessListChangedSubscriber.class.getDeclaredField("registryCenter")).read()).getRepository();
verify(repository).persist("/execution_nodes/foo_process_id/" + instanceId,
"contexts:" + System.lineSeparator() + "- startTimeMillis: 0" + System.lineSeparator());
verify(repository).delete("/nodes/compute_nodes/process_trigger/" + instanceId + ":foo_process_id");
}
@Test
- public void assertKillProcessListId() throws SQLException, NoSuchFieldException, IllegalAccessException {
+ public void assertKillProcessListId() throws SQLException, NoSuchFieldException {
String instanceId = contextManager.getInstanceContext().getInstance().getMetaData().getId();
String processId = "foo_process_id";
subscriber.killProcessListId(new KillProcessListIdEvent(instanceId, processId));
- ClusterPersistRepository repository = ReflectionUtil.getFieldValue(subscriber, "registryCenter", RegistryCenter.class).getRepository();
+ ClusterPersistRepository repository = ((RegistryCenter) new FieldReader(subscriber, ProcessListChangedSubscriber.class.getDeclaredField("registryCenter")).read()).getRepository();
verify(repository).delete("/nodes/compute_nodes/process_kill/" + instanceId + ":foo_process_id");
}
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
deleted file mode 100644
index 1f576de8a22..00000000000
--- a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/util/ReflectionUtil.java
+++ /dev/null
@@ -1,59 +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.mode.manager.cluster.coordinator.util;
-
-import com.google.common.base.Preconditions;
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-
-import java.lang.reflect.Field;
-
-/**
- * Reflection utils.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ReflectionUtil {
-
- /**
- * Get field value from instance target object.
- *
- * @param target target object
- * @param fieldName field name
- * @param valueClass expected value class
- * @param <T> expected value class
- * @return target filed value
- * @throws NoSuchFieldException no such field exception
- * @throws IllegalAccessException illegal access exception
- */
- @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);
- Object value = field.get(target);
- Preconditions.checkNotNull(value);
- if (valueClass.isAssignableFrom(value.getClass())) {
- return (T) value;
- }
- throw new ClassCastException("field " + fieldName + " is " + value.getClass().getName() + " can cast to " + valueClass.getName());
- }
-
- private static Field getField(final Class<?> targetClass, final String fieldName) throws NoSuchFieldException {
- Field result = targetClass.getDeclaredField(fieldName);
- result.setAccessible(true);
- return result;
- }
-}