DATACMNS-1141 - Add missing NonNullApi and Nullable annotations.

This commit is contained in:
Christoph Strobl
2017-08-22 13:31:16 +02:00
committed by Mark Paluch
parent 1e134cbd33
commit 0669632a61
20 changed files with 132 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2011 the original author or authors.
* Copyright 2008-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.
@@ -15,25 +15,29 @@
*/
package org.springframework.data.domain;
import org.springframework.lang.Nullable;
/**
* Simple interface for entities.
*
* @param <ID> the type of the identifier
* @author Oliver Gierke
* @author Christoph Strobl
*/
public interface Persistable<ID> {
/**
* Returns the id of the entity.
*
* @return the id
* @return the id. Can be {@literal null}.
*/
@Nullable
ID getId();
/**
* Returns if the {@code Persistable} is new or was persisted already.
*
* @return if the object is new
* @return if {@literal true} the object is new.
*/
boolean isNew();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -23,6 +23,7 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.Resource;
import org.springframework.data.repository.support.Repositories;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
@@ -30,14 +31,15 @@ import org.springframework.util.Assert;
* a {@link ResourceReader} to hand into the {@link RepositoryPopulator} instance created.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public abstract class AbstractRepositoryPopulatorFactoryBean extends
AbstractFactoryBean<ResourceReaderRepositoryPopulator> implements ApplicationListener<ContextRefreshedEvent>,
ApplicationContextAware {
public abstract class AbstractRepositoryPopulatorFactoryBean
extends AbstractFactoryBean<ResourceReaderRepositoryPopulator>
implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {
private Resource[] resources;
private RepositoryPopulator populator;
private ApplicationContext context;
private @Nullable Resource[] resources;
private @Nullable RepositoryPopulator populator;
private @Nullable ApplicationContext context;
/**
* Configures the {@link Resource}s to be used to load objects from and initialize the repositories eventually.
@@ -45,6 +47,7 @@ public abstract class AbstractRepositoryPopulatorFactoryBean extends
* @param resources must not be {@literal null}.
*/
public void setResources(Resource[] resources) {
Assert.notNull(resources, "Resources must not be null!");
this.resources = resources.clone();
}
@@ -95,4 +98,11 @@ public abstract class AbstractRepositoryPopulatorFactoryBean extends
}
protected abstract ResourceReader getResourceReader();
@Override
public void afterPropertiesSet() throws Exception {
Assert.state(resources != null, "Resources must not be null!");
super.afterPropertiesSet();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-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.
@@ -16,6 +16,7 @@
package org.springframework.data.repository.init;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -23,18 +24,19 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* {@link FactoryBean} to set up a {@link ResourceReaderRepositoryPopulator} with a {@link Jackson2ResourceReader}.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.6
*/
public class Jackson2RepositoryPopulatorFactoryBean extends AbstractRepositoryPopulatorFactoryBean {
private ObjectMapper mapper;
private @Nullable ObjectMapper mapper;
/**
* Configures the {@link ObjectMapper} to be used.
*
* @param mapper
*
* @param mapper can be {@literal null}.
*/
public void setMapper(ObjectMapper mapper) {
public void setMapper(@Nullable ObjectMapper mapper) {
this.mapper = mapper;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-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.
@@ -24,6 +24,8 @@ import java.util.Iterator;
import java.util.List;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import com.fasterxml.jackson.databind.JsonNode;
@@ -33,6 +35,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* A {@link ResourceReader} using Jackson to read JSON into objects.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.6
*/
public class Jackson2ResourceReader implements ResourceReader {
@@ -59,7 +62,7 @@ public class Jackson2ResourceReader implements ResourceReader {
*
* @param mapper
*/
public Jackson2ResourceReader(ObjectMapper mapper) {
public Jackson2ResourceReader(@Nullable ObjectMapper mapper) {
this.mapper = mapper == null ? DEFAULT_MAPPER : mapper;
}
@@ -69,15 +72,17 @@ public class Jackson2ResourceReader implements ResourceReader {
*
* @param typeKey
*/
public void setTypeKey(String typeKey) {
this.typeKey = typeKey;
public void setTypeKey(@Nullable String typeKey) {
this.typeKey = typeKey == null ? DEFAULT_TYPE_KEY : typeKey;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.init.ResourceReader#readFrom(org.springframework.core.io.Resource, java.lang.ClassLoader)
*/
public Object readFrom(Resource resource, ClassLoader classLoader) throws Exception {
public Object readFrom(Resource resource, @Nullable ClassLoader classLoader) throws Exception {
Assert.notNull(resource, "Resource must not be null!");
InputStream stream = resource.getInputStream();
JsonNode node = mapper.readerFor(JsonNode.class).readTree(stream);
@@ -102,10 +107,10 @@ public class Jackson2ResourceReader implements ResourceReader {
* Reads the given {@link JsonNode} into an instance of the type encoded in it using the configured type key.
*
* @param node must not be {@literal null}.
* @param classLoader
* @param classLoader can be {@literal null}.
* @return
*/
private Object readSingle(JsonNode node, ClassLoader classLoader) throws IOException {
private Object readSingle(JsonNode node, @Nullable ClassLoader classLoader) throws IOException {
JsonNode typeNode = node.findValue(typeKey);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -27,8 +27,8 @@ public interface RepositoryPopulator {
/**
* Populates the given {@link Repositories}.
*
* @param repositories
*
* @param repositories must not be {@literal null}.
*/
void populate(Repositories repositories);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -16,15 +16,26 @@
package org.springframework.data.repository.init;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
/**
* @author Oliver Gierke
* @author Christoph Strobl
*/
public interface ResourceReader {
public static enum Type {
enum Type {
XML, JSON;
}
Object readFrom(Resource resource, ClassLoader classLoader) throws Exception;
/**
* Reads a single or {@link java.util.Collection} of target objects from the given {@link Resource}.
*
* @param resource must not be {@literal null}.
* @param classLoader can be {@literal null}.
* @return {@link java.util.Collection} of target objects if resource contains multiple ones of single on. Never
* {@literal null}.
* @throws Exception
*/
Object readFrom(Resource resource, @Nullable ClassLoader classLoader) throws Exception;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-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.
@@ -18,6 +18,7 @@ package org.springframework.data.repository.init;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,12 +31,14 @@ import org.springframework.data.repository.support.DefaultRepositoryInvokerFacto
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.repository.support.RepositoryInvoker;
import org.springframework.data.repository.support.RepositoryInvokerFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* A {@link RepositoryPopulator} using a {@link ResourceReader} to read objects from the configured {@link Resource}s.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.4
*/
public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, ApplicationEventPublisherAware {
@@ -46,8 +49,8 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
private final ResourceReader reader;
private final ClassLoader classLoader;
private ApplicationEventPublisher publisher;
private Collection<Resource> resources;
private @Nullable ApplicationEventPublisher publisher;
private Collection<Resource> resources = Collections.emptySet();
/**
* Creates a new {@link ResourceReaderRepositoryPopulator} using the given {@link ResourceReader}.
@@ -63,9 +66,9 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
* {@link ClassLoader}.
*
* @param reader must not be {@literal null}.
* @param classLoader
* @param classLoader can be {@literal null}.
*/
public ResourceReaderRepositoryPopulator(ResourceReader reader, ClassLoader classLoader) {
public ResourceReaderRepositoryPopulator(ResourceReader reader, @Nullable ClassLoader classLoader) {
Assert.notNull(reader, "Reader must not be null!");
@@ -99,7 +102,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
* (non-Javadoc)
* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
*/
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
public void setApplicationEventPublisher(@Nullable ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@@ -109,6 +112,8 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
*/
public void populate(Repositories repositories) {
Assert.notNull(repositories, "Repositories must not be null!");
RepositoryInvokerFactory invokerFactory = new DefaultRepositoryInvokerFactory(repositories);
for (Resource resource : resources) {
@@ -153,7 +158,7 @@ public class ResourceReaderRepositoryPopulator implements RepositoryPopulator, A
* Persists the given {@link Object} using a suitable repository.
*
* @param object must not be {@literal null}.
* @param repositories must not be {@literal null}.
* @param invokerFactory must not be {@literal null}.
*/
private void persist(Object object, RepositoryInvokerFactory invokerFactory) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -16,6 +16,7 @@
package org.springframework.data.repository.init;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.oxm.Unmarshaller;
import org.springframework.util.Assert;
@@ -23,10 +24,11 @@ import org.springframework.util.Assert;
* {@link FactoryBean} to create a {@link ResourceReaderRepositoryPopulator} using an {@link Unmarshaller}.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class UnmarshallerRepositoryPopulatorFactoryBean extends AbstractRepositoryPopulatorFactoryBean {
private Unmarshaller unmarshaller;
private @Nullable Unmarshaller unmarshaller;
/**
* Configures the {@link Unmarshaller} to be used.
@@ -51,7 +53,8 @@ public class UnmarshallerRepositoryPopulatorFactoryBean extends AbstractReposito
*/
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(unmarshaller, "No Unmarshaller configured!");
Assert.state(unmarshaller != null, "No Unmarshaller configured!");
super.afterPropertiesSet();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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,10 +20,13 @@ import java.io.IOException;
import javax.xml.transform.stream.StreamSource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.oxm.Unmarshaller;
import org.springframework.util.Assert;
/**
* @author Oliver Gierke
* @author Christoph Strobl
*/
public class UnmarshallingResourceReader implements ResourceReader {
@@ -40,7 +43,10 @@ public class UnmarshallingResourceReader implements ResourceReader {
* (non-Javadoc)
* @see org.springframework.data.repository.init.ResourceReader#readFrom(org.springframework.core.io.Resource, java.lang.ClassLoader)
*/
public Object readFrom(Resource resource, ClassLoader classLoader) throws IOException {
public Object readFrom(Resource resource, @Nullable ClassLoader classLoader) throws IOException {
Assert.notNull(resource, "Resource must not be null!");
StreamSource source = new StreamSource(resource.getInputStream());
return unmarshaller.unmarshal(source);
}

View File

@@ -1,4 +1,5 @@
/**
* Support for repository initialization using XML and JSON.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.repository.init;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2010 the original author or authors.
* Copyright 2008-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.
@@ -15,25 +15,29 @@
*/
package org.springframework.data.repository.query;
import org.springframework.lang.Nullable;
/**
* Interface for a query abstraction.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
public interface RepositoryQuery {
/**
* Executes the {@link RepositoryQuery} with the given parameters.
*
* @param parameters
* @return
* @param parameters must not be {@literal null}.
* @return execution result. Can be {@literal null}.
*/
@Nullable
Object execute(Object[] parameters);
/**
* Returns the
* Returns the related {@link QueryMethod}.
*
* @return
* @return never {@literal null}.
*/
QueryMethod getQueryMethod();
}

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.repository.query.parser;
import java.util.Collections;
import java.util.Iterator;
import java.util.Optional;
@@ -31,6 +32,7 @@ import org.springframework.util.Assert;
* @param <S> the intermediate criteria type
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
*/
public abstract class AbstractQueryCreator<T, S> {
@@ -83,10 +85,12 @@ public abstract class AbstractQueryCreator<T, S> {
* Creates the actual query object applying the given {@link Sort} parameter. Use this method in case you haven't
* provided a {@link ParameterAccessor} in the first place but want to apply dynamic sorting nevertheless.
*
* @param dynamicSort
* @param dynamicSort must not be {@literal null}.
* @return
*/
public T createQuery(Sort dynamicSort) {
Assert.notNull(dynamicSort, "DynamicSort must not be null!");
return complete(createCriteria(tree), tree.getSort().and(dynamicSort));
}
@@ -94,13 +98,13 @@ public abstract class AbstractQueryCreator<T, S> {
* Actual query building logic. Traverses the {@link PartTree} and invokes callback methods to delegate actual
* criteria creation and concatenation.
*
* @param tree
* @param tree must not be {@literal null}.
* @return
*/
private S createCriteria(PartTree tree) {
S base = null;
Iterator<Object> iterator = parameters.map(ParameterAccessor::iterator).orElse(null);
Iterator<Object> iterator = parameters.map(ParameterAccessor::iterator).orElse(Collections.emptyIterator());
for (OrPart node : tree) {
@@ -120,8 +124,8 @@ public abstract class AbstractQueryCreator<T, S> {
/**
* Creates a new atomic instance of the criteria object.
*
* @param part
* @param iterator
* @param part must not be {@literal null}.
* @param iterator must not be {@literal null}.
* @return
*/
protected abstract S create(Part part, Iterator<Object> iterator);
@@ -129,9 +133,9 @@ public abstract class AbstractQueryCreator<T, S> {
/**
* Creates a new criteria object from the given part and and-concatenates it to the given base criteria.
*
* @param part
* @param part must not be {@literal null}.
* @param base will never be {@literal null}.
* @param iterator
* @param iterator must not be {@literal null}.
* @return
*/
protected abstract S and(Part part, S base, Iterator<Object> iterator);
@@ -139,8 +143,8 @@ public abstract class AbstractQueryCreator<T, S> {
/**
* Or-concatenates the given base criteria to the given new criteria.
*
* @param base
* @param criteria
* @param base must not be {@literal null}.
* @param criteria must not be {@literal null}.
* @return
*/
protected abstract S or(S base, S criteria);
@@ -149,7 +153,7 @@ public abstract class AbstractQueryCreator<T, S> {
* Actually creates the query object applying the given criteria object and {@link Sort} definition.
*
* @param criteria will never be {@literal null}.
* @param sort might be {@literal null}.
* @param sort must not be {@literal null}.
* @return
*/
protected abstract T complete(S criteria, Sort sort);

View File

@@ -56,7 +56,7 @@ class OrderBySource {
*
* @param clause must not be {@literal null}.
*/
public OrderBySource(String clause) {
OrderBySource(String clause) {
this(clause, Optional.empty());
}
@@ -67,7 +67,7 @@ class OrderBySource {
* @param clause must not be {@literal null}.
* @param domainClass must not be {@literal null}.
*/
public OrderBySource(String clause, Optional<Class<?>> domainClass) {
OrderBySource(String clause, Optional<Class<?>> domainClass) {
this.orders = new ArrayList<>();
@@ -100,8 +100,8 @@ class OrderBySource {
* is given, we will use it for nested property traversal checks.
*
* @param propertySource
* @param direction
* @param domainClass can be {@literal null}.
* @param direction must not be {@literal null}.
* @param domainClass must not be {@literal null}.
* @return
* @see PropertyPath#from(String, Class)
*/

View File

@@ -29,6 +29,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.parser.Part.Type;
import org.springframework.data.repository.query.parser.PartTree.OrPart;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -105,9 +106,9 @@ public class PartTree implements Streamable<OrPart> {
}
/**
* Returns the {@link Sort} specification parsed from the source or <tt>null</tt>.
*
* @return the sort
* Returns the {@link Sort} specification parsed from the source.
*
* @return never {@literal null}.
*/
public Sort getSort() {
return predicate.getOrderBySource().toSort();
@@ -164,9 +165,10 @@ public class PartTree implements Streamable<OrPart> {
/**
* Return the number of maximal results to return or {@literal null} if not restricted.
*
* @return
* @return {@literal null} if not restricted.
* @since 1.9
*/
@Nullable
public Integer getMaxResults() {
return subject.getMaxResults().orElse(null);
}

View File

@@ -1,5 +1,5 @@
/**
* Support classes for parsing queries from method names.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.repository.query.parser;

View File

@@ -1,5 +1,5 @@
/**
* Service provider interfaces to extend the query execution mechanism.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.repository.query.spi;

View File

@@ -0,0 +1,5 @@
/**
* Support for reactive repository.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.repository.reactive;

View File

@@ -3,4 +3,5 @@
*
* @see org.springframework.data.transaction.ChainedTransactionManager
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.transaction;

View File

@@ -30,6 +30,7 @@ import org.springframework.data.querydsl.binding.QuerydslPredicateBuilder;
import org.springframework.data.util.CastUtils;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.support.WebDataBinderFactory;
@@ -90,8 +91,8 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.method.support.ModelAndViewContainer, org.springframework.web.context.request.NativeWebRequest, org.springframework.web.bind.support.WebDataBinderFactory)
*/
@Override
public Predicate resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
public Predicate resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();

View File

@@ -1,4 +1,5 @@
/**
* Querydsl-specific web support.
*/
@org.springframework.lang.NonNullApi
package org.springframework.data.web.querydsl;