From 6d11b40353476b523ed2b85c0a99649225decdb0 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 16 Feb 2018 19:48:43 +0100 Subject: [PATCH] Consistent use of StringUtils.toStringArray --- .../support/DefaultListableBeanFactory.java | 6 +- .../support/DefaultSingletonBeanRegistry.java | 4 +- .../support/StaticListableBeanFactory.java | 4 +- .../PropertyResourceConfigurerTests.java | 7 +- .../CachingConfigurationSelector.java | 7 +- .../core/AttributeAccessorSupport.java | 5 +- .../env/SimpleCommandLinePropertySource.java | 6 +- .../BeanPropertySqlParameterSource.java | 3 +- .../jdbc/core/JdbcTemplateTests.java | 5 +- .../jdbc/object/SqlQueryTests.java | 7 +- .../StompBrokerRelayRegistrationTests.java | 6 +- .../mock/web/MockHttpSession.java | 5 +- .../test/web/ModelAndViewAssert.java | 93 ++++++++----------- .../support/HandlerMethodInvoker.java | 5 +- ...AnnotationConfigWebApplicationContext.java | 4 +- .../web/filter/HttpPutFormContentFilter.java | 7 +- .../web/util/HierarchicalUriComponents.java | 2 +- .../mock/web/test/MockHttpSession.java | 5 +- ...eMarkerConfigurerBeanDefinitionParser.java | 5 +- ...emplateConfigurerBeanDefinitionParser.java | 5 +- .../TilesConfigurerBeanDefinitionParser.java | 5 +- .../annotation/InterceptorRegistration.java | 12 +-- .../servlet/handler/MappedInterceptor.java | 31 +++---- .../resource/VersionResourceResolver.java | 4 +- .../AbstractWebSocketHandlerRegistration.java | 6 +- ...MvcStompWebSocketEndpointRegistration.java | 5 +- .../support/AbstractHandshakeHandler.java | 8 +- 27 files changed, 125 insertions(+), 137 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 8269379bfe..cf7d8de329 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.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. @@ -553,7 +553,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto results.add(beanName); } } - return results.toArray(new String[results.size()]); + return StringUtils.toStringArray(results); } @Override @@ -1009,7 +1009,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } } if (!autowireCandidates.isEmpty()) { - candidateNames = autowireCandidates.toArray(new String[autowireCandidates.size()]); + candidateNames = StringUtils.toStringArray(autowireCandidates); } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java index 17b949a1c4..5952878552 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.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. @@ -504,7 +504,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements if (dependenciesForBean == null) { return new String[0]; } - return dependenciesForBean.toArray(new String[dependenciesForBean.size()]); + return StringUtils.toStringArray(dependenciesForBean); } public void destroySingletons() { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java index 12eaf2d54f..f5aee86bcf 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.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. @@ -333,7 +333,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory { results.add(beanName); } } - return results.toArray(new String[results.size()]); + return StringUtils.toStringArray(results); } @Override diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java index 6cccbc6d01..94d5971be4 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -42,6 +42,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.core.io.Resource; import org.springframework.tests.sample.beans.IndexedTestBean; import org.springframework.tests.sample.beans.TestBean; +import org.springframework.util.StringUtils; import static org.junit.Assert.*; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.*; @@ -838,12 +839,12 @@ public class PropertyResourceConfigurerTests { @Override protected String[] keysSpi() throws BackingStoreException { - return values.keySet().toArray(new String[values.size()]); + return StringUtils.toStringArray(values.keySet()); } @Override protected String[] childrenNamesSpi() throws BackingStoreException { - return children.keySet().toArray(new String[values.size()]); + return StringUtils.toStringArray(children.keySet()); } @Override diff --git a/spring-context/src/main/java/org/springframework/cache/annotation/CachingConfigurationSelector.java b/spring-context/src/main/java/org/springframework/cache/annotation/CachingConfigurationSelector.java index f7f6fa4ec7..d0dddf1fe2 100644 --- a/spring-context/src/main/java/org/springframework/cache/annotation/CachingConfigurationSelector.java +++ b/spring-context/src/main/java/org/springframework/cache/annotation/CachingConfigurationSelector.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. @@ -23,6 +23,7 @@ import org.springframework.context.annotation.AdviceMode; import org.springframework.context.annotation.AdviceModeImportSelector; import org.springframework.context.annotation.AutoProxyRegistrar; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** * Selects which implementation of {@link AbstractCachingConfiguration} should be used @@ -84,7 +85,7 @@ public class CachingConfigurationSelector extends AdviceModeImportSelector list = execute(); - String[] results = list.toArray(new String[list.size()]); - return results; + return StringUtils.toStringArray(execute()); } } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/config/StompBrokerRelayRegistrationTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/config/StompBrokerRelayRegistrationTests.java index fcca909d3b..b9fb84077b 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/config/StompBrokerRelayRegistrationTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/config/StompBrokerRelayRegistrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -22,6 +22,7 @@ import org.springframework.messaging.MessageChannel; import org.springframework.messaging.StubMessageChannel; import org.springframework.messaging.SubscribableChannel; import org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler; +import org.springframework.util.StringUtils; import static org.junit.Assert.*; @@ -33,7 +34,6 @@ import static org.junit.Assert.*; */ public class StompBrokerRelayRegistrationTests { - @Test public void test() { @@ -52,7 +52,7 @@ public class StompBrokerRelayRegistrationTests { StompBrokerRelayMessageHandler handler = registration.getMessageHandler(new StubMessageChannel()); - assertArrayEquals(prefixes, handler.getDestinationPrefixes().toArray(new String[2])); + assertArrayEquals(prefixes, StringUtils.toStringArray(handler.getDestinationPrefixes())); assertEquals("clientlogin", handler.getClientLogin()); assertEquals("clientpasscode", handler.getClientPasscode()); assertEquals("syslogin", handler.getSystemLogin()); diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java index cae49f69fb..11082c8c34 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 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. @@ -30,6 +30,7 @@ import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. @@ -172,7 +173,7 @@ public class MockHttpSession implements HttpSession { @Override public String[] getValueNames() { assertIsValid(); - return this.attributes.keySet().toArray(new String[this.attributes.size()]); + return StringUtils.toStringArray(this.attributes.keySet()); } @Override diff --git a/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java b/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java index 6cbbfcd977..b348695f02 100644 --- a/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java +++ b/spring-test/src/main/java/org/springframework/test/web/ModelAndViewAssert.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 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. @@ -32,8 +32,8 @@ import static org.springframework.test.util.AssertionErrors.*; * A collection of assertions intended to simplify testing scenarios dealing * with Spring Web MVC {@link org.springframework.web.servlet.ModelAndView * ModelAndView} objects. - *

- * Intended for use with JUnit 4 and TestNG. All {@code assert*()} methods + * + *

Intended for use with JUnit 4 and TestNG. All {@code assert*()} methods * throw {@link AssertionError}s. * * @author Sam Brannen @@ -48,94 +48,82 @@ public abstract class ModelAndViewAssert { * Checks whether the model value under the given {@code modelName} * exists and checks it type, based on the {@code expectedType}. If the * model entry exists and the type matches, the model value is returned. - * * @param mav ModelAndView to test against (never {@code null}) - * @param modelName name of the object to add to the model (never - * {@code null}) + * @param modelName name of the object to add to the model (never {@code null}) * @param expectedType expected type of the model value * @return the model value */ @SuppressWarnings("unchecked") public static T assertAndReturnModelAttributeOfType(ModelAndView mav, String modelName, Class expectedType) { assertTrue("ModelAndView is null", mav != null); - assertTrue("Model is null", mav.getModel() != null); Object obj = mav.getModel().get(modelName); assertTrue("Model attribute with name '" + modelName + "' is null", obj != null); - assertTrue("Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" - + obj.getClass().getName() + "'", expectedType.isAssignableFrom(obj.getClass())); + assertTrue("Model attribute is not of expected type '" + expectedType.getName() + "' but rather of type '" + + obj.getClass().getName() + "'", expectedType.isAssignableFrom(obj.getClass())); return (T) obj; } /** * Compare each individual entry in a list, without first sorting the lists. - * * @param mav ModelAndView to test against (never {@code null}) - * @param modelName name of the object to add to the model (never - * {@code null}) + * @param modelName name of the object to add to the model (never {@code null}) * @param expectedList the expected list */ @SuppressWarnings("rawtypes") public static void assertCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList) { assertTrue("ModelAndView is null", mav != null); List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); - assertTrue( - "Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() - + "'", expectedList.size() == modelList.size()); + assertTrue("Size of model list is '" + modelList.size() + "' while size of expected list is '" + + expectedList.size() + "'", expectedList.size() == modelList.size()); assertTrue("List in model under name '" + modelName + "' is not equal to the expected list.", - expectedList.equals(modelList)); + expectedList.equals(modelList)); } /** * Assert whether or not a model attribute is available. - * * @param mav ModelAndView to test against (never {@code null}) - * @param modelName name of the object to add to the model (never - * {@code null}) + * @param modelName name of the object to add to the model (never {@code null}) */ public static void assertModelAttributeAvailable(ModelAndView mav, String modelName) { assertTrue("ModelAndView is null", mav != null); - assertTrue("Model is null", mav.getModel() != null); assertTrue("Model attribute with name '" + modelName + "' is not available", - mav.getModel().containsKey(modelName)); + mav.getModel().containsKey(modelName)); } /** * Compare a given {@code expectedValue} to the value from the model * bound under the given {@code modelName}. - * * @param mav ModelAndView to test against (never {@code null}) - * @param modelName name of the object to add to the model (never - * {@code null}) + * @param modelName name of the object to add to the model (never {@code null}) * @param expectedValue the model value */ public static void assertModelAttributeValue(ModelAndView mav, String modelName, Object expectedValue) { assertTrue("ModelAndView is null", mav != null); Object modelValue = assertAndReturnModelAttributeOfType(mav, modelName, Object.class); - assertTrue("Model value with name '" + modelName + "' is not the same as the expected value which was '" - + expectedValue + "'", modelValue.equals(expectedValue)); + assertTrue("Model value with name '" + modelName + "' is not the same as the expected value which was '" + + expectedValue + "'", modelValue.equals(expectedValue)); } /** * Inspect the {@code expectedModel} to see if all elements in the * model appear and are equal. - * * @param mav ModelAndView to test against (never {@code null}) * @param expectedModel the expected model */ public static void assertModelAttributeValues(ModelAndView mav, Map expectedModel) { assertTrue("ModelAndView is null", mav != null); - assertTrue("Model is null", mav.getModel() != null); + Map model = mav.getModel(); - if (!mav.getModel().keySet().equals(expectedModel.keySet())) { + if (!model.keySet().equals(expectedModel.keySet())) { StringBuilder sb = new StringBuilder("Keyset of expected model does not match.\n"); - appendNonMatchingSetsErrorMessage(expectedModel.keySet(), mav.getModel().keySet(), sb); + appendNonMatchingSetsErrorMessage(expectedModel.keySet(), model.keySet(), sb); fail(sb.toString()); } StringBuilder sb = new StringBuilder(); - for (String modelName : mav.getModel().keySet()) { + for (String modelName : model.keySet()) { Object assertionValue = expectedModel.get(modelName); - Object mavValue = mav.getModel().get(modelName); + Object mavValue = model.get(modelName); if (!assertionValue.equals(mavValue)) { sb.append("Value under name '").append(modelName).append("' differs, should have been '").append( assertionValue).append("' but was '").append(mavValue).append("'\n"); @@ -151,25 +139,20 @@ public abstract class ModelAndViewAssert { /** * Compare each individual entry in a list after having sorted both lists * (optionally using a comparator). - * * @param mav ModelAndView to test against (never {@code null}) - * @param modelName name of the object to add to the model (never - * {@code null}) + * @param modelName name of the object to add to the model (never {@code null}) * @param expectedList the expected list - * @param comparator the comparator to use (may be {@code null}). If - * not specifying the comparator, both lists will be sorted not using any - * comparator. + * @param comparator the comparator to use (may be {@code null}). If not + * specifying the comparator, both lists will be sorted not using any comparator. */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - public static void assertSortAndCompareListModelAttribute(ModelAndView mav, String modelName, List expectedList, - Comparator comparator) { + @SuppressWarnings({"unchecked", "rawtypes"}) + public static void assertSortAndCompareListModelAttribute( + ModelAndView mav, String modelName, List expectedList, Comparator comparator) { assertTrue("ModelAndView is null", mav != null); List modelList = assertAndReturnModelAttributeOfType(mav, modelName, List.class); - - assertTrue( - "Size of model list is '" + modelList.size() + "' while size of expected list is '" + expectedList.size() - + "'", expectedList.size() == modelList.size()); + assertTrue("Size of model list is '" + modelList.size() + "' while size of expected list is '" + + expectedList.size() + "'", expectedList.size() == modelList.size()); if (comparator != null) { Collections.sort(modelList, comparator); @@ -181,30 +164,29 @@ public abstract class ModelAndViewAssert { } assertTrue("List in model under name '" + modelName + "' is not equal to the expected list.", - expectedList.equals(modelList)); + expectedList.equals(modelList)); } /** * Check to see if the view name in the ModelAndView matches the given * {@code expectedName}. - * * @param mav ModelAndView to test against (never {@code null}) * @param expectedName the name of the model value */ public static void assertViewName(ModelAndView mav, String expectedName) { assertTrue("ModelAndView is null", mav != null); assertTrue("View name is not equal to '" + expectedName + "' but was '" + mav.getViewName() + "'", - ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); + ObjectUtils.nullSafeEquals(expectedName, mav.getViewName())); } - private static void appendNonMatchingSetsErrorMessage(Set assertionSet, Set incorrectSet, - StringBuilder sb) { - Set tempSet = new HashSet(); - tempSet.addAll(incorrectSet); + private static void appendNonMatchingSetsErrorMessage( + Set assertionSet, Set incorrectSet, StringBuilder sb) { + + Set tempSet = new HashSet(incorrectSet); tempSet.removeAll(assertionSet); - if (tempSet.size() > 0) { + if (!tempSet.isEmpty()) { sb.append("Set has too many elements:\n"); for (Object element : tempSet) { sb.append('-'); @@ -213,11 +195,10 @@ public abstract class ModelAndViewAssert { } } - tempSet = new HashSet(); - tempSet.addAll(assertionSet); + tempSet = new HashSet(assertionSet); tempSet.removeAll(incorrectSet); - if (tempSet.size() > 0) { + if (!tempSet.isEmpty()) { sb.append("Set is missing elements:\n"); for (Object element : tempSet) { sb.append('-'); diff --git a/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java b/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java index 9287882baf..40a67874af 100644 --- a/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java +++ b/spring-web/src/main/java/org/springframework/web/bind/annotation/support/HandlerMethodInvoker.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -57,6 +57,7 @@ import org.springframework.util.ClassUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; import org.springframework.validation.BindException; import org.springframework.validation.BindingResult; import org.springframework.validation.Errors; @@ -205,7 +206,7 @@ public class HandlerMethodInvoker { Map model = (mavModel != null ? mavModel : implicitModel); if (model != null) { try { - String[] originalAttrNames = model.keySet().toArray(new String[model.size()]); + String[] originalAttrNames = StringUtils.toStringArray(model.keySet()); for (String attrName : originalAttrNames) { Object attrValue = model.get(attrName); boolean isSessionAttr = this.methodResolver.isSessionAttribute( diff --git a/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java index dd116cdf18..a7be81ad6c 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -215,7 +215,7 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe logger.info("Scanning base packages: [" + StringUtils.collectionToCommaDelimitedString(this.basePackages) + "]"); } - scanner.scan(this.basePackages.toArray(new String[this.basePackages.size()])); + scanner.scan(StringUtils.toStringArray(this.basePackages)); } String[] configLocations = getConfigLocations(); diff --git a/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java b/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java index 8a8146cfba..f7e7e83109 100644 --- a/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.java +++ b/spring-web/src/main/java/org/springframework/web/filter/HttpPutFormContentFilter.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. @@ -42,6 +42,7 @@ import org.springframework.http.server.ServletServerHttpRequest; import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; /** * {@link javax.servlet.Filter} that makes form encoded data available through @@ -168,13 +169,13 @@ public class HttpPutFormContentFilter extends OncePerRequestFilter { return parameterValues; } if (parameterValues == null || getQueryString() == null) { - return formParam.toArray(new String[formParam.size()]); + return StringUtils.toStringArray(formParam); } else { List result = new ArrayList(parameterValues.length + formParam.size()); result.addAll(Arrays.asList(parameterValues)); result.addAll(formParam); - return result.toArray(new String[result.size()]); + return StringUtils.toStringArray(result); } } } diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index a652d9384a..560b3ff32e 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -773,7 +773,7 @@ final class HierarchicalUriComponents extends UriComponents { @Override public void copyToUriComponentsBuilder(UriComponentsBuilder builder) { - builder.pathSegment(getPathSegments().toArray(new String[getPathSegments().size()])); + builder.pathSegment(StringUtils.toStringArray(getPathSegments())); } @Override diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java index 6e1b21f0be..128730d37d 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -30,6 +30,7 @@ import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Mock implementation of the {@link javax.servlet.http.HttpSession} interface. @@ -172,7 +173,7 @@ public class MockHttpSession implements HttpSession { @Override public String[] getValueNames() { assertIsValid(); - return this.attributes.keySet().toArray(new String[this.attributes.size()]); + return StringUtils.toStringArray(this.attributes.keySet()); } @Override diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/FreeMarkerConfigurerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/FreeMarkerConfigurerBeanDefinitionParser.java index 7ceec049bd..92227cd8cb 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/FreeMarkerConfigurerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/FreeMarkerConfigurerBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -25,6 +25,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; /** @@ -60,7 +61,7 @@ public class FreeMarkerConfigurerBeanDefinitionParser extends AbstractSingleBean if (locations.isEmpty()) { locations.add("/WEB-INF/"); } - builder.addPropertyValue("templateLoaderPaths", locations.toArray(new String[locations.size()])); + builder.addPropertyValue("templateLoaderPaths", StringUtils.toStringArray(locations)); } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ScriptTemplateConfigurerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ScriptTemplateConfigurerBeanDefinitionParser.java index 13a71ac1e5..a24b124e39 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ScriptTemplateConfigurerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ScriptTemplateConfigurerBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -26,6 +26,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; /** @@ -58,7 +59,7 @@ public class ScriptTemplateConfigurerBeanDefinitionParser extends AbstractSimple for (Element childElement : childElements) { locations.add(childElement.getAttribute("location")); } - builder.addPropertyValue("scripts", locations.toArray(new String[locations.size()])); + builder.addPropertyValue("scripts", StringUtils.toStringArray(locations)); } builder.addPropertyValue("engineName", element.getAttribute("engine-name")); if (element.hasAttribute("render-object")) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/TilesConfigurerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/TilesConfigurerBeanDefinitionParser.java index 3b6079930a..b84a7cda0b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/TilesConfigurerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/TilesConfigurerBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 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. @@ -25,6 +25,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; /** @@ -58,7 +59,7 @@ public class TilesConfigurerBeanDefinitionParser extends AbstractSingleBeanDefin for (Element childElement : childElements) { locations.add(childElement.getAttribute("location")); } - builder.addPropertyValue("definitions", locations.toArray(new String[locations.size()])); + builder.addPropertyValue("definitions", StringUtils.toStringArray(locations)); } if (element.hasAttribute("check-refresh")) { builder.addPropertyValue("checkRefresh", element.getAttribute("check-refresh")); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java index 07ec3c6a48..a0b0342306 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.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. @@ -21,8 +21,8 @@ import java.util.Arrays; import java.util.List; import org.springframework.util.Assert; -import org.springframework.util.CollectionUtils; import org.springframework.util.PathMatcher; +import org.springframework.util.StringUtils; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.handler.MappedInterceptor; @@ -88,8 +88,8 @@ public class InterceptorRegistration { return this.interceptor; } - String[] include = toArray(this.includePatterns); - String[] exclude = toArray(this.excludePatterns); + String[] include = StringUtils.toStringArray(this.includePatterns); + String[] exclude = StringUtils.toStringArray(this.excludePatterns); MappedInterceptor mappedInterceptor = new MappedInterceptor(include, exclude, this.interceptor); if (this.pathMatcher != null) { @@ -99,8 +99,4 @@ public class InterceptorRegistration { return mappedInterceptor; } - private static String[] toArray(List list) { - return (CollectionUtils.isEmpty(list) ? null : list.toArray(new String[list.size()])); - } - } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.java index 0f0d9c51ec..fa1a8b45bd 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MappedInterceptor.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. @@ -31,11 +31,10 @@ import org.springframework.web.servlet.ModelAndView; * Also provides matching logic to test if the interceptor applies to a given request path. * *

A MappedInterceptor can be registered directly with any - * {@link org.springframework.web.servlet.handler.AbstractHandlerMethodMapping - * AbstractHandlerMethodMapping}. Furthermore, beans of type MappedInterceptor - * are automatically detected by {@code AbstractHandlerMethodMapping} (including - * ancestor ApplicationContext's) which effectively means the interceptor is - * registered "globally" with all handler mappings. + * {@link org.springframework.web.servlet.handler.AbstractHandlerMethodMapping}. + * Furthermore, beans of type {@code MappedInterceptor} are automatically detected by + * {@code AbstractHandlerMethodMapping} (including ancestor ApplicationContext's) which + * effectively means the interceptor is registered "globally" with all handler mappings. * * @author Keith Donald * @author Rossen Stoyanchev @@ -55,7 +54,7 @@ public final class MappedInterceptor implements HandlerInterceptor { /** * Create a new MappedInterceptor instance. - * @param includePatterns the path patterns to map with a {@code null} value matching to all paths + * @param includePatterns the path patterns to map (empty for matching to all paths) * @param interceptor the HandlerInterceptor instance to map to the given patterns */ public MappedInterceptor(String[] includePatterns, HandlerInterceptor interceptor) { @@ -64,8 +63,8 @@ public final class MappedInterceptor implements HandlerInterceptor { /** * Create a new MappedInterceptor instance. - * @param includePatterns the path patterns to map with a {@code null} value matching to all paths - * @param excludePatterns the path patterns to exclude + * @param includePatterns the path patterns to map (empty for matching to all paths) + * @param excludePatterns the path patterns to exclude (empty for no specific excludes) * @param interceptor the HandlerInterceptor instance to map to the given patterns */ public MappedInterceptor(String[] includePatterns, String[] excludePatterns, HandlerInterceptor interceptor) { @@ -132,8 +131,8 @@ public final class MappedInterceptor implements HandlerInterceptor { * @param pathMatcher a path matcher for path pattern matching */ public boolean matches(String lookupPath, PathMatcher pathMatcher) { - PathMatcher pathMatcherToUse = (this.pathMatcher != null) ? this.pathMatcher : pathMatcher; - if (this.excludePatterns != null) { + PathMatcher pathMatcherToUse = (this.pathMatcher != null ? this.pathMatcher : pathMatcher); + if (!ObjectUtils.isEmpty(this.excludePatterns)) { for (String pattern : this.excludePatterns) { if (pathMatcherToUse.match(pattern, lookupPath)) { return false; @@ -143,14 +142,12 @@ public final class MappedInterceptor implements HandlerInterceptor { if (ObjectUtils.isEmpty(this.includePatterns)) { return true; } - else { - for (String pattern : this.includePatterns) { - if (pathMatcherToUse.match(pattern, lookupPath)) { - return true; - } + for (String pattern : this.includePatterns) { + if (pathMatcherToUse.match(pattern, lookupPath)) { + return true; } - return false; } + return false; } @Override diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java index aa0aa40c94..87821d2f30 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/VersionResourceResolver.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. @@ -131,7 +131,7 @@ public class VersionResourceResolver extends AbstractResourceResolver { prefixedPatterns.add(versionPrefix + pattern); } } - return addVersionStrategy(new FixedVersionStrategy(version), prefixedPatterns.toArray(new String[0])); + return addVersionStrategy(new FixedVersionStrategy(version), StringUtils.toStringArray(prefixedPatterns)); } /** diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/AbstractWebSocketHandlerRegistration.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/AbstractWebSocketHandlerRegistration.java index 02b3f9f6dc..bd56c76c70 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/AbstractWebSocketHandlerRegistration.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/AbstractWebSocketHandlerRegistration.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. @@ -25,6 +25,7 @@ import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.HandshakeHandler; import org.springframework.web.socket.server.HandshakeInterceptor; @@ -109,8 +110,7 @@ public abstract class AbstractWebSocketHandlerRegistration implements WebSock this.sockJsServiceRegistration.setTransportHandlerOverrides(transportHandler); } if (!this.allowedOrigins.isEmpty()) { - this.sockJsServiceRegistration.setAllowedOrigins( - this.allowedOrigins.toArray(new String[this.allowedOrigins.size()])); + this.sockJsServiceRegistration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins)); } return this.sockJsServiceRegistration; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java index f01696739f..2d5bf37b3d 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompWebSocketEndpointRegistration.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. @@ -25,6 +25,7 @@ import org.springframework.util.Assert; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import org.springframework.web.HttpRequestHandler; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.HandshakeHandler; @@ -106,7 +107,7 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE this.registration.setTransportHandlerOverrides(handler); } if (!this.allowedOrigins.isEmpty()) { - this.registration.setAllowedOrigins(this.allowedOrigins.toArray(new String[this.allowedOrigins.size()])); + this.registration.setAllowedOrigins(StringUtils.toStringArray(this.allowedOrigins)); } return this.registration; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java index ab51f29362..e4b23ff71c 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.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. @@ -183,7 +183,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life * Return the list of supported sub-protocols. */ public String[] getSupportedProtocols() { - return this.supportedProtocols.toArray(new String[this.supportedProtocols.size()]); + return StringUtils.toStringArray(this.supportedProtocols); } @Override @@ -404,8 +404,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life * @param attributes handshake attributes to pass to the WebSocket session * @return the user for the WebSocket session, or {@code null} if not available */ - protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, - Map attributes) { + protected Principal determineUser( + ServerHttpRequest request, WebSocketHandler wsHandler, Map attributes) { return request.getPrincipal(); }