Skip to content

Commit d040f6f

Browse files
committed
Add @Contract and @CheckReturnValue annotations.
Closes: #3195 Original pull request: #3210
1 parent 59518fe commit d040f6f

File tree

175 files changed

+944
-793
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+944
-793
lines changed

src/main/java/org/springframework/data/annotation/AccessType.java

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434

3535
/**
3636
* The access type to be used.
37-
*
38-
* @return
3937
*/
4038
Type value();
4139

src/main/java/org/springframework/data/annotation/PersistenceCreator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
*/
3030
@Retention(RetentionPolicy.RUNTIME)
3131
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
32-
public @interface PersistenceCreator {}
32+
public @interface PersistenceCreator {
33+
}

src/main/java/org/springframework/data/annotation/Reference.java

-4
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,13 @@
3636
/**
3737
* Explicitly define the target type of the reference. Used in case the annotated property is not the target type but
3838
* rather an identifier and/or if that identifier type is not uniquely identifying the target entity.
39-
*
40-
* @return
4139
*/
4240
@AliasFor(attribute = "to")
4341
Class<?> value() default Class.class;
4442

4543
/**
4644
* Explicitly define the target type of the reference. Used in case the annotated property is not the target type but
4745
* rather an identifier and/or if that identifier type is not uniquely identifying the target entity.
48-
*
49-
* @return
5046
*/
5147
@AliasFor(attribute = "value")
5248
Class<?> to() default Class.class;

src/main/java/org/springframework/data/annotation/TypeAlias.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* {@link PersistentEntity}s.
3030
*
3131
* @author Oliver Gierke
32+
* @see org.springframework.data.mapping.Alias
3233
*/
3334
@Documented
3435
@Inherited
@@ -38,9 +39,7 @@
3839
public @interface TypeAlias {
3940

4041
/**
41-
* The type alias to be used when persisting
42-
*
43-
* @return
42+
* The type alias to be used when persisting.
4443
*/
4544
String value();
4645
}

src/main/java/org/springframework/data/annotation/Version.java

-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,4 @@
3333
@Retention(RUNTIME)
3434
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
3535
public @interface Version {
36-
3736
}

src/main/java/org/springframework/data/aot/AotContext.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ default Set<Class<?>> scanPackageForTypes(Collection<Class<? extends Annotation>
140140
}
141141

142142
/**
143-
* Returns a {@link IntrospectedBeanDefinition} to obtain further detail about the underlying bean definition. A
143+
* Returns a {@link IntrospectedBeanDefinition} to obtain further detail about the underlying bean definition. An
144144
* introspected bean definition can also point to an absent bean definition.
145145
*
146146
* @param reference {@link BeanReference} to the managed bean.
@@ -151,7 +151,7 @@ default IntrospectedBeanDefinition introspectBeanDefinition(BeanReference refere
151151
}
152152

153153
/**
154-
* Returns a {@link IntrospectedBeanDefinition} to obtain further detail about the underlying bean definition. A
154+
* Returns a {@link IntrospectedBeanDefinition} to obtain further detail about the underlying bean definition. An
155155
* introspected bean definition can also point to an absent bean definition.
156156
*
157157
* @param beanName {@link String} containing the {@literal name} of the bean to evaluate; must not be {@literal null}.
@@ -251,7 +251,6 @@ interface IntrospectedBeanDefinition {
251251
* bean}.
252252
* @see BeanDefinition
253253
*/
254-
255254
BeanDefinition getBeanDefinition() throws NoSuchBeanDefinitionException;
256255

257256
/**

src/main/java/org/springframework/data/aot/AuditingBeanRegistrationAotProcessor.java

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
*/
4040
class AuditingBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
4141

42-
4342
@Nullable
4443
@Override
4544
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {

src/main/java/org/springframework/data/aot/ManagedTypesBeanFactoryInitializationAotProcessor.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
3030
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
3131
import org.springframework.data.domain.ManagedTypes;
32+
import org.springframework.lang.Contract;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.util.CollectionUtils;
3435
import org.springframework.util.ObjectUtils;
@@ -44,10 +45,11 @@
4445
*/
4546
public class ManagedTypesBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
4647

47-
private static final Log logger = LogFactory.getLog(BeanFactoryInitializationAotProcessor.class);
48+
private static final Log logger = LogFactory.getLog(ManagedTypesBeanFactoryInitializationAotProcessor.class);
4849

49-
@Nullable
5050
@Override
51+
@Nullable
52+
@Contract("_ -> null")
5153
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
5254

5355
processManagedTypes(beanFactory);
@@ -71,9 +73,9 @@ private void postProcessManagedTypes(ConfigurableListableBeanFactory beanFactory
7173

7274
if (hasConstructorArguments(beanDefinition)) {
7375

74-
ValueHolder argumentValue = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, null, null, null);
76+
ValueHolder holder = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, null, null, null);
7577

76-
if (argumentValue.getValue()instanceof Supplier supplier) {
78+
if (holder != null && holder.getValue() instanceof Supplier<?> supplier) {
7779

7880
if (logger.isDebugEnabled()) {
7981
logger.info(String.format("Replacing ManagedType bean definition %s.", beanName));

src/main/java/org/springframework/data/aot/ManagedTypesBeanRegistrationAotProcessor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ private ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
7878
if (beanDefinition.hasConstructorArgumentValues()) {
7979

8080
ValueHolder indexedArgumentValue = beanDefinition.getConstructorArgumentValues().getIndexedArgumentValue(0, null);
81-
Object value = indexedArgumentValue.getValue();
8281

83-
if (value instanceof Collection<?> values && values.stream().allMatch(it -> it instanceof Class)) {
82+
if (indexedArgumentValue != null && indexedArgumentValue.getValue() instanceof Collection<?> values
83+
&& values.stream().allMatch(it -> it instanceof Class)) {
8484
return ManagedTypes.fromIterable((Collection<Class<?>>) values);
8585
}
8686
}
@@ -115,7 +115,6 @@ private ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
115115
* @param managedTypes never {@literal null}.
116116
* @return new instance of {@link BeanRegistrationAotContribution} or {@literal null} if nothing to do.
117117
*/
118-
@Nullable
119118
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes,
120119
RegisteredBean registeredBean) {
121120
return new ManagedTypesRegistrationAotContribution(managedTypes, registeredBean, this::contributeType);

src/main/java/org/springframework/data/aot/ManagedTypesRegistrationAotContribution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.data.aot;
1717

18-
import java.lang.reflect.Executable;
1918
import java.lang.reflect.Method;
2019
import java.util.Collections;
2120
import java.util.List;
@@ -69,8 +68,8 @@
6968
* @author John Blum
7069
* @author Christoph Strobl
7170
* @author Mark Paluch
72-
* @see org.springframework.beans.factory.aot.BeanRegistrationAotContribution
7371
* @since 3.0
72+
* @see org.springframework.beans.factory.aot.BeanRegistrationAotContribution
7473
*/
7574
class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContribution {
7675

@@ -126,7 +125,7 @@ static class ManagedTypesInstanceCodeFragment extends BeanRegistrationCodeFragme
126125
public static final ResolvableType MANAGED_TYPES_TYPE = ResolvableType.forType(ManagedTypes.class);
127126
private final List<Class<?>> sourceTypes;
128127
private final RegisteredBean source;
129-
private final Lazy<Method> instanceMethod = Lazy.of(this::findInstanceFactory);
128+
private final Lazy<Method> instanceMethod;
130129

131130
private static final TypeName WILDCARD = WildcardTypeName.subtypeOf(Object.class);
132131
private static final TypeName CLASS_OF_ANY = ParameterizedTypeName.get(ClassName.get(Class.class), WILDCARD);
@@ -139,6 +138,7 @@ protected ManagedTypesInstanceCodeFragment(List<Class<?>> sourceTypes, Registere
139138

140139
this.sourceTypes = sourceTypes;
141140
this.source = source;
141+
this.instanceMethod = Lazy.of(() -> findInstanceFactory(source.getBeanClass()));
142142
}
143143

144144
@Override
@@ -230,15 +230,15 @@ private CodeBlock toCodeBlock(List<Class<?>> values, boolean allPublic) {
230230
}
231231

232232
@Nullable
233-
private Method findInstanceFactory() {
233+
private static Method findInstanceFactory(Class<?> beanClass) {
234234

235-
for (Method beanMethod : ReflectionUtils.getDeclaredMethods(source.getBeanClass())) {
235+
for (Method beanMethod : ReflectionUtils.getDeclaredMethods(beanClass)) {
236236

237237
if (!isInstanceFactory(beanMethod)) {
238238
continue;
239239
}
240240

241-
ResolvableType parameterType = ResolvableType.forMethodParameter(beanMethod, 0, source.getBeanClass());
241+
ResolvableType parameterType = ResolvableType.forMethodParameter(beanMethod, 0, beanClass);
242242

243243
if (parameterType.isAssignableFrom(LIST_TYPE) || parameterType.isAssignableFrom(MANAGED_TYPES_TYPE)) {
244244
return beanMethod;

src/main/java/org/springframework/data/aot/PublicMethodReflectiveProcessor.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.springframework.aot.hint.annotation.SimpleReflectiveProcessor;
2121

2222
/**
23+
* Extension to {@link SimpleReflectiveProcessor} to register reflective hints for invoking public methods.
24+
*
2325
* @author Christoph Strobl
2426
*/
2527
public class PublicMethodReflectiveProcessor extends SimpleReflectiveProcessor {

src/main/java/org/springframework/data/aot/RegisteredBeanAotContribution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.springframework.beans.factory.support.RegisteredBean;
2020

2121
/**
22-
* Extension to {@link BeanRegistrationAotContribution} that bases its contribution on a {@link RegisteredBean}. This
22+
* Extension to {@link BeanRegistrationAotContribution} that bases its contribution to a {@link RegisteredBean}. This
2323
* interface exposes its {@link #getSource() source}.
2424
*
2525
* @author Christoph Strobl

src/main/java/org/springframework/data/auditing/AuditableBeanWrapper.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import java.time.temporal.TemporalAccessor;
1919
import java.util.Optional;
2020

21+
import org.springframework.lang.Contract;
22+
import org.springframework.lang.Nullable;
23+
2124
/**
2225
* Interface to abstract the ways setting the auditing information can be implemented.
2326
*
@@ -31,7 +34,9 @@ public interface AuditableBeanWrapper<T> {
3134
*
3235
* @param value
3336
*/
34-
Object setCreatedBy(Object value);
37+
@Nullable
38+
@Contract("null -> null; !null -> !null")
39+
Object setCreatedBy(@Nullable Object value);
3540

3641
/**
3742
* Set the date the object was created.
@@ -45,7 +50,9 @@ public interface AuditableBeanWrapper<T> {
4550
*
4651
* @param value
4752
*/
48-
Object setLastModifiedBy(Object value);
53+
@Nullable
54+
@Contract("null -> null; !null -> !null")
55+
Object setLastModifiedBy(@Nullable Object value);
4956

5057
/**
5158
* Returns the date of the last modification date of the backing bean.

src/main/java/org/springframework/data/auditing/AuditingHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ Auditor<?> getAuditor() {
104104
.orElse(Auditor.none());
105105
}
106106

107+
@Override
107108
public void afterPropertiesSet() {
108109

109-
if (!auditorAware.isPresent()) {
110+
if (auditorAware.isEmpty()) {
110111
logger.debug("No AuditorAware set; Auditing will not be applied");
111112
}
112113
}

src/main/java/org/springframework/data/auditing/AuditingHandlerSupport.java

-9
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,6 @@ private <T> T touch(Auditor<?> auditor, T target, boolean isNew) {
151151

152152
/**
153153
* Sets modifying and creating auditor. Creating auditor is only set on new auditables.
154-
*
155-
* @param auditor
156-
* @param wrapper
157-
* @param isNew
158-
* @return
159154
*/
160155
private void touchAuditor(Auditor<?> auditor, AuditableBeanWrapper<?> wrapper, boolean isNew) {
161156

@@ -176,10 +171,6 @@ private void touchAuditor(Auditor<?> auditor, AuditableBeanWrapper<?> wrapper, b
176171

177172
/**
178173
* Touches the auditable regarding modification and creation date. Creation date is only set on new auditables.
179-
*
180-
* @param wrapper
181-
* @param isNew
182-
* @return
183174
*/
184175
private Optional<TemporalAccessor> touchDate(AuditableBeanWrapper<?> wrapper, boolean isNew) {
185176

src/main/java/org/springframework/data/auditing/Auditor.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
class Auditor<T> {
3030

31-
private static final Auditor NONE = new Auditor(null) {
31+
private static final Auditor<Object> NONE = new Auditor<>(null) {
3232

3333
@Override
3434
public boolean isPresent() {
@@ -59,6 +59,7 @@ public T getValue() {
5959
* @param <T>
6060
* @return {@link Auditor#none()} if the given {@literal source} is {@literal null}. }
6161
*/
62+
@SuppressWarnings({ "unchecked", "rawtypes" })
6263
public static <T> Auditor<T> of(@Nullable T source) {
6364

6465
if (source instanceof Auditor) {
@@ -77,7 +78,7 @@ public static <T> Auditor<T> of(@Nullable T source) {
7778
* @param <T>
7879
* @return {@link Auditor#none()} if the given {@literal source} is {@literal null}. }
7980
*/
80-
public static <T> Auditor<T> ofOptional(@Nullable Optional<T> source) {
81+
public static <T> Auditor<T> ofOptional(Optional<T> source) {
8182
return Auditor.of(source.orElse(null));
8283
}
8384

@@ -87,8 +88,9 @@ public static <T> Auditor<T> ofOptional(@Nullable Optional<T> source) {
8788
* @param <T>
8889
* @return never {@literal null}.
8990
*/
91+
@SuppressWarnings("unchecked")
9092
public static <T> Auditor<T> none() {
91-
return NONE;
93+
return (Auditor<T>) NONE;
9294
}
9395

9496
/**

src/main/java/org/springframework/data/auditing/DefaultAuditableBeanWrapperFactory.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public AuditableInterfaceBeanWrapper(ConversionService conversionService,
115115
}
116116

117117
@Override
118-
public Object setCreatedBy(Object value) {
118+
public Object setCreatedBy(@Nullable Object value) {
119119

120120
auditable.setCreatedBy(value);
121121
return value;
@@ -267,7 +267,7 @@ public ReflectionAuditingBeanWrapper(ConversionService conversionService, T targ
267267
}
268268

269269
@Override
270-
public Object setCreatedBy(Object value) {
270+
public Object setCreatedBy(@Nullable Object value) {
271271
return setField(metadata.getCreatedByField(), value);
272272
}
273273

@@ -277,7 +277,7 @@ public TemporalAccessor setCreatedDate(TemporalAccessor value) {
277277
}
278278

279279
@Override
280-
public Object setLastModifiedBy(Object value) {
280+
public Object setLastModifiedBy(@Nullable Object value) {
281281
return setField(metadata.getLastModifiedByField(), value);
282282
}
283283

@@ -308,7 +308,8 @@ public T getBean() {
308308
* @param field
309309
* @param value
310310
*/
311-
private <S> S setField(Optional<Field> field, S value) {
311+
@Nullable
312+
private <S> S setField(Optional<Field> field, @Nullable S value) {
312313

313314
field.ifPresent(it -> ReflectionUtils.setField(it, target, value));
314315

src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import org.springframework.data.mapping.context.MappingContext;
4141
import org.springframework.data.mapping.context.PersistentEntities;
4242
import org.springframework.data.util.Lazy;
43+
import org.springframework.lang.Contract;
44+
import org.springframework.lang.Nullable;
4345
import org.springframework.util.Assert;
4446

4547
/**
@@ -203,7 +205,7 @@ public TemporalAccessor setCreatedDate(TemporalAccessor value) {
203205
}
204206

205207
@Override
206-
public Object setLastModifiedBy(Object value) {
208+
public Object setLastModifiedBy(@Nullable Object value) {
207209
return setProperty(metadata.lastModifiedByPaths, value);
208210
}
209211

@@ -226,8 +228,10 @@ public T getBean() {
226228
return accessor.getBean();
227229
}
228230

231+
@Nullable
232+
@Contract("_, null -> null; _, !null -> !null")
229233
private <S> S setProperty(
230-
PersistentPropertyPaths<?, ? extends PersistentProperty<?>> paths, S value) {
234+
PersistentPropertyPaths<?, ? extends PersistentProperty<?>> paths, @Nullable S value) {
231235

232236
paths.forEach(it -> this.accessor.setProperty(it, value, OPTIONS));
233237

0 commit comments

Comments
 (0)