Improve null-safety to fix some Spring Boot warnings

Issue: SPR-15540
This commit is contained in:
Sebastien Deleuze
2017-05-31 16:55:57 +02:00
parent b47d713e14
commit c3e6afb879
19 changed files with 59 additions and 46 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
/**
* Exception thrown when a {@code BeanFactory} is asked for a bean instance for which it
@@ -100,6 +101,7 @@ public class NoSuchBeanDefinitionException extends BeansException {
/**
* Return the name of the missing bean, if it was a lookup <em>by name</em> that failed.
*/
@Nullable
public String getBeanName() {
return this.beanName;
}
@@ -108,6 +110,7 @@ public class NoSuchBeanDefinitionException extends BeansException {
* Return the required type of the missing bean, if it was a lookup <em>by type</em>
* that failed.
*/
@Nullable
public Class<?> getBeanType() {
return (this.resolvableType != null ? this.resolvableType.resolve() : null);
}
@@ -117,6 +120,7 @@ public class NoSuchBeanDefinitionException extends BeansException {
* <em>by type</em> that failed.
* @since 4.3.4
*/
@Nullable
public ResolvableType getResolvableType() {
return this.resolvableType;
}

View File

@@ -17,6 +17,7 @@
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
@@ -42,7 +43,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* @param msg the detail message
*/
public UnsatisfiedDependencyException(
String resourceDescription, String beanName, String propertyName, String msg) {
@Nullable String resourceDescription, @Nullable String beanName, String propertyName, String msg) {
super(resourceDescription, beanName,
"Unsatisfied dependency expressed through bean property '" + propertyName + "'" +
@@ -57,7 +58,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* @param ex the bean creation exception that indicated the unsatisfied dependency
*/
public UnsatisfiedDependencyException(
String resourceDescription, String beanName, String propertyName, BeansException ex) {
@Nullable String resourceDescription, @Nullable String beanName, String propertyName, BeansException ex) {
this(resourceDescription, beanName, propertyName, "");
initCause(ex);
@@ -72,7 +73,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* @since 4.3
*/
public UnsatisfiedDependencyException(
String resourceDescription, String beanName, InjectionPoint injectionPoint, String msg) {
@Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, String msg) {
super(resourceDescription, beanName,
"Unsatisfied dependency expressed through " + injectionPoint +
@@ -89,7 +90,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* @since 4.3
*/
public UnsatisfiedDependencyException(
String resourceDescription, String beanName, InjectionPoint injectionPoint, BeansException ex) {
@Nullable String resourceDescription, @Nullable String beanName, @Nullable InjectionPoint injectionPoint, BeansException ex) {
this(resourceDescription, beanName, injectionPoint, "");
initCause(ex);
@@ -100,6 +101,7 @@ public class UnsatisfiedDependencyException extends BeanCreationException {
* Return the injection point (field or method/constructor parameter), if known.
* @since 4.3
*/
@Nullable
public InjectionPoint getInjectionPoint() {
return this.injectionPoint;
}

View File

@@ -21,6 +21,7 @@ import java.util.Iterator;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.lang.Nullable;
/**
* Configuration interface to be implemented by most listable bean factories.
@@ -105,7 +106,7 @@ public interface ConfigurableListableBeanFactory
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* defined in this factory
*/
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(@Nullable String beanName) throws NoSuchBeanDefinitionException;
/**
* Return a unified view over all bean names managed by this factory.

View File

@@ -1738,7 +1738,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
* @see ChildBeanDefinition
* @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory#getBeanDefinition
*/
protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;
protected abstract BeanDefinition getBeanDefinition(@Nullable String beanName) throws BeansException;
/**
* Create a bean instance for the given merged bean definition (and arguments).

View File

@@ -20,6 +20,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.AliasRegistry;
import org.springframework.lang.Nullable;
/**
* Interface for registries that hold bean definitions, for example RootBeanDefinition
@@ -74,7 +75,7 @@ public interface BeanDefinitionRegistry extends AliasRegistry {
* @return the BeanDefinition for the given name (never {@code null})
* @throws NoSuchBeanDefinitionException if there is no such bean definition
*/
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(@Nullable String beanName) throws NoSuchBeanDefinitionException;
/**
* Check if this registry contains a bean definition with the given name.

View File

@@ -195,7 +195,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
* Specify an id for serialization purposes, allowing this BeanFactory to be
* deserialized from this id back into the BeanFactory object, if needed.
*/
public void setSerializationId(String serializationId) {
public void setSerializationId(@Nullable String serializationId) {
if (serializationId != null) {
serializableFactories.put(serializationId, new WeakReference<>(this));
}
@@ -671,7 +671,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
public BeanDefinition getBeanDefinition(@Nullable String beanName) throws NoSuchBeanDefinitionException {
BeanDefinition bd = this.beanDefinitionMap.get(beanName);
if (bd == null) {
if (this.logger.isTraceEnabled()) {

View File

@@ -117,7 +117,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @param beanClass the class of the bean to instantiate
* @see #setBeanClass
*/
public RootBeanDefinition(Class<?> beanClass) {
public RootBeanDefinition(@Nullable Class<?> beanClass) {
super();
setBeanClass(beanClass);
}
@@ -131,7 +131,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @since 5.0
* @see #setInstanceSupplier
*/
public <T> RootBeanDefinition(Class<T> beanClass, Supplier<T> instanceSupplier) {
public <T> RootBeanDefinition(@Nullable Class<T> beanClass, @Nullable Supplier<T> instanceSupplier) {
super();
setBeanClass(beanClass);
setInstanceSupplier(instanceSupplier);
@@ -147,7 +147,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @since 5.0
* @see #setInstanceSupplier
*/
public <T> RootBeanDefinition(Class<T> beanClass, String scope, Supplier<T> instanceSupplier) {
public <T> RootBeanDefinition(@Nullable Class<T> beanClass, String scope, @Nullable Supplier<T> instanceSupplier) {
super();
setBeanClass(beanClass);
setScope(scope);
@@ -162,7 +162,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @param dependencyCheck whether to perform a dependency check for objects
* (not applicable to autowiring a constructor, thus ignored there)
*/
public RootBeanDefinition(Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
public RootBeanDefinition(@Nullable Class<?> beanClass, int autowireMode, boolean dependencyCheck) {
super();
setBeanClass(beanClass);
setAutowireMode(autowireMode);
@@ -178,7 +178,7 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
* @param cargs the constructor argument values to apply
* @param pvs the property values to apply
*/
public RootBeanDefinition(Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
public RootBeanDefinition(@Nullable Class<?> beanClass, ConstructorArgumentValues cargs, MutablePropertyValues pvs) {
super(cargs, pvs);
setBeanClass(beanClass);
}

View File

@@ -23,6 +23,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.core.SimpleAliasRegistry;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -57,7 +58,7 @@ public class SimpleBeanDefinitionRegistry extends SimpleAliasRegistry implements
}
@Override
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
public BeanDefinition getBeanDefinition(@Nullable String beanName) throws NoSuchBeanDefinitionException {
BeanDefinition bd = this.beanDefinitionMap.get(beanName);
if (bd == null) {
throw new NoSuchBeanDefinitionException(beanName);