Consistent bridge method handling in annotation post-processors

Issue: SPR-12490
Issue: SPR-12495
This commit is contained in:
Juergen Hoeller
2014-12-07 20:51:35 +01:00
parent 15d3b88037
commit 03d4e1bc22
8 changed files with 118 additions and 80 deletions

View File

@@ -50,6 +50,7 @@ import org.springframework.beans.factory.config.DestructionAwareBeanPostProcesso
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.jndi.JndiLocatorDelegate;
@@ -405,19 +406,21 @@ public class PersistenceAnnotationBeanPostProcessor
do {
LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
for (Field field : targetClass.getDeclaredFields()) {
PersistenceContext pc = field.getAnnotation(PersistenceContext.class);
PersistenceUnit pu = field.getAnnotation(PersistenceUnit.class);
if (pc != null || pu != null) {
if (field.isAnnotationPresent(PersistenceContext.class) ||
field.isAnnotationPresent(PersistenceUnit.class)) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("Persistence annotations are not supported on static fields");
}
currElements.add(new PersistenceElement(field, null));
currElements.add(new PersistenceElement(field, field, null));
}
}
for (Method method : targetClass.getDeclaredMethods()) {
PersistenceContext pc = method.getAnnotation(PersistenceContext.class);
PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class);
if ((pc != null || pu != null) && !method.isBridge() &&
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
continue;
}
if ((bridgedMethod.isAnnotationPresent(PersistenceContext.class) ||
bridgedMethod.isAnnotationPresent(PersistenceUnit.class)) &&
method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (Modifier.isStatic(method.getModifiers())) {
throw new IllegalStateException("Persistence annotations are not supported on static methods");
@@ -425,8 +428,8 @@ public class PersistenceAnnotationBeanPostProcessor
if (method.getParameterTypes().length != 1) {
throw new IllegalStateException("Persistence annotation requires a single-arg method: " + method);
}
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
currElements.add(new PersistenceElement(method, pd));
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
currElements.add(new PersistenceElement(method, bridgedMethod, pd));
}
}
elements.addAll(0, currElements);
@@ -621,9 +624,8 @@ public class PersistenceAnnotationBeanPostProcessor
private Properties properties;
public PersistenceElement(Member member, PropertyDescriptor pd) {
public PersistenceElement(Member member, AnnotatedElement ae, PropertyDescriptor pd) {
super(member, pd);
AnnotatedElement ae = (AnnotatedElement) member;
PersistenceContext pc = ae.getAnnotation(PersistenceContext.class);
PersistenceUnit pu = ae.getAnnotation(PersistenceUnit.class);
Class<?> resourceType = EntityManager.class;