Skip to content

Commit

Permalink
Deprecate ReflectionUtils.invokeJdbcMethod (for removal in 5.2)
Browse files Browse the repository at this point in the history
Includes deprecation of NON_BRIDGED_METHODS constant.

Issue: SPR-17464

(cherry picked from commit 0a7dcf1)
  • Loading branch information
jhoeller committed Nov 5, 2018
1 parent 9b4f483 commit c834790
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 30 deletions.
Expand Up @@ -47,6 +47,30 @@
*/
public abstract class ReflectionUtils {

/**
* Pre-built MethodFilter that matches all non-bridge methods.
* @since 3.0
* @deprecated as of 5.0.11, in favor of a custom {@link MethodFilter}
*/
@Deprecated
public static final MethodFilter NON_BRIDGED_METHODS =
(method -> !method.isBridge());

/**
* Pre-built MethodFilter that matches all non-bridge non-synthetic methods
* which are not declared on {@code java.lang.Object}.
* @since 3.0.5
*/
public static final MethodFilter USER_DECLARED_METHODS =
(method -> (!method.isBridge() && !method.isSynthetic() && method.getDeclaringClass() != Object.class));

/**
* Pre-built FieldFilter that matches all non-static, non-final fields.
*/
public static final FieldFilter COPYABLE_FIELDS =
field -> !(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()));


/**
* Naming prefix for CGLIB-renamed methods.
* @see #isCglibRenamedMethod
Expand Down Expand Up @@ -236,7 +260,9 @@ public static Object invokeMethod(Method method, @Nullable Object target, @Nulla
* @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]);
Expand All @@ -251,7 +277,9 @@ public static Object invokeJdbcMethod(Method method, @Nullable Object target) th
* @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 {
Expand Down Expand Up @@ -511,8 +539,8 @@ public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>.
* on Java 8 based interfaces that the given class implements).
* @param clazz the class to introspect
* @param mc the callback to invoke for each method
* @since 4.2
* @throws IllegalStateException if introspection fails
* @since 4.2
* @see #doWithMethods
*/
public static void doWithLocalMethods(Class<?> clazz, MethodCallback mc) {
Expand Down Expand Up @@ -682,8 +710,8 @@ private static List<Method> findConcreteMethodsOnInterfaces(Class<?> clazz) {
* Invoke the given callback on all locally declared fields in the given class.
* @param clazz the target class to analyze
* @param fc the callback to invoke for each field
* @since 4.2
* @throws IllegalStateException if introspection fails
* @since 4.2
* @see #doWithFields
*/
public static void doWithLocalFields(Class<?> clazz, FieldCallback fc) {
Expand Down Expand Up @@ -846,26 +874,4 @@ public interface FieldFilter {
boolean matches(Field field);
}


/**
* Pre-built FieldFilter that matches all non-static, non-final fields.
*/
public static final FieldFilter COPYABLE_FIELDS =
field -> !(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers()));


/**
* Pre-built MethodFilter that matches all non-bridge methods.
*/
public static final MethodFilter NON_BRIDGED_METHODS =
(method -> !method.isBridge());


/**
* Pre-built MethodFilter that matches all non-bridge non-synthetic methods
* which are not declared on {@code java.lang.Object}.
*/
public static final MethodFilter USER_DECLARED_METHODS =
(method -> (!method.isBridge() && !method.isSynthetic() && method.getDeclaringClass() != Object.class));

}
Expand Up @@ -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;
Expand Down Expand Up @@ -141,7 +142,7 @@ protected Connection doGetConnection(@Nullable String username, @Nullable String
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;
Expand All @@ -163,21 +164,40 @@ protected Connection doGetConnection(@Nullable String username, @Nullable String
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");
}

}

0 comments on commit c834790

Please sign in to comment.