From cf2e0c7959fad8d8f79ffba9a3e14edd65831940 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 27 Aug 2020 14:14:44 +0200 Subject: [PATCH] Selected use of ArrayList instead of LinkedList in common places See gh-25652 --- .../beans/factory/support/ReplaceOverride.java | 7 ++++--- .../validation/AbstractBindingResult.java | 10 +++++----- .../springframework/validation/AbstractErrors.java | 8 ++++---- .../HandlerMethodArgumentResolverComposite.java | 6 +++--- .../HandlerMethodArgumentResolverComposite.java | 11 +++++------ .../messaging/simp/stomp/StompEncoder.java | 7 +++---- .../persistenceunit/MutablePersistenceUnitInfo.java | 12 ++++++------ .../PersistenceAnnotationBeanPostProcessor.java | 5 ++--- .../config/TxAdviceBeanDefinitionParser.java | 10 +++++----- .../HandlerMethodArgumentResolverComposite.java | 6 +++--- .../web/util/pattern/RegexPathElement.java | 6 +++--- .../HandlerMethodArgumentResolverComposite.java | 11 +++++------ 12 files changed, 48 insertions(+), 51 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java index c94ce671762..ea8c3495c54 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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,7 +17,7 @@ package org.springframework.beans.factory.support; import java.lang.reflect.Method; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import org.springframework.lang.Nullable; @@ -39,7 +39,7 @@ public class ReplaceOverride extends MethodOverride { private final String methodReplacerBeanName; - private List typeIdentifiers = new LinkedList<>(); + private final List typeIdentifiers = new ArrayList<>(); /** @@ -70,6 +70,7 @@ public class ReplaceOverride extends MethodOverride { this.typeIdentifiers.add(identifier); } + @Override public boolean matches(Method method) { if (!method.getName().equals(getMethodName())) { diff --git a/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java b/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java index eb8b3104685..b8857b15b50 100644 --- a/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java +++ b/spring-context/src/main/java/org/springframework/validation/AbstractBindingResult.java @@ -18,11 +18,11 @@ package org.springframework.validation; import java.beans.PropertyEditor; import java.io.Serializable; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -50,7 +50,7 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi private MessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver(); - private final List errors = new LinkedList<>(); + private final List errors = new ArrayList<>(); private final Map> fieldTypes = new HashMap<>(); @@ -145,7 +145,7 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @Override public List getGlobalErrors() { - List result = new LinkedList<>(); + List result = new ArrayList<>(); for (ObjectError objectError : this.errors) { if (!(objectError instanceof FieldError)) { result.add(objectError); @@ -167,7 +167,7 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @Override public List getFieldErrors() { - List result = new LinkedList<>(); + List result = new ArrayList<>(); for (ObjectError objectError : this.errors) { if (objectError instanceof FieldError) { result.add((FieldError) objectError); @@ -189,7 +189,7 @@ public abstract class AbstractBindingResult extends AbstractErrors implements Bi @Override public List getFieldErrors(String field) { - List result = new LinkedList<>(); + List result = new ArrayList<>(); String fixedField = fixedField(field); for (ObjectError objectError : this.errors) { if (objectError instanceof FieldError && isMatchingFieldError(fixedField, (FieldError) objectError)) { diff --git a/spring-context/src/main/java/org/springframework/validation/AbstractErrors.java b/spring-context/src/main/java/org/springframework/validation/AbstractErrors.java index 355f0872621..9556dc3a286 100644 --- a/spring-context/src/main/java/org/springframework/validation/AbstractErrors.java +++ b/spring-context/src/main/java/org/springframework/validation/AbstractErrors.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -18,9 +18,9 @@ package org.springframework.validation; import java.io.Serializable; import java.util.ArrayDeque; +import java.util.ArrayList; import java.util.Collections; import java.util.Deque; -import java.util.LinkedList; import java.util.List; import java.util.NoSuchElementException; @@ -146,7 +146,7 @@ public abstract class AbstractErrors implements Errors, Serializable { @Override public List getAllErrors() { - List result = new LinkedList<>(); + List result = new ArrayList<>(); result.addAll(getGlobalErrors()); result.addAll(getFieldErrors()); return Collections.unmodifiableList(result); @@ -199,7 +199,7 @@ public abstract class AbstractErrors implements Errors, Serializable { @Override public List getFieldErrors(String field) { List fieldErrors = getFieldErrors(); - List result = new LinkedList<>(); + List result = new ArrayList<>(); String fixedField = fixedField(field); for (FieldError error : fieldErrors) { if (isMatchingFieldError(fixedField, error)) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java index 937befe33e6..7bde7fb3d37 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodArgumentResolverComposite.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -16,8 +16,8 @@ package org.springframework.messaging.handler.invocation; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -37,7 +37,7 @@ import org.springframework.messaging.Message; */ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver { - private final List argumentResolvers = new LinkedList<>(); + private final List argumentResolvers = new ArrayList<>(); private final Map argumentResolverCache = new ConcurrentHashMap<>(256); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodArgumentResolverComposite.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodArgumentResolverComposite.java index 6822b29f634..3009a05e561 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodArgumentResolverComposite.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodArgumentResolverComposite.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -16,8 +16,8 @@ package org.springframework.messaging.handler.invocation.reactive; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -42,7 +42,7 @@ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgu protected final Log logger = LogFactory.getLog(getClass()); - private final List argumentResolvers = new LinkedList<>(); + private final List argumentResolvers = new ArrayList<>(); private final Map argumentResolverCache = new ConcurrentHashMap<>(256); @@ -113,9 +113,8 @@ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgu public Mono resolveArgument(MethodParameter parameter, Message message) { HandlerMethodArgumentResolver resolver = getArgumentResolver(parameter); if (resolver == null) { - throw new IllegalArgumentException( - "Unsupported parameter type [" + parameter.getParameterType().getName() + "]." + - " supportsParameter should be called first."); + throw new IllegalArgumentException("Unsupported parameter type [" + + parameter.getParameterType().getName() + "]. supportsParameter should be called first."); } return resolver.resolveArgument(parameter, message); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java index a9c163ac691..175043f586d 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java @@ -17,9 +17,9 @@ package org.springframework.messaging.simp.stomp; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -228,15 +228,14 @@ public class StompEncoder { void add(byte b); byte[] toByteArray(); - } + @SuppressWarnings("serial") - private static class DefaultResult extends LinkedList implements Result { + private static class DefaultResult extends ArrayList implements Result { private int size; - public void add(byte[] bytes) { this.size += bytes.length; super.add(bytes); diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/MutablePersistenceUnitInfo.java b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/MutablePersistenceUnitInfo.java index c4c2b1938e5..b006f76bfaf 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/MutablePersistenceUnitInfo.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/persistenceunit/MutablePersistenceUnitInfo.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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,7 +17,7 @@ package org.springframework.orm.jpa.persistenceunit; import java.net.URL; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -61,16 +61,16 @@ public class MutablePersistenceUnitInfo implements SmartPersistenceUnitInfo { @Nullable private DataSource jtaDataSource; - private final List mappingFileNames = new LinkedList<>(); + private final List mappingFileNames = new ArrayList<>(); - private List jarFileUrls = new LinkedList<>(); + private final List jarFileUrls = new ArrayList<>(); @Nullable private URL persistenceUnitRootUrl; - private final List managedClassNames = new LinkedList<>(); + private final List managedClassNames = new ArrayList<>(); - private final List managedPackages = new LinkedList<>(); + private final List managedPackages = new ArrayList<>(); private boolean excludeUnlistedClasses = false; diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java index 3560c02cde3..044ffd71a13 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -24,7 +24,6 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; @@ -423,7 +422,7 @@ public class PersistenceAnnotationBeanPostProcessor Class targetClass = clazz; do { - final LinkedList currElements = new LinkedList<>(); + final List currElements = new ArrayList<>(); ReflectionUtils.doWithLocalFields(targetClass, field -> { if (field.isAnnotationPresent(PersistenceContext.class) || diff --git a/spring-tx/src/main/java/org/springframework/transaction/config/TxAdviceBeanDefinitionParser.java b/spring-tx/src/main/java/org/springframework/transaction/config/TxAdviceBeanDefinitionParser.java index 41a1e613700..e763ade8fb4 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/config/TxAdviceBeanDefinitionParser.java +++ b/spring-tx/src/main/java/org/springframework/transaction/config/TxAdviceBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2020 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. @@ -16,7 +16,7 @@ package org.springframework.transaction.config; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; @@ -127,14 +127,14 @@ class TxAdviceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { attribute.setReadOnly(Boolean.parseBoolean(methodEle.getAttribute(READ_ONLY_ATTRIBUTE))); } - List rollbackRules = new LinkedList<>(); + List rollbackRules = new ArrayList<>(1); if (methodEle.hasAttribute(ROLLBACK_FOR_ATTRIBUTE)) { String rollbackForValue = methodEle.getAttribute(ROLLBACK_FOR_ATTRIBUTE); - addRollbackRuleAttributesTo(rollbackRules,rollbackForValue); + addRollbackRuleAttributesTo(rollbackRules, rollbackForValue); } if (methodEle.hasAttribute(NO_ROLLBACK_FOR_ATTRIBUTE)) { String noRollbackForValue = methodEle.getAttribute(NO_ROLLBACK_FOR_ATTRIBUTE); - addNoRollbackRuleAttributesTo(rollbackRules,noRollbackForValue); + addNoRollbackRuleAttributesTo(rollbackRules, noRollbackForValue); } attribute.setRollbackRules(rollbackRules); diff --git a/spring-web/src/main/java/org/springframework/web/method/support/HandlerMethodArgumentResolverComposite.java b/spring-web/src/main/java/org/springframework/web/method/support/HandlerMethodArgumentResolverComposite.java index 2b1cba3b050..60a27b9ae5b 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/HandlerMethodArgumentResolverComposite.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/HandlerMethodArgumentResolverComposite.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -16,8 +16,8 @@ package org.springframework.web.method.support; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -38,7 +38,7 @@ import org.springframework.web.context.request.NativeWebRequest; */ public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver { - private final List argumentResolvers = new LinkedList<>(); + private final List argumentResolvers = new ArrayList<>(); private final Map argumentResolverCache = new ConcurrentHashMap<>(256); diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java b/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java index dd55d5c39c4..f326a87e134 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/RegexPathElement.java @@ -16,7 +16,7 @@ package org.springframework.web.util.pattern; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,7 +40,7 @@ class RegexPathElement extends PathElement { private static final String DEFAULT_VARIABLE_PATTERN = "(.*)"; - private char[] regex; + private final char[] regex; private final boolean caseSensitive; @@ -48,7 +48,7 @@ class RegexPathElement extends PathElement { private int wildcardCount; - private final List variableNames = new LinkedList<>(); + private final List variableNames = new ArrayList<>(); RegexPathElement(int pos, char[] regex, boolean caseSensitive, char[] completePattern, char separator) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/HandlerMethodArgumentResolverComposite.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/HandlerMethodArgumentResolverComposite.java index 5af5d2679d7..1161c1c8e4a 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/HandlerMethodArgumentResolverComposite.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/HandlerMethodArgumentResolverComposite.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -16,8 +16,8 @@ package org.springframework.web.reactive.result.method; +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -43,7 +43,7 @@ class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentRes protected final Log logger = LogFactory.getLog(getClass()); - private final List argumentResolvers = new LinkedList<>(); + private final List argumentResolvers = new ArrayList<>(); private final Map argumentResolverCache = new ConcurrentHashMap<>(256); @@ -116,9 +116,8 @@ class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentRes HandlerMethodArgumentResolver resolver = getArgumentResolver(parameter); if (resolver == null) { - throw new IllegalArgumentException( - "Unsupported parameter type [" + parameter.getParameterType().getName() + "]." + - " supportsParameter should be called first."); + throw new IllegalArgumentException("Unsupported parameter type [" + + parameter.getParameterType().getName() + "]. supportsParameter should be called first."); } return resolver.resolveArgument(parameter, bindingContext, exchange); }