Commit 77f3400e authored by terrymanu's avatar terrymanu
Browse files

remove useless ReflectiveUtil

parent f75c5b95
Loading
Loading
Loading
Loading
+0 −53
Original line number Original line 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()));
    }
}
+4 −2
Original line number Original line Diff line number Diff line
@@ -21,12 +21,12 @@ import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Getter;
import lombok.Setter;
import lombok.Setter;
import org.apache.shardingsphere.core.constant.DatabaseType;
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.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;


import javax.sql.DataSource;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collection;
@@ -91,7 +91,9 @@ public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOpera
    public void close() throws Exception {
    public void close() throws Exception {
        for (DataSource each : dataSourceMap.values()) {
        for (DataSource each : dataSourceMap.values()) {
            try {
            try {
                ReflectiveUtil.findMethod(each, "close").invoke(each);
                Method method = each.getClass().getDeclaredMethod("close");
                method.setAccessible(true);
                method.invoke(each);
            } catch (final ReflectiveOperationException ignored) {
            } catch (final ReflectiveOperationException ignored) {
            }
            }
        }
        }
+4 −2
Original line number Original line Diff line number Diff line
@@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.core.constant.ConnectionMode;
import org.apache.shardingsphere.core.constant.ConnectionMode;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.exception.ShardingException;
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.backend.BackendDataSource;
import org.apache.shardingsphere.shardingproxypg.config.yaml.YamlDataSourceParameter;
import org.apache.shardingsphere.shardingproxypg.config.yaml.YamlDataSourceParameter;
import org.apache.shardingsphere.shardingproxypg.runtime.GlobalRegistry;
import org.apache.shardingsphere.shardingproxypg.runtime.GlobalRegistry;
@@ -31,6 +30,7 @@ import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager;
import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager;


import javax.sql.DataSource;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -160,7 +160,9 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose
    private void closeDataSource(final Map<String, DataSource> dataSourceMap) {
    private void closeDataSource(final Map<String, DataSource> dataSourceMap) {
        for (DataSource each : dataSourceMap.values()) {
        for (DataSource each : dataSourceMap.values()) {
            try {
            try {
                ReflectiveUtil.findMethod(each, "close").invoke(each);
                Method method = each.getClass().getDeclaredMethod("close");
                method.setAccessible(true);
                method.invoke(each);
            } catch (final ReflectiveOperationException ignored) {
            } catch (final ReflectiveOperationException ignored) {
            }
            }
        }
        }
+4 −2
Original line number Original line Diff line number Diff line
@@ -22,7 +22,6 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.core.constant.ConnectionMode;
import org.apache.shardingsphere.core.constant.ConnectionMode;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.exception.ShardingException;
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.backend.BackendDataSource;
import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter;
import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter;
import org.apache.shardingsphere.shardingproxy.runtime.GlobalRegistry;
import org.apache.shardingsphere.shardingproxy.runtime.GlobalRegistry;
@@ -31,6 +30,7 @@ import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager;
import org.apache.shardingsphere.transaction.spi.ShardingTransactionManager;


import javax.sql.DataSource;
import javax.sql.DataSource;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -160,7 +160,9 @@ public final class JDBCBackendDataSource implements BackendDataSource, AutoClose
    private void closeDataSource(final Map<String, DataSource> dataSourceMap) {
    private void closeDataSource(final Map<String, DataSource> dataSourceMap) {
        for (DataSource each : dataSourceMap.values()) {
        for (DataSource each : dataSourceMap.values()) {
            try {
            try {
                ReflectiveUtil.findMethod(each, "close").invoke(each);
                Method method = each.getClass().getDeclaredMethod("close");
                method.setAccessible(true);
                method.invoke(each);
            } catch (final ReflectiveOperationException ignored) {
            } catch (final ReflectiveOperationException ignored) {
            }
            }
        }
        }
+3 −3
Original line number Original line Diff line number Diff line
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.transaction.xa.jta.connection.dialect;


import lombok.RequiredArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.SneakyThrows;
import org.apache.shardingsphere.core.util.ReflectiveUtil;
import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper;
import org.apache.shardingsphere.transaction.xa.jta.connection.XAConnectionWrapper;


import javax.sql.XAConnection;
import javax.sql.XAConnection;
@@ -39,7 +38,8 @@ public final class MySQLXAConnectionWrapper implements XAConnectionWrapper {
    @Override
    @Override
    public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) {
    public XAConnection wrap(final XADataSource xaDataSource, final Connection connection) {
        Connection physicalConnection = (Connection) connection.unwrap(Class.forName("com.mysql.jdbc.Connection"));
        Connection physicalConnection = (Connection) connection.unwrap(Class.forName("com.mysql.jdbc.Connection"));
        Method wrapConnectionMethod = ReflectiveUtil.findMethod(xaDataSource, "wrapConnection", Connection.class);
        Method method = xaDataSource.getClass().getDeclaredMethod("wrapConnection", Connection.class);
        return (XAConnection) wrapConnectionMethod.invoke(xaDataSource, physicalConnection);
        method.setAccessible(true);
        return (XAConnection) method.invoke(xaDataSource, physicalConnection);
    }
    }
}
}