Consistent exclusion of bridge methods in annotation post-processors (for Java 8 compatibility)
Issue: SPR-12187
This commit is contained in:
parent
dc57cb2c9f
commit
f4219ca06b
|
|
@ -392,9 +392,9 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
}
|
}
|
||||||
for (Method method : targetClass.getDeclaredMethods()) {
|
for (Method method : targetClass.getDeclaredMethods()) {
|
||||||
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
|
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
|
||||||
AnnotationAttributes annotation = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod) ?
|
AnnotationAttributes ann = BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod) ?
|
||||||
findAutowiredAnnotation(bridgedMethod) : findAutowiredAnnotation(method);
|
findAutowiredAnnotation(bridgedMethod) : findAutowiredAnnotation(method);
|
||||||
if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
|
if (ann != null && !method.isBridge() && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
|
||||||
if (Modifier.isStatic(method.getModifiers())) {
|
if (Modifier.isStatic(method.getModifiers())) {
|
||||||
if (logger.isWarnEnabled()) {
|
if (logger.isWarnEnabled()) {
|
||||||
logger.warn("Autowired annotation is not supported on static methods: " + method);
|
logger.warn("Autowired annotation is not supported on static methods: " + method);
|
||||||
|
|
@ -406,7 +406,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
logger.warn("Autowired annotation should be used on methods with actual parameters: " + method);
|
logger.warn("Autowired annotation should be used on methods with actual parameters: " + method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
boolean required = determineRequiredStatus(annotation);
|
boolean required = determineRequiredStatus(ann);
|
||||||
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
|
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method);
|
||||||
currElements.add(new AutowiredMethodElement(method, required, pd));
|
currElements.add(new AutowiredMethodElement(method, required, pd));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -1756,6 +1757,18 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||||
assertSame(bf.getBean(StockMovementDaoImpl.class), service.stockMovementDao);
|
assertSame(bf.getBean(StockMovementDaoImpl.class), service.stockMovementDao);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBridgeMethodHandling() {
|
||||||
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||||
|
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
|
||||||
|
bpp.setBeanFactory(bf);
|
||||||
|
bf.addBeanPostProcessor(bpp);
|
||||||
|
bf.registerBeanDefinition("bean1", new RootBeanDefinition(MyCallable.class));
|
||||||
|
bf.registerBeanDefinition("bean2", new RootBeanDefinition(SecondCallable.class));
|
||||||
|
bf.registerBeanDefinition("bean3", new RootBeanDefinition(FooBar.class));
|
||||||
|
assertNotNull(bf.getBean(FooBar.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class ResourceInjectionBean {
|
public static class ResourceInjectionBean {
|
||||||
|
|
||||||
|
|
@ -2755,4 +2768,45 @@ public class AutowiredAnnotationBeanPostProcessorTests {
|
||||||
private StockMovementDao<StockMovement> stockMovementDao;
|
private StockMovementDao<StockMovement> stockMovementDao;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class MyCallable implements Callable<Thread> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread call() throws Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class SecondCallable implements Callable<Thread>{
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Thread call() throws Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static abstract class Foo<T extends Runnable, RT extends Callable<T>> {
|
||||||
|
|
||||||
|
private RT obj;
|
||||||
|
|
||||||
|
protected void setObj(RT obj) {
|
||||||
|
if (this.obj != null) {
|
||||||
|
throw new IllegalStateException("Already called");
|
||||||
|
}
|
||||||
|
this.obj = obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class FooBar extends Foo<Thread, MyCallable> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Autowired
|
||||||
|
public void setObj(MyCallable obj) {
|
||||||
|
super.setObj(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,6 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.core.BridgeMethodResolver;
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.jndi.support.SimpleJndiBeanFactory;
|
import org.springframework.jndi.support.SimpleJndiBeanFactory;
|
||||||
|
|
@ -348,9 +347,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Method method : targetClass.getDeclaredMethods()) {
|
for (Method method : targetClass.getDeclaredMethods()) {
|
||||||
method = BridgeMethodResolver.findBridgedMethod(method);
|
if (!method.isBridge() && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
|
||||||
Method mostSpecificMethod = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(method, clazz));
|
|
||||||
if (method.equals(mostSpecificMethod)) {
|
|
||||||
if (webServiceRefClass != null && method.isAnnotationPresent(webServiceRefClass)) {
|
if (webServiceRefClass != null && method.isAnnotationPresent(webServiceRefClass)) {
|
||||||
if (Modifier.isStatic(method.getModifiers())) {
|
if (Modifier.isStatic(method.getModifiers())) {
|
||||||
throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods");
|
throw new IllegalStateException("@WebServiceRef annotation is not supported on static methods");
|
||||||
|
|
|
||||||
|
|
@ -402,7 +402,7 @@ public class PersistenceAnnotationBeanPostProcessor
|
||||||
for (Method method : targetClass.getDeclaredMethods()) {
|
for (Method method : targetClass.getDeclaredMethods()) {
|
||||||
PersistenceContext pc = method.getAnnotation(PersistenceContext.class);
|
PersistenceContext pc = method.getAnnotation(PersistenceContext.class);
|
||||||
PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class);
|
PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class);
|
||||||
if ((pc != null || pu != null) &&
|
if ((pc != null || pu != null) && !method.isBridge() &&
|
||||||
method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
|
method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
|
||||||
if (Modifier.isStatic(method.getModifiers())) {
|
if (Modifier.isStatic(method.getModifiers())) {
|
||||||
throw new IllegalStateException("Persistence annotations are not supported on static methods");
|
throw new IllegalStateException("Persistence annotations are not supported on static methods");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue