From bfddbbe7314c9a9ed74d1ce75f2280c055e26c2c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 1 Mar 2018 01:39:06 +0100 Subject: [PATCH] Polishing --- .../groovy/GroovyBeanDefinitionWrapper.java | 3 +- .../test/context/TestContextManager.java | 53 ++++++++++--------- .../condition/CompositeRequestCondition.java | 5 +- .../condition/ConsumesRequestCondition.java | 21 +++----- .../condition/ProducesRequestCondition.java | 18 +++---- .../ConcurrentWebSocketSessionDecorator.java | 8 +-- 6 files changed, 51 insertions(+), 57 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java index fdda11ff56..b54adc2e51 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionWrapper.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. @@ -161,6 +161,7 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport { return this; } + @Override public Object getProperty(String property) { if (this.definitionWrapper.isReadableProperty(property)) { 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 20288d7004..7dc086f74d 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 @@ -94,13 +94,8 @@ public class TestContextManager { private final TestContext testContext; - private final ThreadLocal testContextHolder = new ThreadLocal() { - - @Override - protected TestContext initialValue() { - return copyTestContext(TestContextManager.this.testContext); - } - }; + private final ThreadLocal testContextHolder = + ThreadLocal.withInitial(() -> copyTestContext(TestContextManager.this.testContext)); private final List testExecutionListeners = new ArrayList<>(); @@ -364,11 +359,13 @@ public class TestContextManager { * @see #getTestExecutionListeners() * @see Throwable#addSuppressed(Throwable) */ - public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception { + public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) + throws Exception { + String callbackName = "afterTestExecution"; prepareForAfterCallback(callbackName, testInstance, testMethod, exception); - Throwable afterTestExecutionException = null; + // Traverse the TestExecutionListeners in reverse order to ensure proper // "wrapper"-style execution of listeners. for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { @@ -385,6 +382,7 @@ public class TestContextManager { } } } + if (afterTestExecutionException != null) { ReflectionUtils.rethrowException(afterTestExecutionException); } @@ -414,9 +412,8 @@ public class TestContextManager { * @param testInstance the current test instance (never {@code null}) * @param testMethod the test method which has just been executed on the * test instance - * @param exception the exception that was thrown during execution of the - * test method or by a TestExecutionListener, or {@code null} if none - * was thrown + * @param exception the exception that was thrown during execution of the test + * method or by a TestExecutionListener, or {@code null} if none was thrown * @throws Exception if a registered TestExecutionListener throws an exception * @see #beforeTestMethod * @see #beforeTestExecution @@ -424,11 +421,13 @@ public class TestContextManager { * @see #getTestExecutionListeners() * @see Throwable#addSuppressed(Throwable) */ - public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception { + public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) + throws Exception { + String callbackName = "afterTestMethod"; prepareForAfterCallback(callbackName, testInstance, testMethod, exception); - Throwable afterTestMethodException = null; + // Traverse the TestExecutionListeners in reverse order to ensure proper // "wrapper"-style execution of listeners. for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) { @@ -445,6 +444,7 @@ public class TestContextManager { } } } + if (afterTestMethodException != null) { ReflectionUtils.rethrowException(afterTestMethodException); } @@ -510,33 +510,36 @@ public class TestContextManager { @Nullable Throwable exception) { if (logger.isTraceEnabled()) { - logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", callbackName, testInstance, - testMethod, exception)); + logger.trace(String.format("%s(): instance [%s], method [%s], exception [%s]", + callbackName, testInstance, testMethod, exception)); } getTestContext().updateState(testInstance, testMethod, exception); } private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) throws Exception { + logException(ex, callbackName, testExecutionListener, testInstance, testMethod); ReflectionUtils.rethrowException(ex); } - private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, - Class testClass) { + private void logException( + Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class testClass) { + if (logger.isWarnEnabled()) { logger.warn(String.format("Caught exception while invoking '%s' callback on " + - "TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener, - testClass), ex); + "TestExecutionListener [%s] for test class [%s]", callbackName, testExecutionListener, + testClass), ex); } } private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) { + if (logger.isWarnEnabled()) { logger.warn(String.format("Caught exception while invoking '%s' callback on " + - "TestExecutionListener [%s] for test method [%s] and test instance [%s]", - callbackName, testExecutionListener, testMethod, testInstance), ex); + "TestExecutionListener [%s] for test method [%s] and test instance [%s]", + callbackName, testExecutionListener, testMethod, testInstance), ex); } } @@ -546,8 +549,8 @@ public class TestContextManager { * copy constructor. */ private static TestContext copyTestContext(TestContext testContext) { - Constructor constructor = ClassUtils.getConstructorIfAvailable(testContext.getClass(), - testContext.getClass()); + Constructor constructor = + ClassUtils.getConstructorIfAvailable(testContext.getClass(), testContext.getClass()); if (constructor != null) { try { @@ -563,7 +566,7 @@ public class TestContextManager { } } - // fallback to original instance + // Fallback to original instance return testContext; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.java index 9b64420471..c464d7e548 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.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.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; - import javax.servlet.http.HttpServletRequest; import org.springframework.lang.Nullable; @@ -77,7 +76,7 @@ public class CompositeRequestCondition extends AbstractRequestCondition> getConditions() { return unwrap(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java index 2f43304052..5dc25bae4d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.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. @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.condition; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -49,7 +48,6 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition expressions; @@ -159,7 +157,7 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition result = new LinkedHashSet<>(this.expressions); - for (Iterator iterator = result.iterator(); iterator.hasNext();) { - ConsumeMediaTypeExpression expression = iterator.next(); - if (!expression.match(contentType)) { - iterator.remove(); - } - } - return (result.isEmpty()) ? null : new ConsumesRequestCondition(result); + result.removeIf(expression -> !expression.match(contentType)); + return (!result.isEmpty() ? new ConsumesRequestCondition(result) : null); } /** diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java index 9ccfbe6c47..660b36106d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ProducesRequestCondition.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. @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.condition; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -48,14 +47,14 @@ import org.springframework.web.servlet.mvc.condition.HeadersRequestCondition.Hea */ public final class ProducesRequestCondition extends AbstractRequestCondition { - private final static ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition(); + private static final ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition(); private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition(); - private static final List MEDIA_TYPE_ALL_LIST = Collections.singletonList(new ProduceMediaTypeExpression("*/*")); + private final List expressions; private final ContentNegotiationManager contentNegotiationManager; @@ -194,6 +193,7 @@ public final class ProducesRequestCondition extends AbstractRequestCondition acceptedMediaTypes; try { acceptedMediaTypes = getAcceptedMediaTypes(request); @@ -201,13 +201,9 @@ public final class ProducesRequestCondition extends AbstractRequestCondition result = new LinkedHashSet<>(expressions); - for (Iterator iterator = result.iterator(); iterator.hasNext();) { - ProduceMediaTypeExpression expression = iterator.next(); - if (!expression.match(acceptedMediaTypes)) { - iterator.remove(); - } - } + + Set result = new LinkedHashSet<>(this.expressions); + result.removeIf(expression -> !expression.match(acceptedMediaTypes)); if (!result.isEmpty()) { return new ProducesRequestCondition(result, this.contentNegotiationManager); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java b/spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java index fb0a9b2be2..dc93ade1f9 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecorator.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. @@ -112,6 +112,7 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat return (start > 0 ? (System.currentTimeMillis() - start) : 0); } + @Override public void sendMessage(WebSocketMessage message) throws IOException { if (shouldNotSend()) { @@ -124,10 +125,9 @@ public class ConcurrentWebSocketSessionDecorator extends WebSocketSessionDecorat do { if (!tryFlushMessageBuffer()) { if (logger.isTraceEnabled()) { - String text = String.format("Another send already in progress: " + + logger.trace(String.format("Another send already in progress: " + "session id '%s':, \"in-progress\" send time %d (ms), buffer size %d bytes", - getId(), getTimeSinceSendStarted(), getBufferSize()); - logger.trace(text); + getId(), getTimeSinceSendStarted(), getBufferSize())); } checkSessionLimits(); break;