Bypass method traversal for annotation introspection if possible
The isCandidateClass mechanism is consistently used for a bypass check before method traversal attempts. While by default this is only bypassing standard java types, the same mechanism can be used with index metadata which indicates non-presence of certain annotations. See gh-22420
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -137,6 +137,16 @@ public class AnnotationTransactionAttributeSource extends AbstractFallbackTransa
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isCandidateClass(Class<?> targetClass) {
|
||||
for (TransactionAnnotationParser parser : this.annotationParsers) {
|
||||
if (parser.isCandidateClass(targetClass)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
|
||||
@@ -161,8 +171,8 @@ public class AnnotationTransactionAttributeSource extends AbstractFallbackTransa
|
||||
*/
|
||||
@Nullable
|
||||
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
|
||||
for (TransactionAnnotationParser annotationParser : this.annotationParsers) {
|
||||
TransactionAttribute attr = annotationParser.parseTransactionAnnotation(element);
|
||||
for (TransactionAnnotationParser parser : this.annotationParsers) {
|
||||
TransactionAttribute attr = parser.parseTransactionAnnotation(element);
|
||||
if (attr != null) {
|
||||
return attr;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -21,6 +21,7 @@ import java.lang.reflect.AnnotatedElement;
|
||||
import javax.ejb.ApplicationException;
|
||||
import javax.ejb.TransactionAttributeType;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
|
||||
import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
@@ -35,6 +36,11 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
@SuppressWarnings("serial")
|
||||
public class Ejb3TransactionAnnotationParser implements TransactionAnnotationParser, Serializable {
|
||||
|
||||
@Override
|
||||
public boolean isCandidateClass(Class<?> targetClass) {
|
||||
return AnnotationUtils.isCandidateClass(targetClass, javax.ejb.TransactionAttribute.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,6 +39,11 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
@SuppressWarnings("serial")
|
||||
public class JtaTransactionAnnotationParser implements TransactionAnnotationParser, Serializable {
|
||||
|
||||
@Override
|
||||
public boolean isCandidateClass(Class<?> targetClass) {
|
||||
return AnnotationUtils.isCandidateClass(targetClass, javax.transaction.Transactional.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,6 +39,11 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
@SuppressWarnings("serial")
|
||||
public class SpringTransactionAnnotationParser implements TransactionAnnotationParser, Serializable {
|
||||
|
||||
@Override
|
||||
public boolean isCandidateClass(Class<?> targetClass) {
|
||||
return AnnotationUtils.isCandidateClass(targetClass, Transactional.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -37,6 +37,24 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
|
||||
*/
|
||||
public interface TransactionAnnotationParser {
|
||||
|
||||
/**
|
||||
* Determine whether the given class is a candidate for transaction attributes
|
||||
* in the annotation format of this {@code TransactionAnnotationParser}.
|
||||
* <p>If this method returns {@code false}, the methods on the given class
|
||||
* will not get traversed for {@code #parseTransactionAnnotation} introspection.
|
||||
* Returning {@code false} is therefore an optimization for non-affected
|
||||
* classes, whereas {@code true} simply means that the class needs to get
|
||||
* fully introspected for each method on the given class individually.
|
||||
* @param targetClass the class to introspect
|
||||
* @return {@code false} if the class is known to have no transaction
|
||||
* annotations at class or method level; {@code true} otherwise. The default
|
||||
* implementation returns {@code true}, leading to regular introspection.
|
||||
* @since 5.2
|
||||
*/
|
||||
default boolean isCandidateClass(Class<?> targetClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the transaction attribute for the given method or class,
|
||||
* based on an annotation type understood by this parser.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,7 +39,7 @@ public class CompositeTransactionAttributeSource implements TransactionAttribute
|
||||
* Create a new CompositeTransactionAttributeSource for the given sources.
|
||||
* @param transactionAttributeSources the TransactionAttributeSource instances to combine
|
||||
*/
|
||||
public CompositeTransactionAttributeSource(TransactionAttributeSource[] transactionAttributeSources) {
|
||||
public CompositeTransactionAttributeSource(TransactionAttributeSource... transactionAttributeSources) {
|
||||
Assert.notNull(transactionAttributeSources, "TransactionAttributeSource array must not be null");
|
||||
this.transactionAttributeSources = transactionAttributeSources;
|
||||
}
|
||||
@@ -53,13 +53,23 @@ public class CompositeTransactionAttributeSource implements TransactionAttribute
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isCandidateClass(Class<?> targetClass) {
|
||||
for (TransactionAttributeSource source : this.transactionAttributeSources) {
|
||||
if (source.isCandidateClass(targetClass)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
|
||||
for (TransactionAttributeSource tas : this.transactionAttributeSources) {
|
||||
TransactionAttribute ta = tas.getTransactionAttribute(method, targetClass);
|
||||
if (ta != null) {
|
||||
return ta;
|
||||
for (TransactionAttributeSource source : this.transactionAttributeSources) {
|
||||
TransactionAttribute attr = source.getTransactionAttribute(method, targetClass);
|
||||
if (attr != null) {
|
||||
return attr;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -34,14 +34,31 @@ import org.springframework.lang.Nullable;
|
||||
*/
|
||||
public interface TransactionAttributeSource {
|
||||
|
||||
/**
|
||||
* Determine whether the given class is a candidate for transaction attributes
|
||||
* in the metadata format of this {@code TransactionAttributeSource}.
|
||||
* <p>If this method returns {@code false}, the methods on the given class
|
||||
* will not get traversed for {@link #getTransactionAttribute} introspection.
|
||||
* Returning {@code false} is therefore an optimization for non-affected
|
||||
* classes, whereas {@code true} simply means that the class needs to get
|
||||
* fully introspected for each method on the given class individually.
|
||||
* @param targetClass the class to introspect
|
||||
* @return {@code false} if the class is known to have no transaction
|
||||
* attributes at class or method level; {@code true} otherwise. The default
|
||||
* implementation returns {@code true}, leading to regular introspection.
|
||||
* @since 5.2
|
||||
*/
|
||||
default boolean isCandidateClass(Class<?> targetClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the transaction attribute for the given method,
|
||||
* or {@code null} if the method is non-transactional.
|
||||
* @param method the method to introspect
|
||||
* @param targetClass the target class (may be {@code null},
|
||||
* in which case the declaring class of the method must be used)
|
||||
* @return the TransactionAttribute the matching transaction attribute,
|
||||
* or {@code null} if none found
|
||||
* @return the matching transaction attribute, or {@code null} if none found
|
||||
*/
|
||||
@Nullable
|
||||
TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.transaction.interceptor;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.aop.ClassFilter;
|
||||
import org.springframework.aop.support.StaticMethodMatcherPointcut;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -35,13 +36,13 @@ import org.springframework.util.ObjectUtils;
|
||||
@SuppressWarnings("serial")
|
||||
abstract class TransactionAttributeSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
|
||||
|
||||
protected TransactionAttributeSourcePointcut() {
|
||||
setClassFilter(new TransactionAttributeSourceClassFilter());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean matches(Method method, Class<?> targetClass) {
|
||||
if (TransactionalProxy.class.isAssignableFrom(targetClass) ||
|
||||
PlatformTransactionManager.class.isAssignableFrom(targetClass) ||
|
||||
PersistenceExceptionTranslator.class.isAssignableFrom(targetClass)) {
|
||||
return false;
|
||||
}
|
||||
TransactionAttributeSource tas = getTransactionAttributeSource();
|
||||
return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
|
||||
}
|
||||
@@ -76,4 +77,23 @@ abstract class TransactionAttributeSourcePointcut extends StaticMethodMatcherPoi
|
||||
@Nullable
|
||||
protected abstract TransactionAttributeSource getTransactionAttributeSource();
|
||||
|
||||
|
||||
/**
|
||||
* {@link ClassFilter} that delegates to {@link TransactionAttributeSource#isCandidateClass}
|
||||
* for filtering classes whose methods are not worth searching to begin with.
|
||||
*/
|
||||
private class TransactionAttributeSourceClassFilter implements ClassFilter {
|
||||
|
||||
@Override
|
||||
public boolean matches(Class<?> clazz) {
|
||||
if (TransactionalProxy.class.isAssignableFrom(clazz) ||
|
||||
PlatformTransactionManager.class.isAssignableFrom(clazz) ||
|
||||
PersistenceExceptionTranslator.class.isAssignableFrom(clazz)) {
|
||||
return false;
|
||||
}
|
||||
TransactionAttributeSource tas = getTransactionAttributeSource();
|
||||
return (tas == null || tas.isCandidateClass(clazz));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user