Skip to content

Commit 34cadd0

Browse files
committed
DATACMNS-1224 - Polishing of nullability annotations.
Refactored code to properly check for null fields in Eclipse. Added warning suppressions where suitable.
1 parent d2a42f7 commit 34cadd0

16 files changed

+69
-36
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ protected Collection<Object> domainEvents() {
7575
* @param aggregate must not be {@literal null}.
7676
* @return the aggregate
7777
*/
78+
@SuppressWarnings("unchecked")
7879
protected final A andEventsFrom(A aggregate) {
7980

8081
Assert.notNull(aggregate, "Aggregate must not be null!");
@@ -92,6 +93,7 @@ protected final A andEventsFrom(A aggregate) {
9293
* @return the aggregate
9394
* @see #registerEvent(Object)
9495
*/
96+
@SuppressWarnings("unchecked")
9597
protected final A andEvent(Object event) {
9698

9799
registerEvent(event);

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

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>
5656
private final Lazy<Association<P>> association;
5757
private final @Getter PersistentEntity<?, P> owner;
5858

59+
@SuppressWarnings("null") //
5960
private final @Getter(value = AccessLevel.PROTECTED, onMethod = @__(@SuppressWarnings("null"))) Property property;
6061
private final Lazy<Integer> hashCode;
6162
private final Lazy<Boolean> usePropertyAccess;

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ MethodHandle lookup(Method method) throws ReflectiveOperationException {
133133

134134
MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());
135135

136-
return getLookup(method.getDeclaringClass()).findSpecial(method.getDeclaringClass(), method.getName(),
137-
methodType, method.getDeclaringClass());
136+
return getLookup(method.getDeclaringClass(), privateLookupIn).findSpecial(method.getDeclaringClass(),
137+
method.getName(), methodType, method.getDeclaringClass());
138138
}
139139

140140
/*
@@ -146,14 +146,14 @@ boolean isAvailable() {
146146
return true;
147147
}
148148

149-
private Lookup getLookup(Class<?> declaringClass) {
150-
151-
Lookup lookup = MethodHandles.lookup();
149+
private Lookup getLookup(Class<?> declaringClass, @Nullable Method privateLookupIn) {
152150

153151
if (privateLookupIn == null) {
154-
return lookup;
152+
return MethodHandles.lookup();
155153
}
156154

155+
Lookup lookup = MethodHandles.lookup();
156+
157157
try {
158158
return (Lookup) privateLookupIn.invoke(MethodHandles.class, declaringClass, lookup);
159159
} catch (ReflectiveOperationException e) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static boolean supports(Class<?> repositoryInterface) {
7070
*/
7171
@Nullable
7272
@Override
73-
public Object invoke(MethodInvocation invocation) throws Throwable {
73+
public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {
7474

7575
Method method = invocation.getMethod();
7676
Nullability nullability = nullabilityCache.get(method);

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

+19-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.repository.init;
1717

18+
import javax.annotation.Nonnull;
19+
1820
import org.springframework.beans.factory.FactoryBean;
1921
import org.springframework.beans.factory.config.AbstractFactoryBean;
2022
import org.springframework.context.ApplicationContext;
@@ -37,7 +39,7 @@ public abstract class AbstractRepositoryPopulatorFactoryBean
3739
extends AbstractFactoryBean<ResourceReaderRepositoryPopulator>
3840
implements ApplicationListener<ContextRefreshedEvent>, ApplicationContextAware {
3941

40-
private @Nullable Resource[] resources;
42+
private Resource[] resources = new Resource[0];
4143
private @Nullable RepositoryPopulator populator;
4244
private @Nullable ApplicationContext context;
4345

@@ -64,6 +66,7 @@ public void setApplicationContext(ApplicationContext applicationContext) {
6466
* (non-Javadoc)
6567
* @see org.springframework.beans.factory.config.AbstractFactoryBean#getObjectType()
6668
*/
69+
@Nonnull
6770
@Override
6871
public Class<?> getObjectType() {
6972
return ResourceReaderRepositoryPopulator.class;
@@ -78,7 +81,10 @@ protected ResourceReaderRepositoryPopulator createInstance() {
7881

7982
ResourceReaderRepositoryPopulator initializer = new ResourceReaderRepositoryPopulator(getResourceReader());
8083
initializer.setResources(resources);
81-
initializer.setApplicationEventPublisher(context);
84+
85+
if (context != null) {
86+
initializer.setApplicationEventPublisher(context);
87+
}
8288

8389
this.populator = initializer;
8490

@@ -91,14 +97,25 @@ protected ResourceReaderRepositoryPopulator createInstance() {
9197
*/
9298
public void onApplicationEvent(ContextRefreshedEvent event) {
9399

100+
RepositoryPopulator populator = this.populator;
101+
102+
if (populator == null) {
103+
throw new IllegalStateException("RepositoryPopulator was not properly initialized!");
104+
}
105+
94106
if (event.getApplicationContext().equals(context)) {
107+
95108
Repositories repositories = new Repositories(event.getApplicationContext());
96109
populator.populate(repositories);
97110
}
98111
}
99112

100113
protected abstract ResourceReader getResourceReader();
101114

115+
/*
116+
* (non-Javadoc)
117+
* @see org.springframework.beans.factory.config.AbstractFactoryBean#afterPropertiesSet()
118+
*/
102119
@Override
103120
public void afterPropertiesSet() throws Exception {
104121

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

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public void setUnmarshaller(Unmarshaller unmarshaller) {
4444
*/
4545
@Override
4646
protected ResourceReader getResourceReader() {
47+
48+
Unmarshaller unmarshaller = this.unmarshaller;
49+
50+
if (unmarshaller == null) {
51+
throw new IllegalStateException("No Unmarshaller configured!");
52+
}
53+
4754
return new UnmarshallingResourceReader(unmarshaller);
4855
}
4956

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

-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ public List<String> getInputProperties() {
290290
return inputProperties;
291291
}
292292

293-
@SuppressWarnings({ "unchecked", "rawtypes" })
294293
private List<String> detectConstructorParameterNames(Class<?> type) {
295294

296295
if (!isDto()) {

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.data.repository.query.ParameterAccessor;
2424
import org.springframework.data.repository.query.ParametersParameterAccessor;
2525
import org.springframework.data.repository.query.parser.PartTree.OrPart;
26+
import org.springframework.lang.Nullable;
2627
import org.springframework.util.Assert;
2728

2829
/**
@@ -101,18 +102,24 @@ public T createQuery(Sort dynamicSort) {
101102
* @param tree must not be {@literal null}.
102103
* @return
103104
*/
105+
@Nullable
104106
private S createCriteria(PartTree tree) {
105107

106108
S base = null;
107109
Iterator<Object> iterator = parameters.map(ParameterAccessor::iterator).orElse(Collections.emptyIterator());
108110

109111
for (OrPart node : tree) {
110112

111-
S criteria = null;
113+
Iterator<Part> parts = node.iterator();
112114

113-
for (Part part : node) {
115+
if (!parts.hasNext()) {
116+
throw new IllegalStateException(String.format("No part found in PartTree %s!", tree));
117+
}
118+
119+
S criteria = create(parts.next(), iterator);
114120

115-
criteria = criteria == null ? create(part, iterator) : and(part, criteria, iterator);
121+
while (parts.hasNext()) {
122+
criteria = and(parts.next(), criteria, iterator);
116123
}
117124

118125
base = base == null ? criteria : or(base, criteria);
@@ -152,9 +159,9 @@ private S createCriteria(PartTree tree) {
152159
/**
153160
* Actually creates the query object applying the given criteria object and {@link Sort} definition.
154161
*
155-
* @param criteria will never be {@literal null}.
162+
* @param criteria can be {@literal null}.
156163
* @param sort must not be {@literal null}.
157164
* @return
158165
*/
159-
protected abstract T complete(S criteria, Sort sort);
166+
protected abstract T complete(@Nullable S criteria, Sort sort);
160167
}

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

-4
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ public Collection<String> getKeywords() {
235235
*/
236236
protected boolean supports(String property) {
237237

238-
if (keywords == null) {
239-
return true;
240-
}
241-
242238
for (String keyword : keywords) {
243239
if (property.endsWith(keyword)) {
244240
return true;

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

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.springframework.data.repository.query.ExtensionAwareEvaluationContextProvider;
2121
import org.springframework.expression.EvaluationContext;
22+
import org.springframework.lang.Nullable;
2223

2324
/**
2425
* SPI to allow adding a set of properties and function definitions accessible via the root of an
@@ -59,5 +60,6 @@ public interface EvaluationContextExtension {
5960
*
6061
* @return
6162
*/
63+
@Nullable
6264
Object getRootObject();
6365
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.Collections;
1919
import java.util.Map;
2020

21+
import org.springframework.lang.Nullable;
22+
2123
/**
2224
* A base class for {@link EvaluationContextExtension}s.
2325
*
@@ -49,6 +51,7 @@ public Map<String, Function> getFunctions() {
4951
* (non-Javadoc)
5052
* @see org.springframework.data.repository.query.spi.EvaluationContextExtension#getRootObject()
5153
*/
54+
@Nullable
5255
@Override
5356
public Object getRootObject() {
5457
return null;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.core.convert.TypeDescriptor;
24+
import org.springframework.lang.Nullable;
2425
import org.springframework.util.Assert;
2526
import org.springframework.util.TypeUtils;
2627

@@ -36,7 +37,7 @@
3637
public class Function {
3738

3839
private final Method method;
39-
private final Object target;
40+
private final @Nullable Object target;
4041

4142
/**
4243
* Creates a new {@link Function} to statically invoke the given {@link Method}.
@@ -56,7 +57,7 @@ public Function(Method method) {
5657
* @param method must not be {@literal null}.
5758
* @param target can be {@literal null}, if so, the method
5859
*/
59-
public Function(Method method, Object target) {
60+
public Function(Method method, @Nullable Object target) {
6061

6162
Assert.notNull(method, "Method must not be null!");
6263
Assert.isTrue(target != null || Modifier.isStatic(method.getModifiers()),
@@ -154,7 +155,6 @@ public boolean supportsExact(List<TypeDescriptor> argumentTypes) {
154155
* Checks wether this {@code Function} has the same signature as another {@code Function}.
155156
*
156157
* @param other the {@code Function} to compare {@code this} with.
157-
*
158158
* @return {@code true} iff name and argument list are the same.
159159
*/
160160
public boolean isSignatureEqual(Function other) {

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
28+
import org.springframework.lang.Nullable;
2829
import org.springframework.transaction.CannotCreateTransactionException;
2930
import org.springframework.transaction.HeuristicCompletionException;
3031
import org.springframework.transaction.PlatformTransactionManager;
@@ -86,10 +87,14 @@ public ChainedTransactionManager(PlatformTransactionManager... transactionManage
8687
* (non-Javadoc)
8788
* @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
8889
*/
89-
public MultiTransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
90+
public MultiTransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException {
9091

9192
MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
9293

94+
if (definition == null) {
95+
return mts;
96+
}
97+
9398
if (!synchronizationManager.isSynchronizationActive()) {
9499
synchronizationManager.initSynchronization();
95100
mts.setNewSynchonization();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private static Set<Class<?>> findClasses(String... classNames) {
262262
.collect(Collectors.toSet());
263263
}
264264

265-
@SuppressWarnings("unchecked")
265+
@SuppressWarnings({ "unchecked", "rawtypes" })
266266
private static <T> Optional<Class<T>> findClass(String className) {
267267

268268
try {

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
141141
argumentResolvers.add(pageableResolver());
142142

143143
ProxyingHandlerMethodArgumentResolver resolver = new ProxyingHandlerMethodArgumentResolver(
144-
getRequiredConversionService());
144+
conversionService.getObject());
145145
resolver.setBeanFactory(context);
146146
forwardBeanClassLoader(resolver);
147147

@@ -180,17 +180,6 @@ protected void customizeSortResolver(SortHandlerMethodArgumentResolver sortResol
180180
sortResolverCustomizer.ifPresent(c -> c.customize(sortResolver));
181181
}
182182

183-
private ConversionService getRequiredConversionService() {
184-
185-
ConversionService conversionService = this.conversionService.getObject();
186-
187-
if (conversionService == null) {
188-
throw new IllegalStateException("No ConversionService configured!");
189-
}
190-
191-
return conversionService;
192-
}
193-
194183
private void forwardBeanClassLoader(BeanClassLoaderAware target) {
195184

196185
if (beanClassLoader != null) {

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

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public boolean supportsParameter(MethodParameter parameter) {
9090
* (non-Javadoc)
9191
* @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)
9292
*/
93+
@Nullable
9394
@Override
9495
public Predicate resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
9596
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
@@ -149,6 +150,10 @@ private static TypeInformation<?> detectDomainType(TypeInformation<?> source) {
149150

150151
TypeInformation<?> actualType = source.getActualType();
151152

153+
if (actualType == null) {
154+
throw new IllegalArgumentException(String.format("Could not determine domain type from %s!", source));
155+
}
156+
152157
if (source != actualType) {
153158
return detectDomainType(actualType);
154159
}

0 commit comments

Comments
 (0)