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 852b571  Refactor reflection with lambda in MethodInvocationRecorder 
(#16085)
852b571 is described below

commit 852b5714e60adad3a6552d095c38ee994e6ad5e3
Author: 吴伟杰 <[email protected]>
AuthorDate: Tue Mar 15 16:35:46 2022 +0800

    Refactor reflection with lambda in MethodInvocationRecorder (#16085)
---
 .../jdbc/adapter/AbstractStatementAdapter.java     | 14 +++----
 .../driver/jdbc/adapter/WrapperAdapter.java        |  3 +-
 .../jdbc/adapter/invocation/MethodInvocation.java  | 46 ----------------------
 .../invocation/MethodInvocationRecorder.java       | 27 +++++++------
 .../jdbc/core/connection/ConnectionManager.java    |  8 ++--
 .../core/statement/ShardingSphereStatement.java    |  4 +-
 .../invocation/MethodInvocationRecorderTest.java   | 20 ++++------
 .../adapter/invocation/MethodInvocationTest.java   | 28 -------------
 8 files changed, 37 insertions(+), 113 deletions(-)

diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
index a97982d..c18ac86 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractStatementAdapter.java
@@ -60,7 +60,7 @@ public abstract class AbstractStatementAdapter extends 
AbstractUnsupportedOperat
     @Override
     public final void setPoolable(final boolean poolable) throws SQLException {
         this.poolable = poolable;
-        getMethodInvocationRecorder().record(targetClass, "setPoolable", new 
Class[] {boolean.class}, new Object[] {poolable});
+        getMethodInvocationRecorder().record("setPoolable", statement -> 
statement.setPoolable(poolable));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setPoolable(poolable));
     }
     
@@ -68,7 +68,7 @@ public abstract class AbstractStatementAdapter extends 
AbstractUnsupportedOperat
     @Override
     public final void setFetchSize(final int rows) throws SQLException {
         fetchSize = rows;
-        getMethodInvocationRecorder().record(targetClass, "setFetchSize", new 
Class[] {int.class}, new Object[] {rows});
+        getMethodInvocationRecorder().record("setFetchSize", statement -> 
statement.setFetchSize(rows));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setFetchSize(rows));
     }
     
@@ -76,7 +76,7 @@ public abstract class AbstractStatementAdapter extends 
AbstractUnsupportedOperat
     @Override
     public final void setFetchDirection(final int direction) throws 
SQLException {
         fetchDirection = direction;
-        getMethodInvocationRecorder().record(targetClass, "setFetchDirection", 
new Class[] {int.class}, new Object[] {direction});
+        getMethodInvocationRecorder().record("setFetchDirection", statement -> 
statement.setFetchDirection(direction));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setFetchDirection(direction));
     }
     
@@ -88,7 +88,7 @@ public abstract class AbstractStatementAdapter extends 
AbstractUnsupportedOperat
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
     public final void setMaxFieldSize(final int max) throws SQLException {
-        getMethodInvocationRecorder().record(targetClass, "setMaxFieldSize", 
new Class[] {int.class}, new Object[] {max});
+        getMethodInvocationRecorder().record("setMaxFieldSize", statement -> 
statement.setMaxFieldSize(max));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setMaxFieldSize(max));
     }
     
@@ -101,7 +101,7 @@ public abstract class AbstractStatementAdapter extends 
AbstractUnsupportedOperat
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
     public final void setMaxRows(final int max) throws SQLException {
-        getMethodInvocationRecorder().record(targetClass, "setMaxRows", new 
Class[] {int.class}, new Object[] {max});
+        getMethodInvocationRecorder().record("setMaxRows", statement -> 
statement.setMaxRows(max));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setMaxRows(max));
     }
     
@@ -113,14 +113,14 @@ public abstract class AbstractStatementAdapter extends 
AbstractUnsupportedOperat
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
     public final void setQueryTimeout(final int seconds) throws SQLException {
-        getMethodInvocationRecorder().record(targetClass, "setQueryTimeout", 
new Class[] {int.class}, new Object[] {seconds});
+        getMethodInvocationRecorder().record("setQueryTimeout", statement -> 
statement.setQueryTimeout(seconds));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setQueryTimeout(seconds));
     }
     
     @SuppressWarnings({"unchecked", "rawtypes"})
     @Override
     public final void setEscapeProcessing(final boolean enable) throws 
SQLException {
-        getMethodInvocationRecorder().record(targetClass, 
"setEscapeProcessing", new Class[] {boolean.class}, new Object[] {enable});
+        getMethodInvocationRecorder().record("setEscapeProcessing", statement 
-> statement.setEscapeProcessing(enable));
         forceExecuteTemplate.execute((Collection) getRoutedStatements(), 
statement -> statement.setEscapeProcessing(enable));
     }
     
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapter.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapter.java
index 4b9aa0c..b08cd3e 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapter.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapter.java
@@ -21,6 +21,7 @@ import lombok.Getter;
 import 
org.apache.shardingsphere.driver.jdbc.adapter.invocation.MethodInvocationRecorder;
 
 import java.sql.SQLException;
+import java.sql.Statement;
 import java.sql.Wrapper;
 
 /**
@@ -29,7 +30,7 @@ import java.sql.Wrapper;
 @Getter
 public abstract class WrapperAdapter implements Wrapper {
     
-    private final MethodInvocationRecorder methodInvocationRecorder = new 
MethodInvocationRecorder();
+    private final MethodInvocationRecorder<Statement> methodInvocationRecorder 
= new MethodInvocationRecorder<>();
     
     @SuppressWarnings("unchecked")
     @Override
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocation.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocation.java
deleted file mode 100644
index c3fcca8..0000000
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocation.java
+++ /dev/null
@@ -1,46 +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.driver.jdbc.adapter.invocation;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
-
-import java.lang.reflect.Method;
-
-/**
- * Invocation that reflected call for method.
- */
-@RequiredArgsConstructor
-@Getter
-public class MethodInvocation {
-    
-    private final Method method;
-    
-    private final Object[] arguments;
-    
-    /**
-     * Invoke method.
-     * 
-     * @param target target object
-     */
-    @SneakyThrows(ReflectiveOperationException.class)
-    public void invoke(final Object target) {
-        method.invoke(target, arguments);
-    }
-}
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorder.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorder.java
index 8d3f354..4d272e8 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorder.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorder.java
@@ -17,39 +17,40 @@
 
 package org.apache.shardingsphere.driver.jdbc.adapter.invocation;
 
-import lombok.SneakyThrows;
+import 
org.apache.shardingsphere.driver.jdbc.adapter.executor.ForceExecuteCallback;
 
-import java.lang.reflect.Method;
+import java.sql.SQLException;
 import java.util.LinkedHashMap;
 import java.util.Map;
 
 /**
  * Method invocation recorder.
+ *
+ * @param <T> type of target
  */
-public final class MethodInvocationRecorder {
+public final class MethodInvocationRecorder<T> {
     
-    private final Map<Method, MethodInvocation> methodInvocations = new 
LinkedHashMap<>();
+    private final Map<String, ForceExecuteCallback<T>> methodInvocations = new 
LinkedHashMap<>();
     
     /**
      * Record method invocation.
      *
-     * @param targetClass target class
      * @param methodName method name
-     * @param argumentTypes argument types
-     * @param arguments arguments
+     * @param callback callback
      */
-    @SneakyThrows(ReflectiveOperationException.class)
-    public void record(final Class<?> targetClass, final String methodName, 
final Class<?>[] argumentTypes, final Object[] arguments) {
-        Method method = targetClass.getMethod(methodName, argumentTypes);
-        methodInvocations.put(method, new MethodInvocation(method, arguments));
+    public void record(final String methodName, final ForceExecuteCallback<T> 
callback) {
+        methodInvocations.put(methodName, callback);
     }
     
     /**
      * Replay methods invocation.
      *
      * @param target target object
+     * @throws SQLException SQL Exception
      */
-    public void replay(final Object target) {
-        methodInvocations.forEach((unused, each) -> each.invoke(target));
+    public void replay(final T target) throws SQLException {
+        for (ForceExecuteCallback<T> each : methodInvocations.values()) {
+            each.execute(target);
+        }
     }
 }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
index b1aca6d..3f98e12 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/connection/ConnectionManager.java
@@ -67,7 +67,7 @@ public final class ConnectionManager implements 
ExecutorJDBCConnectionManager, A
     
     private final Multimap<String, Connection> cachedConnections = 
LinkedHashMultimap.create();
     
-    private final MethodInvocationRecorder methodInvocationRecorder = new 
MethodInvocationRecorder();
+    private final MethodInvocationRecorder<Connection> 
methodInvocationRecorder = new MethodInvocationRecorder<>();
     
     private final ForceExecuteTemplate<Connection> forceExecuteTemplate = new 
ForceExecuteTemplate<>();
     
@@ -137,7 +137,7 @@ public final class ConnectionManager implements 
ExecutorJDBCConnectionManager, A
      * @throws SQLException SQL exception
      */
     public void setAutoCommit(final boolean autoCommit) throws SQLException {
-        methodInvocationRecorder.record(Connection.class, "setAutoCommit", new 
Class[]{boolean.class}, new Object[]{autoCommit});
+        methodInvocationRecorder.record("setAutoCommit", target -> 
target.setAutoCommit(autoCommit));
         forceExecuteTemplate.execute(cachedConnections.values(), connection -> 
connection.setAutoCommit(autoCommit));
     }
     
@@ -186,7 +186,7 @@ public final class ConnectionManager implements 
ExecutorJDBCConnectionManager, A
      * @throws SQLException SQL exception
      */
     public void setTransactionIsolation(final int level) throws SQLException {
-        methodInvocationRecorder.record(Connection.class, 
"setTransactionIsolation", new Class[]{int.class}, new Object[]{level});
+        methodInvocationRecorder.record("setTransactionIsolation", connection 
-> connection.setTransactionIsolation(level));
         forceExecuteTemplate.execute(cachedConnections.values(), connection -> 
connection.setTransactionIsolation(level));
     }
     
@@ -197,7 +197,7 @@ public final class ConnectionManager implements 
ExecutorJDBCConnectionManager, A
      * @throws SQLException SQL exception
      */
     public void setReadOnly(final boolean readOnly) throws SQLException {
-        methodInvocationRecorder.record(Connection.class, "setReadOnly", new 
Class[]{boolean.class}, new Object[]{readOnly});
+        methodInvocationRecorder.record("setReadOnly", connection -> 
connection.setReadOnly(readOnly));
         forceExecuteTemplate.execute(cachedConnections.values(), connection -> 
connection.setReadOnly(readOnly));
     }
     
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index 2f0c3f1..481e144 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -485,14 +485,14 @@ public final class ShardingSphereStatement extends 
AbstractStatementAdapter {
                 .prepare(executionContext.getRouteContext(), 
executionContext.getExecutionUnits());
     }
     
-    private void cacheStatements(final 
Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups) {
+    private void cacheStatements(final 
Collection<ExecutionGroup<JDBCExecutionUnit>> executionGroups) throws 
SQLException {
         for (ExecutionGroup<JDBCExecutionUnit> each : executionGroups) {
             
statements.addAll(each.getInputs().stream().map(JDBCExecutionUnit::getStorageResource).collect(Collectors.toList()));
         }
         replay();
     }
     
-    private void replay() {
+    private void replay() throws SQLException {
         for (Statement each : statements) {
             getMethodInvocationRecorder().replay(each);
         }
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorderTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorderTest.java
index b40375b..546bb12 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorderTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationRecorderTest.java
@@ -19,6 +19,7 @@ package 
org.apache.shardingsphere.driver.jdbc.adapter.invocation;
 
 import org.junit.Test;
 
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -29,22 +30,17 @@ import static org.junit.Assert.assertThat;
 public final class MethodInvocationRecorderTest {
     
     @Test
-    public void assertRecordMethodInvocationSuccess() {
-        MethodInvocationRecorder methodInvocationRecorder = new 
MethodInvocationRecorder();
-        methodInvocationRecorder.record(List.class, "isEmpty", new Class[]{}, 
new Object[]{});
+    public void assertRecordMethodInvocationSuccess() throws SQLException {
+        MethodInvocationRecorder<List<?>> methodInvocationRecorder = new 
MethodInvocationRecorder<>();
+        methodInvocationRecorder.record("isEmpty", List::isEmpty);
         methodInvocationRecorder.replay(Collections.emptyList());
     }
     
-    @Test(expected = NoSuchMethodException.class)
-    public void assertRecordMethodInvocationFailure() {
-        new MethodInvocationRecorder().record(String.class, "none", new 
Class[]{}, new Object[]{});
-    }
-    
     @Test
-    public void assertRecordSameMethodTwice() {
-        MethodInvocationRecorder methodInvocationRecorder = new 
MethodInvocationRecorder();
-        methodInvocationRecorder.record(List.class, "add", new 
Class[]{Object.class}, new Object[]{1});
-        methodInvocationRecorder.record(List.class, "add", new 
Class[]{Object.class}, new Object[]{2});
+    public void assertRecordSameMethodTwice() throws SQLException {
+        MethodInvocationRecorder<List<Integer>> methodInvocationRecorder = new 
MethodInvocationRecorder<>();
+        methodInvocationRecorder.record("add", target -> target.add(1));
+        methodInvocationRecorder.record("add", target -> target.add(2));
         List<Integer> actual = new ArrayList<>();
         methodInvocationRecorder.replay(actual);
         assertThat(actual.size(), is(1));
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationTest.java
deleted file mode 100644
index f106552..0000000
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/invocation/MethodInvocationTest.java
+++ /dev/null
@@ -1,28 +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.driver.jdbc.adapter.invocation;
-
-import org.junit.Test;
-
-public final class MethodInvocationTest {
-    
-    @Test
-    public void assertInvokeSuccess() throws NoSuchMethodException {
-        new MethodInvocation(String.class.getMethod("length"), new Object[] 
{}).invoke("");
-    }
-}

Reply via email to