From 2b65f274dc77645d4af6ca76a62008b6cede2675 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 6 May 2022 16:19:27 +0200 Subject: [PATCH] Use Set.of instead of HashSet with Arrays.asList --- .../index/processor/PropertiesMarshaller.java | 6 ++---- .../ConfigurationClassPostProcessor.java | 3 +-- .../MethodExclusionMBeanInfoAssembler.java | 8 +++----- .../MethodNameBasedMBeanInfoAssembler.java | 8 +++----- .../org/springframework/util/ClassUtils.java | 2 +- .../function/server/RequestPredicates.java | 15 ++++++++------- .../web/servlet/function/RequestPredicates.java | 17 ++++++++--------- ...AbstractMessageConverterMethodProcessor.java | 15 +++++++-------- .../web/servlet/view/AbstractView.java | 6 ++---- .../session/AbstractSockJsSession.java | 3 +-- 10 files changed, 36 insertions(+), 47 deletions(-) diff --git a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java index 84a8d838c9..ab6c52aafe 100644 --- a/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java +++ b/spring-context-indexer/src/main/java/org/springframework/context/index/processor/PropertiesMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,6 @@ package org.springframework.context.index.processor; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Arrays; -import java.util.HashSet; import java.util.Properties; import java.util.Set; @@ -44,7 +42,7 @@ abstract class PropertiesMarshaller { Properties props = new Properties(); props.load(in); props.forEach((type, value) -> { - Set candidates = new HashSet<>(Arrays.asList(((String) value).split(","))); + Set candidates = Set.of(((String) value).split(",")); result.add(new ItemMetadata((String) type, candidates)); }); return result; diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index ba42d98f5a..e883195a88 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -17,7 +17,6 @@ package org.springframework.context.annotation; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -371,7 +370,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo candidates.clear(); if (registry.getBeanDefinitionCount() > candidateNames.length) { String[] newCandidateNames = registry.getBeanDefinitionNames(); - Set oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames)); + Set oldCandidateNames = Set.of(candidateNames); Set alreadyParsedClasses = new HashSet<>(); for (ConfigurationClass configurationClass : alreadyParsed) { alreadyParsedClasses.add(configurationClass.getMetadata().getClassName()); diff --git a/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodExclusionMBeanInfoAssembler.java b/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodExclusionMBeanInfoAssembler.java index 1edd14db8a..af780d54a8 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodExclusionMBeanInfoAssembler.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodExclusionMBeanInfoAssembler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2022 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,10 +17,8 @@ package org.springframework.jmx.export.assembler; import java.lang.reflect.Method; -import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -72,7 +70,7 @@ public class MethodExclusionMBeanInfoAssembler extends AbstractConfigurableMBean * @see #setIgnoredMethodMappings(java.util.Properties) */ public void setIgnoredMethods(String... ignoredMethodNames) { - this.ignoredMethods = new HashSet<>(Arrays.asList(ignoredMethodNames)); + this.ignoredMethods = Set.of(ignoredMethodNames); } /** @@ -87,7 +85,7 @@ public class MethodExclusionMBeanInfoAssembler extends AbstractConfigurableMBean for (Enumeration en = mappings.keys(); en.hasMoreElements();) { String beanKey = (String) en.nextElement(); String[] methodNames = StringUtils.commaDelimitedListToStringArray(mappings.getProperty(beanKey)); - this.ignoredMethodMappings.put(beanKey, new HashSet<>(Arrays.asList(methodNames))); + this.ignoredMethodMappings.put(beanKey, Set.of(methodNames)); } } diff --git a/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodNameBasedMBeanInfoAssembler.java b/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodNameBasedMBeanInfoAssembler.java index 0dfe17ef7e..f0d1392d57 100644 --- a/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodNameBasedMBeanInfoAssembler.java +++ b/spring-context/src/main/java/org/springframework/jmx/export/assembler/MethodNameBasedMBeanInfoAssembler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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,10 +17,8 @@ package org.springframework.jmx.export.assembler; import java.lang.reflect.Method; -import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -76,7 +74,7 @@ public class MethodNameBasedMBeanInfoAssembler extends AbstractConfigurableMBean * @see #setMethodMappings */ public void setManagedMethods(String... methodNames) { - this.managedMethods = new HashSet<>(Arrays.asList(methodNames)); + this.managedMethods = Set.of(methodNames); } /** @@ -91,7 +89,7 @@ public class MethodNameBasedMBeanInfoAssembler extends AbstractConfigurableMBean for (Enumeration en = mappings.keys(); en.hasMoreElements();) { String beanKey = (String) en.nextElement(); String[] methodNames = StringUtils.commaDelimitedListToStringArray(mappings.getProperty(beanKey)); - this.methodMappings.put(beanKey, new HashSet<>(Arrays.asList(methodNames))); + this.methodMappings.put(beanKey, Set.of(methodNames)); } } 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 dd54c03ed5..5c4fb6ed22 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -157,7 +157,7 @@ public abstract class ClassUtils { Class[] javaLanguageInterfaceArray = {Serializable.class, Externalizable.class, Closeable.class, AutoCloseable.class, Cloneable.class, Comparable.class}; registerCommonClasses(javaLanguageInterfaceArray); - javaLanguageInterfaces = new HashSet<>(Arrays.asList(javaLanguageInterfaceArray)); + javaLanguageInterfaces = Set.of(javaLanguageInterfaceArray); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java index 3ae0b2f9c3..5531993504 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RequestPredicates.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,6 @@ import java.security.Principal; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -73,6 +72,7 @@ public abstract class RequestPredicates { private static final Log logger = LogFactory.getLog(RequestPredicates.class); + /** * Return a {@code RequestPredicate} that always matches. * @return a predicate that always matches @@ -537,7 +537,6 @@ public abstract class RequestPredicates { public String toString() { return this.pattern.getPatternString(); } - } @@ -566,12 +565,13 @@ public abstract class RequestPredicates { } } + private static class ContentTypePredicate extends HeadersPredicate { private final Set mediaTypes; public ContentTypePredicate(MediaType... mediaTypes) { - this(new HashSet<>(Arrays.asList(mediaTypes))); + this(Set.of(mediaTypes)); } private ContentTypePredicate(Set mediaTypes) { @@ -603,12 +603,13 @@ public abstract class RequestPredicates { } } + private static class AcceptPredicate extends HeadersPredicate { private final Set mediaTypes; public AcceptPredicate(MediaType... mediaTypes) { - this(new HashSet<>(Arrays.asList(mediaTypes))); + this(Set.of(mediaTypes)); } private AcceptPredicate(Set mediaTypes) { @@ -698,7 +699,6 @@ public abstract class RequestPredicates { this.extension : this.extensionPredicate); } - } @@ -809,6 +809,7 @@ public abstract class RequestPredicates { } } + /** * {@link RequestPredicate} that negates a delegate predicate. */ @@ -851,6 +852,7 @@ public abstract class RequestPredicates { } } + /** * {@link RequestPredicate} where either {@code left} or {@code right} predicates * may match. @@ -1094,7 +1096,6 @@ public abstract class RequestPredicates { public String toString() { return method() + " " + path(); } - } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java index 653f3af946..cb9e9afbc3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/function/RequestPredicates.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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.time.Instant; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -73,6 +72,7 @@ public abstract class RequestPredicates { private static final Log logger = LogFactory.getLog(RequestPredicates.class); + /** * Return a {@code RequestPredicate} that always matches. * @return a predicate that always matches @@ -535,7 +535,6 @@ public abstract class RequestPredicates { public String toString() { return this.pattern.getPatternString(); } - } @@ -564,12 +563,13 @@ public abstract class RequestPredicates { } } + private static class ContentTypePredicate extends HeadersPredicate { private final Set mediaTypes; public ContentTypePredicate(MediaType... mediaTypes) { - this(new HashSet<>(Arrays.asList(mediaTypes))); + this(Set.of(mediaTypes)); } private ContentTypePredicate(Set mediaTypes) { @@ -601,12 +601,13 @@ public abstract class RequestPredicates { } } + private static class AcceptPredicate extends HeadersPredicate { private final Set mediaTypes; public AcceptPredicate(MediaType... mediaTypes) { - this(new HashSet<>(Arrays.asList(mediaTypes))); + this(Set.of(mediaTypes)); } private AcceptPredicate(Set mediaTypes) { @@ -696,7 +697,6 @@ public abstract class RequestPredicates { this.extension : this.extensionPredicate); } - } @@ -807,6 +807,7 @@ public abstract class RequestPredicates { } } + /** * {@link RequestPredicate} that negates a delegate predicate. */ @@ -849,6 +850,7 @@ public abstract class RequestPredicates { } } + /** * {@link RequestPredicate} where either {@code left} or {@code right} predicates * may match. @@ -1053,8 +1055,6 @@ public abstract class RequestPredicates { return this.request.session(); } - - @Override public Optional principal() { return this.request.principal(); @@ -1084,7 +1084,6 @@ public abstract class RequestPredicates { public String toString() { return method() + " " + path(); } - } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java index 72f0ab88d5..d1421b2454 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.method.annotation; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -77,19 +76,19 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe implements HandlerMethodReturnValueHandler { /* Extensions associated with the built-in message converters */ - private static final Set SAFE_EXTENSIONS = new HashSet<>(Arrays.asList( + private static final Set SAFE_EXTENSIONS = Set.of( "txt", "text", "yml", "properties", "csv", "json", "xml", "atom", "rss", - "png", "jpe", "jpeg", "jpg", "gif", "wbmp", "bmp")); + "png", "jpe", "jpeg", "jpg", "gif", "wbmp", "bmp"); - private static final Set SAFE_MEDIA_BASE_TYPES = new HashSet<>( - Arrays.asList("audio", "image", "video")); + private static final Set SAFE_MEDIA_BASE_TYPES = + Set.of("audio", "image", "video"); private static final List ALL_APPLICATION_MEDIA_TYPES = - Arrays.asList(MediaType.ALL, new MediaType("application")); + List.of(MediaType.ALL, new MediaType("application")); private static final Type RESOURCE_REGION_LIST_TYPE = - new ParameterizedTypeReference>() { }.getType(); + new ParameterizedTypeReference>() {}.getType(); private final ContentNegotiationManager contentNegotiationManager; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractView.java index 5f47cb303c..719283882b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/AbstractView.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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,7 @@ package org.springframework.web.servlet.view; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @@ -273,7 +271,7 @@ public abstract class AbstractView extends WebApplicationObjectSupport implement * flag on but do not list specific bean names for this property. */ public void setExposedContextBeanNames(String... exposedContextBeanNames) { - this.exposedContextBeanNames = new HashSet<>(Arrays.asList(exposedContextBeanNames)); + this.exposedContextBeanNames = Set.of(exposedContextBeanNames); } /** diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java index 9773f03a6d..ad334fe9e1 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractSockJsSession.java @@ -20,7 +20,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.Date; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -81,7 +80,7 @@ public abstract class AbstractSockJsSession implements SockJsSession { * @see #indicatesDisconnectedClient(Throwable) */ private static final Set DISCONNECTED_CLIENT_EXCEPTIONS = - new HashSet<>(Arrays.asList("ClientAbortException", "EOFException", "EofException")); + Set.of("ClientAbortException", "EOFException", "EofException"); /**