From d51ef73304c72e7c034f7c9cc3fb529ed2274e52 Mon Sep 17 00:00:00 2001 From: May Date: Wed, 24 Jun 2020 09:56:31 +0800 Subject: [PATCH 1/2] simplify the code, replace if with switch --- .../ViewControllerBeanDefinitionParser.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java index 733822d14fec..f08152d3f652 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java @@ -81,24 +81,25 @@ public BeanDefinition parse(Element element, ParserContext parserContext) { } String name = element.getLocalName(); - if (name.equals("view-controller")) { - if (element.hasAttribute("view-name")) { - controller.getPropertyValues().add("viewName", element.getAttribute("view-name")); - } - if (statusCode != null) { + switch (name) { + case "view-controller": + if (element.hasAttribute("view-name")) { + controller.getPropertyValues().add("viewName", element.getAttribute("view-name")); + } + if (statusCode != null) { + controller.getPropertyValues().add("statusCode", statusCode); + } + break; + case "redirect-view-controller": + controller.getPropertyValues().add("view", getRedirectView(element, statusCode, source)); + break; + case "status-controller": controller.getPropertyValues().add("statusCode", statusCode); - } - } - else if (name.equals("redirect-view-controller")) { - controller.getPropertyValues().add("view", getRedirectView(element, statusCode, source)); - } - else if (name.equals("status-controller")) { - controller.getPropertyValues().add("statusCode", statusCode); - controller.getPropertyValues().add("statusOnly", true); - } - else { - // Should never happen... - throw new IllegalStateException("Unexpected tag name: " + name); + controller.getPropertyValues().add("statusOnly", true); + break; + default: + // Should never happen... + throw new IllegalStateException("Unexpected tag name: " + name); } Map urlMap = (Map) hm.getPropertyValues().get("urlMap"); From f914c34c45214d56bf41685668e33e5a1eece06c Mon Sep 17 00:00:00 2001 From: May Date: Fri, 26 Jun 2020 20:06:19 +0800 Subject: [PATCH 2/2] In SimpleBufferingAsyncClientHttpRequest And SimpleStreamingAsyncClientHttpRequest class, Anonymous type can be replaced with lambda --- ...SimpleBufferingAsyncClientHttpRequest.java | 37 +++++++++---------- ...SimpleStreamingAsyncClientHttpRequest.java | 29 +++++++-------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java index 2be5c170df02..25a4ca724449 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleBufferingAsyncClientHttpRequest.java @@ -76,27 +76,24 @@ public URI getURI() { protected ListenableFuture executeInternal( final HttpHeaders headers, final byte[] bufferedOutput) throws IOException { - return this.taskExecutor.submitListenable(new Callable() { - @Override - public ClientHttpResponse call() throws Exception { - SimpleBufferingClientHttpRequest.addHeaders(connection, headers); - // JDK <1.8 doesn't support getOutputStream with HTTP DELETE - if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { - connection.setDoOutput(false); - } - if (connection.getDoOutput() && outputStreaming) { - connection.setFixedLengthStreamingMode(bufferedOutput.length); - } - connection.connect(); - if (connection.getDoOutput()) { - FileCopyUtils.copy(bufferedOutput, connection.getOutputStream()); - } - else { - // Immediately trigger the request in a no-output scenario as well - connection.getResponseCode(); - } - return new SimpleClientHttpResponse(connection); + return this.taskExecutor.submitListenable(() -> { + SimpleBufferingClientHttpRequest.addHeaders(connection, headers); + // JDK <1.8 doesn't support getOutputStream with HTTP DELETE + if (getMethod() == HttpMethod.DELETE && bufferedOutput.length == 0) { + connection.setDoOutput(false); } + if (connection.getDoOutput() && outputStreaming) { + connection.setFixedLengthStreamingMode(bufferedOutput.length); + } + connection.connect(); + if (connection.getDoOutput()) { + FileCopyUtils.copy(bufferedOutput, connection.getOutputStream()); + } + else { + // Immediately trigger the request in a no-output scenario as well + connection.getResponseCode(); + } + return new SimpleClientHttpResponse(connection); }); } diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java index de4da24aabdf..effec019e66e 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleStreamingAsyncClientHttpRequest.java @@ -103,25 +103,22 @@ protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException { @Override protected ListenableFuture executeInternal(final HttpHeaders headers) throws IOException { - return this.taskExecutor.submitListenable(new Callable() { - @Override - public ClientHttpResponse call() throws Exception { - try { - if (body != null) { - body.close(); - } - else { - SimpleBufferingClientHttpRequest.addHeaders(connection, headers); - connection.connect(); - // Immediately trigger the request in a no-output scenario as well - connection.getResponseCode(); - } + return this.taskExecutor.submitListenable(() -> { + try { + if (body != null) { + body.close(); } - catch (IOException ex) { - // ignore + else { + SimpleBufferingClientHttpRequest.addHeaders(connection, headers); + connection.connect(); + // Immediately trigger the request in a no-output scenario as well + connection.getResponseCode(); } - return new SimpleClientHttpResponse(connection); } + catch (IOException ex) { + // ignore + } + return new SimpleClientHttpResponse(connection); }); }