Browse Source

Revise contribution

This commit reverts changes to AbstractCacheManager since iterating
over the caches in a for-loop and a stream is duplicated effort.

This commit reverts changes to DefaultRenderingResponseBuilder,
RouterFunctions, and OriginHandshakeInterceptor since order matters for
those use cases: they were originally based on the semantics of
LinkedHashSet or LinkedHashMap; whereas, Set.copyOf() and Map.copyOf()
do not provide any guarantees regarding ordering.

This commit also applies analogous changes to "sibling" implementations
across Servlet mocks as well as Web MVC and WebFlux.

See gh-29321
pull/29447/head
Sam Brannen 2 years ago
parent
commit
95f3337bb5
  1. 7
      spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java
  2. 2
      spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java
  3. 2
      spring-test/src/main/java/org/springframework/mock/web/HeaderValueHolder.java
  4. 9
      spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/HeaderValueHolder.java
  5. 10
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultExchangeStrategiesBuilder.java
  6. 16
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java
  7. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java
  8. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java
  9. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java
  10. 2
      spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java
  11. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java
  12. 12
      spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java
  13. 3
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java

7
spring-context/src/main/java/org/springframework/cache/support/AbstractCacheManager.java vendored

@ -22,7 +22,6 @@ import java.util.LinkedHashSet; @@ -22,7 +22,6 @@ import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.Cache;
@ -65,13 +64,13 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing @@ -65,13 +64,13 @@ public abstract class AbstractCacheManager implements CacheManager, Initializing
synchronized (this.cacheMap) {
this.cacheNames = Collections.emptySet();
this.cacheMap.clear();
Set<String> cacheNames = new LinkedHashSet<>(caches.size());
for (Cache cache : caches) {
String name = cache.getName();
this.cacheMap.put(name, decorateCache(cache));
cacheNames.add(name);
}
this.cacheNames = caches.stream()
.map(Cache::getName)
.collect(Collectors.toUnmodifiableSet());
this.cacheNames = Collections.unmodifiableSet(cacheNames);
}
}

2
spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java

@ -1,5 +1,5 @@ @@ -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.

2
spring-test/src/main/java/org/springframework/mock/web/HeaderValueHolder.java

@ -1,5 +1,5 @@ @@ -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.

9
spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/HeaderValueHolder.java

@ -1,5 +1,5 @@ @@ -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.
@ -16,7 +16,6 @@ @@ -16,7 +16,6 @@
package org.springframework.web.testfixture.servlet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
@ -61,11 +60,7 @@ class HeaderValueHolder { @@ -61,11 +60,7 @@ class HeaderValueHolder {
}
List<String> getStringValues() {
List<String> stringList = new ArrayList<>(this.values.size());
for (Object value : this.values) {
stringList.add(value.toString());
}
return Collections.unmodifiableList(stringList);
return this.values.stream().map(Object::toString).toList();
}
@Nullable

10
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultExchangeStrategiesBuilder.java

@ -1,5 +1,5 @@ @@ -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.
@ -81,12 +81,8 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build @@ -81,12 +81,8 @@ final class DefaultExchangeStrategiesBuilder implements ExchangeStrategies.Build
public DefaultExchangeStrategies(ClientCodecConfigurer codecConfigurer) {
this.codecConfigurer = codecConfigurer;
this.readers = unmodifiableCopy(this.codecConfigurer.getReaders());
this.writers = unmodifiableCopy(this.codecConfigurer.getWriters());
}
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
return List.copyOf(list);
this.readers = List.copyOf(this.codecConfigurer.getReaders());
this.writers = List.copyOf(this.codecConfigurer.getWriters());
}
@Override

16
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultHandlerStrategiesBuilder.java

@ -1,5 +1,5 @@ @@ -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.
@ -125,18 +125,14 @@ class DefaultHandlerStrategiesBuilder implements HandlerStrategies.Builder { @@ -125,18 +125,14 @@ class DefaultHandlerStrategiesBuilder implements HandlerStrategies.Builder {
List<WebExceptionHandler> exceptionHandlers,
LocaleContextResolver localeContextResolver) {
this.messageReaders = unmodifiableCopy(messageReaders);
this.messageWriters = unmodifiableCopy(messageWriters);
this.viewResolvers = unmodifiableCopy(viewResolvers);
this.webFilters = unmodifiableCopy(webFilters);
this.exceptionHandlers = unmodifiableCopy(exceptionHandlers);
this.messageReaders = List.copyOf(messageReaders);
this.messageWriters = List.copyOf(messageWriters);
this.viewResolvers = List.copyOf(viewResolvers);
this.webFilters = List.copyOf(webFilters);
this.exceptionHandlers = List.copyOf(exceptionHandlers);
this.localeContextResolver = localeContextResolver;
}
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
return List.copyOf(list);
}
@Override
public List<HttpMessageReader<?>> messageReaders() {
return this.messageReaders;

2
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java

@ -171,7 +171,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder @@ -171,7 +171,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder
super(statusCode, headers, cookies, Collections.emptyMap());
this.name = name;
this.model = Map.copyOf(model);
this.model = Collections.unmodifiableMap(new LinkedHashMap<>(model));
}
@Override

2
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java

@ -1,5 +1,5 @@ @@ -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.

2
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/RouterFunctions.java

@ -1175,7 +1175,7 @@ public abstract class RouterFunctions { @@ -1175,7 +1175,7 @@ public abstract class RouterFunctions {
return Collections.emptyMap();
}
else {
return Map.copyOf(attributes);
return Collections.unmodifiableMap(new LinkedHashMap<>(attributes));
}
}

2
spring-webmvc/src/main/java/org/springframework/web/servlet/function/DefaultServerRequest.java

@ -95,7 +95,7 @@ class DefaultServerRequest implements ServerRequest { @@ -95,7 +95,7 @@ class DefaultServerRequest implements ServerRequest {
public DefaultServerRequest(HttpServletRequest servletRequest, List<HttpMessageConverter<?>> messageConverters) {
this.serverHttpRequest = new ServletServerHttpRequest(servletRequest);
this.messageConverters = Collections.unmodifiableList(new ArrayList<>(messageConverters));
this.messageConverters = List.copyOf(messageConverters);
this.headers = new DefaultRequestHeaders(this.serverHttpRequest.getHeaders());
this.params = CollectionUtils.toMultiValueMap(new ServletParametersMap(servletRequest));

5
spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

@ -147,9 +147,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap @@ -147,9 +147,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
public Map<T, HandlerMethod> getHandlerMethods() {
this.mappingRegistry.acquireReadLock();
try {
return Collections.unmodifiableMap(
this.mappingRegistry.getRegistrations().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().handlerMethod)));
return this.mappingRegistry.getRegistrations().entrySet().stream()
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, entry -> entry.getValue().handlerMethod));
}
finally {
this.mappingRegistry.releaseReadLock();

12
spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java

@ -19,9 +19,9 @@ package org.springframework.web.socket.server.support; @@ -19,9 +19,9 @@ package org.springframework.web.socket.server.support;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -95,8 +95,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor { @@ -95,8 +95,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor {
* @since 4.1.5
*/
public Collection<String> getAllowedOrigins() {
return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOrigins()) ? Collections.emptySet() :
Set.copyOf(this.corsConfiguration.getAllowedOrigins()));
List<String> allowedOrigins = this.corsConfiguration.getAllowedOrigins();
return (CollectionUtils.isEmpty(allowedOrigins) ? Collections.emptySet() :
Collections.unmodifiableSet(new LinkedHashSet<>(allowedOrigins)));
}
/**
@ -118,8 +119,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor { @@ -118,8 +119,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor {
* @since 5.3.2
*/
public Collection<String> getAllowedOriginPatterns() {
return (CollectionUtils.isEmpty(this.corsConfiguration.getAllowedOriginPatterns()) ? Collections.emptySet() :
Set.copyOf(this.corsConfiguration.getAllowedOriginPatterns()));
List<String> allowedOriginPatterns = this.corsConfiguration.getAllowedOriginPatterns();
return (CollectionUtils.isEmpty(allowedOriginPatterns) ? Collections.emptySet() :
Collections.unmodifiableSet(new LinkedHashSet<>(allowedOriginPatterns)));
}

3
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportType.java

@ -1,5 +1,5 @@ @@ -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.
@ -49,6 +49,7 @@ public enum TransportType { @@ -49,6 +49,7 @@ public enum TransportType {
private static final Map<String, TransportType> TRANSPORT_TYPES =
Arrays.stream(values()).collect(Collectors.toUnmodifiableMap(type -> type.value, type -> type));
@Nullable
public static TransportType fromValue(String value) {
return TRANSPORT_TYPES.get(value);

Loading…
Cancel
Save