Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ReflectionUtil #23001

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -87,7 +87,7 @@ public void prepare() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -56,7 +56,7 @@ public void beforeMethod(final TargetAdviceObject target, final Method method, f
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,7 +53,7 @@ public final class BitronixXATransactionManagerProviderTest {

@Before
public void setUp() {
ReflectiveUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
ReflectionUtil.setField(transactionManagerProvider, "transactionManager", transactionManager);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Loading