Make getters and setters null-safety consistent

This commit ensure that null-safety is consistent between
getters and setters in order to be able to provide beans
with properties with a common type when type safety is
taken in account like with Kotlin.

It also add a few missing property level @Nullable
annotations.

Issue: SPR-15792
This commit is contained in:
Sebastien Deleuze
2017-07-19 08:55:05 +02:00
parent ff85726fa9
commit fb4ddb0746
201 changed files with 579 additions and 489 deletions

View File

@@ -86,12 +86,15 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
private final CacheOperationExpressionEvaluator evaluator = new CacheOperationExpressionEvaluator();
@Nullable
private CacheOperationSource cacheOperationSource;
private KeyGenerator keyGenerator = new SimpleKeyGenerator();
@Nullable
private CacheResolver cacheResolver;
@Nullable
private BeanFactory beanFactory;
private boolean initialized = false;
@@ -150,8 +153,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
* @see #setCacheManager(org.springframework.cache.CacheManager)
* @see SimpleCacheResolver
*/
public void setCacheResolver(CacheResolver cacheResolver) {
Assert.notNull(cacheResolver, "CacheResolver must not be null");
public void setCacheResolver(@Nullable CacheResolver cacheResolver) {
this.cacheResolver = cacheResolver;
}

View File

@@ -90,7 +90,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
* <p>May also link to an externally defined Properties object, e.g. defined
* through a {@link org.springframework.beans.factory.config.PropertiesFactoryBean}.
*/
public void setCommonMessages(Properties commonMessages) {
public void setCommonMessages(@Nullable Properties commonMessages) {
this.commonMessages = commonMessages;
}

View File

@@ -119,7 +119,7 @@ public abstract class AbstractResourceBasedMessageSource extends AbstractMessage
* <p>Only applies to classic properties files, not to XML files.
* @param defaultEncoding the default charset
*/
public void setDefaultEncoding(String defaultEncoding) {
public void setDefaultEncoding(@Nullable String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}

View File

@@ -70,7 +70,7 @@ public class LocalStatelessSessionProxyFactoryBean extends LocalSlsbInvokerInter
* Using a business methods interface is a best practice when implementing EJBs.
* @param businessInterface set the business interface of the EJB
*/
public void setBusinessInterface(Class<?> businessInterface) {
public void setBusinessInterface(@Nullable Class<?> businessInterface) {
this.businessInterface = businessInterface;
}

View File

@@ -84,7 +84,7 @@ public class SimpleRemoteStatelessSessionProxyFactoryBean extends SimpleRemoteSl
* converted to Spring's generic RemoteAccessException.
* @param businessInterface the business interface of the EJB
*/
public void setBusinessInterface(Class<?> businessInterface) {
public void setBusinessInterface(@Nullable Class<?> businessInterface) {
this.businessInterface = businessInterface;
}

View File

@@ -49,7 +49,7 @@ public class JodaTimeContext {
/**
* Set the user's chronology (calendar system).
*/
public void setChronology(Chronology chronology) {
public void setChronology(@Nullable Chronology chronology) {
this.chronology = chronology;
}
@@ -69,7 +69,7 @@ public class JodaTimeContext {
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
* @see org.springframework.context.i18n.LocaleContextHolder#setLocaleContext
*/
public void setTimeZone(DateTimeZone timeZone) {
public void setTimeZone(@Nullable DateTimeZone timeZone) {
this.timeZone = timeZone;
}

View File

@@ -47,7 +47,7 @@ public class DateTimeContext {
/**
* Set the user's chronology (calendar system).
*/
public void setChronology(Chronology chronology) {
public void setChronology(@Nullable Chronology chronology) {
this.chronology = chronology;
}
@@ -67,7 +67,7 @@ public class DateTimeContext {
* @see org.springframework.context.i18n.LocaleContextHolder#getTimeZone()
* @see org.springframework.context.i18n.LocaleContextHolder#setLocaleContext
*/
public void setTimeZone(ZoneId timeZone) {
public void setTimeZone(@Nullable ZoneId timeZone) {
this.timeZone = timeZone;
}

View File

@@ -158,7 +158,7 @@ public class MBeanClientInterceptor
* Specify the environment for the JMX connector.
* @see javax.management.remote.JMXConnectorFactory#connect(javax.management.remote.JMXServiceURL, java.util.Map)
*/
public void setEnvironment(Map<String, ?> environment) {
public void setEnvironment(@Nullable Map<String, ?> environment) {
this.environment = environment;
}
@@ -229,7 +229,7 @@ public class MBeanClientInterceptor
* setters and getters for MBean attributes and conventional Java methods
* for MBean operations.
*/
public void setManagementInterface(Class<?> managementInterface) {
public void setManagementInterface(@Nullable Class<?> managementInterface) {
this.managementInterface = managementInterface;
}

View File

@@ -82,7 +82,7 @@ public class NotificationListenerRegistrar extends NotificationListenerHolder
* Specify the environment for the JMX connector.
* @see javax.management.remote.JMXConnectorFactory#connect(javax.management.remote.JMXServiceURL, java.util.Map)
*/
public void setEnvironment(Map<String, ?> environment) {
public void setEnvironment(@Nullable Map<String, ?> environment) {
this.environment = environment;
}

View File

@@ -172,6 +172,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
/**
* Default value for the JMX field "currencyTimeLimit".
*/
@Nullable
private Integer defaultCurrencyTimeLimit;
/**
@@ -181,6 +182,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
private boolean exposeClassDescriptor = false;
@Nullable
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
@@ -204,7 +206,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
* @see org.springframework.jmx.export.metadata.AbstractJmxAttribute#setCurrencyTimeLimit
* @see #applyCurrencyTimeLimit(javax.management.Descriptor, int)
*/
public void setDefaultCurrencyTimeLimit(Integer defaultCurrencyTimeLimit) {
public void setDefaultCurrencyTimeLimit(@Nullable Integer defaultCurrencyTimeLimit) {
this.defaultCurrencyTimeLimit = defaultCurrencyTimeLimit;
}
@@ -266,7 +268,7 @@ public abstract class AbstractReflectiveMBeanInfoAssembler extends AbstractMBean
* names if needed (e.g. for parameter names of MBean operation methods).
* <p>Default is a {@link DefaultParameterNameDiscoverer}.
*/
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) {
public void setParameterNameDiscoverer(@Nullable ParameterNameDiscoverer parameterNameDiscoverer) {
this.parameterNameDiscoverer = parameterNameDiscoverer;
}

View File

@@ -27,10 +27,13 @@ import org.springframework.util.StringUtils;
*/
public class ManagedNotification {
@Nullable
private String[] notificationTypes;
@Nullable
private String name;
@Nullable
private String description;
@@ -45,7 +48,7 @@ public class ManagedNotification {
/**
* Set a list of notification types.
*/
public void setNotificationTypes(String... notificationTypes) {
public void setNotificationTypes(@Nullable String... notificationTypes) {
this.notificationTypes = notificationTypes;
}
@@ -60,13 +63,14 @@ public class ManagedNotification {
/**
* Set the name of this notification.
*/
public void setName(String name) {
public void setName(@Nullable String name) {
this.name = name;
}
/**
* Return the name of this notification.
*/
@Nullable
public String getName() {
return this.name;
}
@@ -74,7 +78,7 @@ public class ManagedNotification {
/**
* Set a description for this notification.
*/
public void setDescription(String description) {
public void setDescription(@Nullable String description) {
this.description = description;
}

View File

@@ -96,7 +96,7 @@ public class MBeanRegistrationSupport {
* be registered. The {@code MBeanExporter} will attempt to locate an
* existing {@code MBeanServer} if none is supplied.
*/
public void setServer(MBeanServer server) {
public void setServer(@Nullable MBeanServer server) {
this.server = server;
}

View File

@@ -62,7 +62,7 @@ public abstract class JndiObjectLocator extends JndiLocatorSupport implements In
* @param jndiName the JNDI name to look up
* @see #setResourceRef
*/
public void setJndiName(String jndiName) {
public void setJndiName(@Nullable String jndiName) {
this.jndiName = jndiName;
}

View File

@@ -181,7 +181,7 @@ public class RemoteInvocation implements Serializable {
* @see #addAttribute
* @see #getAttribute
*/
public void setAttributes(Map<String, Serializable> attributes) {
public void setAttributes(@Nullable Map<String, Serializable> attributes) {
this.attributes = attributes;
}

View File

@@ -41,8 +41,10 @@ public class RemoteInvocationResult implements Serializable {
private static final long serialVersionUID = 2138555143707773549L;
@Nullable
private Object value;
@Nullable
private Throwable exception;
@@ -81,7 +83,7 @@ public class RemoteInvocationResult implements Serializable {
* Use {@link #RemoteInvocationResult(Object)} otherwise.
* @see #RemoteInvocationResult()
*/
public void setValue(Object value) {
public void setValue(@Nullable Object value) {
this.value = value;
}
@@ -102,7 +104,7 @@ public class RemoteInvocationResult implements Serializable {
* Use {@link #RemoteInvocationResult(Throwable)} otherwise.
* @see #RemoteInvocationResult()
*/
public void setException(Throwable exception) {
public void setException(@Nullable Throwable exception) {
this.exception = exception;
}

View File

@@ -92,9 +92,11 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
* {@link java.util.concurrent.ScheduledExecutorService} to be wrapped as a
* {@code TaskScheduler}.
*/
public void setScheduler(Object scheduler) {
Assert.notNull(scheduler, "Scheduler object must not be null");
if (scheduler instanceof TaskScheduler) {
public void setScheduler(@Nullable Object scheduler) {
if (scheduler == null) {
this.taskScheduler = null;
}
else if (scheduler instanceof TaskScheduler) {
this.taskScheduler = (TaskScheduler) scheduler;
}
else if (scheduler instanceof ScheduledExecutorService) {

View File

@@ -424,7 +424,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* @see #setDisallowedFields
* @see #isAllowed(String)
*/
public void setAllowedFields(String... allowedFields) {
public void setAllowedFields(@Nullable String... allowedFields) {
this.allowedFields = PropertyAccessorUtils.canonicalPropertyNames(allowedFields);
}
@@ -448,7 +448,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* @see #setAllowedFields
* @see #isAllowed(String)
*/
public void setDisallowedFields(String... disallowedFields) {
public void setDisallowedFields(@Nullable String... disallowedFields) {
this.disallowedFields = PropertyAccessorUtils.canonicalPropertyNames(disallowedFields);
}
@@ -471,7 +471,7 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* @see #setBindingErrorProcessor
* @see DefaultBindingErrorProcessor#MISSING_FIELD_ERROR_CODE
*/
public void setRequiredFields(String... requiredFields) {
public void setRequiredFields(@Nullable String... requiredFields) {
this.requiredFields = PropertyAccessorUtils.canonicalPropertyNames(requiredFields);
if (logger.isDebugEnabled()) {
logger.debug("DataBinder requires binding of required fields [" +
@@ -526,14 +526,18 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter {
* @see #addValidators(Validator...)
* @see #replaceValidators(Validator...)
*/
public void setValidator(Validator validator) {
public void setValidator(@Nullable Validator validator) {
assertValidators(validator);
this.validators.clear();
this.validators.add(validator);
if (validator != null) {
this.validators.add(validator);
}
}
private void assertValidators(Validator... validators) {
Assert.notNull(validators, "Validators required");
if (validators == null) {
return;
}
for (Validator validator : validators) {
if (validator != null && (getTarget() != null && !validator.supports(getTarget().getClass()))) {
throw new IllegalStateException("Invalid target for Validator [" + validator + "]: " + getTarget());