From a5cbf5fe246e362c939db006cb44793b22bdc741 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 22 Feb 2018 11:29:46 +0100 Subject: [PATCH] Consistent use of Collection.toArray with zero-sized array argument Includes consistent use of ClassUtils.toClassArray (as non-null variant) Issue: SPR-16523 --- .../config/ConfigBeanDefinitionParser.java | 6 +-- .../aop/framework/AdvisedSupport.java | 6 +-- .../DefaultAdvisorAdapterRegistry.java | 4 +- .../autoproxy/AbstractAutoProxyCreator.java | 2 +- .../support/DefaultIntroductionAdvisor.java | 4 +- .../aop/support/IntroductionInfoSupport.java | 2 +- .../beans/AbstractPropertyAccessor.java | 5 +- .../beans/ExtendedBeanInfo.java | 4 +- .../beans/MutablePropertyValues.java | 4 +- .../beans/factory/BeanCreationException.java | 4 +- .../AutowiredAnnotationBeanPostProcessor.java | 2 +- .../parsing/BeanComponentDefinition.java | 6 +-- .../parsing/CompositeComponentDefinition.java | 4 +- .../AbstractAutowireCapableBeanFactory.java | 15 ++---- .../factory/support/ConstructorResolver.java | 4 +- .../support/DefaultListableBeanFactory.java | 2 +- .../DefaultBeanDefinitionDocumentReader.java | 4 +- .../AbstractJCacheKeyOperation.java | 4 +- .../interceptor/AbstractJCacheOperation.java | 4 +- .../interceptor/KeyGeneratorAdapter.java | 9 ++-- .../mail/MailSendException.java | 4 +- .../mail/javamail/JavaMailSenderImpl.java | 6 +-- .../FreeMarkerConfigurationFactory.java | 7 ++- .../support/AbstractMessageSource.java | 4 +- .../jmx/export/MBeanExporter.java | 3 +- ...bstractConfigurableMBeanInfoAssembler.java | 4 +- .../AbstractReflectiveMBeanInfoAssembler.java | 6 +-- .../jmx/support/MBeanRegistrationSupport.java | 4 +- .../SpringValidatorAdapter.java | 9 ++-- .../core/convert/Property.java | 4 +- .../PathMatchingResourcePatternResolver.java | 6 +-- .../support/ResourceArrayPropertyEditor.java | 4 +- .../RecursiveAnnotationArrayVisitor.java | 5 +- .../org/springframework/util/ClassUtils.java | 24 ++++------ .../springframework/util/ReflectionUtils.java | 6 +-- .../org/springframework/util/StopWatch.java | 4 +- .../org/springframework/util/StringUtils.java | 5 +- .../common/TemplateAwareExpressionParser.java | 6 +-- .../InternalSpelExpressionParser.java | 47 ++++++++----------- .../config/SortedResourcesFactoryBean.java | 2 +- .../jdbc/core/simple/AbstractJdbcInsert.java | 6 +-- .../connection/CachingConnectionFactory.java | 7 ++- .../connection/ChainedExceptionListener.java | 4 +- .../connection/SingleConnectionFactory.java | 9 ++-- ...ransactionAwareConnectionFactoryProxy.java | 15 +++--- .../jpa/AbstractEntityManagerFactoryBean.java | 5 +- .../orm/jpa/ExtendedEntityManagerCreator.java | 4 +- .../PersistenceUnitReader.java | 4 +- .../oxm/jaxb/ClassPathJaxb2TypeScanner.java | 4 +- .../mock/web/MockHttpServletResponse.java | 2 +- .../test/context/TestContextManager.java | 4 +- .../jdbc/SqlScriptsTestExecutionListener.java | 4 +- .../AbstractTestContextBootstrapper.java | 3 +- .../AnnotationConfigContextLoaderUtils.java | 5 +- .../context/support/DefaultTestContext.java | 5 +- .../htmlunit/HtmlUnitRequestBuilder.java | 4 +- .../MockHttpServletRequestBuilder.java | 2 +- .../servlet/setup/AbstractMockMvcBuilder.java | 6 +-- ...ChainedPersistenceExceptionTranslator.java | 4 +- .../http/server/reactive/DefaultSslInfo.java | 4 +- ...AnnotationConfigWebApplicationContext.java | 2 +- .../support/MultipartResolutionDelegate.java | 6 +-- .../server/handler/FilteringWebHandler.java | 10 ++-- .../web/test/MockHttpServletResponse.java | 2 +- .../reactive/resource/ResourceWebHandler.java | 4 +- .../web/servlet/HandlerExecutionChain.java | 4 +- .../handler/AbstractHandlerMapping.java | 11 ++--- .../RequestMappingHandlerAdapter.java | 29 +++++++----- .../resource/ResourceHttpRequestHandler.java | 4 +- .../view/groovy/GroovyMarkupConfigurer.java | 2 +- .../AbstractWebSocketHandlerRegistration.java | 2 +- ...MvcStompWebSocketEndpointRegistration.java | 11 ++--- 72 files changed, 209 insertions(+), 234 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java b/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java index 90fe0f0543a..1bd7eac2900 100644 --- a/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.java +++ b/spring-aop/src/main/java/org/springframework/aop/config/ConfigBeanDefinitionParser.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. @@ -253,8 +253,8 @@ class ConfigBeanDefinitionParser implements BeanDefinitionParser { Element aspectElement, String aspectId, List beanDefs, List beanRefs, ParserContext parserContext) { - BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[beanDefs.size()]); - BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[beanRefs.size()]); + BeanDefinition[] beanDefArray = beanDefs.toArray(new BeanDefinition[0]); + BeanReference[] beanRefArray = beanRefs.toArray(new BeanReference[0]); Object source = parserContext.extractSource(aspectElement); return new AspectComponentDefinition(aspectId, beanDefArray, beanRefArray, source); } diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java index e66e32daeeb..93e22e368b5 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AdvisedSupport.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. @@ -228,7 +228,7 @@ public class AdvisedSupport extends ProxyConfig implements Advised { @Override public Class[] getProxiedInterfaces() { - return this.interfaces.toArray(new Class[this.interfaces.size()]); + return ClassUtils.toClassArray(this.interfaces); } @Override @@ -372,7 +372,7 @@ public class AdvisedSupport extends ProxyConfig implements Advised { * Bring the array up to date with the list. */ protected final void updateAdvisorArray() { - this.advisorArray = this.advisors.toArray(new Advisor[this.advisors.size()]); + this.advisorArray = this.advisors.toArray(new Advisor[0]); } /** diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java index a11c40b8f8a..ebdd27c514f 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/adapter/DefaultAdvisorAdapterRegistry.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. @@ -90,7 +90,7 @@ public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Se if (interceptors.isEmpty()) { throw new UnknownAdviceTypeException(advisor.getAdvice()); } - return interceptors.toArray(new MethodInterceptor[interceptors.size()]); + return interceptors.toArray(new MethodInterceptor[0]); } @Override diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java index fd097b22590..e19354723a0 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java @@ -554,7 +554,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport advisors.add(this.advisorAdapterRegistry.wrap(next)); } } - return advisors.toArray(new Advisor[advisors.size()]); + return advisors.toArray(new Advisor[0]); } /** diff --git a/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java b/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java index 980bac49c1c..3b4896273fb 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/DefaultIntroductionAdvisor.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. @@ -105,7 +105,7 @@ public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFil @Override public Class[] getInterfaces() { - return this.interfaces.toArray(new Class[this.interfaces.size()]); + return ClassUtils.toClassArray(this.interfaces); } @Override diff --git a/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java b/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java index d74bd23a1d6..1edddac2bb8 100644 --- a/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java +++ b/spring-aop/src/main/java/org/springframework/aop/support/IntroductionInfoSupport.java @@ -61,7 +61,7 @@ public class IntroductionInfoSupport implements IntroductionInfo, Serializable { @Override public Class[] getInterfaces() { - return this.publishedInterfaces.toArray(new Class[this.publishedInterfaces.size()]); + return ClassUtils.toClassArray(this.publishedInterfaces); } /** diff --git a/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java b/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java index 34310708f7f..71395c9328b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/AbstractPropertyAccessor.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. @@ -118,8 +118,7 @@ public abstract class AbstractPropertyAccessor extends TypeConverterSupport impl // If we encountered individual exceptions, throw the composite exception. if (propertyAccessExceptions != null) { - PropertyAccessException[] paeArray = - propertyAccessExceptions.toArray(new PropertyAccessException[propertyAccessExceptions.size()]); + PropertyAccessException[] paeArray = propertyAccessExceptions.toArray(new PropertyAccessException[0]); throw new PropertyBatchUpdateException(paeArray); } } diff --git a/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java b/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java index 089b38b2deb..2f87b10a365 100644 --- a/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java +++ b/spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.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. @@ -220,7 +220,7 @@ class ExtendedBeanInfo implements BeanInfo { */ @Override public PropertyDescriptor[] getPropertyDescriptors() { - return this.propertyDescriptors.toArray(new PropertyDescriptor[this.propertyDescriptors.size()]); + return this.propertyDescriptors.toArray(new PropertyDescriptor[0]); } @Override diff --git a/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java b/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java index bf726a5f9e0..c04615b9361 100644 --- a/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.java +++ b/spring-beans/src/main/java/org/springframework/beans/MutablePropertyValues.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. @@ -246,7 +246,7 @@ public class MutablePropertyValues implements PropertyValues, Serializable { @Override public PropertyValue[] getPropertyValues() { - return this.propertyValueList.toArray(new PropertyValue[this.propertyValueList.size()]); + return this.propertyValueList.toArray(new PropertyValue[0]); } @Override diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/BeanCreationException.java b/spring-beans/src/main/java/org/springframework/beans/factory/BeanCreationException.java index 21884a1f295..cb968c736b9 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/BeanCreationException.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/BeanCreationException.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. @@ -149,7 +149,7 @@ public class BeanCreationException extends FatalBeanException { if (this.relatedCauses == null) { return null; } - return this.relatedCauses.toArray(new Throwable[this.relatedCauses.size()]); + return this.relatedCauses.toArray(new Throwable[0]); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index d34419464ff..86ade366988 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -342,7 +342,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean "default constructor to fall back to: " + candidates.get(0)); } } - candidateConstructors = candidates.toArray(new Constructor[candidates.size()]); + candidateConstructors = candidates.toArray(new Constructor[0]); } else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) { candidateConstructors = new Constructor[] {rawCandidates[0]}; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java index c51bf4c6e1c..5a2e42deb8a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/parsing/BeanComponentDefinition.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. @@ -83,8 +83,8 @@ public class BeanComponentDefinition extends BeanDefinitionHolder implements Com references.add((BeanReference) value); } } - this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[innerBeans.size()]); - this.beanReferences = references.toArray(new BeanReference[references.size()]); + this.innerBeanDefinitions = innerBeans.toArray(new BeanDefinition[0]); + this.beanReferences = references.toArray(new BeanReference[0]); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/parsing/CompositeComponentDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/parsing/CompositeComponentDefinition.java index 6da88b36318..29d45dd4f54 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/parsing/CompositeComponentDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/parsing/CompositeComponentDefinition.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. @@ -79,7 +79,7 @@ public class CompositeComponentDefinition extends AbstractComponentDefinition { * @return the array of nested components, or an empty array if none */ public ComponentDefinition[] getNestedComponents() { - return this.nestedComponents.toArray(new ComponentDefinition[this.nestedComponents.size()]); + return this.nestedComponents.toArray(new ComponentDefinition[0]); } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 182088403b8..168d64b224a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.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. @@ -29,7 +29,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -1496,15 +1495,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * @see #isExcludedFromDependencyCheck */ protected PropertyDescriptor[] filterPropertyDescriptorsForDependencyCheck(BeanWrapper bw) { - List pds = - new LinkedList<>(Arrays.asList(bw.getPropertyDescriptors())); - for (Iterator it = pds.iterator(); it.hasNext();) { - PropertyDescriptor pd = it.next(); - if (isExcludedFromDependencyCheck(pd)) { - it.remove(); - } - } - return pds.toArray(new PropertyDescriptor[pds.size()]); + List pds = new LinkedList<>(Arrays.asList(bw.getPropertyDescriptors())); + pds.removeIf(this::isExcludedFromDependencyCheck); + return pds.toArray(new PropertyDescriptor[0]); } /** diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index 219d8f75ca4..2c20e39838a 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.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. @@ -419,7 +419,7 @@ class ConstructorResolver { candidateSet.add(candidate); } } - Method[] candidates = candidateSet.toArray(new Method[candidateSet.size()]); + Method[] candidates = candidateSet.toArray(new Method[0]); AutowireUtils.sortFactoryMethods(candidates); ConstructorArgumentValues resolvedValues = null; 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 ffec78d3f3a..bac78d02ecd 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 @@ -1780,7 +1780,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto if (targetType != null && targetType != obj.getClass()) { sources.add(targetType); } - return sources.toArray(new Object[sources.size()]); + return sources.toArray(); } @Nullable diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java b/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java index 436658546ec..374093cc918 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/xml/DefaultBeanDefinitionDocumentReader.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. @@ -266,7 +266,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume ele, ex); } } - Resource[] actResArray = actualResources.toArray(new Resource[actualResources.size()]); + Resource[] actResArray = actualResources.toArray(new Resource[0]); getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele)); } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java index 40cb70e5f6a..1edd3cf7c86 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheKeyOperation.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. @@ -82,7 +82,7 @@ abstract class AbstractJCacheKeyOperation extends Abstract } result.add(keyParameterDetail.toCacheInvocationParameter(values[parameterPosition])); } - return result.toArray(new CacheInvocationParameter[result.size()]); + return result.toArray(new CacheInvocationParameter[0]); } diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java index fd7d932fa02..4d7319f2cd5 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.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. @@ -110,7 +110,7 @@ abstract class AbstractJCacheOperation implements JCacheOp for (int i = 0; i < this.allParameterDetails.size(); i++) { result.add(this.allParameterDetails.get(i).toCacheInvocationParameter(values[i])); } - return result.toArray(new CacheInvocationParameter[result.size()]); + return result.toArray(new CacheInvocationParameter[0]); } protected ExceptionTypeFilter createExceptionTypeFilter( diff --git a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java index 706e157b720..f0d3f2ba771 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java +++ b/spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/KeyGeneratorAdapter.java @@ -104,15 +104,14 @@ class KeyGeneratorAdapter implements KeyGenerator { parameters.add(value); } } - return keyGenerator.generate(context.getTarget(), context.getMethod(), - parameters.toArray(new Object[parameters.size()])); - + return keyGenerator.generate(context.getTarget(), context.getMethod(), parameters.toArray()); } @SuppressWarnings("unchecked") - private CacheKeyInvocationContext createCacheKeyInvocationContext(Object target, - JCacheOperation operation, Object[] params) { + private CacheKeyInvocationContext createCacheKeyInvocationContext( + Object target, JCacheOperation operation, Object[] params) { + AbstractJCacheKeyOperation keyCacheOperation = (AbstractJCacheKeyOperation) operation; return new DefaultCacheKeyInvocationContext<>(keyCacheOperation, target, params); } diff --git a/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java b/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java index b0c66d26877..1625af12fda 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/MailSendException.java +++ b/spring-context-support/src/main/java/org/springframework/mail/MailSendException.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. @@ -71,7 +71,7 @@ public class MailSendException extends MailException { public MailSendException(@Nullable String msg, @Nullable Throwable cause, Map failedMessages) { super(msg, cause); this.failedMessages = new LinkedHashMap<>(failedMessages); - this.messageExceptions = failedMessages.values().toArray(new Exception[failedMessages.size()]); + this.messageExceptions = failedMessages.values().toArray(new Exception[0]); } /** diff --git a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java index 787e1e68807..6b81645f2d2 100644 --- a/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.java +++ b/spring-context-support/src/main/java/org/springframework/mail/javamail/JavaMailSenderImpl.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. @@ -318,7 +318,7 @@ public class JavaMailSenderImpl implements JavaMailSender { simpleMessage.copyTo(message); mimeMessages.add(message.getMimeMessage()); } - doSend(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]), simpleMessages); + doSend(mimeMessages.toArray(new MimeMessage[0]), simpleMessages); } @@ -373,7 +373,7 @@ public class JavaMailSenderImpl implements JavaMailSender { preparator.prepare(mimeMessage); mimeMessages.add(mimeMessage); } - send(mimeMessages.toArray(new MimeMessage[mimeMessages.size()])); + send(mimeMessages.toArray(new MimeMessage[0])); } catch (MailException ex) { throw ex; diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java index 37793c9e2f4..633eeea655e 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerConfigurationFactory.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. @@ -390,15 +390,14 @@ public class FreeMarkerConfigurationFactory { */ @Nullable protected TemplateLoader getAggregateTemplateLoader(List templateLoaders) { - int loaderCount = templateLoaders.size(); - switch (loaderCount) { + switch (templateLoaders.size()) { case 0: logger.info("No FreeMarker TemplateLoaders specified"); return null; case 1: return templateLoaders.get(0); default: - TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[loaderCount]); + TemplateLoader[] loaders = templateLoaders.toArray(new TemplateLoader[0]); return new MultiTemplateLoader(loaders); } } diff --git a/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java index 7f3f849256f..86adef36e9b 100644 --- a/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/AbstractMessageSource.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. @@ -337,7 +337,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme resolvedArgs.add(arg); } } - return resolvedArgs.toArray(new Object[resolvedArgs.size()]); + return resolvedArgs.toArray(); } /** diff --git a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java index 335adf6216a..5606e0039e8 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java @@ -383,8 +383,7 @@ public class MBeanExporter extends MBeanRegistrationSupport implements MBeanExpo notificationListeners.add(bean); }); - this.notificationListeners = - notificationListeners.toArray(new NotificationListenerBean[notificationListeners.size()]); + this.notificationListeners = notificationListeners.toArray(new NotificationListenerBean[0]); } @Override diff --git a/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractConfigurableMBeanInfoAssembler.java b/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractConfigurableMBeanInfoAssembler.java index 940bf1b21a8..7514de37731 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractConfigurableMBeanInfoAssembler.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractConfigurableMBeanInfoAssembler.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. @@ -87,7 +87,7 @@ public abstract class AbstractConfigurableMBeanInfoAssembler extends AbstractRef ManagedNotification mn = (ManagedNotification) colValue; result.add(JmxMetadataUtils.convertToModelMBeanNotificationInfo(mn)); } - return result.toArray(new ModelMBeanNotificationInfo[result.size()]); + return result.toArray(new ModelMBeanNotificationInfo[0]); } else { throw new IllegalArgumentException( diff --git a/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractReflectiveMBeanInfoAssembler.java b/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractReflectiveMBeanInfoAssembler.java index 35a6f0f4542..0475e6bb74a 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractReflectiveMBeanInfoAssembler.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/assembler/AbstractReflectiveMBeanInfoAssembler.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 abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean } } - return infos.toArray(new ModelMBeanAttributeInfo[infos.size()]); + return infos.toArray(new ModelMBeanAttributeInfo[0]); } /** @@ -401,7 +401,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean } } - return infos.toArray(new ModelMBeanOperationInfo[infos.size()]); + return infos.toArray(new ModelMBeanOperationInfo[0]); } /** diff --git a/spring-context/src/main/java/org/springframework/jmx/support/MBeanRegistrationSupport.java b/spring-context/src/main/java/org/springframework/jmx/support/MBeanRegistrationSupport.java index 63f8f3b4a4f..fdf2e7246f3 100644 --- a/spring-context/src/main/java/org/springframework/jmx/support/MBeanRegistrationSupport.java +++ b/spring-context/src/main/java/org/springframework/jmx/support/MBeanRegistrationSupport.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. @@ -228,7 +228,7 @@ public class MBeanRegistrationSupport { */ protected final ObjectName[] getRegisteredObjectNames() { synchronized (this.registeredBeans) { - return this.registeredBeans.toArray(new ObjectName[this.registeredBeans.size()]); + return this.registeredBeans.toArray(new ObjectName[0]); } } diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java index 59d824a0d4a..3f6463bc8ad 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java @@ -17,9 +17,9 @@ package org.springframework.validation.beanvalidation; import java.io.Serializable; +import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -37,6 +37,7 @@ import org.springframework.context.MessageSourceResolvable; import org.springframework.context.support.DefaultMessageSourceResolvable; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.Errors; import org.springframework.validation.FieldError; @@ -116,7 +117,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. } } processConstraintViolations( - this.targetValidator.validate(target, groups.toArray(new Class[groups.size()])), errors); + this.targetValidator.validate(target, ClassUtils.toClassArray(groups)), errors); } } @@ -241,7 +242,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. * @see org.springframework.validation.DefaultBindingErrorProcessor#getArgumentsForBindError */ protected Object[] getArgumentsForConstraint(String objectName, String field, ConstraintDescriptor descriptor) { - List arguments = new LinkedList<>(); + List arguments = new ArrayList<>(); arguments.add(getResolvableField(objectName, field)); // Using a TreeMap for alphabetical ordering of attribute names Map attributesToExpose = new TreeMap<>(); @@ -254,7 +255,7 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation. } }); arguments.addAll(attributesToExpose.values()); - return arguments.toArray(new Object[arguments.size()]); + return arguments.toArray(); } /** diff --git a/spring-core/src/main/java/org/springframework/core/convert/Property.java b/spring-core/src/main/java/org/springframework/core/convert/Property.java index b4c0e284994..cd8fdff39b6 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/Property.java +++ b/spring-core/src/main/java/org/springframework/core/convert/Property.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. @@ -211,7 +211,7 @@ public final class Property { addAnnotationsToMap(annotationMap, getReadMethod()); addAnnotationsToMap(annotationMap, getWriteMethod()); addAnnotationsToMap(annotationMap, getField()); - annotations = annotationMap.values().toArray(new Annotation[annotationMap.size()]); + annotations = annotationMap.values().toArray(new Annotation[0]); annotationCache.put(this, annotations); } return annotations; diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java index cdcb8ed4997..1e9b8983d43 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.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. @@ -320,7 +320,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol if (logger.isDebugEnabled()) { logger.debug("Resolved classpath location [" + location + "] to resources " + result); } - return result.toArray(new Resource[result.size()]); + return result.toArray(new Resource[0]); } /** @@ -515,7 +515,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol if (logger.isDebugEnabled()) { logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result); } - return result.toArray(new Resource[result.size()]); + return result.toArray(new Resource[0]); } /** diff --git a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java index db6917ea727..7975f574026 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/ResourceArrayPropertyEditor.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. @@ -162,7 +162,7 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport { Resource.class.getName() + "]: only location String and Resource object supported"); } } - super.setValue(merged.toArray(new Resource[merged.size()])); + super.setValue(merged.toArray(new Resource[0])); } else { diff --git a/spring-core/src/main/java/org/springframework/core/type/classreading/RecursiveAnnotationArrayVisitor.java b/spring-core/src/main/java/org/springframework/core/type/classreading/RecursiveAnnotationArrayVisitor.java index e55a05477b7..af97f44ecfd 100644 --- a/spring-core/src/main/java/org/springframework/core/type/classreading/RecursiveAnnotationArrayVisitor.java +++ b/spring-core/src/main/java/org/springframework/core/type/classreading/RecursiveAnnotationArrayVisitor.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. @@ -78,8 +78,7 @@ class RecursiveAnnotationArrayVisitor extends AbstractRecursiveAnnotationVisitor @Override public void visitEnd() { if (!this.allNestedAttributes.isEmpty()) { - this.attributes.put(this.attributeName, - this.allNestedAttributes.toArray(new AnnotationAttributes[this.allNestedAttributes.size()])); + this.attributes.put(this.attributeName, this.allNestedAttributes.toArray(new AnnotationAttributes[0])); } } diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index a000c5b4e50..daabe589e5a 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -1058,18 +1058,15 @@ public abstract class ClassUtils { } /** - * Copy the given Collection into a Class array. - * The Collection must contain Class elements only. - * @param collection the Collection to copy - * @return the Class array ({@code null} if the passed-in - * Collection was {@code null}) - */ - @Nullable - public static Class[] toClassArray(@Nullable Collection> collection) { - if (collection == null) { - return null; - } - return collection.toArray(new Class[collection.size()]); + * Copy the given {@code Collection} into a {@code Class} array. + *

The {@code Collection} must contain {@code Class} elements only. + * @param collection the {@code Collection} to copy + * @return the {@code Class} array + * @since 3.1 + * @see StringUtils#toStringArray + */ + public static Class[] toClassArray(Collection> collection) { + return collection.toArray(new Class[0]); } /** @@ -1104,8 +1101,7 @@ public abstract class ClassUtils { * @return all interfaces that the given object implements as an array */ public static Class[] getAllInterfacesForClass(Class clazz, @Nullable ClassLoader classLoader) { - Set> ifcs = getAllInterfacesForClassAsSet(clazz, classLoader); - return ifcs.toArray(new Class[ifcs.size()]); + return toClassArray(getAllInterfacesForClassAsSet(clazz, classLoader)); } /** diff --git a/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java b/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java index a087055d8a4..853694d059a 100644 --- a/spring-core/src/main/java/org/springframework/util/ReflectionUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ReflectionUtils.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. @@ -584,7 +584,7 @@ public abstract class ReflectionUtils { public static Method[] getAllDeclaredMethods(Class leafClass) { final List methods = new ArrayList<>(32); doWithMethods(leafClass, methods::add); - return methods.toArray(new Method[methods.size()]); + return methods.toArray(new Method[0]); } /** @@ -620,7 +620,7 @@ public abstract class ReflectionUtils { methods.add(method); } }); - return methods.toArray(new Method[methods.size()]); + return methods.toArray(new Method[0]); } /** diff --git a/spring-core/src/main/java/org/springframework/util/StopWatch.java b/spring-core/src/main/java/org/springframework/util/StopWatch.java index 8cf6a790b78..7627067e829 100644 --- a/spring-core/src/main/java/org/springframework/util/StopWatch.java +++ b/spring-core/src/main/java/org/springframework/util/StopWatch.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. @@ -229,7 +229,7 @@ public class StopWatch { if (!this.keepTaskList) { throw new UnsupportedOperationException("Task info is not being kept!"); } - return this.taskList.toArray(new TaskInfo[this.taskList.size()]); + return this.taskList.toArray(new TaskInfo[0]); } diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index ca22f467a3c..20100bb7b91 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -945,7 +945,7 @@ public abstract class StringUtils { * @return the {@code String} array */ public static String[] toStringArray(Collection collection) { - return collection.toArray(new String[collection.size()]); + return collection.toArray(new String[0]); } /** @@ -955,8 +955,7 @@ public abstract class StringUtils { * @return the {@code String} array */ public static String[] toStringArray(Enumeration enumeration) { - List list = Collections.list(enumeration); - return list.toArray(new String[list.size()]); + return toStringArray(Collections.list(enumeration)); } /** diff --git a/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java b/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java index 0100ef333d1..9a50606e0cc 100644 --- a/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java +++ b/spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.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. @@ -17,9 +17,9 @@ package org.springframework.expression.common; import java.util.ArrayDeque; +import java.util.Deque; import java.util.LinkedList; import java.util.List; -import java.util.Deque; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; @@ -128,7 +128,7 @@ public abstract class TemplateAwareExpressionParser implements ExpressionParser } } - return expressions.toArray(new Expression[expressions.size()]); + return expressions.toArray(new Expression[0]); } /** diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/standard/InternalSpelExpressionParser.java b/spring-expression/src/main/java/org/springframework/expression/spel/standard/InternalSpelExpressionParser.java index 0d2ce810e1f..9b2480d75b5 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/standard/InternalSpelExpressionParser.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/standard/InternalSpelExpressionParser.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. @@ -365,7 +365,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser { } return new CompoundExpression(toPos(start.getStartPosition(), nodes.get(nodes.size() - 1).getEndPosition()), - nodes.toArray(new SpelNodeImpl[nodes.size()])); + nodes.toArray(new SpelNodeImpl[0])); } // node : ((DOT dottedNode) | (SAFE_NAVI dottedNode) | nonDottedNode)+; @@ -443,7 +443,7 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser { List args = new ArrayList<>(); consumeArguments(args); eatToken(TokenKind.RPAREN); - return args.toArray(new SpelNodeImpl[args.size()]); + return args.toArray(new SpelNodeImpl[0]); } private void eatConstructorArgs(List accumulatedArguments) { @@ -646,36 +646,33 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser { // ',' - more expressions in this list // ':' - this is a map! if (peekToken(TokenKind.RCURLY)) { // list with one item in it - List listElements = new ArrayList<>(); - listElements.add(firstExpression); + List elements = new ArrayList<>(); + elements.add(firstExpression); closingCurly = eatToken(TokenKind.RCURLY); - expr = new InlineList(toPos(t.startPos, closingCurly.endPos), - listElements.toArray(new SpelNodeImpl[listElements.size()])); + expr = new InlineList(toPos(t.startPos, closingCurly.endPos), elements.toArray(new SpelNodeImpl[0])); } else if (peekToken(TokenKind.COMMA, true)) { // multi-item list - List listElements = new ArrayList<>(); - listElements.add(firstExpression); + List elements = new ArrayList<>(); + elements.add(firstExpression); do { - listElements.add(eatExpression()); + elements.add(eatExpression()); } while (peekToken(TokenKind.COMMA, true)); closingCurly = eatToken(TokenKind.RCURLY); - expr = new InlineList(toPos(t.startPos, closingCurly.endPos), - listElements.toArray(new SpelNodeImpl[listElements.size()])); + expr = new InlineList(toPos(t.startPos, closingCurly.endPos), elements.toArray(new SpelNodeImpl[0])); } else if (peekToken(TokenKind.COLON, true)) { // map! - List mapElements = new ArrayList<>(); - mapElements.add(firstExpression); - mapElements.add(eatExpression()); + List elements = new ArrayList<>(); + elements.add(firstExpression); + elements.add(eatExpression()); while (peekToken(TokenKind.COMMA, true)) { - mapElements.add(eatExpression()); + elements.add(eatExpression()); eatToken(TokenKind.COLON); - mapElements.add(eatExpression()); + elements.add(eatExpression()); } closingCurly = eatToken(TokenKind.RCURLY); - expr = new InlineMap(toPos(t.startPos, closingCurly.endPos), - mapElements.toArray(new SpelNodeImpl[mapElements.size()])); + expr = new InlineMap(toPos(t.startPos, closingCurly.endPos), elements.toArray(new SpelNodeImpl[0])); } else { throw internalException(t.startPos, SpelMessage.OOD); @@ -743,10 +740,8 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser { throw internalException(node.startPos, SpelMessage.NOT_EXPECTED_TOKEN, "qualified ID", node.getKind().toString().toLowerCase()); } - int pos = toPos(qualifiedIdPieces.getFirst().getStartPosition(), - qualifiedIdPieces.getLast().getEndPosition()); - return new QualifiedIdentifier(pos, - qualifiedIdPieces.toArray(new SpelNodeImpl[qualifiedIdPieces.size()])); + int pos = toPos(qualifiedIdPieces.getFirst().getStartPosition(), qualifiedIdPieces.getLast().getEndPosition()); + return new QualifiedIdentifier(pos, qualifiedIdPieces.toArray(new SpelNodeImpl[0])); } private boolean isValidQualifiedId(@Nullable Token node) { @@ -812,15 +807,13 @@ class InternalSpelExpressionParser extends TemplateAwareExpressionParser { nodes.add(pop()); } push(new ConstructorReference(toPos(newToken), - dimensions.toArray(new SpelNodeImpl[dimensions.size()]), - nodes.toArray(new SpelNodeImpl[nodes.size()]))); + dimensions.toArray(new SpelNodeImpl[0]), nodes.toArray(new SpelNodeImpl[0]))); } else { // regular constructor invocation eatConstructorArgs(nodes); // TODO correct end position? - push(new ConstructorReference(toPos(newToken), - nodes.toArray(new SpelNodeImpl[nodes.size()]))); + push(new ConstructorReference(toPos(newToken), nodes.toArray(new SpelNodeImpl[0]))); } return true; } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java b/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java index fb6dbc9e6e6..3cee085f15b 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/config/SortedResourcesFactoryBean.java @@ -84,7 +84,7 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean }); scripts.addAll(resources); } - return scripts.toArray(new Resource[scripts.size()]); + return scripts.toArray(new Resource[0]); } } diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java index 20f5e91c6d3..af11cf708be 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/simple/AbstractJdbcInsert.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. @@ -462,8 +462,8 @@ public abstract class AbstractJdbcInsert { // clause while HSQL uses a second query that has to be executed with the same connection. if (keyQuery.toUpperCase().startsWith("RETURNING")) { - Long key = getJdbcTemplate().queryForObject(getInsertString() + " " + keyQuery, - values.toArray(new Object[values.size()]), Long.class); + Long key = getJdbcTemplate().queryForObject( + getInsertString() + " " + keyQuery, values.toArray(), Long.class); Map keys = new HashMap<>(1); keys.put(getGeneratedKeyNames()[0], key); keyHolder.getKeyList().add(keys); diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java b/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java index ff2b550b8a3..2426c0d0b91 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/CachingConnectionFactory.java @@ -41,6 +41,7 @@ import javax.jms.TopicSession; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; /** @@ -254,10 +255,8 @@ public class CachingConnectionFactory extends SingleConnectionFactory { if (target instanceof TopicSession) { classes.add(TopicSession.class); } - return (Session) Proxy.newProxyInstance( - SessionProxy.class.getClassLoader(), - classes.toArray(new Class[classes.size()]), - new CachedSessionInvocationHandler(target, sessionList)); + return (Session) Proxy.newProxyInstance(SessionProxy.class.getClassLoader(), + ClassUtils.toClassArray(classes), new CachedSessionInvocationHandler(target, sessionList)); } diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/ChainedExceptionListener.java b/spring-jms/src/main/java/org/springframework/jms/connection/ChainedExceptionListener.java index 19d29cfbdfc..379dbe8dcc1 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/ChainedExceptionListener.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/ChainedExceptionListener.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. @@ -48,7 +48,7 @@ public class ChainedExceptionListener implements ExceptionListener { * Return all registered ExceptionListener delegates (as array). */ public final ExceptionListener[] getDelegates() { - return this.delegates.toArray(new ExceptionListener[this.delegates.size()]); + return this.delegates.toArray(new ExceptionListener[0]); } diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/SingleConnectionFactory.java b/spring-jms/src/main/java/org/springframework/jms/connection/SingleConnectionFactory.java index 7705ab3dc12..f2f5fccebfc 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/SingleConnectionFactory.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/SingleConnectionFactory.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.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** * A JMS ConnectionFactory adapter that returns the same Connection @@ -519,10 +520,8 @@ public class SingleConnectionFactory implements ConnectionFactory, QueueConnecti if (target instanceof TopicConnection) { classes.add(TopicConnection.class); } - return (Connection) Proxy.newProxyInstance( - Connection.class.getClassLoader(), - classes.toArray(new Class[classes.size()]), - new SharedConnectionInvocationHandler()); + return (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), + ClassUtils.toClassArray(classes), new SharedConnectionInvocationHandler()); } diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/TransactionAwareConnectionFactoryProxy.java b/spring-jms/src/main/java/org/springframework/jms/connection/TransactionAwareConnectionFactoryProxy.java index dd55e88daa9..6b096569c8d 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/TransactionAwareConnectionFactoryProxy.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/TransactionAwareConnectionFactoryProxy.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. @@ -37,6 +37,7 @@ import javax.jms.TransactionInProgressException; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** * Proxy for a target JMS {@link javax.jms.ConnectionFactory}, adding awareness of @@ -229,10 +230,8 @@ public class TransactionAwareConnectionFactoryProxy if (target instanceof TopicConnection) { classes.add(TopicConnection.class); } - return (Connection) Proxy.newProxyInstance( - Connection.class.getClassLoader(), - classes.toArray(new Class[classes.size()]), - new TransactionAwareConnectionInvocationHandler(target)); + return (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), + ClassUtils.toClassArray(classes), new TransactionAwareConnectionInvocationHandler(target)); } @@ -301,10 +300,8 @@ public class TransactionAwareConnectionFactoryProxy if (target instanceof TopicSession) { classes.add(TopicSession.class); } - return (Session) Proxy.newProxyInstance( - SessionProxy.class.getClassLoader(), - classes.toArray(new Class[classes.size()]), - new CloseSuppressingSessionInvocationHandler(target)); + return (Session) Proxy.newProxyInstance(SessionProxy.class.getClassLoader(), + ClassUtils.toClassArray(classes), new CloseSuppressingSessionInvocationHandler(target)); } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java b/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java index 34790119835..f07b2db19ce 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/AbstractEntityManagerFactoryBean.java @@ -418,9 +418,8 @@ public abstract class AbstractEntityManagerFactoryBean implements } ifcs.add(EntityManagerFactoryInfo.class); try { - return (EntityManagerFactory) Proxy.newProxyInstance( - this.beanClassLoader, ifcs.toArray(new Class[ifcs.size()]), - new ManagedEntityManagerFactoryInvocationHandler(this)); + return (EntityManagerFactory) Proxy.newProxyInstance(this.beanClassLoader, + ClassUtils.toClassArray(ifcs), new ManagedEntityManagerFactoryInvocationHandler(this)); } catch (IllegalArgumentException ex) { if (entityManagerFactoryInterface != null) { diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java b/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java index d3afe0fa7bc..55fdc94e797 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.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. @@ -232,7 +232,7 @@ public abstract class ExtendedEntityManagerCreator { ifcs.add(EntityManagerProxy.class); return (EntityManager) Proxy.newProxyInstance( (cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()), - ifcs.toArray(new Class[ifcs.size()]), + ClassUtils.toClassArray(ifcs), new ExtendedEntityManagerInvocationHandler( rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction)); } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceUnitReader.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceUnitReader.java index decfb3615e7..e4980b765f4 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceUnitReader.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/PersistenceUnitReader.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. @@ -150,7 +150,7 @@ final class PersistenceUnitReader { throw new IllegalArgumentException("Internal error parsing persistence unit from " + resourceLocation); } - return infos.toArray(new SpringPersistenceUnitInfo[infos.size()]); + return infos.toArray(new SpringPersistenceUnitInfo[0]); } diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/ClassPathJaxb2TypeScanner.java b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/ClassPathJaxb2TypeScanner.java index f1e0396b3f4..5e394c078d2 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/ClassPathJaxb2TypeScanner.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/ClassPathJaxb2TypeScanner.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. @@ -94,7 +94,7 @@ class ClassPathJaxb2TypeScanner { } } } - return jaxb2Classes.toArray(new Class[jaxb2Classes.size()]); + return ClassUtils.toClassArray(jaxb2Classes); } catch (IOException ex) { throw new UncategorizedMappingException("Failed to scan classpath for unlisted classes", ex); diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java index 6ba3f2cfc30..13bed201c37 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockHttpServletResponse.java @@ -357,7 +357,7 @@ public class MockHttpServletResponse implements HttpServletResponse { } public Cookie[] getCookies() { - return this.cookies.toArray(new Cookie[this.cookies.size()]); + return this.cookies.toArray(new Cookie[0]); } @Nullable 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 26b7b527204..c28982d06fb 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 @@ -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. @@ -149,7 +149,7 @@ public class TestContextManager { * @see #registerTestExecutionListeners(TestExecutionListener...) */ public void registerTestExecutionListeners(List testExecutionListeners) { - registerTestExecutionListeners(testExecutionListeners.toArray(new TestExecutionListener[testExecutionListeners.size()])); + registerTestExecutionListeners(testExecutionListeners.toArray(new TestExecutionListener[0])); } /** diff --git a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java index 1e54f550da2..d34562c24e7 100644 --- a/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/jdbc/SqlScriptsTestExecutionListener.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. @@ -185,7 +185,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen scriptResources.add(new ByteArrayResource(stmt.getBytes(), "from inlined SQL statement: " + stmt)); } } - populator.setScripts(scriptResources.toArray(new Resource[scriptResources.size()])); + populator.setScripts(scriptResources.toArray(new Resource[0])); if (logger.isDebugEnabled()) { logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scriptResources)); } diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java index e096e216e36..1c66973a7e4 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java @@ -385,8 +385,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils.buildMergedTestPropertySources(testClass); MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass, - StringUtils.toStringArray(locations), - ClassUtils.toClassArray(classes), + StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes), ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList), ActiveProfilesUtils.resolveActiveProfiles(testClass), mergedTestPropertySources.getLocations(), diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoaderUtils.java b/spring-test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoaderUtils.java index 0e5bde969b9..93a1ae3e520 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoaderUtils.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoaderUtils.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. @@ -28,6 +28,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.lang.Nullable; import org.springframework.test.context.SmartContextLoader; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; /** * Utility methods for {@link SmartContextLoader SmartContextLoaders} that deal @@ -85,7 +86,7 @@ public abstract class AnnotationConfigContextLoaderUtils { } } - return configClasses.toArray(new Class[configClasses.size()]); + return ClassUtils.toClassArray(configClasses); } /** diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContext.java b/spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContext.java index 52c868cb108..300038fcd05 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContext.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DefaultTestContext.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. @@ -29,6 +29,7 @@ import org.springframework.test.context.CacheAwareContextLoaderDelegate; import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.TestContext; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * Default implementation of the {@link TestContext} interface. @@ -195,7 +196,7 @@ public class DefaultTestContext implements TestContext { @Override public String[] attributeNames() { synchronized (this.attributes) { - return this.attributes.keySet().stream().toArray(String[]::new); + return StringUtils.toStringArray(this.attributes.keySet()); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java index c72efb945f5..87eb3802376 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.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. @@ -295,7 +295,7 @@ final class HtmlUnitRequestBuilder implements RequestBuilder, Mergeable { } if (!ObjectUtils.isEmpty(cookies)) { - request.setCookies(cookies.toArray(new Cookie[cookies.size()])); + request.setCookies(cookies.toArray(new Cookie[0])); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java index a8adb2f6145..6953ec4b4d3 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/request/MockHttpServletRequestBuilder.java @@ -652,7 +652,7 @@ public class MockHttpServletRequestBuilder } if (!ObjectUtils.isEmpty(this.cookies)) { - request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()])); + request.setCookies(this.cookies.toArray(new Cookie[0])); } if (!ObjectUtils.isEmpty(this.locales)) { request.setPreferredLocales(this.locales); diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java index 9d83632dbff..4505e4639fb 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.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.lang.Nullable; import org.springframework.mock.web.MockServletConfig; import org.springframework.test.web.servlet.DispatcherServletCustomizer; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MockMvcBuilder; import org.springframework.test.web.servlet.MockMvcBuilderSupport; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.ResultHandler; @@ -34,7 +35,6 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.util.Assert; import org.springframework.web.context.WebApplicationContext; -import org.springframework.test.web.servlet.MockMvcBuilder; /** * Abstract implementation of {@link MockMvcBuilder} with common methods for @@ -144,7 +144,7 @@ public abstract class AbstractMockMvcBuilder } } - Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]); + Filter[] filterArray = this.filters.toArray(new Filter[0]); return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder, this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers); diff --git a/spring-tx/src/main/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslator.java b/spring-tx/src/main/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslator.java index 32d53f2994a..4e92008a3d1 100644 --- a/spring-tx/src/main/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslator.java +++ b/spring-tx/src/main/java/org/springframework/dao/support/ChainedPersistenceExceptionTranslator.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. @@ -50,7 +50,7 @@ public class ChainedPersistenceExceptionTranslator implements PersistenceExcepti * Return all registered PersistenceExceptionTranslator delegates (as array). */ public final PersistenceExceptionTranslator[] getDelegates() { - return this.delegates.toArray(new PersistenceExceptionTranslator[this.delegates.size()]); + return this.delegates.toArray(new PersistenceExceptionTranslator[0]); } diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultSslInfo.java b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultSslInfo.java index f705fef9dac..3085b60004f 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultSslInfo.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/DefaultSslInfo.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. @@ -103,7 +103,7 @@ final class DefaultSslInfo implements SslInfo { result.add((X509Certificate) certificate); } } - return !result.isEmpty() ? result.toArray(new X509Certificate[result.size()]) : null; + return (!result.isEmpty() ? result.toArray(new X509Certificate[0]) : null); } } 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 615f6b7d255..115a39a669a 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 @@ -213,7 +213,7 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe logger.info("Registering annotated classes: [" + StringUtils.collectionToCommaDelimitedString(this.annotatedClasses) + "]"); } - reader.register(this.annotatedClasses.toArray(new Class[this.annotatedClasses.size()])); + reader.register(ClassUtils.toClassArray(this.annotatedClasses)); } if (!this.basePackages.isEmpty()) { diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartResolutionDelegate.java b/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartResolutionDelegate.java index 30cdecad09c..8fc79bda1f6 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartResolutionDelegate.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartResolutionDelegate.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. @@ -93,7 +93,7 @@ public abstract class MultipartResolutionDelegate { } if (multipartRequest != null) { List multipartFiles = multipartRequest.getFiles(name); - return multipartFiles.toArray(new MultipartFile[multipartFiles.size()]); + return multipartFiles.toArray(new MultipartFile[0]); } else { return null; @@ -164,7 +164,7 @@ public abstract class MultipartResolutionDelegate { result.add(part); } } - return result.toArray(new Part[result.size()]); + return result.toArray(new Part[0]); } } diff --git a/spring-web/src/main/java/org/springframework/web/server/handler/FilteringWebHandler.java b/spring-web/src/main/java/org/springframework/web/server/handler/FilteringWebHandler.java index 6488b3c15a3..7d12e735e0d 100644 --- a/spring-web/src/main/java/org/springframework/web/server/handler/FilteringWebHandler.java +++ b/spring-web/src/main/java/org/springframework/web/server/handler/FilteringWebHandler.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,14 +21,13 @@ import java.util.List; import reactor.core.publisher.Mono; -import org.springframework.util.CollectionUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebHandler; /** - * WebHandler decorator that invokes a chain of {@link WebFilter}s before the - * delegate {@link WebHandler}. + * {@link WebHandler} decorator that invokes a chain of {@link WebFilter}s + * before the delegate {@link WebHandler}. * * @author Rossen Stoyanchev * @since 5.0 @@ -44,8 +43,7 @@ public class FilteringWebHandler extends WebHandlerDecorator { */ public FilteringWebHandler(WebHandler webHandler, List filters) { super(webHandler); - this.filters = !CollectionUtils.isEmpty(filters) ? - filters.toArray(new WebFilter[filters.size()]) : new WebFilter[0]; + this.filters = filters.toArray(new WebFilter[0]); } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java index cd8a3909bba..727faafd0c7 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockHttpServletResponse.java @@ -349,7 +349,7 @@ public class MockHttpServletResponse implements HttpServletResponse { } public Cookie[] getCookies() { - return this.cookies.toArray(new Cookie[this.cookies.size()]); + return this.cookies.toArray(new Cookie[0]); } public Cookie getCookie(String name) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java index b9dbb5c4272..62641b9e301 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.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. @@ -223,7 +223,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { if (getResourceResolvers().get(i) instanceof PathResourceResolver) { PathResourceResolver resolver = (PathResourceResolver) getResourceResolvers().get(i); if (ObjectUtils.isEmpty(resolver.getAllowedLocations())) { - resolver.setAllowedLocations(getLocations().toArray(new Resource[getLocations().size()])); + resolver.setAllowedLocations(getLocations().toArray(new Resource[0])); } break; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerExecutionChain.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerExecutionChain.java index 6867a569503..6c58f5dbc4b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerExecutionChain.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/HandlerExecutionChain.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. @@ -116,7 +116,7 @@ public class HandlerExecutionChain { @Nullable public HandlerInterceptor[] getInterceptors() { if (this.interceptors == null && this.interceptorList != null) { - this.interceptors = this.interceptorList.toArray(new HandlerInterceptor[this.interceptorList.size()]); + this.interceptors = this.interceptorList.toArray(new HandlerInterceptor[0]); } return this.interceptors; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java index 0cf6e2d19e0..45389c38a63 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMapping.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. @@ -317,8 +317,8 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport */ @Nullable protected final HandlerInterceptor[] getAdaptedInterceptors() { - int count = this.adaptedInterceptors.size(); - return (count > 0 ? this.adaptedInterceptors.toArray(new HandlerInterceptor[count]) : null); + return (!this.adaptedInterceptors.isEmpty() ? + this.adaptedInterceptors.toArray(new HandlerInterceptor[0]) : null); } /** @@ -327,14 +327,13 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport */ @Nullable protected final MappedInterceptor[] getMappedInterceptors() { - List mappedInterceptors = new ArrayList<>(); + List mappedInterceptors = new ArrayList<>(this.adaptedInterceptors.size()); for (HandlerInterceptor interceptor : this.adaptedInterceptors) { if (interceptor instanceof MappedInterceptor) { mappedInterceptors.add((MappedInterceptor) interceptor); } } - int count = mappedInterceptors.size(); - return (count > 0 ? mappedInterceptors.toArray(new MappedInterceptor[count]) : null); + return (!mappedInterceptors.isEmpty() ? mappedInterceptors.toArray(new MappedInterceptor[0]) : null); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index 0b33b8876e6..0945146f3d8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.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. @@ -47,7 +47,6 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage import org.springframework.http.converter.xml.SourceHttpMessageConverter; import org.springframework.lang.Nullable; import org.springframework.ui.ModelMap; -import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.ReflectionUtils.MethodFilter; import org.springframework.web.accept.ContentNegotiationManager; @@ -151,7 +150,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter private DeferredResultProcessingInterceptor[] deferredResultInterceptors = new DeferredResultProcessingInterceptor[0]; - private ReactiveAdapterRegistry reactiveRegistry = ReactiveAdapterRegistry.getSharedInstance(); + private ReactiveAdapterRegistry reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); private boolean ignoreDefaultModelOnRedirect = false; @@ -410,8 +409,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter * @param interceptors the interceptors to register */ public void setCallableInterceptors(List interceptors) { - Assert.notNull(interceptors, "CallableProcessingInterceptor List must not be null"); - this.callableInterceptors = interceptors.toArray(new CallableProcessingInterceptor[interceptors.size()]); + this.callableInterceptors = interceptors.toArray(new CallableProcessingInterceptor[0]); } /** @@ -419,18 +417,27 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter * @param interceptors the interceptors to register */ public void setDeferredResultInterceptors(List interceptors) { - Assert.notNull(interceptors, "DeferredResultProcessingInterceptor List must not be null"); - this.deferredResultInterceptors = interceptors.toArray(new DeferredResultProcessingInterceptor[interceptors.size()]); + this.deferredResultInterceptors = interceptors.toArray(new DeferredResultProcessingInterceptor[0]); } /** * Configure the registry for reactive library types to be supported as * return values from controller methods. * @since 5.0 + * @deprecated as of 5.0.5, in favor of {@link #setReactiveAdapterRegistry} */ + @Deprecated public void setReactiveRegistry(ReactiveAdapterRegistry reactiveRegistry) { - Assert.notNull(reactiveRegistry, "ReactiveAdapterRegistry is required"); - this.reactiveRegistry = reactiveRegistry; + this.reactiveAdapterRegistry = reactiveRegistry; + } + + /** + * Configure the registry for reactive library types to be supported as + * return values from controller methods. + * @since 5.0.5 + */ + public void setReactiveAdapterRegistry(ReactiveAdapterRegistry reactiveAdapterRegistry) { + this.reactiveAdapterRegistry = reactiveAdapterRegistry; } /** @@ -438,7 +445,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter * @since 5.0 */ public ReactiveAdapterRegistry getReactiveAdapterRegistry() { - return this.reactiveRegistry; + return this.reactiveAdapterRegistry; } /** @@ -702,7 +709,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter handlers.add(new ModelMethodProcessor()); handlers.add(new ViewMethodReturnValueHandler()); handlers.add(new ResponseBodyEmitterReturnValueHandler(getMessageConverters(), - this.reactiveRegistry, this.taskExecutor, this.contentNegotiationManager)); + this.reactiveAdapterRegistry, this.taskExecutor, this.contentNegotiationManager)); handlers.add(new StreamingResponseBodyReturnValueHandler()); handlers.add(new HttpEntityMethodProcessor(getMessageConverters(), this.contentNegotiationManager, this.requestResponseBodyAdvice)); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java index 320df25feb5..6bd08483420 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.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. @@ -387,7 +387,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator if (getResourceResolvers().get(i) instanceof PathResourceResolver) { PathResourceResolver pathResolver = (PathResourceResolver) getResourceResolvers().get(i); if (ObjectUtils.isEmpty(pathResolver.getAllowedLocations())) { - pathResolver.setAllowedLocations(getLocations().toArray(new Resource[getLocations().size()])); + pathResolver.setAllowedLocations(getLocations().toArray(new Resource[0])); } if (this.urlPathHelper != null) { pathResolver.setLocationCharsets(this.locationCharsets); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupConfigurer.java index 2599c227457..620e869f356 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupConfigurer.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupConfigurer.java @@ -180,7 +180,7 @@ public class GroovyMarkupConfigurer extends TemplateConfiguration } ClassLoader classLoader = getApplicationContext().getClassLoader(); Assert.state(classLoader != null, "No ClassLoader"); - return (!urls.isEmpty() ? new URLClassLoader(urls.toArray(new URL[urls.size()]), classLoader) : classLoader); + return (!urls.isEmpty() ? new URLClassLoader(urls.toArray(new URL[0]), classLoader) : classLoader); } /** 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 3c2b0e4ed66..fe3d1b9ed5d 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 @@ -135,7 +135,7 @@ public abstract class AbstractWebSocketHandlerRegistration implements WebSock List interceptors = new ArrayList<>(this.interceptors.size() + 1); interceptors.addAll(this.interceptors); interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins)); - return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]); + return interceptors.toArray(new HandshakeInterceptor[0]); } /** 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 edb80030944..8a8a4a5fa97 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 @@ -62,8 +62,8 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE private SockJsServiceRegistration registration; - public WebMvcStompWebSocketEndpointRegistration(String[] paths, WebSocketHandler webSocketHandler, - TaskScheduler sockJsTaskScheduler) { + public WebMvcStompWebSocketEndpointRegistration( + String[] paths, WebSocketHandler webSocketHandler, TaskScheduler sockJsTaskScheduler) { Assert.notEmpty(paths, "No paths specified"); Assert.notNull(webSocketHandler, "WebSocketHandler must not be null"); @@ -76,7 +76,6 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE @Override public StompWebSocketEndpointRegistration setHandshakeHandler(HandshakeHandler handshakeHandler) { - Assert.notNull(handshakeHandler, "'handshakeHandler' must not be null"); this.handshakeHandler = handshakeHandler; return this; } @@ -117,10 +116,10 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE } protected HandshakeInterceptor[] getInterceptors() { - List interceptors = new ArrayList<>(); + List interceptors = new ArrayList<>(this.interceptors.size() + 1); interceptors.addAll(this.interceptors); interceptors.add(new OriginHandshakeInterceptor(this.allowedOrigins)); - return interceptors.toArray(new HandshakeInterceptor[interceptors.size()]); + return interceptors.toArray(new HandshakeInterceptor[0]); } public final MultiValueMap getMappings() { @@ -128,7 +127,7 @@ public class WebMvcStompWebSocketEndpointRegistration implements StompWebSocketE if (this.registration != null) { SockJsService sockJsService = this.registration.getSockJsService(); for (String path : this.paths) { - String pattern = path.endsWith("/") ? path + "**" : path + "/**"; + String pattern = (path.endsWith("/") ? path + "**" : path + "/**"); SockJsHttpRequestHandler handler = new SockJsHttpRequestHandler(sockJsService, this.webSocketHandler); mappings.add(handler, pattern); }