Skip to content

Commit c834790

Browse files
committed
Deprecate ReflectionUtils.invokeJdbcMethod (for removal in 5.2)
Includes deprecation of NON_BRIDGED_METHODS constant. Issue: SPR-17464 (cherry picked from commit 0a7dcf1)
1 parent 9b4f483 commit c834790

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@
4747
*/
4848
public abstract class ReflectionUtils {
4949

50+
/**
51+
* Pre-built MethodFilter that matches all non-bridge methods.
52+
* @since 3.0
53+
* @deprecated as of 5.0.11, in favor of a custom {@link MethodFilter}
54+
*/
55+
@Deprecated
56+
public static final MethodFilter NON_BRIDGED_METHODS =
57+
(method -> !method.isBridge());
58+
59+
/**
60+
* Pre-built MethodFilter that matches all non-bridge non-synthetic methods
61+
* which are not declared on {@code java.lang.Object}.
62+
* @since 3.0.5
63+
*/
64+
public static final MethodFilter USER_DECLARED_METHODS =
65+
(method -> (!method.isBridge() && !method.isSynthetic() && method.getDeclaringClass() != Object.class));
66+
67+
/**
68+
* Pre-built FieldFilter that matches all non-static, non-final fields.
69+
*/
70+
public static final FieldFilter COPYABLE_FIELDS =
71+
field -> !(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()));
72+
73+
5074
/**
5175
* Naming prefix for CGLIB-renamed methods.
5276
* @see #isCglibRenamedMethod
@@ -236,7 +260,9 @@ public static Object invokeMethod(Method method, @Nullable Object target, @Nulla
236260
* @return the invocation result, if any
237261
* @throws SQLException the JDBC API SQLException to rethrow (if any)
238262
* @see #invokeJdbcMethod(java.lang.reflect.Method, Object, Object[])
263+
* @deprecated as of 5.0.11, in favor of custom SQLException handling
239264
*/
265+
@Deprecated
240266
@Nullable
241267
public static Object invokeJdbcMethod(Method method, @Nullable Object target) throws SQLException {
242268
return invokeJdbcMethod(method, target, new Object[0]);
@@ -251,7 +277,9 @@ public static Object invokeJdbcMethod(Method method, @Nullable Object target) th
251277
* @return the invocation result, if any
252278
* @throws SQLException the JDBC API SQLException to rethrow (if any)
253279
* @see #invokeMethod(java.lang.reflect.Method, Object, Object[])
280+
* @deprecated as of 5.0.11, in favor of custom SQLException handling
254281
*/
282+
@Deprecated
255283
@Nullable
256284
public static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args)
257285
throws SQLException {
@@ -511,8 +539,8 @@ public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>.
511539
* on Java 8 based interfaces that the given class implements).
512540
* @param clazz the class to introspect
513541
* @param mc the callback to invoke for each method
514-
* @since 4.2
515542
* @throws IllegalStateException if introspection fails
543+
* @since 4.2
516544
* @see #doWithMethods
517545
*/
518546
public static void doWithLocalMethods(Class<?> clazz, MethodCallback mc) {
@@ -682,8 +710,8 @@ private static List<Method> findConcreteMethodsOnInterfaces(Class<?> clazz) {
682710
* Invoke the given callback on all locally declared fields in the given class.
683711
* @param clazz the target class to analyze
684712
* @param fc the callback to invoke for each field
685-
* @since 4.2
686713
* @throws IllegalStateException if introspection fails
714+
* @since 4.2
687715
* @see #doWithFields
688716
*/
689717
public static void doWithLocalFields(Class<?> clazz, FieldCallback fc) {
@@ -846,26 +874,4 @@ public interface FieldFilter {
846874
boolean matches(Field field);
847875
}
848876

849-
850-
/**
851-
* Pre-built FieldFilter that matches all non-static, non-final fields.
852-
*/
853-
public static final FieldFilter COPYABLE_FIELDS =
854-
field -> !(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()));
855-
856-
857-
/**
858-
* Pre-built MethodFilter that matches all non-bridge methods.
859-
*/
860-
public static final MethodFilter NON_BRIDGED_METHODS =
861-
(method -> !method.isBridge());
862-
863-
864-
/**
865-
* Pre-built MethodFilter that matches all non-bridge non-synthetic methods
866-
* which are not declared on {@code java.lang.Object}.
867-
*/
868-
public static final MethodFilter USER_DECLARED_METHODS =
869-
(method -> (!method.isBridge() && !method.isSynthetic() && method.getDeclaringClass() != Object.class));
870-
871877
}

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)