Java 5 code style
This commit is contained in:
@@ -60,6 +60,32 @@ public abstract class BeanUtils {
|
||||
Collections.synchronizedMap(new WeakHashMap<Class, Boolean>());
|
||||
|
||||
|
||||
/**
|
||||
* Convenience method to instantiate a class using its no-arg constructor.
|
||||
* As this method doesn't try to load classes by name, it should avoid
|
||||
* class-loading issues.
|
||||
* <p>Note that this method tries to set the constructor accessible
|
||||
* if given a non-accessible (that is, non-public) constructor.
|
||||
* @param clazz class to instantiate
|
||||
* @return the new instance
|
||||
* @throws BeanInstantiationException if the bean cannot be instantiated
|
||||
*/
|
||||
public static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException {
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
if (clazz.isInterface()) {
|
||||
throw new BeanInstantiationException(clazz, "Specified class is an interface");
|
||||
}
|
||||
try {
|
||||
return clazz.newInstance();
|
||||
}
|
||||
catch (InstantiationException ex) {
|
||||
throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to instantiate a class using its no-arg constructor.
|
||||
* As this method doesn't try to load classes by name, it should avoid
|
||||
@@ -106,7 +132,7 @@ public abstract class BeanUtils {
|
||||
}
|
||||
catch (IllegalAccessException ex) {
|
||||
throw new BeanInstantiationException(ctor.getDeclaringClass(),
|
||||
"Has the class definition changed? Is the constructor accessible?", ex);
|
||||
"Is the constructor accessible?", ex);
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
throw new BeanInstantiationException(ctor.getDeclaringClass(),
|
||||
|
||||
@@ -100,7 +100,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
|
||||
/**
|
||||
* Map with cached nested BeanWrappers: nested path -> BeanWrapper instance.
|
||||
*/
|
||||
private Map nestedBeanWrappers;
|
||||
private Map<String, BeanWrapperImpl> nestedBeanWrappers;
|
||||
|
||||
|
||||
/**
|
||||
@@ -431,7 +431,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
|
||||
*/
|
||||
private BeanWrapperImpl getNestedBeanWrapper(String nestedProperty) {
|
||||
if (this.nestedBeanWrappers == null) {
|
||||
this.nestedBeanWrappers = new HashMap();
|
||||
this.nestedBeanWrappers = new HashMap<String, BeanWrapperImpl>();
|
||||
}
|
||||
// Get value of bean property.
|
||||
PropertyTokenHolder tokens = getPropertyNameTokens(nestedProperty);
|
||||
@@ -442,7 +442,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
|
||||
}
|
||||
|
||||
// Lookup cached sub-BeanWrapper, create new one if not found.
|
||||
BeanWrapperImpl nestedBw = (BeanWrapperImpl) this.nestedBeanWrappers.get(canonicalName);
|
||||
BeanWrapperImpl nestedBw = this.nestedBeanWrappers.get(canonicalName);
|
||||
if (nestedBw == null || nestedBw.getWrappedInstance() != propertyValue) {
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Creating new nested BeanWrapper for property '" + canonicalName + "'");
|
||||
@@ -805,7 +805,7 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
|
||||
readMethod.setAccessible(true);
|
||||
}
|
||||
try {
|
||||
oldValue = readMethod.invoke(this.object, new Object[0]);
|
||||
oldValue = readMethod.invoke(this.object);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
@@ -816,13 +816,13 @@ public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWra
|
||||
}
|
||||
valueToApply = this.typeConverterDelegate.convertIfNecessary(oldValue, originalValue, pd);
|
||||
}
|
||||
pv.getOriginalPropertyValue().conversionNecessary = Boolean.valueOf(valueToApply != originalValue);
|
||||
pv.getOriginalPropertyValue().conversionNecessary = (valueToApply != originalValue);
|
||||
}
|
||||
Method writeMethod = pd.getWriteMethod();
|
||||
if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
|
||||
writeMethod.setAccessible(true);
|
||||
}
|
||||
writeMethod.invoke(this.object, new Object[] {valueToApply});
|
||||
writeMethod.invoke(this.object, valueToApply);
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
PropertyChangeEvent propertyChangeEvent =
|
||||
|
||||
@@ -46,7 +46,7 @@ public class DirectFieldAccessor extends AbstractPropertyAccessor {
|
||||
|
||||
private final Object target;
|
||||
|
||||
private final Map fieldMap = new HashMap();
|
||||
private final Map<String, Field> fieldMap = new HashMap<String, Field>();
|
||||
|
||||
private final TypeConverterDelegate typeConverterDelegate;
|
||||
|
||||
@@ -79,7 +79,7 @@ public class DirectFieldAccessor extends AbstractPropertyAccessor {
|
||||
|
||||
@Override
|
||||
public Class getPropertyType(String propertyName) throws BeansException {
|
||||
Field field = (Field) this.fieldMap.get(propertyName);
|
||||
Field field = this.fieldMap.get(propertyName);
|
||||
if (field != null) {
|
||||
return field.getType();
|
||||
}
|
||||
@@ -88,7 +88,7 @@ public class DirectFieldAccessor extends AbstractPropertyAccessor {
|
||||
|
||||
@Override
|
||||
public Object getPropertyValue(String propertyName) throws BeansException {
|
||||
Field field = (Field) this.fieldMap.get(propertyName);
|
||||
Field field = this.fieldMap.get(propertyName);
|
||||
if (field == null) {
|
||||
throw new NotReadablePropertyException(
|
||||
this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist");
|
||||
@@ -104,7 +104,7 @@ public class DirectFieldAccessor extends AbstractPropertyAccessor {
|
||||
|
||||
@Override
|
||||
public void setPropertyValue(String propertyName, Object newValue) throws BeansException {
|
||||
Field field = (Field) this.fieldMap.get(propertyName);
|
||||
Field field = this.fieldMap.get(propertyName);
|
||||
if (field == null) {
|
||||
throw new NotWritablePropertyException(
|
||||
this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist");
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.beans.factory;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
@@ -210,4 +211,42 @@ $ * <p>Does not consider any hierarchy this factory may participate in.
|
||||
<T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
|
||||
throws BeansException;
|
||||
|
||||
/**
|
||||
* Find all beans whose <code>Class</code> has the supplied {@link java.lang.annotation.Annotation} type.
|
||||
* @param annotationType the type of annotation to look for
|
||||
* @return a Map with the matching beans, containing the bean names as
|
||||
* keys and the corresponding bean instances as values
|
||||
* @throws BeansException if a bean could not be created
|
||||
*/
|
||||
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
|
||||
throws BeansException;
|
||||
|
||||
/**
|
||||
* Find all beans whose <code>Class</code> has the supplied {@link java.lang.annotation.Annotation} type.
|
||||
* @param annotationType the type of annotation to look for
|
||||
* @return a Map with the matching beans, containing the bean names as
|
||||
* keys and the corresponding bean instances as values
|
||||
* @param includeNonSingletons whether to include prototype or scoped beans too
|
||||
* or just singletons (also applies to FactoryBeans)
|
||||
* @param allowEagerInit whether to initialize <i>lazy-init singletons</i> and
|
||||
* <i>objects created by FactoryBeans</i> (or by factory methods with a
|
||||
* "factory-bean" reference) for the type check. Note that FactoryBeans need to be
|
||||
* eagerly initialized to determine their type: So be aware that passing in "true"
|
||||
* for this flag will initialize FactoryBeans and "factory-bean" references.
|
||||
* @throws BeansException if a bean could not be created
|
||||
*/
|
||||
Map<String, Object> getBeansWithAnnotation(
|
||||
Class<? extends Annotation> annotationType, boolean includeNonSingletons, boolean allowEagerInit)
|
||||
throws BeansException;
|
||||
|
||||
/**
|
||||
* Find a {@link Annotation} of <code>annotationType</code> on the specified
|
||||
* bean, traversing its interfaces and super classes if no annotation can be
|
||||
* found on the given class itself.
|
||||
* @param beanName the name of the bean to look for annotations on
|
||||
* @param annotationType the annotation class to look for
|
||||
* @return the annotation of the given type found, or <code>null</code>
|
||||
*/
|
||||
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType);
|
||||
|
||||
}
|
||||
|
||||
@@ -277,7 +277,7 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
protected static final Log logger = LogFactory.getLog(SingletonBeanFactoryLocator.class);
|
||||
|
||||
/** The keyed BeanFactory instances */
|
||||
private static Map instances = new HashMap();
|
||||
private static final Map<String, BeanFactoryLocator> instances = new HashMap<String, BeanFactoryLocator>();
|
||||
|
||||
|
||||
/**
|
||||
@@ -324,7 +324,7 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
logger.trace("SingletonBeanFactoryLocator.getInstance(): instances.hashCode=" +
|
||||
instances.hashCode() + ", instances=" + instances);
|
||||
}
|
||||
BeanFactoryLocator bfl = (BeanFactoryLocator) instances.get(resourceLocation);
|
||||
BeanFactoryLocator bfl = instances.get(resourceLocation);
|
||||
if (bfl == null) {
|
||||
bfl = new SingletonBeanFactoryLocator(resourceLocation);
|
||||
instances.put(resourceLocation, bfl);
|
||||
@@ -335,9 +335,9 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
|
||||
|
||||
// We map BeanFactoryGroup objects by String keys, and by the definition object.
|
||||
private final Map bfgInstancesByKey = new HashMap();
|
||||
private final Map<String, BeanFactoryGroup> bfgInstancesByKey = new HashMap<String, BeanFactoryGroup>();
|
||||
|
||||
private final Map bfgInstancesByObj = new HashMap();
|
||||
private final Map<BeanFactory, BeanFactoryGroup> bfgInstancesByObj = new HashMap<BeanFactory, BeanFactoryGroup>();
|
||||
|
||||
private final String resourceLocation;
|
||||
|
||||
@@ -354,7 +354,7 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
|
||||
public BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException {
|
||||
synchronized (this.bfgInstancesByKey) {
|
||||
BeanFactoryGroup bfg = (BeanFactoryGroup) this.bfgInstancesByKey.get(this.resourceLocation);
|
||||
BeanFactoryGroup bfg = this.bfgInstancesByKey.get(this.resourceLocation);
|
||||
|
||||
if (bfg != null) {
|
||||
bfg.refCount++;
|
||||
@@ -394,11 +394,10 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
try {
|
||||
BeanFactory beanFactory = null;
|
||||
if (factoryKey != null) {
|
||||
beanFactory = (BeanFactory) bfg.definition.getBean(factoryKey, BeanFactory.class);
|
||||
beanFactory = bfg.definition.getBean(factoryKey, BeanFactory.class);
|
||||
}
|
||||
else if (bfg.definition instanceof ListableBeanFactory) {
|
||||
beanFactory = (BeanFactory)
|
||||
BeanFactoryUtils.beanOfType((ListableBeanFactory) bfg.definition, BeanFactory.class);
|
||||
beanFactory = BeanFactoryUtils.beanOfType((ListableBeanFactory) bfg.definition, BeanFactory.class);
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException(
|
||||
@@ -518,7 +517,7 @@ public class SingletonBeanFactoryLocator implements BeanFactoryLocator {
|
||||
BeanFactory savedRef = this.groupContextRef;
|
||||
if (savedRef != null) {
|
||||
this.groupContextRef = null;
|
||||
BeanFactoryGroup bfg = (BeanFactoryGroup) bfgInstancesByObj.get(savedRef);
|
||||
BeanFactoryGroup bfg = bfgInstancesByObj.get(savedRef);
|
||||
if (bfg != null) {
|
||||
bfg.refCount--;
|
||||
if (bfg.refCount == 0) {
|
||||
|
||||
@@ -57,8 +57,8 @@ import org.springframework.util.ReflectionUtils;
|
||||
* @see #setSingleton
|
||||
* @see #createInstance()
|
||||
*/
|
||||
public abstract class AbstractFactoryBean
|
||||
implements FactoryBean, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
|
||||
public abstract class AbstractFactoryBean<T>
|
||||
implements FactoryBean<T>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean {
|
||||
|
||||
/** Logger available to subclasses */
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
@@ -71,9 +71,9 @@ public abstract class AbstractFactoryBean
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
private Object singletonInstance;
|
||||
private T singletonInstance;
|
||||
|
||||
private Object earlySingletonInstance;
|
||||
private T earlySingletonInstance;
|
||||
|
||||
|
||||
/**
|
||||
@@ -138,7 +138,7 @@ public abstract class AbstractFactoryBean
|
||||
* @see #createInstance()
|
||||
* @see #getEarlySingletonInterfaces()
|
||||
*/
|
||||
public final Object getObject() throws Exception {
|
||||
public final T getObject() throws Exception {
|
||||
if (isSingleton()) {
|
||||
return (this.initialized ? this.singletonInstance : getEarlySingletonInstance());
|
||||
}
|
||||
@@ -151,14 +151,15 @@ public abstract class AbstractFactoryBean
|
||||
* Determine an 'eager singleton' instance, exposed in case of a
|
||||
* circular reference. Not called in a non-circular scenario.
|
||||
*/
|
||||
private Object getEarlySingletonInstance() throws Exception {
|
||||
@SuppressWarnings("unchecked")
|
||||
private T getEarlySingletonInstance() throws Exception {
|
||||
Class[] ifcs = getEarlySingletonInterfaces();
|
||||
if (ifcs == null) {
|
||||
throw new FactoryBeanNotInitializedException(
|
||||
getClass().getName() + " does not support circular references");
|
||||
}
|
||||
if (this.earlySingletonInstance == null) {
|
||||
this.earlySingletonInstance = Proxy.newProxyInstance(
|
||||
this.earlySingletonInstance = (T) Proxy.newProxyInstance(
|
||||
this.beanClassLoader, ifcs, new EarlySingletonInvocationHandler());
|
||||
}
|
||||
return this.earlySingletonInstance;
|
||||
@@ -169,7 +170,7 @@ public abstract class AbstractFactoryBean
|
||||
* @return the singleton instance that this FactoryBean holds
|
||||
* @throws IllegalStateException if the singleton instance is not initialized
|
||||
*/
|
||||
private Object getSingletonInstance() throws IllegalStateException {
|
||||
private T getSingletonInstance() throws IllegalStateException {
|
||||
if (!this.initialized) {
|
||||
throw new IllegalStateException("Singleton instance not initialized yet");
|
||||
}
|
||||
@@ -192,7 +193,7 @@ public abstract class AbstractFactoryBean
|
||||
* interface, for a consistent offering of abstract template methods.
|
||||
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
|
||||
*/
|
||||
public abstract Class getObjectType();
|
||||
public abstract Class<? extends T> getObjectType();
|
||||
|
||||
/**
|
||||
* Template method that subclasses must override to construct
|
||||
@@ -203,7 +204,7 @@ public abstract class AbstractFactoryBean
|
||||
* @throws Exception if an exception occured during object creation
|
||||
* @see #getObject()
|
||||
*/
|
||||
protected abstract Object createInstance() throws Exception;
|
||||
protected abstract T createInstance() throws Exception;
|
||||
|
||||
/**
|
||||
* Return an array of interfaces that a singleton object exposed by this
|
||||
@@ -231,7 +232,7 @@ public abstract class AbstractFactoryBean
|
||||
* @throws Exception in case of shutdown errors
|
||||
* @see #createInstance()
|
||||
*/
|
||||
protected void destroyInstance(Object instance) throws Exception {
|
||||
protected void destroyInstance(T instance) throws Exception {
|
||||
}
|
||||
|
||||
|
||||
@@ -240,11 +241,11 @@ public abstract class AbstractFactoryBean
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
if (ReflectionUtils.isEqualsMethod(method)) {
|
||||
// Only consider equal when proxies are identical.
|
||||
return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
|
||||
return (proxy == args[0]);
|
||||
}
|
||||
else if (ReflectionUtils.isHashCodeMethod(method)) {
|
||||
// Use hashCode of reference proxy.
|
||||
return new Integer(System.identityHashCode(proxy));
|
||||
return System.identityHashCode(proxy);
|
||||
}
|
||||
else if (!initialized && ReflectionUtils.isToStringMethod(method)) {
|
||||
return "Early singleton proxy for interfaces " +
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.core.GenericCollectionTypeResolver;
|
||||
* @see SetFactoryBean
|
||||
* @see MapFactoryBean
|
||||
*/
|
||||
public class ListFactoryBean extends AbstractFactoryBean {
|
||||
public class ListFactoryBean extends AbstractFactoryBean<List> {
|
||||
|
||||
private List sourceList;
|
||||
|
||||
@@ -64,13 +64,13 @@ public class ListFactoryBean extends AbstractFactoryBean {
|
||||
|
||||
|
||||
@Override
|
||||
public Class getObjectType() {
|
||||
public Class<List> getObjectType() {
|
||||
return List.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object createInstance() {
|
||||
protected List createInstance() {
|
||||
if (this.sourceList == null) {
|
||||
throw new IllegalArgumentException("'sourceList' is required");
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.core.GenericCollectionTypeResolver;
|
||||
* @see SetFactoryBean
|
||||
* @see ListFactoryBean
|
||||
*/
|
||||
public class MapFactoryBean extends AbstractFactoryBean {
|
||||
public class MapFactoryBean extends AbstractFactoryBean<Map> {
|
||||
|
||||
private Map<?, ?> sourceMap;
|
||||
|
||||
@@ -64,13 +64,13 @@ public class MapFactoryBean extends AbstractFactoryBean {
|
||||
|
||||
|
||||
@Override
|
||||
public Class getObjectType() {
|
||||
public Class<Map> getObjectType() {
|
||||
return Map.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object createInstance() {
|
||||
protected Map createInstance() {
|
||||
if (this.sourceMap == null) {
|
||||
throw new IllegalArgumentException("'sourceMap' is required");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -43,11 +43,11 @@ import org.springframework.core.io.support.PropertiesLoaderSupport;
|
||||
* @see java.util.Properties
|
||||
*/
|
||||
public class PropertiesFactoryBean extends PropertiesLoaderSupport
|
||||
implements FactoryBean, InitializingBean {
|
||||
implements FactoryBean<Properties>, InitializingBean {
|
||||
|
||||
private boolean singleton = true;
|
||||
|
||||
private Object singletonInstance;
|
||||
private Properties singletonInstance;
|
||||
|
||||
|
||||
/**
|
||||
@@ -66,36 +66,21 @@ public class PropertiesFactoryBean extends PropertiesLoaderSupport
|
||||
|
||||
public final void afterPropertiesSet() throws IOException {
|
||||
if (this.singleton) {
|
||||
this.singletonInstance = createInstance();
|
||||
this.singletonInstance = mergeProperties();
|
||||
}
|
||||
}
|
||||
|
||||
public final Object getObject() throws IOException {
|
||||
public final Properties getObject() throws IOException {
|
||||
if (this.singleton) {
|
||||
return this.singletonInstance;
|
||||
}
|
||||
else {
|
||||
return createInstance();
|
||||
return mergeProperties();
|
||||
}
|
||||
}
|
||||
|
||||
public Class getObjectType() {
|
||||
public Class<Properties> getObjectType() {
|
||||
return Properties.class;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Template method that subclasses may override to construct the object
|
||||
* returned by this factory. The default implementation returns the
|
||||
* plain merged Properties instance.
|
||||
* <p>Invoked on initialization of this FactoryBean in case of a
|
||||
* shared singleton; else, on each {@link #getObject()} call.
|
||||
* @return the object returned by this factory
|
||||
* @throws IOException if an exception occured during properties loading
|
||||
* @see #mergeProperties()
|
||||
*/
|
||||
protected Object createInstance() throws IOException {
|
||||
return mergeProperties();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.core.GenericCollectionTypeResolver;
|
||||
* @see ListFactoryBean
|
||||
* @see MapFactoryBean
|
||||
*/
|
||||
public class SetFactoryBean extends AbstractFactoryBean {
|
||||
public class SetFactoryBean extends AbstractFactoryBean<Set> {
|
||||
|
||||
private Set sourceSet;
|
||||
|
||||
@@ -64,13 +64,13 @@ public class SetFactoryBean extends AbstractFactoryBean {
|
||||
|
||||
|
||||
@Override
|
||||
public Class getObjectType() {
|
||||
public Class<Set> getObjectType() {
|
||||
return Set.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object createInstance() {
|
||||
protected Set createInstance() {
|
||||
if (this.sourceSet == null) {
|
||||
throw new IllegalArgumentException("'sourceSet' is required");
|
||||
}
|
||||
|
||||
@@ -1,140 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.beans.factory.generic;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple wrapper around a {@link ListableBeanFactory} that provides typed, generics-based
|
||||
* access to key methods. This removes the need for casting in many cases and should
|
||||
* increase compile-time type safety.
|
||||
*
|
||||
* <p>Provides a simple mechanism for accessing all beans with a particular {@link Annotation}.
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.0
|
||||
*/
|
||||
public class GenericBeanFactoryAccessor {
|
||||
|
||||
/**
|
||||
* The {@link ListableBeanFactory} being wrapped.
|
||||
*/
|
||||
private final ListableBeanFactory beanFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a <code>GenericBeanFactoryAccessor</code> that wraps the supplied {@link ListableBeanFactory}.
|
||||
*/
|
||||
public GenericBeanFactoryAccessor(ListableBeanFactory beanFactory) {
|
||||
Assert.notNull(beanFactory, "Bean factory must not be null");
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the wrapped {@link ListableBeanFactory}.
|
||||
*/
|
||||
public final ListableBeanFactory getBeanFactory() {
|
||||
return this.beanFactory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.BeanFactory#getBean(String)
|
||||
*/
|
||||
public <T> T getBean(String name) throws BeansException {
|
||||
return (T) this.beanFactory.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.beans.factory.BeanFactory#getBean(String, Class)
|
||||
*/
|
||||
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
|
||||
return (T) this.beanFactory.getBean(name, requiredType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory#getBeansOfType(Class)
|
||||
*/
|
||||
public <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
|
||||
return this.beanFactory.getBeansOfType(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ListableBeanFactory#getBeansOfType(Class, boolean, boolean)
|
||||
*/
|
||||
public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
|
||||
throws BeansException {
|
||||
|
||||
return this.beanFactory.getBeansOfType(type, includeNonSingletons, allowEagerInit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all beans whose <code>Class</code> has the supplied {@link Annotation} type.
|
||||
* @param annotationType the type of annotation to look for
|
||||
* @return a Map with the matching beans, containing the bean names as
|
||||
* keys and the corresponding bean instances as values
|
||||
*/
|
||||
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
|
||||
Map<String, Object> results = new LinkedHashMap<String, Object>();
|
||||
for (String beanName : this.beanFactory.getBeanNamesForType(Object.class)) {
|
||||
if (findAnnotationOnBean(beanName, annotationType) != null) {
|
||||
results.put(beanName, this.beanFactory.getBean(beanName));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a {@link Annotation} of <code>annotationType</code> on the specified
|
||||
* bean, traversing its interfaces and super classes if no annotation can be
|
||||
* found on the given class itself, as well as checking its raw bean class
|
||||
* if not found on the exposed bean reference (e.g. in case of a proxy).
|
||||
* @param beanName the name of the bean to look for annotations on
|
||||
* @param annotationType the annotation class to look for
|
||||
* @return the annotation of the given type found, or <code>null</code>
|
||||
* @see org.springframework.core.annotation.AnnotationUtils#findAnnotation(Class, Class)
|
||||
*/
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
|
||||
Class<?> handlerType = this.beanFactory.getType(beanName);
|
||||
A ann = AnnotationUtils.findAnnotation(handlerType, annotationType);
|
||||
if (ann == null && this.beanFactory instanceof ConfigurableBeanFactory &&
|
||||
this.beanFactory.containsBeanDefinition(beanName)) {
|
||||
ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) this.beanFactory;
|
||||
BeanDefinition bd = cbf.getMergedBeanDefinition(beanName);
|
||||
if (bd instanceof AbstractBeanDefinition) {
|
||||
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
|
||||
if (abd.hasBeanClass()) {
|
||||
Class<?> beanClass = abd.getBeanClass();
|
||||
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ann;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
<html>
|
||||
<body>
|
||||
|
||||
Support package for generic BeanFactory access,
|
||||
leveraging Java 5 generics in the accessor API.
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -167,15 +167,15 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
public Object getBean(String name) throws BeansException {
|
||||
return getBean(name, null, null);
|
||||
return doGetBean(name, null, null, false);
|
||||
}
|
||||
|
||||
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
|
||||
return getBean(name, requiredType, null);
|
||||
return doGetBean(name, requiredType, null, false);
|
||||
}
|
||||
|
||||
public Object getBean(String name, Object[] args) throws BeansException {
|
||||
return getBean(name, null, args);
|
||||
return doGetBean(name, null, args, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,6 +202,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||
* @return an instance of the bean
|
||||
* @throws BeansException if the bean could not be created
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <T> T doGetBean(
|
||||
final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
|
||||
throws BeansException {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
@@ -43,6 +44,7 @@ import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -327,6 +329,44 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
|
||||
return getBeansWithAnnotation(annotationType, true, true);
|
||||
}
|
||||
|
||||
public Map<String, Object> getBeansWithAnnotation(
|
||||
Class<? extends Annotation> annotationType, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
|
||||
Map<String, Object> results = new LinkedHashMap<String, Object>();
|
||||
for (String beanName : getBeanNamesForType(Object.class, includeNonSingletons, allowEagerInit)) {
|
||||
if (findAnnotationOnBean(beanName, annotationType) != null) {
|
||||
results.put(beanName, getBean(beanName));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a {@link Annotation} of <code>annotationType</code> on the specified
|
||||
* bean, traversing its interfaces and super classes if no annotation can be
|
||||
* found on the given class itself, as well as checking its raw bean class
|
||||
* if not found on the exposed bean reference (e.g. in case of a proxy).
|
||||
*/
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
|
||||
Class<?> handlerType = getType(beanName);
|
||||
A ann = AnnotationUtils.findAnnotation(handlerType, annotationType);
|
||||
if (ann == null && containsBeanDefinition(beanName)) {
|
||||
BeanDefinition bd = getMergedBeanDefinition(beanName);
|
||||
if (bd instanceof AbstractBeanDefinition) {
|
||||
AbstractBeanDefinition abd = (AbstractBeanDefinition) bd;
|
||||
if (abd.hasBeanClass()) {
|
||||
Class<?> beanClass = abd.getBeanClass();
|
||||
ann = AnnotationUtils.findAnnotation(beanClass, annotationType);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ann;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// Implementation of ConfigurableListableBeanFactory interface
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
|
||||
package org.springframework.beans.factory.support;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -30,6 +32,7 @@ import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.SmartFactoryBean;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -95,9 +98,12 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
|
||||
}
|
||||
}
|
||||
return bean;
|
||||
else {
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
|
||||
Object bean = getBean(name);
|
||||
if (requiredType != null && !requiredType.isAssignableFrom(bean.getClass())) {
|
||||
@@ -106,6 +112,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
return (T) bean;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Object getBean(String name, Object[] args) throws BeansException {
|
||||
if (args != null) {
|
||||
throw new UnsupportedOperationException(
|
||||
@@ -203,6 +210,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
return getBeansOfType(type, true, true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean includeFactoryBeans)
|
||||
throws BeansException {
|
||||
|
||||
@@ -238,4 +246,27 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
|
||||
return matches;
|
||||
}
|
||||
|
||||
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
|
||||
throws BeansException {
|
||||
|
||||
return getBeansWithAnnotation(annotationType, true, true);
|
||||
}
|
||||
|
||||
public Map<String, Object> getBeansWithAnnotation(
|
||||
Class<? extends Annotation> annotationType, boolean includeNonSingletons, boolean allowEagerInit) {
|
||||
|
||||
Map<String, Object> results = new LinkedHashMap<String, Object>();
|
||||
for (String beanName : this.beans.keySet()) {
|
||||
if (findAnnotationOnBean(beanName, annotationType) != null) {
|
||||
results.put(beanName, getBean(beanName));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
|
||||
Class<?> handlerType = getType(beanName);
|
||||
return AnnotationUtils.findAnnotation(handlerType, annotationType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.beans.FatalBeanException;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link NamespaceHandlerResolver} interface.
|
||||
@@ -63,7 +64,7 @@ public class DefaultNamespaceHandlerResolver implements NamespaceHandlerResolver
|
||||
private final String handlerMappingsLocation;
|
||||
|
||||
/** Stores the mappings from namespace URI to NamespaceHandler class name / instance */
|
||||
private Map handlerMappings;
|
||||
private Map<String, NamespaceHandler> handlerMappings;
|
||||
|
||||
|
||||
/**
|
||||
@@ -109,7 +110,7 @@ public class DefaultNamespaceHandlerResolver implements NamespaceHandlerResolver
|
||||
* @return the located {@link NamespaceHandler}, or <code>null</code> if none found
|
||||
*/
|
||||
public NamespaceHandler resolve(String namespaceUri) {
|
||||
Map handlerMappings = getHandlerMappings();
|
||||
Map<String, NamespaceHandler> handlerMappings = getHandlerMappings();
|
||||
Object handlerOrClassName = handlerMappings.get(namespaceUri);
|
||||
if (handlerOrClassName == null) {
|
||||
return null;
|
||||
@@ -144,7 +145,7 @@ public class DefaultNamespaceHandlerResolver implements NamespaceHandlerResolver
|
||||
/**
|
||||
* Load the specified NamespaceHandler mappings lazily.
|
||||
*/
|
||||
private Map getHandlerMappings() {
|
||||
private Map<String, NamespaceHandler> getHandlerMappings() {
|
||||
if (this.handlerMappings == null) {
|
||||
try {
|
||||
Properties mappings =
|
||||
@@ -152,7 +153,8 @@ public class DefaultNamespaceHandlerResolver implements NamespaceHandlerResolver
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Loaded mappings [" + mappings + "]");
|
||||
}
|
||||
this.handlerMappings = new HashMap(mappings);
|
||||
this.handlerMappings = new HashMap<String, NamespaceHandler>();
|
||||
CollectionUtils.mergePropertiesIntoMap(mappings, this.handlerMappings);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException(
|
||||
|
||||
@@ -136,9 +136,7 @@ public class ArgumentConvertingMethodInvoker extends MethodInvoker {
|
||||
Method[] candidates = ReflectionUtils.getAllDeclaredMethods(getTargetClass());
|
||||
int minTypeDiffWeight = Integer.MAX_VALUE;
|
||||
Object[] argumentsToUse = null;
|
||||
|
||||
for (int i = 0; i < candidates.length; i++) {
|
||||
Method candidate = candidates[i];
|
||||
for (Method candidate : candidates) {
|
||||
if (candidate.getName().equals(targetMethod)) {
|
||||
// Check if the inspected method has the correct number of parameters.
|
||||
Class[] paramTypes = candidate.getParameterTypes();
|
||||
@@ -166,13 +164,11 @@ public class ArgumentConvertingMethodInvoker extends MethodInvoker {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (matchingMethod != null) {
|
||||
setArguments(argumentsToUse);
|
||||
return matchingMethod;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user