Avoid defining nullability for non-relevant API

Defining nullability of some API like EnvironmentCapable
or ConditionContext causes issues in Spring Boot because
in the context where they are used, it is known for sure
they will return non-null values even if their API can in
other context return null values.

It is better in this case for both Java and Kotlin to
not define at all the nullabity of such API.

In practice, this is achieved by removing the package level
@NonNullApi annotation and adding it only on the
relevant classes.

Issue: SPR-15540
This commit is contained in:
Sebastien Deleuze
2017-05-31 22:45:31 +02:00
parent 1f28825f9d
commit b494c53b40
71 changed files with 146 additions and 16 deletions

View File

@@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.SpringProperties;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -51,6 +52,7 @@ import org.springframework.util.StringUtils;
* @see ConfigurableEnvironment
* @see StandardEnvironment
*/
@NonNullApi
public abstract class AbstractEnvironment implements ConfigurableEnvironment {
/**

View File

@@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -38,6 +39,7 @@ import org.springframework.util.SystemPropertyUtils;
* @author Juergen Hoeller
* @since 3.1
*/
@NonNullApi
public abstract class AbstractPropertyResolver implements ConfigurablePropertyResolver {
protected final Log logger = LogFactory.getLog(getClass());

View File

@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
/**
@@ -33,6 +34,7 @@ import org.springframework.lang.Nullable;
* @since 3.1
* @see SimpleCommandLineArgsParser
*/
@NonNullApi
class CommandLineArgs {
private final Map<String, List<String>> optionArgs = new HashMap<>();

View File

@@ -19,6 +19,7 @@ package org.springframework.core.env;
import java.util.Collection;
import java.util.List;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
@@ -204,6 +205,7 @@ import org.springframework.util.StringUtils;
* @see SimpleCommandLinePropertySource
* @see JOptCommandLinePropertySource
*/
@NonNullApi
public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T> {
/** The default name given to {@link CommandLinePropertySource} instances: {@value} */

View File

@@ -23,6 +23,7 @@ import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.springframework.lang.NonNullApi;
import org.springframework.util.StringUtils;
/**
@@ -39,6 +40,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @since 3.1.1
*/
@NonNullApi
public class CompositePropertySource extends EnumerablePropertySource<Object> {
private final Set<PropertySource<?>> propertySources = new LinkedHashSet<>();

View File

@@ -18,6 +18,8 @@ package org.springframework.core.env;
import java.util.Map;
import org.springframework.lang.NonNullApi;
/**
* Configuration interface to be implemented by most if not all {@link Environment} types.
* Provides facilities for setting active and default profiles and manipulating underlying
@@ -69,6 +71,7 @@ import java.util.Map;
* @see StandardEnvironment
* @see org.springframework.context.ConfigurableApplicationContext#getEnvironment
*/
@NonNullApi
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver {
/**

View File

@@ -17,6 +17,7 @@
package org.springframework.core.env;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
/**
@@ -28,6 +29,7 @@ import org.springframework.lang.Nullable;
* @author Chris Beams
* @since 3.1
*/
@NonNullApi
public interface ConfigurablePropertyResolver extends PropertyResolver {
/**

View File

@@ -16,6 +16,7 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
import org.springframework.util.ObjectUtils;
/**
@@ -41,6 +42,7 @@ import org.springframework.util.ObjectUtils;
* @author Juergen Hoeller
* @since 3.1
*/
@NonNullApi
public abstract class EnumerablePropertySource<T> extends PropertySource<T> {
public EnumerablePropertySource(String name, T source) {

View File

@@ -16,6 +16,8 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
/**
* Interface representing the environment in which the current application is running.
* Models two key aspects of the application environment: <em>profiles</em> and
@@ -68,6 +70,7 @@ package org.springframework.core.env;
* @see org.springframework.context.ConfigurableApplicationContext#setEnvironment
* @see org.springframework.context.support.AbstractApplicationContext#createEnvironment
*/
@NonNullApi
public interface Environment extends PropertyResolver {
/**

View File

@@ -16,8 +16,6 @@
package org.springframework.core.env;
import org.springframework.lang.Nullable;
/**
* Interface indicating a component that contains and exposes an {@link Environment} reference.
*
@@ -46,7 +44,6 @@ public interface EnvironmentCapable {
* Return the {@link Environment} associated with this component
* (may be {@code null} or a default environment).
*/
@Nullable
Environment getEnvironment();
}

View File

@@ -23,6 +23,8 @@ import java.util.List;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.springframework.lang.NonNullApi;
/**
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
*
@@ -54,6 +56,7 @@ import joptsimple.OptionSpec;
* @see joptsimple.OptionParser
* @see joptsimple.OptionSet
*/
@NonNullApi
public class JOptCommandLinePropertySource extends CommandLinePropertySource<OptionSet> {
/**

View File

@@ -18,6 +18,7 @@ package org.springframework.core.env;
import java.util.Map;
import org.springframework.lang.NonNullApi;
import org.springframework.util.StringUtils;
/**
@@ -28,6 +29,7 @@ import org.springframework.util.StringUtils;
* @since 3.1
* @see PropertiesPropertySource
*/
@NonNullApi
public class MapPropertySource extends EnumerablePropertySource<Map<String, Object>> {
public MapPropertySource(String name, Map<String, Object> source) {

View File

@@ -19,6 +19,8 @@ package org.springframework.core.env;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.lang.NonNullApi;
/**
* Exception thrown when required properties are not found.
*
@@ -29,6 +31,7 @@ import java.util.Set;
* @see org.springframework.context.support.AbstractApplicationContext#prepareRefresh()
*/
@SuppressWarnings("serial")
@NonNullApi
public class MissingRequiredPropertiesException extends IllegalStateException {
private final Set<String> missingRequiredProperties = new LinkedHashSet<>();

View File

@@ -23,6 +23,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
@@ -40,6 +41,7 @@ import org.springframework.util.StringUtils;
* @since 3.1
* @see PropertySourcesPropertyResolver
*/
@NonNullApi
public class MutablePropertySources implements PropertySources {
private final Log logger;

View File

@@ -19,6 +19,8 @@ package org.springframework.core.env;
import java.util.Map;
import java.util.Properties;
import org.springframework.lang.NonNullApi;
/**
* {@link PropertySource} implementation that extracts properties from a
* {@link java.util.Properties} object.
@@ -33,6 +35,7 @@ import java.util.Properties;
* @author Juergen Hoeller
* @since 3.1
*/
@NonNullApi
public class PropertiesPropertySource extends MapPropertySource {
@SuppressWarnings({"unchecked", "rawtypes"})

View File

@@ -16,6 +16,7 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
/**
@@ -27,6 +28,7 @@ import org.springframework.lang.Nullable;
* @see Environment
* @see PropertySourcesPropertyResolver
*/
@NonNullApi
public interface PropertyResolver {
/**

View File

@@ -19,6 +19,7 @@ package org.springframework.core.env;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@@ -56,6 +57,7 @@ import org.springframework.util.ObjectUtils;
* @see MutablePropertySources
* @see org.springframework.context.annotation.PropertySource
*/
@NonNullApi
public abstract class PropertySource<T> {
protected final Log logger = LogFactory.getLog(getClass());

View File

@@ -16,6 +16,7 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
/**
@@ -24,6 +25,7 @@ import org.springframework.lang.Nullable;
* @author Chris Beams
* @since 3.1
*/
@NonNullApi
public interface PropertySources extends Iterable<PropertySource<?>> {
/**

View File

@@ -16,6 +16,7 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
/**
@@ -29,6 +30,7 @@ import org.springframework.lang.Nullable;
* @see PropertySources
* @see AbstractEnvironment
*/
@NonNullApi
public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
private final PropertySources propertySources;

View File

@@ -21,6 +21,8 @@ import java.util.Collections;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.NonNullApi;
/**
* Read-only {@code Map<String, String>} implementation that is backed by system
* properties or environment variables.
@@ -35,6 +37,7 @@ import java.util.Set;
* @author Chris Beams
* @since 3.0
*/
@NonNullApi
abstract class ReadOnlySystemAttributesMap implements Map<String, String> {
@Override

View File

@@ -16,6 +16,8 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
/**
* Parses a {@code String[]} of command line arguments in order to populate a
* {@link CommandLineArgs} object.
@@ -49,6 +51,7 @@ package org.springframework.core.env;
* @author Chris Beams
* @since 3.1
*/
@NonNullApi
class SimpleCommandLineArgsParser {
/**

View File

@@ -18,6 +18,8 @@ package org.springframework.core.env;
import java.util.List;
import org.springframework.lang.NonNullApi;
/**
* {@link CommandLinePropertySource} implementation backed by a simple String array.
*
@@ -75,6 +77,7 @@ import java.util.List;
* @see CommandLinePropertySource
* @see JOptCommandLinePropertySource
*/
@NonNullApi
public class SimpleCommandLinePropertySource extends CommandLinePropertySource<CommandLineArgs> {
/**

View File

@@ -16,6 +16,8 @@
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;
/**
* {@link Environment} implementation suitable for use in 'standard' (i.e. non-web)
* applications.
@@ -51,6 +53,7 @@ package org.springframework.core.env;
* @see SystemEnvironmentPropertySource
* @see org.springframework.web.context.support.StandardServletEnvironment
*/
@NonNullApi
public class StandardEnvironment extends AbstractEnvironment {
/** System environment property source name: {@value} */

View File

@@ -18,6 +18,7 @@ package org.springframework.core.env;
import java.util.Map;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -63,6 +64,7 @@ import org.springframework.util.Assert;
* @see AbstractEnvironment#getSystemEnvironment()
* @see AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
*/
@NonNullApi
public class SystemEnvironmentPropertySource extends MapPropertySource {
/**

View File

@@ -2,7 +2,4 @@
* Spring's environment abstraction consisting of bean definition
* profile and hierarchical property source support.
*/
@NonNullApi
package org.springframework.core.env;
import org.springframework.lang.NonNullApi;

View File

@@ -23,7 +23,7 @@ import javax.annotation.meta.TypeQualifierDefault;
*/
@Documented
@Nonnull
@Target(ElementType.PACKAGE)
@Target({ElementType.PACKAGE, ElementType.TYPE})
@TypeQualifierDefault({ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface NonNullApi {