Skip to content

Commit f4219ca

Browse files
committed
Consistent exclusion of bridge methods in annotation post-processors (for Java 8 compatibility)
Issue: SPR-12187
1 parent dc57cb2 commit f4219ca

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
392392
}
393393
for (Method method : targetClass.getDeclaredMethods()) {
394394
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
395-
AnnotationAttributes annotation = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod) ?
395+
AnnotationAttributes ann = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod) ?
396396
findAutowiredAnnotation(bridgedMethod) : findAutowiredAnnotation(method);
397-
if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
397+
if (ann != null && !method.isBridge() && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
398398
if (Modifier.isStatic(method.getModifiers())) {
399399
if (logger.isWarnEnabled()) {
400400
logger.warn("Autowired annotation is not supported on static methods: " + method);
@@ -406,7 +406,7 @@ private InjectionMetadata buildAutowiringMetadata(Class<?> clazz) {
406406
logger.warn("Autowired annotation should be used on methods with actual parameters: " + method);
407407
}
408408
}
409-
boolean required = determineRequiredStatus(annotation);
409+
boolean required = determineRequiredStatus(ann);
410410
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
411411
currElements.add(new AutowiredMethodElement(method, required, pd));
412412
}

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.lang.reflect.Proxy;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.concurrent.Callable;
2930

3031
import org.junit.Ignore;
3132
import org.junit.Test;
@@ -1756,6 +1757,18 @@ public void testCircularTypeReference() {
17561757
assertSame(bf.getBean(StockMovementDaoImpl.class), service.stockMovementDao);
17571758
}
17581759

1760+
@Test
1761+
public void testBridgeMethodHandling() {
1762+
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
1763+
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
1764+
bpp.setBeanFactory(bf);
1765+
bf.addBeanPostProcessor(bpp);
1766+
bf.registerBeanDefinition("bean1", new RootBeanDefinition(MyCallable.class));
1767+
bf.registerBeanDefinition("bean2", new RootBeanDefinition(SecondCallable.class));
1768+
bf.registerBeanDefinition("bean3", new RootBeanDefinition(FooBar.class));
1769+
assertNotNull(bf.getBean(FooBar.class));
1770+
}
1771+
17591772

17601773
public static class ResourceInjectionBean {
17611774

@@ -2755,4 +2768,45 @@ public static class StockServiceImpl {
27552768
private StockMovementDao<StockMovement> stockMovementDao;
27562769
}
27572770

2771+
2772+
public static class MyCallable implements Callable<Thread> {
2773+
2774+
@Override
2775+
public Thread call() throws Exception {
2776+
return null;
2777+
}
2778+
}
2779+
2780+
2781+
public static class SecondCallable implements Callable<Thread>{
2782+
2783+
@Override
2784+
public Thread call() throws Exception {
2785+
return null;
2786+
}
2787+
}
2788+
2789+
2790+
public static abstract class Foo<T extends Runnable, RT extends Callable<T>> {
2791+
2792+
private RT obj;
2793+
2794+
protected void setObj(RT obj) {
2795+
if (this.obj != null) {
2796+
throw new IllegalStateException("Already called");
2797+
}
2798+
this.obj = obj;
2799+
}
2800+
}
2801+
2802+
2803+
public static class FooBar extends Foo<Thread, MyCallable> {
2804+
2805+
@Override
2806+
@Autowired
2807+
public void setObj(MyCallable obj) {
2808+
super.setObj(obj);
2809+
}
2810+
}
2811+
27582812
}

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.springframework.beans.factory.config.DependencyDescriptor;
5959
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
6060
import org.springframework.beans.factory.support.RootBeanDefinition;
61-
import org.springframework.core.BridgeMethodResolver;
6261
import org.springframework.core.MethodParameter;
6362
import org.springframework.core.Ordered;
6463
import org.springframework.jndi.support.SimpleJndiBeanFactory;
@@ -348,9 +347,7 @@ else if (field.isAnnotationPresent(Resource.class)) {
348347
}
349348
}
350349
for (Method method : targetClass.getDeclaredMethods()) {
351-
method = BridgeMethodResolver.findBridgedMethod(method);
352-
Method mostSpecificMethod = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(method, clazz));
353-
if (method.equals(mostSpecificMethod)) {
350+
if (!method.isBridge() && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
354351
if (webServiceRefClass != null && method.isAnnotationPresent(webServiceRefClass)) {
355352
if (Modifier.isStatic(method.getModifiers())) {
356353
throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods");

spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?
402402
for (Method method : targetClass.getDeclaredMethods()) {
403403
PersistenceContext pc = method.getAnnotation(PersistenceContext.class);
404404
PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class);
405-
if ((pc != null || pu != null) &&
405+
if ((pc != null || pu != null) && !method.isBridge() &&
406406
method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
407407
if (Modifier.isStatic(method.getModifiers())) {
408408
throw new IllegalStateException("Persistence annotations are not supported on static methods");

0 commit comments

Comments
 (0)