Loading sharding-core/src/main/java/org/apache/shardingsphere/core/util/ReflectiveUtil.javadeleted 100644 → 0 +0 −53 Original line number Diff line number Diff line /* * 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.core.util; import java.lang.reflect.Method; /** * Reflective util. * * @author zhaojun */ public class ReflectiveUtil { /** * Find method using reflect. * * @param target target object * @param methodName method name * @param parameterTypes parameter types * @return method * @throws NoSuchMethodException No such method exception */ @SuppressWarnings("unchecked") public static Method findMethod(final Object target, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException { Method result; Class clazz = target.getClass(); while (null != clazz) { try { result = clazz.getDeclaredMethod(methodName, parameterTypes); result.setAccessible(true); return result; } catch (NoSuchMethodException ignored) { } clazz = clazz.getSuperclass(); } throw new NoSuchMethodException(String.format("Cannot find method '%s' in %s", methodName, target.getClass().getName())); } } sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractDataSourceAdapter.java +4 −2 Original line number Diff line number Diff line Loading @@ -21,12 +21,12 @@ import com.google.common.base.Preconditions; import lombok.Getter; import lombok.Setter; import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource; import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine; import javax.sql.DataSource; import java.io.PrintWriter; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; import java.util.Collection; Loading Loading @@ -91,7 +91,9 @@ public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOpera public void close() throws Exception { for (DataSource each : dataSourceMap.values()) { try { ReflectiveUtil.findMethod(each, "close").invoke(each); Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } Loading sharding-proxy-postgresql/src/main/java/org/apache/shardingsphere/shardingproxypg/backend/communication/jdbc/datasource/JDBCBackendDataSource.java +4 −2 Original line number Diff line number Diff line Loading @@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.core.constant.ConnectionMode; import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.shardingproxypg.backend.BackendDataSource; import org.apache.shardingsphere.shardingproxypg.config.yaml.YamlDataSourceParameter; import org.apache.shardingsphere.shardingproxypg.runtime.GlobalRegistry; Loading @@ -31,6 +30,7 @@ import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager; import javax.sql.DataSource; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; Loading Loading @@ -160,7 +160,9 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose private void closeDataSource(final Map<String, DataSource> dataSourceMap) { for (DataSource each : dataSourceMap.values()) { try { ReflectiveUtil.findMethod(each, "close").invoke(each); Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } Loading sharding-proxy/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/datasource/JDBCBackendDataSource.java +4 −2 Original line number Diff line number Diff line Loading @@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.core.constant.ConnectionMode; import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.shardingproxy.backend.BackendDataSource; import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter; import org.apache.shardingsphere.shardingproxy.runtime.GlobalRegistry; Loading @@ -31,6 +30,7 @@ import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager; import javax.sql.DataSource; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; Loading Loading @@ -160,7 +160,9 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose private void closeDataSource(final Map<String, DataSource> dataSourceMap) { for (DataSource each : dataSourceMap.values()) { try { ReflectiveUtil.findMethod(each, "close").invoke(each); Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } Loading sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/connection/dialect/MySQLXAConnectionWrapper.java +3 −3 Original line number Diff line number Diff line Loading @@ -19,7 +19,6 @@ package org.apache.shardingsphere.transaction.xa.jta.connection.dialect; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper; import javax.sql.XAConnection; Loading @@ -39,7 +38,8 @@ public final class MySQLXAConnectionWrapper implements XAConnectionWrapper { @Override public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) { Connection physicalConnection = (Connection) connection.unwrap(Class.forName("com.mysql.jdbc.Connection")); Method wrapConnectionMethod = ReflectiveUtil.findMethod(xaDataSource, "wrapConnection", Connection.class); return (XAConnection) wrapConnectionMethod.invoke(xaDataSource, physicalConnection); Method method = xaDataSource.getClass().getDeclaredMethod("wrapConnection", Connection.class); method.setAccessible(true); return (XAConnection) method.invoke(xaDataSource, physicalConnection); } } Loading
sharding-core/src/main/java/org/apache/shardingsphere/core/util/ReflectiveUtil.javadeleted 100644 → 0 +0 −53 Original line number Diff line number Diff line /* * 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.core.util; import java.lang.reflect.Method; /** * Reflective util. * * @author zhaojun */ public class ReflectiveUtil { /** * Find method using reflect. * * @param target target object * @param methodName method name * @param parameterTypes parameter types * @return method * @throws NoSuchMethodException No such method exception */ @SuppressWarnings("unchecked") public static Method findMethod(final Object target, final String methodName, final Class<?>... parameterTypes) throws NoSuchMethodException { Method result; Class clazz = target.getClass(); while (null != clazz) { try { result = clazz.getDeclaredMethod(methodName, parameterTypes); result.setAccessible(true); return result; } catch (NoSuchMethodException ignored) { } clazz = clazz.getSuperclass(); } throw new NoSuchMethodException(String.format("Cannot find method '%s' in %s", methodName, target.getClass().getName())); } }
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractDataSourceAdapter.java +4 −2 Original line number Diff line number Diff line Loading @@ -21,12 +21,12 @@ import com.google.common.base.Preconditions; import lombok.Getter; import lombok.Setter; import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource; import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine; import javax.sql.DataSource; import java.io.PrintWriter; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; import java.util.Collection; Loading Loading @@ -91,7 +91,9 @@ public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOpera public void close() throws Exception { for (DataSource each : dataSourceMap.values()) { try { ReflectiveUtil.findMethod(each, "close").invoke(each); Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } Loading
sharding-proxy-postgresql/src/main/java/org/apache/shardingsphere/shardingproxypg/backend/communication/jdbc/datasource/JDBCBackendDataSource.java +4 −2 Original line number Diff line number Diff line Loading @@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.core.constant.ConnectionMode; import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.shardingproxypg.backend.BackendDataSource; import org.apache.shardingsphere.shardingproxypg.config.yaml.YamlDataSourceParameter; import org.apache.shardingsphere.shardingproxypg.runtime.GlobalRegistry; Loading @@ -31,6 +30,7 @@ import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager; import javax.sql.DataSource; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; Loading Loading @@ -160,7 +160,9 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose private void closeDataSource(final Map<String, DataSource> dataSourceMap) { for (DataSource each : dataSourceMap.values()) { try { ReflectiveUtil.findMethod(each, "close").invoke(each); Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } Loading
sharding-proxy/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/datasource/JDBCBackendDataSource.java +4 −2 Original line number Diff line number Diff line Loading @@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.core.constant.ConnectionMode; import org.apache.shardingsphere.core.constant.DatabaseType; import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.shardingproxy.backend.BackendDataSource; import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter; import org.apache.shardingsphere.shardingproxy.runtime.GlobalRegistry; Loading @@ -31,6 +30,7 @@ import org.apache.shardingsphere.transaction.core.TransactionType; import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager; import javax.sql.DataSource; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; import java.util.ArrayList; Loading Loading @@ -160,7 +160,9 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose private void closeDataSource(final Map<String, DataSource> dataSourceMap) { for (DataSource each : dataSourceMap.values()) { try { ReflectiveUtil.findMethod(each, "close").invoke(each); Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } Loading
sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/connection/dialect/MySQLXAConnectionWrapper.java +3 −3 Original line number Diff line number Diff line Loading @@ -19,7 +19,6 @@ package org.apache.shardingsphere.transaction.xa.jta.connection.dialect; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.apache.shardingsphere.core.util.ReflectiveUtil; import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper; import javax.sql.XAConnection; Loading @@ -39,7 +38,8 @@ public final class MySQLXAConnectionWrapper implements XAConnectionWrapper { @Override public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) { Connection physicalConnection = (Connection) connection.unwrap(Class.forName("com.mysql.jdbc.Connection")); Method wrapConnectionMethod = ReflectiveUtil.findMethod(xaDataSource, "wrapConnection", Connection.class); return (XAConnection) wrapConnectionMethod.invoke(xaDataSource, physicalConnection); Method method = xaDataSource.getClass().getDeclaredMethod("wrapConnection", Connection.class); method.setAccessible(true); return (XAConnection) method.invoke(xaDataSource, physicalConnection); } }