From 1afdd9bd758d2d5ef86b4de0c2ceabc9ccbc5eed Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 30 Apr 2014 00:01:07 +0200 Subject: [PATCH] Polishing (cherry picked from commit 02aca9c) --- .../configuration/Spr10744Tests.java | 11 ++-- .../core/annotation/AnnotationUtils.java | 36 +++++------ .../metadata/OracleTableMetaDataProvider.java | 5 +- .../test/context/TestContextManager.java | 62 +++++++++---------- 4 files changed, 55 insertions(+), 59 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/Spr10744Tests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/Spr10744Tests.java index 541407c3cf2..abc3f495a75 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/Spr10744Tests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/Spr10744Tests.java @@ -41,14 +41,15 @@ public class Spr10744Tests { @Test public void testSpr10744() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); - MyTestScope scope = new MyTestScope(); - context.getBeanFactory().registerScope("myTestScope", scope); + context.getBeanFactory().registerScope("myTestScope", new MyTestScope()); context.register(MyTestConfiguration.class); context.refresh(); + Foo bean1 = context.getBean("foo", Foo.class); Foo bean2 = context.getBean("foo", Foo.class); assertThat(bean1, sameInstance(bean2)); - // Should have created a single instance for the proxy + + // Should not have invoked constructor for the proxy instance assertThat(createCount, equalTo(0)); assertThat(scopeCount, equalTo(0)); @@ -118,9 +119,9 @@ public class Spr10744Tests { @Configuration static class MyTestConfiguration extends MyConfiguration { - @Override - @Scope(value = "myTestScope", proxyMode = ScopedProxyMode.TARGET_CLASS) @Bean + @Scope(value = "myTestScope", proxyMode = ScopedProxyMode.TARGET_CLASS) + @Override public Foo foo() { return new Foo(); } diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java index 1dd20887940..fa418d3b020 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java @@ -70,8 +70,8 @@ public abstract class AnnotationUtils { * Get a single {@link Annotation} of {@code annotationType} from the supplied * annotation: either the given annotation itself or a meta-annotation thereof. * @param ann the Annotation to check - * @param annotationType the annotation class to look for, both locally and as a meta-annotation - * @return the matching annotation or {@code null} if not found + * @param annotationType the annotation type to look for, both locally and as a meta-annotation + * @return the matching annotation, or {@code null} if none found * @since 4.0 */ @SuppressWarnings("unchecked") @@ -87,8 +87,8 @@ public abstract class AnnotationUtils { * Method, Constructor or Field. Meta-annotations will be searched if the annotation * is not declared locally on the supplied element. * @param ae the Method, Constructor or Field from which to get the annotation - * @param annotationType the annotation class to look for, both locally and as a meta-annotation - * @return the matching annotation or {@code null} if not found + * @param annotationType the annotation type to look for, both locally and as a meta-annotation + * @return the matching annotation, or {@code null} if none found * @since 3.1 */ public static T getAnnotation(AnnotatedElement ae, Class annotationType) { @@ -119,7 +119,7 @@ public abstract class AnnotationUtils { * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method}. *

Correctly handles bridge {@link Method Methods} generated by the compiler. * @param method the method to look for annotations on - * @param annotationType the annotation class to look for + * @param annotationType the annotation type to look for * @return the annotations found * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) */ @@ -135,7 +135,7 @@ public abstract class AnnotationUtils { *

Correctly handles bridge {@link Method Methods} generated by the compiler. * @param method the method to look for annotations on * @param containerAnnotationType the class of the container that holds the annotations - * @param annotationType the annotation class to look for + * @param annotationType the annotation type to look for * @return the annotations found * @since 4.0 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) @@ -154,7 +154,7 @@ public abstract class AnnotationUtils { *

Correctly handles bridge {@link Method Methods} generated by the compiler. * @param annotatedElement the element to look for annotations on * @param containerAnnotationType the class of the container that holds the annotations - * @param annotationType the annotation class to look for + * @param annotationType the annotation type to look for * @return the annotations found * @since 4.0 * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method) @@ -170,13 +170,13 @@ public abstract class AnnotationUtils { /** * Find a single {@link Annotation} of {@code annotationType} from the supplied - * {@link Method}, traversing its super methods (i.e., from super classes and + * {@link Method}, traversing its super methods (i.e., from superclasses and * interfaces) if no annotation can be found on the given method itself. *

Annotations on methods are not inherited by default, so we need to handle * this explicitly. * @param method the method to look for annotations on - * @param annotationType the annotation class to look for - * @return the annotation found, or {@code null} if none found + * @param annotationType the annotation type to look for + * @return the annotation found, or {@code null} if none */ public static A findAnnotation(Method method, Class annotationType) { A annotation = getAnnotation(method, annotationType); @@ -288,8 +288,7 @@ public abstract class AnnotationUtils { } for (Annotation ann : clazz.getDeclaredAnnotations()) { if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) { - A annotation = findAnnotation(ann.annotationType(), annotationType, - visited); + A annotation = findAnnotation(ann.annotationType(), annotationType, visited); if (annotation != null) { return annotation; } @@ -312,8 +311,8 @@ public abstract class AnnotationUtils { *

The standard {@link Class} API does not provide a mechanism for determining which class * in an inheritance hierarchy actually declares an {@link Annotation}, so we need to handle * this explicitly. - * @param annotationType the annotation class to look for, both locally and as a meta-annotation - * @param clazz the class on which to check for the annotation, or {@code null} + * @param annotationType the annotation type to look for, both locally and as a meta-annotation + * @param clazz the class on which to check for the annotation (may be {@code null}) * @return the first {@link Class} in the inheritance hierarchy of the specified {@code clazz} * which declares an annotation for the specified {@code annotationType}, or {@code null} * if not found @@ -509,8 +508,7 @@ public abstract class AnnotationUtils { Annotation[] realAnnotations = (Annotation[]) value; AnnotationAttributes[] mappedAnnotations = new AnnotationAttributes[realAnnotations.length]; for (int i = 0; i < realAnnotations.length; i++) { - mappedAnnotations[i] = getAnnotationAttributes( - realAnnotations[i], classValuesAsString, true); + mappedAnnotations[i] = getAnnotationAttributes(realAnnotations[i], classValuesAsString, true); } attrs.put(method.getName(), mappedAnnotations); } @@ -634,7 +632,7 @@ public abstract class AnnotationUtils { this.result.add((A) annotation); } else if (ObjectUtils.nullSafeEquals(this.containerAnnotationType, annotation.annotationType())) { - result.addAll(Arrays.asList(getValue(annotation))); + this.result.addAll(Arrays.asList(getValue(annotation))); } else if (!isInJavaLangAnnotationPackage(annotation)) { process(annotation.annotationType()); @@ -651,8 +649,8 @@ public abstract class AnnotationUtils { return (A[]) method.invoke(annotation); } catch (Exception ex) { - throw new IllegalStateException("Unable to read value from repeating annotation container " - + this.containerAnnotationType.getName(), ex); + throw new IllegalStateException("Unable to read value from repeating annotation container " + + this.containerAnnotationType.getName(), ex); } } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/OracleTableMetaDataProvider.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/OracleTableMetaDataProvider.java index d9f56fcb83e..7c827621768 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/OracleTableMetaDataProvider.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/metadata/OracleTableMetaDataProvider.java @@ -29,8 +29,8 @@ import org.springframework.util.ReflectionUtils; /** * Oracle-specific implementation of the {@link org.springframework.jdbc.core.metadata.TableMetaDataProvider}. - * Supports a feature for including synonyms in the metadata lookup. Also supports lookup of current schema using - * the sys_context. + * Supports a feature for including synonyms in the metadata lookup. Also supports lookup of current schema + * using the sys_context. * *

Thanks to Mike Youngstrom and Bruce Campbell for submitting the original suggestion for the Oracle * current schema lookup implementation. @@ -128,7 +128,6 @@ public class OracleTableMetaDataProvider extends GenericTableMetaDataProvider { /* * Oracle-based implementation for detecting the current schema. - * @param databaseMetaData */ private void lookupDefaultSchema(DatabaseMetaData databaseMetaData) { try { diff --git a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java index 5daa3f3f97c..78aaf22402b 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java @@ -28,30 +28,24 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanUtils; -import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.AnnotationAttributes; -import org.springframework.test.context.MetaAnnotationUtils.*; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; -import static org.springframework.test.context.MetaAnnotationUtils.*; - /** - *

- * {@code TestContextManager} is the main entry point into the - * Spring TestContext Framework, which provides support for loading and - * accessing {@link ApplicationContext application contexts}, dependency - * injection of test instances, - * {@link org.springframework.transaction.annotation.Transactional - * transactional} execution of test methods, etc. - *

- *

- * Specifically, a {@code TestContextManager} is responsible for managing a + * {@code TestContextManager} is the main entry point into the Spring + * TestContext Framework, which provides support for loading and accessing + * {@link org.springframework.context.ApplicationContext application contexts}, + * dependency injection of test instances, + * {@link org.springframework.transaction.annotation.Transactional transactional} + * execution of test methods, etc. + * + *

Specifically, a {@code TestContextManager} is responsible for managing a * single {@link TestContext} and signaling events to all registered * {@link TestExecutionListener TestExecutionListeners} at well defined test * execution points: - *

+ * *
    *
  • {@link #beforeTestClass() before test class execution}: prior to any * before class methods of a particular testing framework (e.g., JUnit @@ -88,9 +82,10 @@ public class TestContextManager { private static final Log logger = LogFactory.getLog(TestContextManager.class); /** - * Cache of Spring application contexts. This needs to be static, as tests - * may be destroyed and recreated between running individual test methods, - * for example with JUnit. + * Cache of Spring application contexts. + *

    This needs to be static, since test instances may be destroyed and + * recreated between invocations of individual test methods, as is the case + * with JUnit. */ static final ContextCache contextCache = new ContextCache(); @@ -100,7 +95,11 @@ public class TestContextManager { /** - * Delegates to {@link #TestContextManager(Class, String)} with a value of + * Construct a new {@code TestContextManager} for the specified {@linkplain Class test class} + * and automatically {@link #registerTestExecutionListeners register} the + * {@link TestExecutionListener TestExecutionListeners} configured for the test class + * via the {@link TestExecutionListeners @TestExecutionListeners} annotation. + *

    Delegates to {@link #TestContextManager(Class, String)} with a value of * {@code null} for the default {@code ContextLoader} class name. */ public TestContextManager(Class testClass) { @@ -108,16 +107,14 @@ public class TestContextManager { } /** - * Constructs a new {@code TestContextManager} for the specified {@linkplain Class - * test class} and automatically {@link #registerTestExecutionListeners registers} the + * Construct a new {@code TestContextManager} for the specified {@linkplain Class test class} + * and automatically {@link #registerTestExecutionListeners register} the * {@link TestExecutionListener TestExecutionListeners} configured for the test class - * via the {@link TestExecutionListeners @TestExecutionListeners} annotation. + * via the {@link TestExecutionListeners @TestExecutionListeners} annotation. * @param testClass the test class to be managed - * @param defaultContextLoaderClassName the name of the default {@code ContextLoader} - * class to use (may be {@code null}) - * @see #registerTestExecutionListeners(TestExecutionListener...) - * @deprecated Spring Framework 4.1 will introduce a bootstrap strategy for - * the TestContext framework at which point this constructor will be removed. + * @param defaultContextLoaderClassName the name of the default {@code ContextLoader} class + * to use (may be {@code null}) + * @see #registerTestExecutionListeners */ @Deprecated public TestContextManager(Class testClass, String defaultContextLoaderClassName) { @@ -125,9 +122,9 @@ public class TestContextManager { registerTestExecutionListeners(retrieveTestExecutionListeners(testClass)); } + /** - * Returns the {@link TestContext} managed by this - * {@code TestContextManager}. + * Get the {@link TestContext} managed by this {@code TestContextManager}. */ protected final TestContext getTestContext() { return this.testContext; @@ -183,7 +180,8 @@ public class TestContextManager { Class annotationType = TestExecutionListeners.class; List> classesList = new ArrayList>(); - AnnotationDescriptor descriptor = findAnnotationDescriptor(clazz, annotationType); + MetaAnnotationUtils.AnnotationDescriptor descriptor = + MetaAnnotationUtils.findAnnotationDescriptor(clazz, annotationType); // Use defaults? if (descriptor == null) { @@ -223,8 +221,8 @@ public class TestContextManager { classesList.addAll(0, Arrays.> asList(listenerClasses)); } - descriptor = (annAttrs.getBoolean("inheritListeners") ? findAnnotationDescriptor( - descriptor.getRootDeclaringClass().getSuperclass(), annotationType) : null); + descriptor = (annAttrs.getBoolean("inheritListeners") ? MetaAnnotationUtils.findAnnotationDescriptor( + descriptor.getRootDeclaringClass().getSuperclass(), annotationType) : null); } }