Nullability fine-tuning around declaration inconsistencies

Issue: SPR-15720
Issue: SPR-15792
This commit is contained in:
Juergen Hoeller
2017-07-19 22:22:14 +02:00
parent 68e6b148cb
commit 46eba3dbfa
186 changed files with 986 additions and 619 deletions

View File

@@ -578,6 +578,7 @@ public abstract class AbstractNestablePropertyAccessor extends AbstractPropertyA
@Nullable Object newValue, @Nullable Class<?> requiredType, @Nullable TypeDescriptor td)
throws TypeMismatchException {
Assert.state(this.typeConverterDelegate != null, "No TypeConverterDelegate");
try {
return this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, newValue, requiredType, td);
}

View File

@@ -33,8 +33,10 @@ public class BeanInstantiationException extends FatalBeanException {
private Class<?> beanClass;
@Nullable
private Constructor<?> constructor;
@Nullable
private Method constructingMethod;

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import org.springframework.core.ResolvableType;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;
/**
@@ -134,7 +135,7 @@ public class DirectFieldAccessor extends AbstractNestablePropertyAccessor {
}
@Override
public void setValue(Object value) throws Exception {
public void setValue(@Nullable Object value) throws Exception {
try {
ReflectionUtils.makeAccessible(this.field);
this.field.set(getWrappedInstance(), value);

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.
@@ -22,6 +22,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Base implementation of the {@link TypeConverter} interface, using a package-private delegate.
@@ -33,6 +34,7 @@ import org.springframework.lang.Nullable;
*/
public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport implements TypeConverter {
@Nullable
TypeConverterDelegate typeConverterDelegate;
@@ -59,6 +61,7 @@ public abstract class TypeConverterSupport extends PropertyEditorRegistrySupport
private <T> T doConvert(@Nullable Object value,@Nullable Class<T> requiredType,
@Nullable MethodParameter methodParam, @Nullable Field field) throws TypeMismatchException {
Assert.state(this.typeConverterDelegate != null, "No TypeConverterDelegate");
try {
if (field != null) {
return this.typeConverterDelegate.convertIfNecessary(value, requiredType, field);

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.
@@ -35,6 +35,7 @@ public class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionExcepti
private int numberOfBeansFound;
@Nullable
private Collection<String> beanNamesFound;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 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.
@@ -20,6 +20,7 @@ import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.StandardAnnotationMetadata;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -44,6 +45,7 @@ public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implem
private final AnnotationMetadata metadata;
@Nullable
private MethodMetadata factoryMethodMetadata;
@@ -98,6 +100,7 @@ public class AnnotatedGenericBeanDefinition extends GenericBeanDefinition implem
}
@Override
@Nullable
public final MethodMetadata getFactoryMethodMetadata() {
return this.factoryMethodMetadata;
}

View File

@@ -89,6 +89,7 @@ public class RequiredAnnotationBeanPostProcessor extends InstantiationAwareBeanP
private int order = Ordered.LOWEST_PRECEDENCE - 1;
@Nullable
private ConfigurableListableBeanFactory beanFactory;
/**

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.
@@ -22,6 +22,7 @@ import java.util.Properties;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.support.PropertiesLoaderSupport;
import org.springframework.lang.Nullable;
/**
* Allows for making a properties file from a classpath location available
@@ -47,6 +48,7 @@ public class PropertiesFactoryBean extends PropertiesLoaderSupport
private boolean singleton = true;
@Nullable
private Properties singletonInstance;

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.
@@ -47,8 +47,7 @@ public class BeanComponentDefinition extends BeanDefinitionHolder implements Com
* @param beanName the name of the bean
*/
public BeanComponentDefinition(BeanDefinition beanDefinition, String beanName) {
super(beanDefinition, beanName);
findInnerBeanDefinitionsAndBeanReferences(beanDefinition);
this(new BeanDefinitionHolder(beanDefinition, beanName));
}
/**
@@ -58,27 +57,21 @@ public class BeanComponentDefinition extends BeanDefinitionHolder implements Com
* @param aliases alias names for the bean, or {@code null} if none
*/
public BeanComponentDefinition(BeanDefinition beanDefinition, String beanName, @Nullable String[] aliases) {
super(beanDefinition, beanName, aliases);
findInnerBeanDefinitionsAndBeanReferences(beanDefinition);
this(new BeanDefinitionHolder(beanDefinition, beanName, aliases));
}
/**
* Create a new BeanComponentDefinition for the given bean.
* @param holder the BeanDefinitionHolder encapsulating the
* bean definition as well as the name of the bean
* @param beanDefinitionHolder the BeanDefinitionHolder encapsulating
* the bean definition as well as the name of the bean
*/
public BeanComponentDefinition(BeanDefinitionHolder holder) {
super(holder);
findInnerBeanDefinitionsAndBeanReferences(holder.getBeanDefinition());
}
public BeanComponentDefinition(BeanDefinitionHolder beanDefinitionHolder) {
super(beanDefinitionHolder);
private void findInnerBeanDefinitionsAndBeanReferences(BeanDefinition beanDefinition) {
List<BeanDefinition> innerBeans = new ArrayList<>();
List<BeanReference> references = new ArrayList<>();
PropertyValues propertyValues = beanDefinition.getPropertyValues();
for (int i = 0; i < propertyValues.getPropertyValues().length; i++) {
PropertyValue propertyValue = propertyValues.getPropertyValues()[i];
PropertyValues propertyValues = beanDefinitionHolder.getBeanDefinition().getPropertyValues();
for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
Object value = propertyValue.getValue();
if (value instanceof BeanDefinitionHolder) {
innerBeans.add(((BeanDefinitionHolder) value).getBeanDefinition());

View File

@@ -40,9 +40,7 @@ public class BeanDefinitionBuilder {
* Create a new {@code BeanDefinitionBuilder} used to construct a {@link GenericBeanDefinition}.
*/
public static BeanDefinitionBuilder genericBeanDefinition() {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new GenericBeanDefinition();
return builder;
return new BeanDefinitionBuilder(new GenericBeanDefinition());
}
/**
@@ -50,8 +48,7 @@ public class BeanDefinitionBuilder {
* @param beanClassName the class name for the bean that the definition is being created for
*/
public static BeanDefinitionBuilder genericBeanDefinition(String beanClassName) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new GenericBeanDefinition();
BeanDefinitionBuilder builder = new BeanDefinitionBuilder(new GenericBeanDefinition());
builder.beanDefinition.setBeanClassName(beanClassName);
return builder;
}
@@ -61,8 +58,7 @@ public class BeanDefinitionBuilder {
* @param beanClass the {@code Class} of the bean that the definition is being created for
*/
public static BeanDefinitionBuilder genericBeanDefinition(Class<?> beanClass) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new GenericBeanDefinition();
BeanDefinitionBuilder builder = new BeanDefinitionBuilder(new GenericBeanDefinition());
builder.beanDefinition.setBeanClass(beanClass);
return builder;
}
@@ -76,8 +72,7 @@ public class BeanDefinitionBuilder {
public static <T> BeanDefinitionBuilder genericBeanDefinition(
@Nullable Class<T> beanClass, Supplier<T> instanceSupplier) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new GenericBeanDefinition();
BeanDefinitionBuilder builder = new BeanDefinitionBuilder(new GenericBeanDefinition());
builder.beanDefinition.setBeanClass(beanClass);
builder.beanDefinition.setInstanceSupplier(instanceSupplier);
return builder;
@@ -97,8 +92,7 @@ public class BeanDefinitionBuilder {
* @param factoryMethodName the name of the method to use to construct the bean instance
*/
public static BeanDefinitionBuilder rootBeanDefinition(String beanClassName, @Nullable String factoryMethodName) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new RootBeanDefinition();
BeanDefinitionBuilder builder = new BeanDefinitionBuilder(new RootBeanDefinition());
builder.beanDefinition.setBeanClassName(beanClassName);
builder.beanDefinition.setFactoryMethodName(factoryMethodName);
return builder;
@@ -118,8 +112,7 @@ public class BeanDefinitionBuilder {
* @param factoryMethodName the name of the method to use to construct the bean instance
*/
public static BeanDefinitionBuilder rootBeanDefinition(Class<?> beanClass, @Nullable String factoryMethodName) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new RootBeanDefinition();
BeanDefinitionBuilder builder = new BeanDefinitionBuilder(new RootBeanDefinition());
builder.beanDefinition.setBeanClass(beanClass);
builder.beanDefinition.setFactoryMethodName(factoryMethodName);
return builder;
@@ -130,16 +123,14 @@ public class BeanDefinitionBuilder {
* @param parentName the name of the parent bean
*/
public static BeanDefinitionBuilder childBeanDefinition(String parentName) {
BeanDefinitionBuilder builder = new BeanDefinitionBuilder();
builder.beanDefinition = new ChildBeanDefinition(parentName);
return builder;
return new BeanDefinitionBuilder(new ChildBeanDefinition(parentName));
}
/**
* The {@code BeanDefinition} instance we are creating.
*/
private AbstractBeanDefinition beanDefinition;
private final AbstractBeanDefinition beanDefinition;
/**
* Our current position with respect to constructor args.
@@ -150,7 +141,8 @@ public class BeanDefinitionBuilder {
/**
* Enforce the use of factory methods.
*/
private BeanDefinitionBuilder() {
private BeanDefinitionBuilder(AbstractBeanDefinition beanDefinition) {
this.beanDefinition = beanDefinition;
}
/**

View File

@@ -35,6 +35,7 @@ import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
@@ -76,8 +77,10 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
protected final Log logger = LogFactory.getLog(getClass());
@Nullable
private XmlReaderContext readerContext;
@Nullable
private BeanDefinitionParserDelegate delegate;
@@ -99,6 +102,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
* Return the descriptor for the XML resource that this parser works on.
*/
protected final XmlReaderContext getReaderContext() {
Assert.state(this.readerContext != null, "No XmlReaderContext available");
return this.readerContext;
}
@@ -148,7 +152,7 @@ public class DefaultBeanDefinitionDocumentReader implements BeanDefinitionDocume
}
protected BeanDefinitionParserDelegate createDelegate(
XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
XmlReaderContext readerContext, Element root, @Nullable BeanDefinitionParserDelegate parentDelegate) {
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
delegate.initDefaults(root, parentDelegate);