From ea534b68201682a9352964b1f9e5b5b50ef932ed Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 29 Jun 2018 20:07:53 +0200 Subject: [PATCH] Polishing --- .../aspectj/JtaAnnotationTransactionAspect.aj | 2 +- .../MessageMethodArgumentResolver.java | 4 +-- .../reactive/MockClientHttpRequest.java | 5 ++-- .../reactive/MockServerHttpResponse.java | 5 +--- .../AbstractRequestExpectationManager.java | 5 +++- .../ServerSentEventHttpMessageReader.java | 2 +- .../AbstractMessageWriterResultHandler.java | 4 +-- .../reactive/result/view/AbstractView.java | 13 +++++---- ...ionConfigDispatcherHandlerInitializer.java | 1 - .../annotation/ReactiveTypeHandler.java | 27 +++++++++---------- ...ResponseBodyEmitterReturnValueHandler.java | 16 +++++------ .../ServletInvocableHandlerMethod.java | 4 +-- ...reamingResponseBodyReturnValueHandler.java | 5 ++-- 13 files changed, 42 insertions(+), 51 deletions(-) diff --git a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj index 6b02e1de3a5..22a3bb134b5 100644 --- a/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj +++ b/spring-aspects/src/main/java/org/springframework/transaction/aspectj/JtaAnnotationTransactionAspect.aj @@ -45,7 +45,7 @@ import org.springframework.transaction.annotation.AnnotationTransactionAttribute * @see javax.transaction.Transactional * @see AnnotationTransactionAspect */ -@RequiredTypes({"javax.transaction.Transactional"}) +@RequiredTypes("javax.transaction.Transactional") public aspect JtaAnnotationTransactionAspect extends AbstractTransactionAspect { public JtaAnnotationTransactionAspect() { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java index 2d695d8adff..8a337715a50 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 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. @@ -97,7 +97,7 @@ public class MessageMethodArgumentResolver implements HandlerMethodArgumentResol private Class getPayloadType(MethodParameter parameter) { Type genericParamType = parameter.getGenericParameterType(); ResolvableType resolvableType = ResolvableType.forType(genericParamType).as(Message.class); - return resolvableType.getGeneric(0).resolve(Object.class); + return resolvableType.getGeneric().resolve(Object.class); } /** diff --git a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java index d6fc9ebd002..0ebf84dc1d8 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/http/client/reactive/MockClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 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. @@ -36,6 +36,7 @@ import org.springframework.web.util.UriComponentsBuilder; /** * Mock implementation of {@link ClientHttpRequest}. + * * @author Brian Clozel * @author Rossen Stoyanchev * @since 5.0 @@ -97,11 +98,9 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest { /** * Configure a custom handler for writing the request body. - * *

The default write handler consumes and caches the request body so it * may be accessed subsequently, e.g. in test assertions. Use this property * when the request body is an infinite stream. - * * @param writeHandler the write handler to use returning {@code Mono} * when the body has been "written" (i.e. consumed). */ diff --git a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java index 14665c9c93a..325fb3d4619 100644 --- a/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/http/server/reactive/MockServerHttpResponse.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. @@ -125,10 +125,8 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse { * charset or "UTF-8" by default. */ public Mono getBodyAsString() { - Charset charset = Optional.ofNullable(getHeaders().getContentType()).map(MimeType::getCharset) .orElse(StandardCharsets.UTF_8); - return getBody() .reduce(bufferFactory().allocateBuffer(), (previous, current) -> { previous.write(current); @@ -139,7 +137,6 @@ public class MockServerHttpResponse extends AbstractServerHttpResponse { } private static String bufferToString(DataBuffer buffer, Charset charset) { - Assert.notNull(charset, "'charset' must not be null"); byte[] bytes = new byte[buffer.readableByteCount()]; buffer.read(bytes); return new String(bytes, charset); diff --git a/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java b/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java index c62200b3e7b..460d7b4f3e5 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java @@ -239,13 +239,16 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect /** * Add expectations to this group. - * @deprecated as of 5.0.3 please use {@link #addAllExpectations(Collection)} instead. + * @deprecated as of 5.0.3, if favor of {@link #addAllExpectations} */ @Deprecated public void updateAll(Collection expectations) { expectations.forEach(this::updateInternal); } + /** + * Reset all expectations for this group. + */ public void reset() { this.expectations.clear(); } diff --git a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageReader.java b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageReader.java index 1d17ba88363..1897a56d741 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageReader.java +++ b/spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageReader.java @@ -103,7 +103,7 @@ public class ServerSentEventHttpMessageReader implements HttpMessageReader hints) { boolean shouldWrap = isServerSentEvent(elementType); - ResolvableType valueType = (shouldWrap ? elementType.getGeneric(0) : elementType); + ResolvableType valueType = (shouldWrap ? elementType.getGeneric() : elementType); return stringDecoder.decode(message.getBody(), STRING_TYPE, null, Collections.emptyMap()) .bufferUntil(line -> line.equals("")) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java index c73daa3f82a..8fbfe0af9c3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.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. @@ -123,7 +123,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa ResolvableType elementType; if (adapter != null) { publisher = adapter.toPublisher(body); - ResolvableType genericType = bodyType.getGeneric(0); + ResolvableType genericType = bodyType.getGeneric(); elementType = getElementType(adapter, genericType); } else { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java index 5841b899a9a..601ca736f8a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java @@ -56,9 +56,9 @@ public abstract class AbstractView implements View, ApplicationContextAware { private static final Object NO_VALUE = new Object(); - private final List mediaTypes = new ArrayList<>(4); + private final ReactiveAdapterRegistry reactiveAdapterRegistry; - private final ReactiveAdapterRegistry adapterRegistry; + private final List mediaTypes = new ArrayList<>(4); private Charset defaultCharset = StandardCharsets.UTF_8; @@ -73,9 +73,9 @@ public abstract class AbstractView implements View, ApplicationContextAware { this(ReactiveAdapterRegistry.getSharedInstance()); } - public AbstractView(ReactiveAdapterRegistry registry) { + public AbstractView(ReactiveAdapterRegistry reactiveAdapterRegistry) { + this.reactiveAdapterRegistry = reactiveAdapterRegistry; this.mediaTypes.add(ViewResolverSupport.DEFAULT_CONTENT_TYPE); - this.adapterRegistry = registry; } @@ -155,7 +155,7 @@ public abstract class AbstractView implements View, ApplicationContextAware { /** * Prepare the model to render. - * @param model Map with name Strings as keys and corresponding model + * @param model a Map with name Strings as keys and corresponding model * objects as values (Map can also be {@code null} in case of empty model) * @param contentType the content type selected to render with which should * match one of the {@link #getSupportedMediaTypes() supported media types}. @@ -209,7 +209,6 @@ public abstract class AbstractView implements View, ApplicationContextAware { * @return {@code Mono} for the completion of async attributes resolution */ protected Mono resolveAsyncAttributes(Map model) { - List names = new ArrayList<>(); List> valueMonos = new ArrayList<>(); @@ -218,7 +217,7 @@ public abstract class AbstractView implements View, ApplicationContextAware { if (value == null) { continue; } - ReactiveAdapter adapter = this.adapterRegistry.getAdapter(null, value); + ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(null, value); if (adapter != null) { names.add(entry.getKey()); if (adapter.isMultiValue()) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractAnnotationConfigDispatcherHandlerInitializer.java b/spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractAnnotationConfigDispatcherHandlerInitializer.java index ff9dafe9ef8..f731a901c9c 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractAnnotationConfigDispatcherHandlerInitializer.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractAnnotationConfigDispatcherHandlerInitializer.java @@ -35,7 +35,6 @@ import org.springframework.util.ObjectUtils; public abstract class AbstractAnnotationConfigDispatcherHandlerInitializer extends AbstractDispatcherHandlerInitializer { - /** * {@inheritDoc} *

This implementation creates an {@link AnnotationConfigApplicationContext}, diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java index 0a6a2fd15c1..bd4ecd73f3d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.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. @@ -86,9 +86,7 @@ class ReactiveTypeHandler { this(ReactiveAdapterRegistry.getSharedInstance(), new SyncTaskExecutor(), new ContentNegotiationManager()); } - ReactiveTypeHandler(ReactiveAdapterRegistry registry, TaskExecutor executor, - ContentNegotiationManager manager) { - + ReactiveTypeHandler(ReactiveAdapterRegistry registry, TaskExecutor executor, ContentNegotiationManager manager) { Assert.notNull(registry, "ReactiveAdapterRegistry is required"); Assert.notNull(executor, "TaskExecutor is required"); Assert.notNull(manager, "ContentNegotiationManager is required"); @@ -120,7 +118,7 @@ class ReactiveTypeHandler { ReactiveAdapter adapter = this.reactiveRegistry.getAdapter(returnValue.getClass()); Assert.state(adapter != null, "Unexpected return value: " + returnValue); - ResolvableType elementType = ResolvableType.forMethodParameter(returnType).getGeneric(0); + ResolvableType elementType = ResolvableType.forMethodParameter(returnType).getGeneric(); Class elementClass = elementType.resolve(Object.class); Collection mediaTypes = getMediaTypes(request); @@ -249,7 +247,7 @@ class ReactiveTypeHandler { schedule(); } } - + private void schedule() { try { this.taskExecutor.execute(this); @@ -264,7 +262,7 @@ class ReactiveTypeHandler { } } } - + @Override public void run() { if (this.done) { @@ -310,7 +308,7 @@ class ReactiveTypeHandler { } return; } - + if (this.executing.decrementAndGet() != 0) { schedule(); } @@ -324,7 +322,6 @@ class ReactiveTypeHandler { this.subscription.cancel(); } } - } @@ -407,16 +404,12 @@ class ReactiveTypeHandler { private final CollectedValuesList values; - - DeferredResultSubscriber(DeferredResult result, ReactiveAdapter adapter, - ResolvableType elementType) { - + DeferredResultSubscriber(DeferredResult result, ReactiveAdapter adapter, ResolvableType elementType) { this.result = result; this.multiValueSource = adapter.isMultiValue(); this.values = new CollectedValuesList(elementType); } - public void connect(ReactiveAdapter adapter, Object returnValue) { Publisher publisher = adapter.toPublisher(returnValue); publisher.subscribe(this); @@ -452,6 +445,10 @@ class ReactiveTypeHandler { } } + + /** + * List of collect values where all elements are a specified type. + */ @SuppressWarnings("serial") static class CollectedValuesList extends ArrayList { @@ -466,4 +463,4 @@ class ReactiveTypeHandler { } } -} \ No newline at end of file +} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java index d361090cc80..168e2004e11 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.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. @@ -20,7 +20,6 @@ import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.function.Consumer; - import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletResponse; @@ -83,17 +82,14 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur /** * Complete constructor with pluggable "reactive" type support. - * * @param messageConverters converters to write emitted objects with * @param reactiveRegistry for reactive return value type support * @param executor for blocking I/O writes of items emitted from reactive types * @param manager for detecting streaming media types - * * @since 5.0 */ public ResponseBodyEmitterReturnValueHandler(List> messageConverters, - ReactiveAdapterRegistry reactiveRegistry, TaskExecutor executor, - ContentNegotiationManager manager) { + ReactiveAdapterRegistry reactiveRegistry, TaskExecutor executor, ContentNegotiationManager manager) { Assert.notEmpty(messageConverters, "HttpMessageConverter List must not be empty"); this.messageConverters = messageConverters; @@ -103,16 +99,16 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur @Override public boolean supportsReturnType(MethodParameter returnType) { - Class bodyType = ResponseEntity.class.isAssignableFrom(returnType.getParameterType()) ? - ResolvableType.forMethodParameter(returnType).getGeneric(0).resolve() : + ResolvableType.forMethodParameter(returnType).getGeneric().resolve() : returnType.getParameterType(); - return bodyType != null && (ResponseBodyEmitter.class.isAssignableFrom(bodyType) || - this.reactiveHandler.isReactiveType(bodyType)); + return (bodyType != null && (ResponseBodyEmitter.class.isAssignableFrom(bodyType) || + this.reactiveHandler.isReactiveType(bodyType))); } @Override + @SuppressWarnings("resource") public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java index dc1206f438c..8df1a2dc0c2 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletInvocableHandlerMethod.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. @@ -259,7 +259,7 @@ public class ServletInvocableHandlerMethod extends InvocableHandlerMethod { this.returnValue = returnValue; this.returnType = (returnValue instanceof ReactiveTypeHandler.CollectedValuesList ? ((ReactiveTypeHandler.CollectedValuesList) returnValue).getReturnType() : - ResolvableType.forType(super.getGenericParameterType()).getGeneric(0)); + ResolvableType.forType(super.getGenericParameterType()).getGeneric()); } public ConcurrentResultMethodParameter(ConcurrentResultMethodParameter original) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.java index 92e076a1fcc..9d85287dc04 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/StreamingResponseBodyReturnValueHandler.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. @@ -50,13 +50,14 @@ public class StreamingResponseBodyReturnValueHandler implements HandlerMethodRet return true; } else if (ResponseEntity.class.isAssignableFrom(returnType.getParameterType())) { - Class bodyType = ResolvableType.forMethodParameter(returnType).getGeneric(0).resolve(); + Class bodyType = ResolvableType.forMethodParameter(returnType).getGeneric().resolve(); return (bodyType != null && StreamingResponseBody.class.isAssignableFrom(bodyType)); } return false; } @Override + @SuppressWarnings("resource") public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {