Skip to content

Commit df977a2

Browse files
committed
Nullability refinements and related polishing
1 parent 99a1388 commit df977a2

File tree

9 files changed

+32
-16
lines changed

9 files changed

+32
-16
lines changed

spring-beans/src/main/java/org/springframework/beans/TypeMismatchException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,10 +41,10 @@ public class TypeMismatchException extends PropertyAccessException {
4141
private String propertyName;
4242

4343
@Nullable
44-
private transient Object value;
44+
private final transient Object value;
4545

4646
@Nullable
47-
private Class<?> requiredType;
47+
private final Class<?> requiredType;
4848

4949

5050
/**

spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public void afterPropertiesSet() {
294294
}
295295
List<InputStream> mappingStreams = null;
296296
if (this.mappingLocations != null) {
297-
mappingStreams = new ArrayList<>(mappingLocations.length);
297+
mappingStreams = new ArrayList<>(this.mappingLocations.length);
298298
for (Resource location : this.mappingLocations) {
299299
try {
300300
InputStream stream = location.getInputStream();

spring-context/src/test/java/org/springframework/context/annotation/EnableLoadTimeWeavingTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ public class EnableLoadTimeWeavingTests {
4040
@Test
4141
public void control() {
4242
GenericXmlApplicationContext ctx =
43-
new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
43+
new GenericXmlApplicationContext(getClass(), "EnableLoadTimeWeavingTests-context.xml");
4444
ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class);
4545
}
4646

@@ -73,9 +73,11 @@ public void enableLTW_withAjWeavingEnabled() {
7373
verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class));
7474
}
7575

76+
7677
@Configuration
7778
@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.DISABLED)
7879
static class EnableLTWConfig_withAjWeavingDisabled implements LoadTimeWeavingConfigurer {
80+
7981
@Override
8082
public LoadTimeWeaver getLoadTimeWeaver() {
8183
return mock(LoadTimeWeaver.class);
@@ -85,6 +87,7 @@ public LoadTimeWeaver getLoadTimeWeaver() {
8587
@Configuration
8688
@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.AUTODETECT)
8789
static class EnableLTWConfig_withAjWeavingAutodetect implements LoadTimeWeavingConfigurer {
90+
8891
@Override
8992
public LoadTimeWeaver getLoadTimeWeaver() {
9093
return mock(LoadTimeWeaver.class);
@@ -94,9 +97,11 @@ public LoadTimeWeaver getLoadTimeWeaver() {
9497
@Configuration
9598
@EnableLoadTimeWeaving(aspectjWeaving=AspectJWeaving.ENABLED)
9699
static class EnableLTWConfig_withAjWeavingEnabled implements LoadTimeWeavingConfigurer {
100+
97101
@Override
98102
public LoadTimeWeaver getLoadTimeWeaver() {
99103
return mock(LoadTimeWeaver.class);
100104
}
101105
}
106+
102107
}

spring-test/src/main/java/org/springframework/test/context/event/ApplicationEventsHolder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,7 +67,6 @@ public static ApplicationEvents getApplicationEvents() {
6767
* @throws IllegalStateException if an instance of {@code ApplicationEvents}
6868
* has not been registered for the current thread
6969
*/
70-
@Nullable
7170
public static ApplicationEvents getRequiredApplicationEvents() {
7271
ApplicationEvents events = applicationEvents.get();
7372
Assert.state(events != null, "Failed to retrieve ApplicationEvents for the current thread. " +

spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpResponse.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.http.HttpStatus;
3333
import org.springframework.http.ResponseCookie;
3434
import org.springframework.lang.Nullable;
35+
import org.springframework.util.Assert;
3536
import org.springframework.util.ClassUtils;
3637
import org.springframework.util.CollectionUtils;
3738
import org.springframework.util.LinkedMultiValueMap;
@@ -147,10 +148,12 @@ public static HttpHeaders getHttpHeaders(ReactiveResponse response) {
147148
HttpHeaders headers = new HttpHeaders();
148149
Iterable<?> iterator = (Iterable<?>)
149150
ReflectionUtils.invokeMethod(getHeadersMethod, response.getResponse());
151+
Assert.notNull(iterator, "Iterator must not be null");
150152
for (Object field : iterator) {
151-
headers.add(
152-
(String) ReflectionUtils.invokeMethod(getNameMethod, field),
153-
(String) ReflectionUtils.invokeMethod(getValueMethod, field));
153+
String name = (String) ReflectionUtils.invokeMethod(getNameMethod, field);
154+
Assert.notNull(name, "Header name must not be null");
155+
String value = (String) ReflectionUtils.invokeMethod(getValueMethod, field);
156+
headers.add(name, value);
154157
}
155158
return headers;
156159
}

spring-web/src/main/java/org/springframework/http/converter/HttpMessageConverter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Arjen Poutsma
3232
* @author Juergen Hoeller
33+
* @author Rossen Stoyanchev
3334
* @since 3.0
3435
* @param <T> the converted object type
3536
*/
@@ -67,8 +68,8 @@ public interface HttpMessageConverter<T> {
6768
/**
6869
* Return the list of media types supported by this converter for the given
6970
* class. The list may differ from {@link #getSupportedMediaTypes()} if the
70-
* converter doesn't support given Class or if it support it only for a
71-
* subset of media types.
71+
* converter does not support the given Class or if it supports it only for
72+
* a subset of media types.
7273
* @param clazz the type of class to check
7374
* @return the list of media types supported for the given class
7475
* @since 5.3.4

spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/upgrade/Jetty10RequestUpgradeStrategy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.reactive.socket.server.upgrade;
1718

1819
import java.lang.reflect.Method;
@@ -35,6 +36,7 @@
3536
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
3637
import org.springframework.lang.NonNull;
3738
import org.springframework.lang.Nullable;
39+
import org.springframework.util.Assert;
3840
import org.springframework.util.ReflectionUtils;
3941
import org.springframework.web.reactive.socket.HandshakeInfo;
4042
import org.springframework.web.reactive.socket.WebSocketHandler;
@@ -67,7 +69,9 @@ public class Jetty10RequestUpgradeStrategy implements RequestUpgradeStrategy {
6769

6870
Class<?> type = loader.loadClass("org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer");
6971
getContainerMethod = type.getMethod("getContainer", ServletContext.class);
70-
upgradeMethod = ReflectionUtils.findMethod(type, "upgrade", (Class<?>[]) null);
72+
Method upgrade = ReflectionUtils.findMethod(type, "upgrade", (Class<?>[]) null);
73+
Assert.state(upgrade != null, "Upgrade method not found");
74+
upgradeMethod = upgrade;
7175

7276
type = loader.loadClass("org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse");
7377
setAcceptedSubProtocol = type.getMethod("setAcceptedSubProtocol", String.class);

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/AbstractMessageConverterMethodArgumentResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, Me
203203
(noContentType && !message.hasBody())) {
204204
return null;
205205
}
206-
throw new HttpMediaTypeNotSupportedException(contentType, getSupportedMediaTypes(targetClass));
206+
throw new HttpMediaTypeNotSupportedException(contentType,
207+
getSupportedMediaTypes(targetClass != null ? targetClass : Object.class));
207208
}
208209

209210
MediaType selectedContentType = contentType;

spring-websocket/src/main/java/org/springframework/web/socket/server/jetty/Jetty10RequestUpgradeStrategy.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.socket.server.jetty;
1718

1819
import java.lang.reflect.Method;
@@ -72,7 +73,9 @@ public class Jetty10RequestUpgradeStrategy implements RequestUpgradeStrategy {
7273

7374
Class<?> type = loader.loadClass("org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer");
7475
getContainerMethod = type.getMethod("getContainer", ServletContext.class);
75-
upgradeMethod = ReflectionUtils.findMethod(type, "upgrade", (Class<?>[]) null);
76+
Method upgrade = ReflectionUtils.findMethod(type, "upgrade", (Class<?>[]) null);
77+
Assert.state(upgrade != null, "Upgrade method not found");
78+
upgradeMethod = upgrade;
7679

7780
type = loader.loadClass("org.eclipse.jetty.websocket.server.JettyServerUpgradeResponse");
7881
setAcceptedSubProtocol = type.getMethod("setAcceptedSubProtocol", String.class);

0 commit comments

Comments
 (0)