From 27c2e8c80da75deee2c0f6b76acd43e41afd1421 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 29 Dec 2015 21:40:20 +0100 Subject: [PATCH] Polishing --- .../annotation/SpringCacheAnnotationParser.java | 4 ++-- .../context/annotation/BeanAnnotationHelper.java | 11 ++++++----- .../annotation/ComponentScanAnnotationTests.java | 6 +++--- ...mponentScanParserBeanDefinitionDefaultsTests.java | 7 +++---- .../ComponentScanParserScopedProxyTests.java | 6 ++++++ .../context/annotation/ComponentScanParserTests.java | 6 +++--- ...nentScanParserWithUserDefinedStrategiesTests.java | 8 ++++---- .../SynthesizedAnnotationInvocationHandler.java | 12 ++++++------ 8 files changed, 33 insertions(+), 27 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java b/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java index f982b388a0..4865c1a908 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/SpringCacheAnnotationParser.java @@ -196,7 +196,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria } private Collection getAnnotations(AnnotatedElement ae, Class annotationType) { - Collection anns = new ArrayList(2); + Collection anns = new ArrayList(1); // look at raw annotation A ann = ae.getAnnotation(annotationType); @@ -212,7 +212,7 @@ public class SpringCacheAnnotationParser implements CacheAnnotationParser, Seria } } - return (anns.isEmpty() ? null : anns); + return (!anns.isEmpty() ? anns : null); } /** diff --git a/spring-context/src/main/java/org/springframework/context/annotation/BeanAnnotationHelper.java b/spring-context/src/main/java/org/springframework/context/annotation/BeanAnnotationHelper.java index f44d7b5051..626a328f41 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/BeanAnnotationHelper.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/BeanAnnotationHelper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 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. @@ -29,17 +29,18 @@ import org.springframework.core.annotation.AnnotationUtils; class BeanAnnotationHelper { /** - * Return whether the given method is annotated directly or indirectly with @Bean. + * Return whether the given method is directly or indirectly annotated with + * the {@link Bean} annotation. */ public static boolean isBeanAnnotated(Method method) { - return AnnotationUtils.findAnnotation(method, Bean.class) != null; + return (AnnotationUtils.findAnnotation(method, Bean.class) != null); } public static String determineBeanNameFor(Method beanMethod) { - // by default the bean name is the name of the @Bean-annotated method + // By default, the bean name is the name of the @Bean-annotated method String beanName = beanMethod.getName(); - // check to see if the user has explicitly set the bean name + // Check to see if the user has explicitly set a custom bean name... Bean bean = AnnotationUtils.findAnnotation(beanMethod, Bean.class); if (bean != null && bean.name().length > 0) { beanName = bean.name()[0]; diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationTests.java index ec17007cdc..2325b36f66 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -44,7 +44,7 @@ public class ComponentScanAnnotationTests { @Configuration @ComponentScan( - basePackageClasses={TestBean.class}, + basePackageClasses = TestBean.class, nameGenerator = DefaultBeanNameGenerator.class, scopedProxy = ScopedProxyMode.NO, scopeResolver = AnnotationScopeMetadataResolver.class, @@ -61,6 +61,6 @@ public class ComponentScanAnnotationTests { class MyConfig { } -@ComponentScan(basePackageClasses=example.scannable.NamedComponent.class) +@ComponentScan(basePackageClasses = example.scannable.NamedComponent.class) class SimpleConfig { } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserBeanDefinitionDefaultsTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserBeanDefinitionDefaultsTests.java index daad584f1c..bef499ff74 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserBeanDefinitionDefaultsTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserBeanDefinitionDefaultsTests.java @@ -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. @@ -122,7 +122,7 @@ public class ComponentScanParserBeanDefinitionDefaultsTests { context.refresh(); fail("expected exception due to multiple matches for byType autowiring"); } - catch (UnsatisfiedDependencyException e) { + catch (UnsatisfiedDependencyException ex) { // expected } } @@ -161,7 +161,7 @@ public class ComponentScanParserBeanDefinitionDefaultsTests { context.refresh(); fail("expected exception due to dependency check"); } - catch (UnsatisfiedDependencyException e) { + catch (UnsatisfiedDependencyException ex) { // expected } } @@ -230,7 +230,6 @@ public class ComponentScanParserBeanDefinitionDefaultsTests { private boolean destroyed; - public DefaultsTestBean() { INIT_COUNT++; } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserScopedProxyTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserScopedProxyTests.java index 5d93bbbc99..f8a50d76d8 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserScopedProxyTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserScopedProxyTests.java @@ -42,11 +42,13 @@ public class ComponentScanParserScopedProxyTests { @Rule public final ExpectedException exception = ExpectedException.none(); + @Test public void testDefaultScopedProxy() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyDefaultTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); + ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean"); // should not be a proxy assertFalse(AopUtils.isAopProxy(bean)); @@ -58,6 +60,7 @@ public class ComponentScanParserScopedProxyTests { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyNoTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); + ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean"); // should not be a proxy assertFalse(AopUtils.isAopProxy(bean)); @@ -69,6 +72,7 @@ public class ComponentScanParserScopedProxyTests { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyInterfacesTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); + // should cast to the interface FooService bean = (FooService) context.getBean("scopedProxyTestBean"); // should be dynamic proxy @@ -86,6 +90,7 @@ public class ComponentScanParserScopedProxyTests { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "org/springframework/context/annotation/scopedProxyTargetClassTests.xml"); context.getBeanFactory().registerScope("myScope", new SimpleMapScope()); + ScopedProxyTestBean bean = (ScopedProxyTestBean) context.getBean("scopedProxyTestBean"); // should be a class-based proxy assertTrue(AopUtils.isCglibProxy(bean)); @@ -103,6 +108,7 @@ public class ComponentScanParserScopedProxyTests { exception.expect(BeanDefinitionParsingException.class); exception.expectMessage(containsString("Cannot define both 'scope-resolver' and 'scoped-proxy' on tag")); exception.expectMessage(containsString("Offending resource: class path resource [org/springframework/context/annotation/scopedProxyInvalidConfigTests.xml]")); + new ClassPathXmlApplicationContext("org/springframework/context/annotation/scopedProxyInvalidConfigTests.xml"); } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java index 9298ad6caf..20d62e14a9 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -145,9 +145,9 @@ public class ComponentScanParserTests { } - @Target({ ElementType.TYPE, ElementType.FIELD }) + @Target({ElementType.TYPE, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) - public static @interface CustomAnnotation { + public @interface CustomAnnotation { } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserWithUserDefinedStrategiesTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserWithUserDefinedStrategiesTests.java index 6c2984cdad..ad7fbc39c6 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserWithUserDefinedStrategiesTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanParserWithUserDefinedStrategiesTests.java @@ -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. @@ -53,7 +53,7 @@ public class ComponentScanParserWithUserDefinedStrategiesTests { "org/springframework/context/annotation/invalidConstructorNameGeneratorTests.xml"); fail("should have failed: no-arg constructor is required"); } - catch (BeansException e) { + catch (BeansException ex) { // expected } } @@ -65,9 +65,9 @@ public class ComponentScanParserWithUserDefinedStrategiesTests { "org/springframework/context/annotation/invalidClassNameScopeResolverTests.xml"); fail("should have failed: no such class"); } - catch (BeansException e) { + catch (BeansException ex) { // expected } } -} \ No newline at end of file +} diff --git a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java index b933823f54..c8614eb575 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java @@ -60,6 +60,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { this.attributeExtractor = attributeExtractor; } + @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (isEqualsMethod(method)) { @@ -75,8 +76,8 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { return annotationType(); } if (!isAttributeMethod(method)) { - String msg = String.format("Method [%s] is unsupported for synthesized annotation type [%s]", method, - annotationType()); + String msg = String.format("Method [%s] is unsupported for synthesized annotation type [%s]", + method, annotationType()); throw new AnnotationConfigurationException(msg); } return getAttributeValue(method); @@ -92,9 +93,9 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { if (value == null) { value = this.attributeExtractor.getAttributeValue(attributeMethod); if (value == null) { - throw new IllegalStateException(String.format( - "%s returned null for attribute name [%s] from attribute source [%s]", - this.attributeExtractor.getClass().getName(), attributeName, this.attributeExtractor.getSource())); + String msg = String.format("%s returned null for attribute name [%s] from attribute source [%s]", + this.attributeExtractor.getClass().getName(), attributeName, this.attributeExtractor.getSource()); + throw new IllegalStateException(msg); } // Synthesize nested annotations before returning them. @@ -200,7 +201,6 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { * in Spring's {@link ObjectUtils} because those hash code generation * algorithms do not comply with the requirements specified in * {@link Annotation#hashCode()}. - * * @param array the array to compute the hash code for */ private int hashCodeForArray(Object array) {