Polishing

This commit is contained in:
Juergen Hoeller 2015-11-05 12:30:29 +01:00
parent 6cedaa1091
commit 2ba5cb2734
4 changed files with 26 additions and 25 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@ -59,12 +59,12 @@ public abstract class AnnotationBeanUtils {
* @see org.springframework.beans.BeanWrapper
*/
public static void copyPropertiesToBean(Annotation ann, Object bean, StringValueResolver valueResolver, String... excludedProperties) {
Set<String> excluded = new HashSet<String>(Arrays.asList(excludedProperties));
Set<String> excluded = new HashSet<String>(Arrays.asList(excludedProperties));
Method[] annotationProperties = ann.annotationType().getDeclaredMethods();
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean);
for (Method annotationProperty : annotationProperties) {
String propertyName = annotationProperty.getName();
if ((!excluded.contains(propertyName)) && bw.isWritableProperty(propertyName)) {
if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) {
Object value = ReflectionUtils.invokeMethod(annotationProperty, ann);
if (valueResolver != null && value instanceof String) {
value = valueResolver.resolveStringValue((String) value);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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,6 +25,7 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation6.ComponentForScanning;
import org.springframework.context.annotation6.ConfigForScanning;
import org.springframework.context.annotation6.Jsr330NamedForScanning;
@ -40,10 +41,10 @@ import static org.springframework.util.StringUtils.*;
*/
public class AnnotationConfigApplicationContextTests {
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void nullGetBeanParameterIsDisallowed() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
context.getBean((Class<?>)null);
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
context.getBean((Class<?>) null);
}
@Test
@ -83,7 +84,7 @@ public class AnnotationConfigApplicationContextTests {
@Test
public void getBeanByType() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
TestBean testBean = context.getBean(TestBean.class);
assertNotNull("getBean() should not return null", testBean);
assertThat(testBean.name, equalTo("foo"));
@ -95,7 +96,7 @@ public class AnnotationConfigApplicationContextTests {
*/
@Test
public void defaultConfigClassBeanNameIsGeneratedProperly() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
// attempt to retrieve the instance by its generated bean name
Config configObject = (Config) context.getBean("annotationConfigApplicationContextTests.Config");
@ -108,18 +109,16 @@ public class AnnotationConfigApplicationContextTests {
*/
@Test
public void explicitConfigClassBeanNameIsRespected() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ConfigWithCustomName.class);
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigWithCustomName.class);
// attempt to retrieve the instance by its specified name
ConfigWithCustomName configObject =
(ConfigWithCustomName) context.getBean("customConfigBeanName");
ConfigWithCustomName configObject = (ConfigWithCustomName) context.getBean("customConfigBeanName");
assertNotNull(configObject);
}
@Test
public void getBeanByTypeRaisesNoSuchBeanDefinitionException() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
// attempt to retrieve a bean that does not exist
Class<?> targetType = Pattern.class;
@ -135,7 +134,7 @@ public class AnnotationConfigApplicationContextTests {
@Test
public void getBeanByTypeAmbiguityRaisesException() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TwoTestBeanConfig.class);
ApplicationContext context = new AnnotationConfigApplicationContext(TwoTestBeanConfig.class);
try {
context.getBean(TestBean.class);
@ -153,7 +152,7 @@ public class AnnotationConfigApplicationContextTests {
@Test
public void autowiringIsEnabledByDefault() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AutowiredConfig.class);
ApplicationContext context = new AnnotationConfigApplicationContext(AutowiredConfig.class);
assertThat(context.getBean(TestBean.class).name, equalTo("foo"));
}
@ -255,13 +254,14 @@ public class AnnotationConfigApplicationContextTests {
}
class TestBean {
String name;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (name == null ? 0 : name.hashCode());
return result;
}
@ -277,7 +277,8 @@ class TestBean {
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
}
else if (!name.equals(other.name))
return false;
return true;
}

View File

@ -121,12 +121,12 @@ public abstract class AnnotationUtils {
private static final Map<AnnotationCacheKey, Annotation> findAnnotationCache =
new ConcurrentReferenceHashMap<AnnotationCacheKey, Annotation>(256);
private static final Map<Class<?>, Boolean> annotatedInterfaceCache =
new ConcurrentReferenceHashMap<Class<?>, Boolean>(256);
private static final Map<AnnotationCacheKey, Boolean> metaPresentCache =
new ConcurrentReferenceHashMap<AnnotationCacheKey, Boolean>(256);
private static final Map<Class<?>, Boolean> annotatedInterfaceCache =
new ConcurrentReferenceHashMap<Class<?>, Boolean>(256);
private static final Map<Class<? extends Annotation>, Boolean> synthesizableCache =
new ConcurrentReferenceHashMap<Class<? extends Annotation>, Boolean>(256);
@ -787,7 +787,7 @@ public abstract class AnnotationUtils {
* @see #isAnnotationDeclaredLocally(Class, Class)
*/
public static Class<?> findAnnotationDeclaringClassForTypes(List<Class<? extends Annotation>> annotationTypes, Class<?> clazz) {
Assert.notEmpty(annotationTypes, "The list of annotation types must not be empty");
Assert.notEmpty(annotationTypes, "List of annotation types must not be empty");
if (clazz == null || Object.class == clazz) {
return null;
}
@ -1055,7 +1055,7 @@ public abstract class AnnotationUtils {
try {
Object value = method.invoke(annotation);
Object defaultValue = method.getDefaultValue();
if (mergeMode && (defaultValue != null)) {
if (mergeMode && defaultValue != null) {
if (ObjectUtils.nullSafeEquals(value, defaultValue)) {
value = DEFAULT_VALUE_PLACEHOLDER;
}

View File

@ -56,6 +56,7 @@ public class AnnotatedElementUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void getMetaAnnotationTypesOnNonAnnotatedClass() {
assertNull(getMetaAnnotationTypes(NonAnnotatedClass.class, TransactionalComponent.class));
@ -69,8 +70,7 @@ public class AnnotatedElementUtilsTests {
@Test
public void getMetaAnnotationTypesOnClassWithMetaDepth2() {
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class,
ComposedTransactionalComponent.class);
Set<String> names = getMetaAnnotationTypes(ComposedTransactionalComponentClass.class, ComposedTransactionalComponent.class);
assertEquals(names(TransactionalComponent.class, Transactional.class, Component.class), names);
}