Browse Source

DATACMNS-1224 - Polishing of nullability annotations.

Refactored code to properly check for null fields in Eclipse. Added warning suppressions where suitable.
pull/271/head
Oliver Gierke 8 years ago
parent
commit
e537f5c4b9
  1. 2
      src/main/java/org/springframework/data/domain/AbstractAggregateRoot.java
  2. 1
      src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java
  3. 12
      src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java
  4. 2
      src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java
  5. 21
      src/main/java/org/springframework/data/repository/init/AbstractRepositoryPopulatorFactoryBean.java
  6. 7
      src/main/java/org/springframework/data/repository/init/UnmarshallerRepositoryPopulatorFactoryBean.java
  7. 1
      src/main/java/org/springframework/data/repository/query/ReturnedType.java
  8. 17
      src/main/java/org/springframework/data/repository/query/parser/AbstractQueryCreator.java
  9. 4
      src/main/java/org/springframework/data/repository/query/parser/Part.java
  10. 2
      src/main/java/org/springframework/data/repository/query/spi/EvaluationContextExtension.java
  11. 3
      src/main/java/org/springframework/data/repository/query/spi/EvaluationContextExtensionSupport.java
  12. 6
      src/main/java/org/springframework/data/repository/query/spi/Function.java
  13. 7
      src/main/java/org/springframework/data/transaction/ChainedTransactionManager.java
  14. 2
      src/main/java/org/springframework/data/util/NullableUtils.java
  15. 13
      src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java
  16. 5
      src/main/java/org/springframework/data/web/querydsl/QuerydslPredicateArgumentResolver.java

2
src/main/java/org/springframework/data/domain/AbstractAggregateRoot.java

@ -75,6 +75,7 @@ public class AbstractAggregateRoot<A extends AbstractAggregateRoot<A>> {
* @param aggregate must not be {@literal null}. * @param aggregate must not be {@literal null}.
* @return the aggregate * @return the aggregate
*/ */
@SuppressWarnings("unchecked")
protected final A andEventsFrom(A aggregate) { protected final A andEventsFrom(A aggregate) {
Assert.notNull(aggregate, "Aggregate must not be null!"); Assert.notNull(aggregate, "Aggregate must not be null!");
@ -92,6 +93,7 @@ public class AbstractAggregateRoot<A extends AbstractAggregateRoot<A>> {
* @return the aggregate * @return the aggregate
* @see #registerEvent(Object) * @see #registerEvent(Object)
*/ */
@SuppressWarnings("unchecked")
protected final A andEvent(Object event) { protected final A andEvent(Object event) {
registerEvent(event); registerEvent(event);

1
src/main/java/org/springframework/data/mapping/model/AbstractPersistentProperty.java

@ -56,6 +56,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
private final Lazy<Association<P>> association; private final Lazy<Association<P>> association;
private final @Getter PersistentEntity<?, P> owner; private final @Getter PersistentEntity<?, P> owner;
@SuppressWarnings("null") //
private final @Getter(value = AccessLevel.PROTECTED, onMethod = @__(@SuppressWarnings("null"))) Property property; private final @Getter(value = AccessLevel.PROTECTED, onMethod = @__(@SuppressWarnings("null"))) Property property;
private final Lazy<Integer> hashCode; private final Lazy<Integer> hashCode;
private final Lazy<Boolean> usePropertyAccess; private final Lazy<Boolean> usePropertyAccess;

12
src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java

@ -133,8 +133,8 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes()); MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
return getLookup(method.getDeclaringClass()).findSpecial(method.getDeclaringClass(), method.getName(), return getLookup(method.getDeclaringClass(), privateLookupIn).findSpecial(method.getDeclaringClass(),
methodType, method.getDeclaringClass()); method.getName(), methodType, method.getDeclaringClass());
} }
/* /*
@ -146,14 +146,14 @@ public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor
return true; return true;
} }
private Lookup getLookup(Class<?> declaringClass) { private Lookup getLookup(Class<?> declaringClass, @Nullable Method privateLookupIn) {
Lookup lookup = MethodHandles.lookup();
if (privateLookupIn == null) { if (privateLookupIn == null) {
return lookup; return MethodHandles.lookup();
} }
Lookup lookup = MethodHandles.lookup();
try { try {
return (Lookup) privateLookupIn.invoke(MethodHandles.class, declaringClass, lookup); return (Lookup) privateLookupIn.invoke(MethodHandles.class, declaringClass, lookup);
} catch (ReflectiveOperationException e) { } catch (ReflectiveOperationException e) {

2
src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java

@ -70,7 +70,7 @@ public class MethodInvocationValidator implements MethodInterceptor {
*/ */
@Nullable @Nullable
@Override @Override
public Object invoke(MethodInvocation invocation) throws Throwable { public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod(); Method method = invocation.getMethod();
Nullability nullability = nullabilityCache.get(method); Nullability nullability = nullabilityCache.get(method);

21
src/main/java/org/springframework/data/repository/init/AbstractRepositoryPopulatorFactoryBean.java

@ -15,6 +15,8 @@
*/ */
package org.springframework.data.repository.init; package org.springframework.data.repository.init;
import javax.annotation.Nonnull;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.AbstractFactoryBean; import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -37,7 +39,7 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
extends AbstractFactoryBean<ResourceReaderRepositoryPopulator> extends AbstractFactoryBean<ResourceReaderRepositoryPopulator>
implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware { implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {
private @Nullable Resource[] resources; private Resource[] resources = new Resource[0];
private @Nullable RepositoryPopulator populator; private @Nullable RepositoryPopulator populator;
private @Nullable ApplicationContext context; private @Nullable ApplicationContext context;
@ -64,6 +66,7 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.beans.factory.config.AbstractFactoryBean#getObjectType() * @see org.springframework.beans.factory.config.AbstractFactoryBean#getObjectType()
*/ */
@Nonnull
@Override @Override
public Class<?> getObjectType() { public Class<?> getObjectType() {
return ResourceReaderRepositoryPopulator.class; return ResourceReaderRepositoryPopulator.class;
@ -78,7 +81,10 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
ResourceReaderRepositoryPopulator initializer = new ResourceReaderRepositoryPopulator(getResourceReader()); ResourceReaderRepositoryPopulator initializer = new ResourceReaderRepositoryPopulator(getResourceReader());
initializer.setResources(resources); initializer.setResources(resources);
initializer.setApplicationEventPublisher(context);
if (context != null) {
initializer.setApplicationEventPublisher(context);
}
this.populator = initializer; this.populator = initializer;
@ -91,7 +97,14 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
*/ */
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(ContextRefreshedEvent event) {
RepositoryPopulator populator = this.populator;
if (populator == null) {
throw new IllegalStateException("RepositoryPopulator was not properly initialized!");
}
if (event.getApplicationContext().equals(context)) { if (event.getApplicationContext().equals(context)) {
Repositories repositories = new Repositories(event.getApplicationContext()); Repositories repositories = new Repositories(event.getApplicationContext());
populator.populate(repositories); populator.populate(repositories);
} }
@ -99,6 +112,10 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
protected abstract ResourceReader getResourceReader(); protected abstract ResourceReader getResourceReader();
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.AbstractFactoryBean#afterPropertiesSet()
*/
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {

7
src/main/java/org/springframework/data/repository/init/UnmarshallerRepositoryPopulatorFactoryBean.java

@ -44,6 +44,13 @@ public class UnmarshallerRepositoryPopulatorFactoryBean extends AbstractReposito
*/ */
@Override @Override
protected ResourceReader getResourceReader() { protected ResourceReader getResourceReader() {
Unmarshaller unmarshaller = this.unmarshaller;
if (unmarshaller == null) {
throw new IllegalStateException("No Unmarshaller configured!");
}
return new UnmarshallingResourceReader(unmarshaller); return new UnmarshallingResourceReader(unmarshaller);
} }

1
src/main/java/org/springframework/data/repository/query/ReturnedType.java

@ -290,7 +290,6 @@ public abstract class ReturnedType {
return inputProperties; return inputProperties;
} }
@SuppressWarnings({ "unchecked", "rawtypes" })
private List<String> detectConstructorParameterNames(Class<?> type) { private List<String> detectConstructorParameterNames(Class<?> type) {
if (!isDto()) { if (!isDto()) {

17
src/main/java/org/springframework/data/repository/query/parser/AbstractQueryCreator.java

@ -23,6 +23,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.ParameterAccessor; import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.ParametersParameterAccessor; import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree.OrPart; import org.springframework.data.repository.query.parser.PartTree.OrPart;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
@ -101,6 +102,7 @@ public abstract class AbstractQueryCreator<T, S> {
* @param tree must not be {@literal null}. * @param tree must not be {@literal null}.
* @return * @return
*/ */
@Nullable
private S createCriteria(PartTree tree) { private S createCriteria(PartTree tree) {
S base = null; S base = null;
@ -108,11 +110,16 @@ public abstract class AbstractQueryCreator<T, S> {
for (OrPart node : tree) { for (OrPart node : tree) {
S criteria = null; Iterator<Part> parts = node.iterator();
for (Part part : node) { if (!parts.hasNext()) {
throw new IllegalStateException(String.format("No part found in PartTree %s!", tree));
}
S criteria = create(parts.next(), iterator);
criteria = criteria == null ? create(part, iterator) : and(part, criteria, iterator); while (parts.hasNext()) {
criteria = and(parts.next(), criteria, iterator);
} }
base = base == null ? criteria : or(base, criteria); base = base == null ? criteria : or(base, criteria);
@ -152,9 +159,9 @@ public abstract class AbstractQueryCreator<T, S> {
/** /**
* Actually creates the query object applying the given criteria object and {@link Sort} definition. * Actually creates the query object applying the given criteria object and {@link Sort} definition.
* *
* @param criteria will never be {@literal null}. * @param criteria can be {@literal null}.
* @param sort must not be {@literal null}. * @param sort must not be {@literal null}.
* @return * @return
*/ */
protected abstract T complete(S criteria, Sort sort); protected abstract T complete(@Nullable S criteria, Sort sort);
} }

4
src/main/java/org/springframework/data/repository/query/parser/Part.java

@ -235,10 +235,6 @@ public class Part {
*/ */
protected boolean supports(String property) { protected boolean supports(String property) {
if (keywords == null) {
return true;
}
for (String keyword : keywords) { for (String keyword : keywords) {
if (property.endsWith(keyword)) { if (property.endsWith(keyword)) {
return true; return true;

2
src/main/java/org/springframework/data/repository/query/spi/EvaluationContextExtension.java

@ -19,6 +19,7 @@ import java.util.Map;
import org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider; import org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider;
import org.springframework.expression.EvaluationContext; import org.springframework.expression.EvaluationContext;
import org.springframework.lang.Nullable;
/** /**
* SPI to allow adding a set of properties and function definitions accessible via the root of an * SPI to allow adding a set of properties and function definitions accessible via the root of an
@ -59,5 +60,6 @@ public interface EvaluationContextExtension {
* *
* @return * @return
*/ */
@Nullable
Object getRootObject(); Object getRootObject();
} }

3
src/main/java/org/springframework/data/repository/query/spi/EvaluationContextExtensionSupport.java

@ -18,6 +18,8 @@ package org.springframework.data.repository.query.spi;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.springframework.lang.Nullable;
/** /**
* A base class for {@link EvaluationContextExtension}s. * A base class for {@link EvaluationContextExtension}s.
* *
@ -49,6 +51,7 @@ public abstract class EvaluationContextExtensionSupport implements EvaluationCon
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.repository.query.spi.EvaluationContextExtension#getRootObject() * @see org.springframework.data.repository.query.spi.EvaluationContextExtension#getRootObject()
*/ */
@Nullable
@Override @Override
public Object getRootObject() { public Object getRootObject() {
return null; return null;

6
src/main/java/org/springframework/data/repository/query/spi/Function.java

@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.TypeUtils; import org.springframework.util.TypeUtils;
@ -36,7 +37,7 @@ import org.springframework.util.TypeUtils;
public class Function { public class Function {
private final Method method; private final Method method;
private final Object target; private final @Nullable Object target;
/** /**
* Creates a new {@link Function} to statically invoke the given {@link Method}. * Creates a new {@link Function} to statically invoke the given {@link Method}.
@ -56,7 +57,7 @@ public class Function {
* @param method must not be {@literal null}. * @param method must not be {@literal null}.
* @param target can be {@literal null}, if so, the method * @param target can be {@literal null}, if so, the method
*/ */
public Function(Method method, Object target) { public Function(Method method, @Nullable Object target) {
Assert.notNull(method, "Method must not be null!"); Assert.notNull(method, "Method must not be null!");
Assert.isTrue(target != null || Modifier.isStatic(method.getModifiers()), Assert.isTrue(target != null || Modifier.isStatic(method.getModifiers()),
@ -154,7 +155,6 @@ public class Function {
* Checks wether this {@code Function} has the same signature as another {@code Function}. * Checks wether this {@code Function} has the same signature as another {@code Function}.
* *
* @param other the {@code Function} to compare {@code this} with. * @param other the {@code Function} to compare {@code this} with.
*
* @return {@code true} iff name and argument list are the same. * @return {@code true} iff name and argument list are the same.
*/ */
public boolean isSignatureEqual(Function other) { public boolean isSignatureEqual(Function other) {

7
src/main/java/org/springframework/data/transaction/ChainedTransactionManager.java

@ -25,6 +25,7 @@ import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;
import org.springframework.transaction.CannotCreateTransactionException; import org.springframework.transaction.CannotCreateTransactionException;
import org.springframework.transaction.HeuristicCompletionException; import org.springframework.transaction.HeuristicCompletionException;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
@ -86,10 +87,14 @@ public class ChainedTransactionManager implements PlatformTransactionManager {
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition) * @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
*/ */
public MultiTransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException { public MultiTransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException {
MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0)); MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
if (definition == null) {
return mts;
}
if (!synchronizationManager.isSynchronizationActive()) { if (!synchronizationManager.isSynchronizationActive()) {
synchronizationManager.initSynchronization(); synchronizationManager.initSynchronization();
mts.setNewSynchonization(); mts.setNewSynchonization();

2
src/main/java/org/springframework/data/util/NullableUtils.java

@ -262,7 +262,7 @@ public class NullableUtils {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
} }
@SuppressWarnings("unchecked") @SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> Optional<Class<T>> findClass(String className) { private static <T> Optional<Class<T>> findClass(String className) {
try { try {

13
src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java

@ -141,7 +141,7 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo
argumentResolvers.add(pageableResolver()); argumentResolvers.add(pageableResolver());
ProxyingHandlerMethodArgumentResolver resolver = new ProxyingHandlerMethodArgumentResolver( ProxyingHandlerMethodArgumentResolver resolver = new ProxyingHandlerMethodArgumentResolver(
getRequiredConversionService()); conversionService.getObject());
resolver.setBeanFactory(context); resolver.setBeanFactory(context);
forwardBeanClassLoader(resolver); forwardBeanClassLoader(resolver);
@ -180,17 +180,6 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo
sortResolverCustomizer.ifPresent(c -> c.customize(sortResolver)); sortResolverCustomizer.ifPresent(c -> c.customize(sortResolver));
} }
private ConversionService getRequiredConversionService() {
ConversionService conversionService = this.conversionService.getObject();
if (conversionService == null) {
throw new IllegalStateException("No ConversionService configured!");
}
return conversionService;
}
private void forwardBeanClassLoader(BeanClassLoaderAware target) { private void forwardBeanClassLoader(BeanClassLoaderAware target) {
if (beanClassLoader != null) { if (beanClassLoader != null) {

5
src/main/java/org/springframework/data/web/querydsl/QuerydslPredicateArgumentResolver.java

@ -90,6 +90,7 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
* (non-Javadoc) * (non-Javadoc)
* @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) * @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)
*/ */
@Nullable
@Override @Override
public Predicate resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, public Predicate resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
@ -149,6 +150,10 @@ public class QuerydslPredicateArgumentResolver implements HandlerMethodArgumentR
TypeInformation<?> actualType = source.getActualType(); TypeInformation<?> actualType = source.getActualType();
if (actualType == null) {
throw new IllegalArgumentException(String.format("Could not determine domain type from %s!", source));
}
if (source != actualType) { if (source != actualType) {
return detectDomainType(actualType); return detectDomainType(actualType);
} }

Loading…
Cancel
Save