ResolvableType.forRawClass as a straight wrapper for Class.isAssignableFrom

Issue: SPR-12846
This commit is contained in:
Juergen Hoeller
2015-03-24 19:20:15 +01:00
parent b2308926bc
commit 09027f7972
5 changed files with 238 additions and 137 deletions

View File

@@ -561,7 +561,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
@Override
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
return isTypeMatch(name, ResolvableType.forClass(typeToMatch != null ? typeToMatch : Object.class));
return isTypeMatch(name, ResolvableType.forRawClass(typeToMatch));
}
@Override

View File

@@ -412,8 +412,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
@Override
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
return doGetBeanNamesForType(ResolvableType.forClass(type != null ? type : Object.class),
includeNonSingletons, allowEagerInit);
return doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, allowEagerInit);
}
Map<Class<?>, String[]> cache =
(includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType);
@@ -421,7 +420,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (resolvedBeanNames != null) {
return resolvedBeanNames;
}
resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forClass(type), includeNonSingletons, allowEagerInit);
resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, true);
if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) {
cache.put(type, resolvedBeanNames);
}

View File

@@ -17,6 +17,7 @@
package org.springframework.beans.factory;
import java.io.Closeable;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.security.AccessControlContext;
@@ -32,6 +33,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;
import javax.annotation.Priority;
import javax.security.auth.Subject;
@@ -1669,13 +1671,21 @@ public class DefaultListableBeanFactoryTests {
@Test
public void testGetBeanNamesForTypeBeforeFactoryBeanCreation() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName()));
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
assertFalse(lbf.containsSingleton("factoryBean"));
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(Callable.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
@@ -1684,13 +1694,21 @@ public class DefaultListableBeanFactoryTests {
@Test
public void testGetBeanNamesForTypeAfterFactoryBeanCreation() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class.getName()));
lbf.registerBeanDefinition("factoryBean", new RootBeanDefinition(FactoryBeanThatShouldntBeCalled.class));
lbf.getBean("&factoryBean");
String[] beanNames = lbf.getBeanNamesForType(Runnable.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(Callable.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(RepositoryFactoryInformation.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
beanNames = lbf.getBeanNamesForType(FactoryBean.class, false, false);
assertEquals(1, beanNames.length);
assertEquals("&factoryBean", beanNames[0]);
@@ -2892,10 +2910,24 @@ public class DefaultListableBeanFactoryTests {
}
public static class FactoryBeanThatShouldntBeCalled implements FactoryBean<Object>, Runnable {
public interface Repository<T, ID extends Serializable> {
}
public interface RepositoryFactoryInformation<T, ID extends Serializable> {
}
public static abstract class RepositoryFactoryBeanSupport<T extends Repository<S, ID>, S, ID extends Serializable>
implements RepositoryFactoryInformation<S, ID>, FactoryBean<T> {
}
public static class FactoryBeanThatShouldntBeCalled<T extends Repository<S, ID>, S, ID extends Serializable>
extends RepositoryFactoryBeanSupport<T, S, ID> implements Runnable, Callable<T> {
@Override
public Object getObject() {
public T getObject() {
throw new IllegalStateException();
}
@@ -2913,6 +2945,11 @@ public class DefaultListableBeanFactoryTests {
public void run() {
throw new IllegalStateException();
}
@Override
public T call() throws Exception {
throw new IllegalStateException();
}
}