Consistent use of @Nullable across the codebase (even for internals)

Beyond just formally declaring the current behavior, this revision actually enforces non-null behavior in selected signatures now, not tolerating null values anymore when not explicitly documented. It also changes some utility methods with historic null-in/null-out tolerance towards enforced non-null return values, making them a proper citizen in non-null assignments.

Some issues are left as to-do: in particular a thorough revision of spring-test, and a few tests with unclear failures (ignored as "TODO: NULLABLE") to be sorted out in a follow-up commit.

Issue: SPR-15540
This commit is contained in:
Juergen Hoeller
2017-06-07 14:17:48 +02:00
parent ffc3f6d87d
commit f813712f5b
1493 changed files with 10670 additions and 9172 deletions

View File

@@ -333,7 +333,7 @@ public class CacheReproTests {
@Override
@Nullable
protected Collection<String> getCacheNames(@Nullable CacheOperationInvocationContext<?> context) {
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
String cacheName = (String) context.getArgs()[0];
if (cacheName != null) {
return Collections.singleton(cacheName);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010-2013 the original author or authors.
* Copyright 2010-2017 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.
@@ -50,7 +50,7 @@ public class NoOpCacheManagerTests {
cache.put(key, new Object());
assertNull(cache.get(key));
assertNull(cache.get(key, Object.class));
assertNull(cache.getNativeCache());
assertSame(cache, cache.getNativeCache());
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -263,7 +263,7 @@ public class CacheResolverCustomizationTests {
@Override
@Nullable
protected Collection<String> getCacheNames(@Nullable CacheOperationInvocationContext<?> context) {
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
String cacheName = (String) context.getArgs()[1];
return Collections.singleton(cacheName);
}
@@ -278,7 +278,7 @@ public class CacheResolverCustomizationTests {
@Override
@Nullable
protected Collection<String> getCacheNames(@Nullable CacheOperationInvocationContext<?> context) {
protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) {
return null;
}
}

View File

@@ -292,7 +292,7 @@ public class ComponentScanAnnotationIntegrationTests {
}
@Override
public void setBeanClassLoader(@Nullable ClassLoader classLoader) {
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

View File

@@ -81,7 +81,7 @@ public class ImportBeanDefinitionRegistrarTests {
static Environment environment;
@Override
public void setBeanClassLoader(@Nullable ClassLoader classLoader) {
public void setBeanClassLoader(ClassLoader classLoader) {
SampleRegistrar.classLoader = classLoader;
}

View File

@@ -113,7 +113,7 @@ public class ImportSelectorTests {
static Environment environment;
@Override
public void setBeanClassLoader(@Nullable ClassLoader classLoader) {
public void setBeanClassLoader(ClassLoader classLoader) {
SampleRegistrar.classLoader = classLoader;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2017 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.
@@ -182,25 +182,6 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
verify(ejb, times(2)).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithHomeInterface() throws Exception {
Object retVal = new Object();
final RemoteInterface ejb = mock(RemoteInterface.class);
given(ejb.targetMethod()).willReturn(retVal);
final String jndiName= "foobar";
Context mockContext = mockContext(jndiName, ejb);
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
si.setHomeInterface(SlsbHome.class);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertTrue(target.targetMethod() == retVal);
verify(mockContext).close();
verify(ejb).remove();
}
@Test
public void testInvokesMethodOnEjbInstanceWithRemoteException() throws Exception {
final RemoteInterface ejb = mock(RemoteInterface.class);

View File

@@ -52,7 +52,7 @@ public class NumberFormattingTests {
DefaultConversionService.addDefaultConverters(conversionService);
conversionService.setEmbeddedValueResolver(new StringValueResolver() {
@Override
public String resolveStringValue(@Nullable String strVal) {
public String resolveStringValue(String strVal) {
if ("${pattern}".equals(strVal)) {
return "#,##.00";
}

View File

@@ -495,13 +495,6 @@ public class MBeanExporterTests extends AbstractMBeanServerTests {
exporter.setAutodetectMode(5);
}
@Test
public void testSetAutodetectModeNameToNull() {
MBeanExporter exporter = new MBeanExporter();
thrown.expect(IllegalArgumentException.class);
exporter.setAutodetectModeName(null);
}
@Test
public void testSetAutodetectModeNameToAnEmptyString() {
MBeanExporter exporter = new MBeanExporter();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -431,7 +431,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
registrar.setServer(server);
registrar.setNotificationListener(listener);
//registrar.setMappedObjectNames(new Object[] {objectName, objectName2});
registrar.setMappedObjectNames(new String[] { "spring:name=Test", "spring:name=Test2" });
registrar.setMappedObjectNames("spring:name=Test", "spring:name=Test2");
registrar.afterPropertiesSet();
// update the attribute

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 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,8 +21,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
/**
* Simple {@link ConfigurableEnvironment} implementation exposing
* {@link #setProperty(String, String)} and {@link #withProperty(String, String)}
* methods for testing purposes.
* {@link #setProperty} and {@link #withProperty} methods for testing purposes.
*
* @author Chris Beams
* @author Sam Brannen
@@ -31,30 +30,32 @@ import org.springframework.core.env.ConfigurableEnvironment;
*/
public class MockEnvironment extends AbstractEnvironment {
private MockPropertySource propertySource = new MockPropertySource();
private final MockPropertySource propertySource = new MockPropertySource();
/**
* Create a new {@code MockEnvironment} with a single {@link MockPropertySource}.
*/
public MockEnvironment() {
getPropertySources().addLast(propertySource);
getPropertySources().addLast(this.propertySource);
}
/**
* Set a property on the underlying {@link MockPropertySource} for this environment.
*/
public void setProperty(String key, String value) {
propertySource.setProperty(key, value);
this.propertySource.setProperty(key, value);
}
/**
* Convenient synonym for {@link #setProperty} that returns the current instance.
* Useful for method chaining and fluent-style use.
* @return this {@link MockEnvironment} instance
* @see MockPropertySource#withProperty(String, String)
* @see MockPropertySource#withProperty
*/
public MockEnvironment withProperty(String key, String value) {
this.setProperty(key, value);
setProperty(key, value);
return this;
}