diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java index ebc3cbf53c3..0d7eb6ee3b9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 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. @@ -141,8 +141,7 @@ public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanP @Override public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) - throws BeansException { + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { if (!this.validatedBeanNames.contains(beanName)) { if (!shouldSkip(this.beanFactory, beanName)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java index f508e3fff11..0186221b588 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -96,14 +96,13 @@ public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { * dependency types - which the factory handles specifically - already filtered out) * @param bean the bean instance created, but whose properties have not yet been set * @param beanName the name of the bean - * @return the actual property values to apply to to the given bean + * @return the actual property values to apply to the given bean * (can be the passed-in PropertyValues instance), or {@code null} * to skip property population * @throws org.springframework.beans.BeansException in case of errors * @see org.springframework.beans.MutablePropertyValues */ PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) - throws BeansException; + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException; } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java index 3e02846fcb5..3f7e1746b70 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -66,8 +66,7 @@ public abstract class InstantiationAwareBeanPostProcessorAdapter implements Smar @Override public PropertyValues postProcessPropertyValues( - PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) - throws BeansException { + PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { return pvs; } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/Scope.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/Scope.java index 8777fa61e07..4e313c7919c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/Scope.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/Scope.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -68,6 +68,7 @@ public interface Scope { * @param objectFactory the {@link ObjectFactory} to use to create the scoped * object if it is not present in the underlying storage mechanism * @return the desired object (never {@code null}) + * @throws IllegalStateException if the underlying scope is not currently active */ Object get(String name, ObjectFactory objectFactory); @@ -84,6 +85,7 @@ public interface Scope { * removing an object. * @param name the name of the object to remove * @return the removed object, or {@code null} if no object was present + * @throws IllegalStateException if the underlying scope is not currently active * @see #registerDestructionCallback */ Object remove(String name); @@ -100,7 +102,7 @@ public interface Scope { * at the appropriate time. If such a callback is not supported by the * underlying runtime environment at all, the callback must be * ignored and a corresponding warning should be logged. - *

Note that 'destruction' refers to to automatic destruction of + *

Note that 'destruction' refers to automatic destruction of * the object as part of the scope's own lifecycle, not to the individual * scoped object having been explicitly removed by the application. * If a scoped object gets removed via this facade's {@link #remove(String)} @@ -112,6 +114,7 @@ public interface Scope { * so it can safely be executed without an enclosing try-catch block. * Furthermore, the Runnable will usually be serializable, provided * that its target object is serializable as well. + * @throws IllegalStateException if the underlying scope is not currently active * @see org.springframework.beans.factory.DisposableBean * @see org.springframework.beans.factory.support.AbstractBeanDefinition#getDestroyMethodName() * @see DestructionAwareBeanPostProcessor @@ -123,6 +126,7 @@ public interface Scope { * E.g. the HttpServletRequest object for key "request". * @param key the contextual key * @return the corresponding object, or {@code null} if none found + * @throws IllegalStateException if the underlying scope is not currently active */ Object resolveContextualObject(String key); @@ -139,6 +143,7 @@ public interface Scope { * underlying storage mechanism has no obvious candidate for such an ID. * @return the conversation ID, or {@code null} if there is no * conversation ID for the current scope + * @throws IllegalStateException if the underlying scope is not currently active */ String getConversationId(); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index f8bd3818830..1273d61f4c0 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -500,7 +500,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * @see #instantiateUsingFactoryMethod * @see #autowireConstructor */ - protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { + protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) + throws BeanCreationException { + // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { @@ -515,7 +517,13 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac // Allow post-processors to modify the merged bean definition. synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { - applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); + try { + applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); + } + catch (Throwable ex) { + throw new BeanCreationException(mbd.getResourceDescription(), beanName, + "Post-processing of merged bean definition failed", ex); + } mbd.postProcessed = true; } } @@ -550,7 +558,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac throw (BeanCreationException) ex; } else { - throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); + throw new BeanCreationException( + mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } @@ -586,7 +595,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { - throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); + throw new BeanCreationException( + mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; @@ -624,7 +634,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac protected Class determineTargetType(String beanName, RootBeanDefinition mbd, Class... typesToMatch) { Class targetType = mbd.getTargetType(); if (targetType == null) { - targetType = (mbd.getFactoryMethodName() != null ? getTypeForFactoryMethod(beanName, mbd, typesToMatch) : + targetType = (mbd.getFactoryMethodName() != null ? + getTypeForFactoryMethod(beanName, mbd, typesToMatch) : resolveBeanClass(mbd, beanName, typesToMatch)); if (ObjectUtils.isEmpty(typesToMatch) || getTempClassLoader() == null) { mbd.setTargetType(targetType); @@ -772,10 +783,11 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac ReflectionUtils.doWithMethods(fbClass, new ReflectionUtils.MethodCallback() { @Override - public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { + public void doWith(Method method) { if (method.getName().equals(factoryMethodName) && FactoryBean.class.isAssignableFrom(method.getReturnType())) { - objectType.value = GenericTypeResolver.resolveReturnTypeArgument(method, FactoryBean.class); + objectType.value = GenericTypeResolver.resolveReturnTypeArgument( + method, FactoryBean.class); } } }); @@ -926,24 +938,15 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * @param mbd the merged bean definition for the bean * @param beanType the actual type of the managed bean instance * @param beanName the name of the bean - * @throws BeansException if any post-processing failed * @see MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition */ - protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class beanType, String beanName) - throws BeansException { - - try { - for (BeanPostProcessor bp : getBeanPostProcessors()) { - if (bp instanceof MergedBeanDefinitionPostProcessor) { - MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; - bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); - } + protected void applyMergedBeanDefinitionPostProcessors(RootBeanDefinition mbd, Class beanType, String beanName) { + for (BeanPostProcessor bp : getBeanPostProcessors()) { + if (bp instanceof MergedBeanDefinitionPostProcessor) { + MergedBeanDefinitionPostProcessor bdp = (MergedBeanDefinitionPostProcessor) bp; + bdp.postProcessMergedBeanDefinition(mbd, beanType, beanName); } } - catch (Exception ex) { - throw new BeanCreationException(mbd.getResourceDescription(), beanName, - "Post-processing failed of bean type [" + beanType + "] failed", ex); - } } /** @@ -980,12 +983,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * @param beanClass the class of the bean to be instantiated * @param beanName the name of the bean * @return the bean object to use instead of a default instance of the target bean, or {@code null} - * @throws BeansException if any post-processing failed * @see InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation */ - protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass, String beanName) - throws BeansException { - + protected Object applyBeanPostProcessorsBeforeInstantiation(Class beanClass, String beanName) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; @@ -1106,7 +1106,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac return bw; } catch (Throwable ex) { - throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex); + throw new BeanCreationException( + mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex); } } @@ -1658,7 +1659,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * methods with arguments. * @see #invokeInitMethods */ - protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { + protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) + throws Throwable { + String initMethodName = mbd.getInitMethodName(); final Method initMethod = (mbd.isNonPublicAccessAllowed() ? BeanUtils.findMethod(bean.getClass(), initMethodName) : diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 80e91528cfa..cc6c6149394 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -287,13 +287,13 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp // Guarantee initialization of beans that the current bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { - for (String dependsOnBean : dependsOn) { - if (isDependent(beanName, dependsOnBean)) { + for (String dep : dependsOn) { + if (isDependent(beanName, dep)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, - "Circular depends-on relationship between '" + beanName + "' and '" + dependsOnBean + "'"); + "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } - registerDependentBean(dependsOnBean, beanName); - getBean(dependsOnBean); + registerDependentBean(dep, beanName); + getBean(dep); } } @@ -460,19 +460,19 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp return false; } if (isFactoryBean(beanName, mbd)) { - final FactoryBean factoryBean = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName); + final FactoryBean fb = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName); if (System.getSecurityManager() != null) { return AccessController.doPrivileged(new PrivilegedAction() { @Override public Boolean run() { - return ((factoryBean instanceof SmartFactoryBean && ((SmartFactoryBean) factoryBean).isPrototype()) || - !factoryBean.isSingleton()); + return ((fb instanceof SmartFactoryBean && ((SmartFactoryBean) fb).isPrototype()) || + !fb.isSingleton()); } }, getAccessControlContext()); } else { - return ((factoryBean instanceof SmartFactoryBean && ((SmartFactoryBean) factoryBean).isPrototype()) || - !factoryBean.isSingleton()); + return ((fb instanceof SmartFactoryBean && ((SmartFactoryBean) fb).isPrototype()) || + !fb.isSingleton()); } } else { @@ -800,12 +800,15 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp @Override public String resolveEmbeddedValue(String value) { + if (value == null) { + return null; + } String result = value; for (StringValueResolver resolver : this.embeddedValueResolvers) { + result = resolver.resolveStringValue(result); if (result == null) { return null; } - result = resolver.resolveStringValue(result); } return result; } @@ -1045,11 +1048,11 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp * Destroy the given bean instance (usually a prototype instance * obtained from this factory) according to the given bean definition. * @param beanName the name of the bean definition - * @param beanInstance the bean instance to destroy + * @param bean the bean instance to destroy * @param mbd the merged bean definition */ - protected void destroyBean(String beanName, Object beanInstance, RootBeanDefinition mbd) { - new DisposableBeanAdapter(beanInstance, beanName, mbd, getBeanPostProcessors(), getAccessControlContext()).destroy(); + protected void destroyBean(String beanName, Object bean, RootBeanDefinition mbd) { + new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), getAccessControlContext()).destroy(); } @Override @@ -1230,12 +1233,13 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp pbd = getMergedBeanDefinition(parentBeanName); } else { - if (getParentBeanFactory() instanceof ConfigurableBeanFactory) { - pbd = ((ConfigurableBeanFactory) getParentBeanFactory()).getMergedBeanDefinition(parentBeanName); + BeanFactory parent = getParentBeanFactory(); + if (parent instanceof ConfigurableBeanFactory) { + pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName); } else { - throw new NoSuchBeanDefinitionException(bd.getParentName(), - "Parent name '" + bd.getParentName() + "' is equal to bean name '" + beanName + + throw new NoSuchBeanDefinitionException(parentBeanName, + "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName + "': cannot be resolved without an AbstractBeanFactory parent"); } } @@ -1351,12 +1355,14 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp catch (ClassNotFoundException ex) { throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex); } - catch (LinkageError err) { - throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err); + catch (LinkageError ex) { + throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex); } } - private Class doResolveBeanClass(RootBeanDefinition mbd, Class... typesToMatch) throws ClassNotFoundException { + private Class doResolveBeanClass(RootBeanDefinition mbd, Class... typesToMatch) + throws ClassNotFoundException { + ClassLoader beanClassLoader = getBeanClassLoader(); ClassLoader classLoaderToUse = beanClassLoader; if (!ObjectUtils.isEmpty(typesToMatch)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java index d3d21347023..17b949a1c41 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java @@ -214,7 +214,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements if (singletonObject == null) { if (this.singletonsCurrentlyInDestruction) { throw new BeanCreationNotAllowedException(beanName, - "Singleton bean creation not allowed while the singletons of this factory are in destruction " + + "Singleton bean creation not allowed while singletons of this factory are in destruction " + "(Do not request a bean from a BeanFactory in a destroy method implementation!)"); } if (logger.isDebugEnabled()) { diff --git a/spring-context/src/test/java/org/springframework/context/event/ApplicationListenerMethodAdapterTests.java b/spring-context/src/test/java/org/springframework/context/event/ApplicationListenerMethodAdapterTests.java index b9cc4dd0d80..be5d1be8736 100644 --- a/spring-context/src/test/java/org/springframework/context/event/ApplicationListenerMethodAdapterTests.java +++ b/spring-context/src/test/java/org/springframework/context/event/ApplicationListenerMethodAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -50,52 +50,48 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv private final ApplicationContext context = mock(ApplicationContext.class); + @Test public void rawListener() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleRaw", ApplicationEvent.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class); supportsEventType(true, method, getGenericApplicationEventType("applicationEvent")); } @Test public void rawListenerWithGenericEvent() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleRaw", ApplicationEvent.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleRaw", ApplicationEvent.class); supportsEventType(true, method, getGenericApplicationEventType("stringEvent")); } @Test public void genericListener() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericString", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericString", GenericTestEvent.class); supportsEventType(true, method, getGenericApplicationEventType("stringEvent")); } @Test public void genericListenerWrongParameterizedType() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericString", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericString", GenericTestEvent.class); supportsEventType(false, method, getGenericApplicationEventType("longEvent")); } @Test public void listenerWithPayloadAndGenericInformation() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleString", String.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class); supportsEventType(true, method, createGenericEventType(String.class)); } @Test public void listenerWithInvalidPayloadAndGenericInformation() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleString", String.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class); supportsEventType(false, method, createGenericEventType(Integer.class)); } @Test - public void listenerWithPayloadTypeErasure() { // Always accept such event when the type is unknown - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleString", String.class); + public void listenerWithPayloadTypeErasure() { // Always accept such event when the type is unknown + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class); supportsEventType(true, method, ResolvableType.forClass(PayloadApplicationEvent.class)); } @@ -108,36 +104,32 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void listenerWithSubTypeSeveralGenericsResolved() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleString", String.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class); supportsEventType(true, method, ResolvableType.forClass(PayloadStringTestEvent.class)); } @Test public void listenerWithAnnotationValue() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringAnnotationValue"); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationValue"); supportsEventType(true, method, createGenericEventType(String.class)); } @Test public void listenerWithAnnotationClasses() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringAnnotationClasses"); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses"); supportsEventType(true, method, createGenericEventType(String.class)); } @Test public void listenerWithAnnotationValueAndParameter() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringAnnotationValueAndParameter", String.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class); supportsEventType(true, method, createGenericEventType(String.class)); } @Test public void listenerWithSeveralTypes() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringOrInteger"); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger"); supportsEventType(true, method, createGenericEventType(String.class)); supportsEventType(true, method, createGenericEventType(Integer.class)); supportsEventType(false, method, createGenericEventType(Double.class)); @@ -145,51 +137,47 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void listenerWithTooManyParameters() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "tooManyParameters", String.class, String.class); - - thrown.expect(IllegalStateException.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "tooManyParameters", String.class, String.class); + this.thrown.expect(IllegalStateException.class); createTestInstance(method); } @Test public void listenerWithNoParameter() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "noParameter"); - - thrown.expect(IllegalStateException.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "noParameter"); + this.thrown.expect(IllegalStateException.class); createTestInstance(method); } @Test public void listenerWithMoreThanOneParameter() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "moreThanOneParameter", String.class, Integer.class); - - thrown.expect(IllegalStateException.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "moreThanOneParameter", String.class, Integer.class); + this.thrown.expect(IllegalStateException.class); createTestInstance(method); } @Test public void defaultOrder() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericString", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericString", GenericTestEvent.class); ApplicationListenerMethodAdapter adapter = createTestInstance(method); assertEquals(0, adapter.getOrder()); } @Test public void specifiedOrder() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleRaw", ApplicationEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleRaw", ApplicationEvent.class); ApplicationListenerMethodAdapter adapter = createTestInstance(method); assertEquals(42, adapter.getOrder()); } @Test public void invokeListener() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericString", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericString", GenericTestEvent.class); GenericTestEvent event = createGenericTestEvent("test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleGenericString(event); @@ -197,8 +185,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithGenericEvent() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericString", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericString", GenericTestEvent.class); GenericTestEvent event = new SmartGenericTestEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleGenericString(event); @@ -206,8 +194,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithGenericPayload() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericStringPayload", EntityWrapper.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericStringPayload", EntityWrapper.class); EntityWrapper payload = new EntityWrapper<>("test"); invokeListener(method, new PayloadApplicationEvent<>(this, payload)); verify(this.sampleEvents, times(1)).handleGenericStringPayload(payload); @@ -215,8 +203,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithWrongGenericPayload() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericStringPayload", EntityWrapper.class); + Method method = ReflectionUtils.findMethod + (SampleEvents.class, "handleGenericStringPayload", EntityWrapper.class); EntityWrapper payload = new EntityWrapper<>(123); invokeListener(method, new PayloadApplicationEvent<>(this, payload)); verify(this.sampleEvents, times(0)).handleGenericStringPayload(any()); @@ -224,8 +212,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithAnyGenericPayload() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericAnyPayload", EntityWrapper.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericAnyPayload", EntityWrapper.class); EntityWrapper payload = new EntityWrapper<>("test"); invokeListener(method, new PayloadApplicationEvent<>(this, payload)); verify(this.sampleEvents, times(1)).handleGenericAnyPayload(payload); @@ -233,24 +221,24 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerRuntimeException() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "generateRuntimeException", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "generateRuntimeException", GenericTestEvent.class); GenericTestEvent event = createGenericTestEvent("fail"); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("Test exception"); - thrown.expectCause(is(isNull(Throwable.class))); + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("Test exception"); + this.thrown.expectCause(is((Throwable) isNull())); invokeListener(method, event); } @Test public void invokeListenerCheckedException() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "generateCheckedException", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "generateCheckedException", GenericTestEvent.class); GenericTestEvent event = createGenericTestEvent("fail"); - thrown.expect(UndeclaredThrowableException.class); - thrown.expectCause(is(instanceOf(IOException.class))); + this.thrown.expect(UndeclaredThrowableException.class); + this.thrown.expectCause(is(instanceOf(IOException.class))); invokeListener(method, event); } @@ -262,18 +250,18 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv proxyFactory.addInterface(SimpleService.class); Object bean = proxyFactory.getProxy(getClass().getClassLoader()); - Method method = ReflectionUtils.findMethod(InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class); + Method method = ReflectionUtils.findMethod( + InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class); StaticApplicationListenerMethodAdapter listener = new StaticApplicationListenerMethodAdapter(method, bean); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("handleIt2"); + this.thrown.expect(IllegalStateException.class); + this.thrown.expectMessage("handleIt2"); listener.onApplicationEvent(createGenericTestEvent("test")); } @Test public void invokeListenerWithPayload() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleString", String.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class); PayloadApplicationEvent event = new PayloadApplicationEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleString("test"); @@ -281,8 +269,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithPayloadWrongType() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleString", String.class); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class); PayloadApplicationEvent event = new PayloadApplicationEvent<>(this, 123L); invokeListener(method, event); verify(this.sampleEvents, never()).handleString(anyString()); @@ -290,8 +277,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithAnnotationValue() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringAnnotationClasses"); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringAnnotationClasses"); PayloadApplicationEvent event = new PayloadApplicationEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleStringAnnotationClasses(); @@ -299,8 +285,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithAnnotationValueAndParameter() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringAnnotationValueAndParameter", String.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleStringAnnotationValueAndParameter", String.class); PayloadApplicationEvent event = new PayloadApplicationEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test"); @@ -308,8 +294,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Test public void invokeListenerWithSeveralTypes() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleStringOrInteger"); + Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleStringOrInteger"); PayloadApplicationEvent event = new PayloadApplicationEvent<>(this, "test"); invokeListener(method, event); verify(this.sampleEvents, times(1)).handleStringOrInteger(); @@ -321,11 +306,10 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv verify(this.sampleEvents, times(2)).handleStringOrInteger(); } - @Test public void beanInstanceRetrievedAtEveryInvocation() { - Method method = ReflectionUtils.findMethod(SampleEvents.class, - "handleGenericString", GenericTestEvent.class); + Method method = ReflectionUtils.findMethod( + SampleEvents.class, "handleGenericString", GenericTestEvent.class); when(this.context.getBean("testBean")).thenReturn(this.sampleEvents); ApplicationListenerMethodAdapter listener = new ApplicationListenerMethodAdapter( "testBean", GenericTestEvent.class, method); @@ -342,6 +326,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv verify(this.context, times(2)).getBean("testBean"); } + private void supportsEventType(boolean match, Method method, ResolvableType eventType) { ApplicationListenerMethodAdapter adapter = createTestInstance(method); assertEquals("Wrong match for event '" + eventType + "' on " + method, @@ -361,8 +346,8 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv return ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, payloadType); } - private static class StaticApplicationListenerMethodAdapter - extends ApplicationListenerMethodAdapter { + + private static class StaticApplicationListenerMethodAdapter extends ApplicationListenerMethodAdapter { private final Object targetBean; @@ -373,14 +358,13 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv @Override public Object getTargetBean() { - return targetBean; + return this.targetBean; } } private static class SampleEvents { - @EventListener @Order(42) public void handleRaw(ApplicationEvent event) { @@ -449,13 +433,15 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv } } + interface SimpleService { void handleIt(ApplicationEvent event); - } + private static class EntityWrapper implements ResolvableTypeProvider { + private final T entity; public EntityWrapper(T entity) { @@ -468,6 +454,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv } } + static class InvalidProxyTestBean implements SimpleService { @Override @@ -479,6 +466,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv } } + @SuppressWarnings({"unused", "serial"}) static class PayloadTestEvent extends PayloadApplicationEvent { @@ -490,8 +478,10 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv } } + @SuppressWarnings({ "serial" }) static class PayloadStringTestEvent extends PayloadTestEvent { + public PayloadStringTestEvent(Object source, String payload, Long something) { super(source, payload, something); } diff --git a/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java b/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java index 301ad5a71a6..8209d79b67c 100644 --- a/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java @@ -607,6 +607,7 @@ public abstract class ReflectionUtils { * @see Class#getDeclaredMethods() */ private static Method[] getDeclaredMethods(Class clazz) { + Assert.notNull(clazz, "Class must not be null"); Method[] result = declaredMethodsCache.get(clazz); if (result == null) { Method[] declaredMethods = clazz.getDeclaredMethods(); @@ -708,6 +709,7 @@ public abstract class ReflectionUtils { * @see Class#getDeclaredFields() */ private static Field[] getDeclaredFields(Class clazz) { + Assert.notNull(clazz, "Class must not be null"); Field[] result = declaredFieldsCache.get(clazz); if (result == null) { result = clazz.getDeclaredFields(); diff --git a/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java index 594c2fb9dd6..e33a15381ca 100644 --- a/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/AbstractAsyncClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 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. @@ -73,12 +73,11 @@ abstract class AbstractAsyncClientHttpRequest implements AsyncClientHttpRequest protected abstract OutputStream getBodyInternal(HttpHeaders headers) throws IOException; /** - * Abstract template method that writes the given headers and content to the HTTP - * request. + * Abstract template method that writes the given headers and content to the HTTP request. * @param headers the HTTP headers * @return the response object for the executed request */ - protected abstract ListenableFuture executeInternal( - HttpHeaders headers) throws IOException; + protected abstract ListenableFuture executeInternal(HttpHeaders headers) + throws IOException; } diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java b/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java index a768dfb78f2..14883542a9e 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpClientErrorException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -34,8 +34,8 @@ public class HttpClientErrorException extends HttpStatusCodeException { /** - * Construct a new instance of {@code HttpClientErrorException} based on an - * {@link HttpStatus}. + * Construct a new instance of {@code HttpClientErrorException} based on + * an {@link HttpStatus}. * @param statusCode the status code */ public HttpClientErrorException(HttpStatus statusCode) { @@ -43,8 +43,8 @@ public class HttpClientErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpClientErrorException} based on an - * {@link HttpStatus} and status text. + * Construct a new instance of {@code HttpClientErrorException} based on + * an {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -53,30 +53,32 @@ public class HttpClientErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpClientErrorException} based on an - * {@link HttpStatus}, status text, and response body content. + * Construct a new instance of {@code HttpClientErrorException} based on + * an {@link HttpStatus}, status text, and response body content. * @param statusCode the status code * @param statusText the status text - * @param responseBody the response body content, may be {@code null} - * @param responseCharset the response body charset, may be {@code null} + * @param responseBody the response body content (may be {@code null}) + * @param responseCharset the response body charset (may be {@code null}) */ public HttpClientErrorException(HttpStatus statusCode, String statusText, byte[] responseBody, Charset responseCharset) { + super(statusCode, statusText, responseBody, responseCharset); } /** - * Construct a new instance of {@code HttpClientErrorException} based on an - * {@link HttpStatus}, status text, and response body content. + * Construct a new instance of {@code HttpClientErrorException} based on + * an {@link HttpStatus}, status text, and response body content. * @param statusCode the status code * @param statusText the status text - * @param responseHeaders the response headers, may be {@code null} - * @param responseBody the response body content, may be {@code null} - * @param responseCharset the response body charset, may be {@code null} + * @param responseHeaders the response headers (may be {@code null}) + * @param responseBody the response body content (may be {@code null}) + * @param responseCharset the response body charset (may be {@code null}) * @since 3.1.2 */ public HttpClientErrorException(HttpStatus statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { + super(statusCode, statusText, responseHeaders, responseBody, responseCharset); } diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java b/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java index 5f9ba72cd54..f4e494d9315 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpServerErrorException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 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. @@ -25,8 +25,8 @@ import org.springframework.http.HttpStatus; * Exception thrown when an HTTP 5xx is received. * * @author Arjen Poutsma - * @see DefaultResponseErrorHandler * @since 3.0 + * @see DefaultResponseErrorHandler */ public class HttpServerErrorException extends HttpStatusCodeException { @@ -34,8 +34,8 @@ public class HttpServerErrorException extends HttpStatusCodeException { /** - * Construct a new instance of {@code HttpServerErrorException} based on an - * {@link HttpStatus}. + * Construct a new instance of {@code HttpServerErrorException} based on + * an {@link HttpStatus}. * @param statusCode the status code */ public HttpServerErrorException(HttpStatus statusCode) { @@ -43,8 +43,8 @@ public class HttpServerErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpServerErrorException} based on an - * {@link HttpStatus} and status text. + * Construct a new instance of {@code HttpServerErrorException} based on + * an {@link HttpStatus} and status text. * @param statusCode the status code * @param statusText the status text */ @@ -53,31 +53,34 @@ public class HttpServerErrorException extends HttpStatusCodeException { } /** - * Construct a new instance of {@code HttpServerErrorException} based on an - * {@link HttpStatus}, status text, and response body content. - * @param statusCode the status code - * @param statusText the status text - * @param responseBody the response body content, may be {@code null} - * @param responseCharset the response body charset, may be {@code null} + * Construct a new instance of {@code HttpServerErrorException} based on + * an {@link HttpStatus}, status text, and response body content. + * @param statusCode the status code + * @param statusText the status text + * @param responseBody the response body content (may be {@code null}) + * @param responseCharset the response body charset (may be {@code null}) * @since 3.0.5 */ public HttpServerErrorException(HttpStatus statusCode, String statusText, byte[] responseBody, Charset responseCharset) { + super(statusCode, statusText, responseBody, responseCharset); } /** - * Construct a new instance of {@code HttpServerErrorException} based on a - * {@link HttpStatus}, status text, and response body content. + * Construct a new instance of {@code HttpServerErrorException} based on + * an {@link HttpStatus}, status text, and response body content. * @param statusCode the status code * @param statusText the status text - * @param responseHeaders the response headers, may be {@code null} - * @param responseBody the response body content, may be {@code null} - * @param responseCharset the response body charset, may be {@code null} + * @param responseHeaders the response headers (may be {@code null}) + * @param responseBody the response body content (may be {@code null}) + * @param responseCharset the response body charset (may be {@code null}) * @since 3.1.2 */ public HttpServerErrorException(HttpStatus statusCode, String statusText, HttpHeaders responseHeaders, byte[] responseBody, Charset responseCharset) { + super(statusCode, statusText, responseHeaders, responseBody, responseCharset); } + }