Skip to content

Commit d371886

Browse files
author
Phillip Webb
committed
Allow null method for getTransactionAttribute
Update MatchAlwaysTransactionAttributeSource.getTransactionAttribute to allow a null method argument. Passing a null method is not recommended and is not indicated as valid in the Javadoc, however, this was allowed in previous versions of Spring. Issue: SPR-11048
1 parent 5d8fac8 commit d371886

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,12 @@ public static Method getMostSpecificMethod(Method method, Class<?> targetClass)
778778
* @return {@code true} if the method can be considered as user-declared; [@code false} otherwise
779779
*/
780780
public static boolean isUserLevelMethod(Method method) {
781-
return (method.isBridge() ||
782-
(!method.isSynthetic() && !method.getDeclaringClass().getName().equals("groovy.lang.GroovyObject")));
781+
Assert.notNull(method, "Method must not be null");
782+
return (method.isBridge() || (!method.isSynthetic() && !isGroovyObjectMethod(method)));
783+
}
784+
785+
private static boolean isGroovyObjectMethod(Method method) {
786+
return method.getDeclaringClass().getName().equals("groovy.lang.GroovyObject");
783787
}
784788

785789
/**

spring-tx/src/main/java/org/springframework/transaction/interceptor/MatchAlwaysTransactionAttributeSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void setTransactionAttribute(TransactionAttribute transactionAttribute) {
5353

5454
@Override
5555
public TransactionAttribute getTransactionAttribute(Method method, Class<?> targetClass) {
56-
return (ClassUtils.isUserLevelMethod(method) ? this.transactionAttribute : null);
56+
return (method == null || ClassUtils.isUserLevelMethod(method) ? this.transactionAttribute : null);
5757
}
5858

5959

spring-tx/src/test/java/org/springframework/transaction/interceptor/TransactionAttributeSourceTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public void testMatchAlwaysTransactionAttributeSource() throws Exception {
5454
assertTrue(TransactionDefinition.PROPAGATION_SUPPORTS == ta.getPropagationBehavior());
5555
}
5656

57+
@Test
58+
public void testMatchAlwaysTransactionAttributeSourceWithNulls() throws Exception {
59+
MatchAlwaysTransactionAttributeSource tas = new MatchAlwaysTransactionAttributeSource();
60+
TransactionDefinition definition = tas.getTransactionAttribute(null, null);
61+
assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, definition.getPropagationBehavior());
62+
assertEquals(TransactionDefinition.ISOLATION_DEFAULT, definition.getIsolationLevel());
63+
assertEquals(TransactionDefinition.TIMEOUT_DEFAULT, definition.getTimeout());
64+
assertFalse(definition.isReadOnly());
65+
}
66+
5767
@SuppressWarnings("unchecked")
5868
@Ignore // no longer works now that setMethodMap has been parameterized
5969
@Test

0 commit comments

Comments
 (0)