From 0a7dcf14f97913aa9de540d003cb3d9d6403b1e4 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 5 Nov 2018 12:26:20 +0100 Subject: [PATCH] Deprecate ReflectionUtils.invokeJdbcMethod (for removal in 5.2) Issue: SPR-17464 --- .../springframework/util/ReflectionUtils.java | 4 +++ .../WebSphereDataSourceAdapter.java | 32 +++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java b/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java index a04f1739404..68a6ce54682 100644 --- a/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java @@ -257,7 +257,9 @@ public abstract class ReflectionUtils { * @return the invocation result, if any * @throws SQLException the JDBC API SQLException to rethrow (if any) * @see #invokeJdbcMethod(java.lang.reflect.Method, Object, Object[]) + * @deprecated as of 5.0.11, in favor of custom SQLException handling */ + @Deprecated @Nullable public static Object invokeJdbcMethod(Method method, @Nullable Object target) throws SQLException { return invokeJdbcMethod(method, target, new Object[0]); @@ -272,7 +274,9 @@ public abstract class ReflectionUtils { * @return the invocation result, if any * @throws SQLException the JDBC API SQLException to rethrow (if any) * @see #invokeMethod(java.lang.reflect.Method, Object, Object[]) + * @deprecated as of 5.0.11, in favor of custom SQLException handling */ + @Deprecated @Nullable public static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args) throws SQLException { diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/WebSphereDataSourceAdapter.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/WebSphereDataSourceAdapter.java index 12ca3e856b0..8f82c8caf44 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/WebSphereDataSourceAdapter.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/WebSphereDataSourceAdapter.java @@ -16,6 +16,7 @@ package org.springframework.jdbc.datasource; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.Connection; import java.sql.SQLException; @@ -141,7 +142,7 @@ public class WebSphereDataSourceAdapter extends IsolationLevelDataSourceAdapter getTargetDataSource() + "], using ConnectionSpec [" + connSpec + "]"); } // Create Connection through invoking WSDataSource.getConnection(JDBCConnectionSpec) - Connection con = (Connection) ReflectionUtils.invokeJdbcMethod( + Connection con = (Connection) invokeJdbcMethod( this.wsDataSourceGetConnectionMethod, obtainTargetDataSource(), connSpec); Assert.state(con != null, "No Connection"); return con; @@ -163,21 +164,40 @@ public class WebSphereDataSourceAdapter extends IsolationLevelDataSourceAdapter protected Object createConnectionSpec(@Nullable Integer isolationLevel, @Nullable Boolean readOnlyFlag, @Nullable String username, @Nullable String password) throws SQLException { - Object connSpec = ReflectionUtils.invokeJdbcMethod(this.newJdbcConnSpecMethod, null); + Object connSpec = invokeJdbcMethod(this.newJdbcConnSpecMethod, null); Assert.state(connSpec != null, "No JDBCConnectionSpec"); if (isolationLevel != null) { - ReflectionUtils.invokeJdbcMethod(this.setTransactionIsolationMethod, connSpec, isolationLevel); + invokeJdbcMethod(this.setTransactionIsolationMethod, connSpec, isolationLevel); } if (readOnlyFlag != null) { - ReflectionUtils.invokeJdbcMethod(this.setReadOnlyMethod, connSpec, readOnlyFlag); + invokeJdbcMethod(this.setReadOnlyMethod, connSpec, readOnlyFlag); } // If the username is empty, we'll simply let the target DataSource // use its default credentials. if (StringUtils.hasLength(username)) { - ReflectionUtils.invokeJdbcMethod(this.setUserNameMethod, connSpec, username); - ReflectionUtils.invokeJdbcMethod(this.setPasswordMethod, connSpec, password); + invokeJdbcMethod(this.setUserNameMethod, connSpec, username); + invokeJdbcMethod(this.setPasswordMethod, connSpec, password); } return connSpec; } + + @Nullable + private static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args) + throws SQLException { + try { + return method.invoke(target, args); + } + catch (IllegalAccessException ex) { + ReflectionUtils.handleReflectionException(ex); + } + catch (InvocationTargetException ex) { + if (ex.getTargetException() instanceof SQLException) { + throw (SQLException) ex.getTargetException(); + } + ReflectionUtils.handleInvocationTargetException(ex); + } + throw new IllegalStateException("Should never get here"); + } + }