From 1195b3a0b0b7399d9fd2ea79e1d3c2031a4d8119 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 8 Dec 2020 10:39:56 +0100 Subject: [PATCH] Polishing --- .../aop/aspectj/AspectJProxyUtils.java | 6 ++++-- ...linSerializationJsonHttpMessageConverter.java | 4 ++-- .../json/KotlinSerializationJsonDecoderTests.kt | 3 ++- .../json/KotlinSerializationJsonEncoderTests.kt | 3 ++- ...SerializationJsonHttpMessageConverterTests.kt | 10 ++++++---- .../support/OriginHandshakeInterceptor.java | 16 ++++++++-------- 6 files changed, 24 insertions(+), 18 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java index 5c0bc7c998c..e161007abe9 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJProxyUtils.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. @@ -21,6 +21,7 @@ import java.util.List; import org.springframework.aop.Advisor; import org.springframework.aop.PointcutAdvisor; import org.springframework.aop.interceptor.ExposeInvocationInterceptor; +import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; /** @@ -74,7 +75,7 @@ public abstract class AspectJProxyUtils { ((PointcutAdvisor) advisor).getPointcut() instanceof AspectJExpressionPointcut)); } - static boolean isVariableName(String name) { + static boolean isVariableName(@Nullable String name) { if (!StringUtils.hasLength(name)) { return false; } @@ -88,4 +89,5 @@ public abstract class AspectJProxyUtils { } return true; } + } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java index 7fe280a770c..7149868217a 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/KotlinSerializationJsonHttpMessageConverter.java @@ -100,9 +100,9 @@ public class KotlinSerializationJsonHttpMessageConverter extends AbstractGeneric } @Override - public boolean canWrite(@Nullable Type type, @Nullable Class clazz, @Nullable MediaType mediaType) { + public boolean canWrite(@Nullable Type type, Class clazz, @Nullable MediaType mediaType) { try { - serializer(GenericTypeResolver.resolveType(type, clazz)); + serializer(type != null ? GenericTypeResolver.resolveType(type, clazz) : clazz); return canWrite(mediaType); } catch (Exception ex) { diff --git a/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonDecoderTests.kt b/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonDecoderTests.kt index 2dedc390e33..f1489814db1 100644 --- a/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonDecoderTests.kt +++ b/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonDecoderTests.kt @@ -97,7 +97,8 @@ class KotlinSerializationJsonDecoderTests : AbstractDecoderTests>(), null, MediaType.APPLICATION_JSON)).isTrue() - assertThat(converter.canWrite(typeTokenOf>(), null, MediaType.APPLICATION_JSON)).isTrue() - assertThat(converter.canWrite(typeTokenOf>(), null, MediaType.APPLICATION_JSON)).isTrue() - assertThat(converter.canWrite(typeTokenOf>(), null, MediaType.APPLICATION_PDF)).isFalse() + assertThat(converter.canWrite(typeTokenOf>(), List::class.java, MediaType.APPLICATION_JSON)).isTrue() + assertThat(converter.canWrite(typeTokenOf>(), List::class.java, MediaType.APPLICATION_JSON)).isTrue() + assertThat(converter.canWrite(typeTokenOf>(), List::class.java, MediaType.APPLICATION_JSON)).isTrue() + assertThat(converter.canWrite(typeTokenOf>(), List::class.java, MediaType.APPLICATION_PDF)).isFalse() } @Test @@ -297,6 +297,7 @@ class KotlinSerializationJsonHttpMessageConverterTests { assertThat(result).isEqualTo("\"H\u00e9llo W\u00f6rld\"") } + @Serializable @Suppress("ArrayInDataClass") data class SerializableBean( @@ -317,4 +318,5 @@ class KotlinSerializationJsonHttpMessageConverterTests { val superType = base::class.java.genericSuperclass!! return (superType as ParameterizedType).actualTypeArguments.first()!! } + } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java index 69bd7833d26..919e2dae831 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/OriginHandshakeInterceptor.java @@ -19,7 +19,7 @@ package org.springframework.web.socket.server.support; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -31,6 +31,7 @@ import org.springframework.http.server.ServerHttpRequest; import org.springframework.http.server.ServerHttpResponse; import org.springframework.lang.Nullable; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.server.HandshakeInterceptor; @@ -84,9 +85,9 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor { * @since 4.1.5 */ public Collection getAllowedOrigins() { - return (this.corsConfiguration.getAllowedOrigins() != null ? - Collections.unmodifiableSet(new HashSet<>(this.corsConfiguration.getAllowedOrigins())) : - Collections.emptyList()); + List allowedOrigins = this.corsConfiguration.getAllowedOrigins(); + return (CollectionUtils.isEmpty(allowedOrigins) ? Collections.emptySet() : + Collections.unmodifiableSet(new LinkedHashSet<>(allowedOrigins))); } /** @@ -104,14 +105,13 @@ public class OriginHandshakeInterceptor implements HandshakeInterceptor { /** * Return the allowed {@code Origin} pattern header values. - * * @since 5.3.2 * @see CorsConfiguration#getAllowedOriginPatterns() */ public Collection getAllowedOriginPatterns() { - return (this.corsConfiguration.getAllowedOriginPatterns() != null ? - Collections.unmodifiableSet(new HashSet<>(this.corsConfiguration.getAllowedOriginPatterns())) : - Collections.emptyList()); + List allowedOriginPatterns = this.corsConfiguration.getAllowedOriginPatterns(); + return (CollectionUtils.isEmpty(allowedOriginPatterns) ? Collections.emptySet() : + Collections.unmodifiableSet(new LinkedHashSet<>(allowedOriginPatterns))); }