From ae6d778c3eeeda819361d44cfaea956bfb58138d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 31 Jul 2018 22:25:44 +0200 Subject: [PATCH] Polishing --- .../annotation/AnnotatedElementUtils.java | 25 +++++++--------- .../AnnotationAttributesReadingVisitor.java | 30 ++++++++++--------- .../test/web/servlet/DefaultMvcResult.java | 10 ++++--- .../MessageBrokerBeanDefinitionParser.java | 28 ++++++++--------- 4 files changed, 46 insertions(+), 47 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java index 75f42f02de..c67cd0cd68 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedElementUtils.java @@ -479,12 +479,12 @@ public class AnnotatedElementUtils { * @param annotationType the annotation type to find (never {@code null}) * @return the set of all merged repeatable {@code Annotations} found, * or an empty set if none were found + * @throws IllegalArgumentException if the {@code element} or {@code annotationType} + * is {@code null}, or if the container type cannot be resolved * @since 4.3 * @see #getMergedAnnotation(AnnotatedElement, Class) * @see #getAllMergedAnnotations(AnnotatedElement, Class) * @see #getMergedRepeatableAnnotations(AnnotatedElement, Class, Class) - * @throws IllegalArgumentException if the {@code element} or {@code annotationType} - * is {@code null}, or if the container type cannot be resolved */ public static Set getMergedRepeatableAnnotations(AnnotatedElement element, Class annotationType) { @@ -510,13 +510,13 @@ public class AnnotatedElementUtils { * {@link java.lang.annotation.Repeatable} * @return the set of all merged repeatable {@code Annotations} found, * or an empty set if none were found - * @since 4.3 - * @see #getMergedAnnotation(AnnotatedElement, Class) - * @see #getAllMergedAnnotations(AnnotatedElement, Class) * @throws IllegalArgumentException if the {@code element} or {@code annotationType} * is {@code null}, or if the container type cannot be resolved * @throws AnnotationConfigurationException if the supplied {@code containerType} * is not a valid container annotation for the supplied {@code annotationType} + * @since 4.3 + * @see #getMergedAnnotation(AnnotatedElement, Class) + * @see #getAllMergedAnnotations(AnnotatedElement, Class) */ public static Set getMergedRepeatableAnnotations(AnnotatedElement element, Class annotationType, Class containerType) { @@ -803,12 +803,12 @@ public class AnnotatedElementUtils { * @param annotationType the annotation type to find (never {@code null}) * @return the set of all merged repeatable {@code Annotations} found, * or an empty set if none were found + * @throws IllegalArgumentException if the {@code element} or {@code annotationType} + * is {@code null}, or if the container type cannot be resolved * @since 4.3 * @see #findMergedAnnotation(AnnotatedElement, Class) * @see #findAllMergedAnnotations(AnnotatedElement, Class) * @see #findMergedRepeatableAnnotations(AnnotatedElement, Class, Class) - * @throws IllegalArgumentException if the {@code element} or {@code annotationType} - * is {@code null}, or if the container type cannot be resolved */ public static Set findMergedRepeatableAnnotations(AnnotatedElement element, Class annotationType) { @@ -834,13 +834,13 @@ public class AnnotatedElementUtils { * {@link java.lang.annotation.Repeatable} * @return the set of all merged repeatable {@code Annotations} found, * or an empty set if none were found - * @since 4.3 - * @see #findMergedAnnotation(AnnotatedElement, Class) - * @see #findAllMergedAnnotations(AnnotatedElement, Class) * @throws IllegalArgumentException if the {@code element} or {@code annotationType} * is {@code null}, or if the container type cannot be resolved * @throws AnnotationConfigurationException if the supplied {@code containerType} * is not a valid container annotation for the supplied {@code annotationType} + * @since 4.3 + * @see #findMergedAnnotation(AnnotatedElement, Class) + * @see #findAllMergedAnnotations(AnnotatedElement, Class) */ public static Set findMergedRepeatableAnnotations(AnnotatedElement element, Class annotationType, Class containerType) { @@ -1326,9 +1326,9 @@ public class AnnotatedElementUtils { * annotation for the supplied repeatable {@code annotationType} (i.e., * that it declares a {@code value} attribute that holds an array of the * {@code annotationType}). - * @since 4.3 * @throws AnnotationConfigurationException if the supplied {@code containerType} * is not a valid container annotation for the supplied {@code annotationType} + * @since 4.3 */ private static void validateContainerType(Class annotationType, Class containerType) { @@ -1351,9 +1351,6 @@ public class AnnotatedElementUtils { } } - /** - * @since 4.3 - */ private static Set postProcessAndSynthesizeAggregatedResults(AnnotatedElement element, Class annotationType, List aggregatedResults) { diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java index 6788b197ad..46c89574cf 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -63,7 +63,7 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib public void visitEnd() { super.visitEnd(); - Class annotationClass = this.attributes.annotationType(); + Class annotationClass = this.attributes.annotationType(); if (annotationClass != null) { List attributeList = this.attributesMap.get(this.annotationType); if (attributeList == null) { @@ -72,21 +72,23 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib else { attributeList.add(0, this.attributes); } - Set visited = new LinkedHashSet(); - Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass); - if (!ObjectUtils.isEmpty(metaAnnotations)) { - for (Annotation metaAnnotation : metaAnnotations) { - if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) { - recursivelyCollectMetaAnnotations(visited, metaAnnotation); + if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotationClass.getName())) { + Set visited = new LinkedHashSet(); + Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass); + if (!ObjectUtils.isEmpty(metaAnnotations)) { + for (Annotation metaAnnotation : metaAnnotations) { + if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) { + recursivelyCollectMetaAnnotations(visited, metaAnnotation); + } } } - } - if (this.metaAnnotationMap != null) { - Set metaAnnotationTypeNames = new LinkedHashSet(visited.size()); - for (Annotation ann : visited) { - metaAnnotationTypeNames.add(ann.annotationType().getName()); + if (this.metaAnnotationMap != null) { + Set metaAnnotationTypeNames = new LinkedHashSet(visited.size()); + for (Annotation ann : visited) { + metaAnnotationTypeNames.add(ann.annotationType().getName()); + } + this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames); } - this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames); } } } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java b/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java index a38a86bf3d..894116a4d2 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java @@ -136,20 +136,22 @@ class DefaultMvcResult implements MvcResult { " was not set during the specified timeToWait=" + timeToWait); } Object result = this.asyncResult.get(); - Assert.state(result != RESULT_NONE, "Async result for handler [" + this.handler + "] was not set"); + if (result == RESULT_NONE) { + throw new IllegalStateException("Async result for handler [" + this.handler + "] was not set"); + } return this.asyncResult.get(); } /** - * True if is there a latch was not set, or the latch count reached 0. + * True if the latch count reached 0 within the specified timeout. */ private boolean awaitAsyncDispatch(long timeout) { Assert.state(this.asyncDispatchLatch != null, - "The asynDispatch CountDownLatch was not set by the TestDispatcherServlet.\n"); + "The asyncDispatch CountDownLatch was not set by the TestDispatcherServlet."); try { return this.asyncDispatchLatch.await(timeout, TimeUnit.MILLISECONDS); } - catch (InterruptedException e) { + catch (InterruptedException ex) { return false; } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java index 84069c1734..aa2dd5bd4e 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParser.java @@ -119,7 +119,6 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { @Override public BeanDefinition parse(Element element, ParserContext context) { - Object source = context.extractSource(element); CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source); context.pushContainingComponent(compDefinition); @@ -150,13 +149,12 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { for (Element endpointElem : DomUtils.getChildElementsByTagName(element, "stomp-endpoint")) { RuntimeBeanReference requestHandler = registerRequestHandler(endpointElem, stompHandler, context, source); String pathAttribute = endpointElem.getAttribute("path"); - Assert.state(StringUtils.hasText(pathAttribute), "Invalid (no path mapping)"); - List paths = Arrays.asList(StringUtils.tokenizeToStringArray(pathAttribute, ",")); - for (String path : paths) { + Assert.hasText(pathAttribute, "Invalid (no path mapping)"); + for (String path : StringUtils.tokenizeToStringArray(pathAttribute, ",")) { path = path.trim(); - Assert.state(StringUtils.hasText(path), "Invalid path attribute: " + pathAttribute); + Assert.hasText(path, "Invalid path attribute: " + pathAttribute); if (DomUtils.getChildElementByTagName(endpointElem, "sockjs") != null) { - path = path.endsWith("/") ? path + "**" : path + "/**"; + path = (path.endsWith("/") ? path + "**" : path + "/**"); } urlMap.put(path, requestHandler); } @@ -174,7 +172,6 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { } private RuntimeBeanReference registerUserRegistry(Element element, ParserContext context, Object source) { - Element relayElement = DomUtils.getChildElementByTagName(element, "stomp-broker-relay"); boolean multiServer = (relayElement != null && relayElement.hasAttribute("user-registry-broadcast")); @@ -192,9 +189,7 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { } } - private ManagedMap registerHandlerMapping(Element element, - ParserContext context, Object source) { - + private ManagedMap registerHandlerMapping(Element element, ParserContext context, Object source) { RootBeanDefinition handlerMappingDef = new RootBeanDefinition(WebSocketHandlerMapping.class); String orderAttribute = element.getAttribute("order"); @@ -240,6 +235,7 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { } } } + ConstructorArgumentValues argValues = new ConstructorArgumentValues(); if (executor != null) { executor.getPropertyValues().add("threadNamePrefix", name + "-"); @@ -247,6 +243,7 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { registerBeanDefByName(executorName, executor, context, source); argValues.addIndexedArgumentValue(0, new RuntimeBeanReference(executorName)); } + RootBeanDefinition channelDef = new RootBeanDefinition(ExecutorSubscribableChannel.class, argValues, null); ManagedList interceptors = new ManagedList(); if (element != null) { @@ -644,28 +641,29 @@ class MessageBrokerBeanDefinitionParser implements BeanDefinitionParser { return name; } - private static void registerBeanDefByName(String name, RootBeanDefinition beanDef, ParserContext context, Object source) { + private static void registerBeanDefByName( + String name, RootBeanDefinition beanDef, ParserContext context, Object source) { + beanDef.setSource(source); beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); context.getRegistry().registerBeanDefinition(name, beanDef); context.registerComponent(new BeanComponentDefinition(beanDef, name)); } + private static class DecoratingFactoryBean implements FactoryBean { private final WebSocketHandler handler; private final List factories; - - @SuppressWarnings("unused") - private DecoratingFactoryBean(WebSocketHandler handler, List factories) { + public DecoratingFactoryBean(WebSocketHandler handler, List factories) { this.handler = handler; this.factories = factories; } @Override - public WebSocketHandler getObject() throws Exception { + public WebSocketHandler getObject() { WebSocketHandler result = this.handler; for (WebSocketHandlerDecoratorFactory factory : this.factories) { result = factory.decorate(result);