Skip to content

Commit 0a7dcf1

Browse files
committed
Deprecate ReflectionUtils.invokeJdbcMethod (for removal in 5.2)
Issue: SPR-17464
1 parent 8684650 commit 0a7dcf1

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

spring-core/src/main/java/org/springframework/util/ReflectionUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ public static Object invokeMethod(Method method, @Nullable Object target, @Nulla
257257
* @return the invocation result, if any
258258
* @throws SQLException the JDBC API SQLException to rethrow (if any)
259259
* @see #invokeJdbcMethod(java.lang.reflect.Method, Object, Object[])
260+
* @deprecated as of 5.0.11, in favor of custom SQLException handling
260261
*/
262+
@Deprecated
261263
@Nullable
262264
public static Object invokeJdbcMethod(Method method, @Nullable Object target) throws SQLException {
263265
return invokeJdbcMethod(method, target, new Object[0]);
@@ -272,7 +274,9 @@ public static Object invokeJdbcMethod(Method method, @Nullable Object target) th
272274
* @return the invocation result, if any
273275
* @throws SQLException the JDBC API SQLException to rethrow (if any)
274276
* @see #invokeMethod(java.lang.reflect.Method, Object, Object[])
277+
* @deprecated as of 5.0.11, in favor of custom SQLException handling
275278
*/
279+
@Deprecated
276280
@Nullable
277281
public static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args)
278282
throws SQLException {

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/WebSphereDataSourceAdapter.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.jdbc.datasource;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
2021
import java.sql.Connection;
2122
import java.sql.SQLException;
@@ -141,7 +142,7 @@ protected Connection doGetConnection(@Nullable String username, @Nullable String
141142
getTargetDataSource() + "], using ConnectionSpec [" + connSpec + "]");
142143
}
143144
// Create Connection through invoking WSDataSource.getConnection(JDBCConnectionSpec)
144-
Connection con = (Connection) ReflectionUtils.invokeJdbcMethod(
145+
Connection con = (Connection) invokeJdbcMethod(
145146
this.wsDataSourceGetConnectionMethod, obtainTargetDataSource(), connSpec);
146147
Assert.state(con != null, "No Connection");
147148
return con;
@@ -163,21 +164,40 @@ protected Connection doGetConnection(@Nullable String username, @Nullable String
163164
protected Object createConnectionSpec(@Nullable Integer isolationLevel, @Nullable Boolean readOnlyFlag,
164165
@Nullable String username, @Nullable String password) throws SQLException {
165166

166-
Object connSpec = ReflectionUtils.invokeJdbcMethod(this.newJdbcConnSpecMethod, null);
167+
Object connSpec = invokeJdbcMethod(this.newJdbcConnSpecMethod, null);
167168
Assert.state(connSpec != null, "No JDBCConnectionSpec");
168169
if (isolationLevel != null) {
169-
ReflectionUtils.invokeJdbcMethod(this.setTransactionIsolationMethod, connSpec, isolationLevel);
170+
invokeJdbcMethod(this.setTransactionIsolationMethod, connSpec, isolationLevel);
170171
}
171172
if (readOnlyFlag != null) {
172-
ReflectionUtils.invokeJdbcMethod(this.setReadOnlyMethod, connSpec, readOnlyFlag);
173+
invokeJdbcMethod(this.setReadOnlyMethod, connSpec, readOnlyFlag);
173174
}
174175
// If the username is empty, we'll simply let the target DataSource
175176
// use its default credentials.
176177
if (StringUtils.hasLength(username)) {
177-
ReflectionUtils.invokeJdbcMethod(this.setUserNameMethod, connSpec, username);
178-
ReflectionUtils.invokeJdbcMethod(this.setPasswordMethod, connSpec, password);
178+
invokeJdbcMethod(this.setUserNameMethod, connSpec, username);
179+
invokeJdbcMethod(this.setPasswordMethod, connSpec, password);
179180
}
180181
return connSpec;
181182
}
182183

184+
185+
@Nullable
186+
private static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args)
187+
throws SQLException {
188+
try {
189+
return method.invoke(target, args);
190+
}
191+
catch (IllegalAccessException ex) {
192+
ReflectionUtils.handleReflectionException(ex);
193+
}
194+
catch (InvocationTargetException ex) {
195+
if (ex.getTargetException() instanceof SQLException) {
196+
throw (SQLException) ex.getTargetException();
197+
}
198+
ReflectionUtils.handleInvocationTargetException(ex);
199+
}
200+
throw new IllegalStateException("Should never get here");
201+
}
202+
183203
}

0 commit comments

Comments
 (0)