Skip to content

Mark API with @Contract and @CheckReturnValue annotations #3210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.5.0-SNAPSHOT</version>
<version>3.5.0-GH-3195-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@

/**
* The access type to be used.
*
* @return
*/
Type value();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
public @interface PersistenceCreator {}
public @interface PersistenceCreator {
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@
/**
* Explicitly define the target type of the reference. Used in case the annotated property is not the target type but
* rather an identifier and/or if that identifier type is not uniquely identifying the target entity.
*
* @return
*/
@AliasFor(attribute = "to")
Class<?> value() default Class.class;

/**
* Explicitly define the target type of the reference. Used in case the annotated property is not the target type but
* rather an identifier and/or if that identifier type is not uniquely identifying the target entity.
*
* @return
*/
@AliasFor(attribute = "value")
Class<?> to() default Class.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* {@link PersistentEntity}s.
*
* @author Oliver Gierke
* @see org.springframework.data.mapping.Alias
*/
@Documented
@Inherited
Expand All @@ -38,9 +39,7 @@
public @interface TypeAlias {

/**
* The type alias to be used when persisting
*
* @return
* The type alias to be used when persisting.
*/
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@
@Retention(RUNTIME)
@Target(value = { FIELD, METHOD, ANNOTATION_TYPE })
public @interface Version {

}
5 changes: 2 additions & 3 deletions src/main/java/org/springframework/data/aot/AotContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ default Set<Class<?>> scanPackageForTypes(Collection<Class<? extends Annotation>
}

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

/**
* Returns a {@link IntrospectedBeanDefinition} to obtain further detail about the underlying bean definition. A
* Returns a {@link IntrospectedBeanDefinition} to obtain further detail about the underlying bean definition. An
* introspected bean definition can also point to an absent bean definition.
*
* @param beanName {@link String} containing the {@literal name} of the bean to evaluate; must not be {@literal null}.
Expand Down Expand Up @@ -251,7 +251,6 @@ interface IntrospectedBeanDefinition {
* bean}.
* @see BeanDefinition
*/

BeanDefinition getBeanDefinition() throws NoSuchBeanDefinitionException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
*/
class AuditingBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {


@Nullable
@Override
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.data.domain.ManagedTypes;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
Expand All @@ -44,10 +45,11 @@
*/
public class ManagedTypesBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {

private static final Log logger = LogFactory.getLog(BeanFactoryInitializationAotProcessor.class);
private static final Log logger = LogFactory.getLog(ManagedTypesBeanFactoryInitializationAotProcessor.class);

@Nullable
@Override
@Nullable
@Contract("_ -> null")
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {

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

if (hasConstructorArguments(beanDefinition)) {

ValueHolder argumentValue = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, null, null, null);
ValueHolder holder = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, null, null, null);

if (argumentValue.getValue()instanceof Supplier supplier) {
if (holder != null && holder.getValue() instanceof Supplier<?> supplier) {

if (logger.isDebugEnabled()) {
logger.info(String.format("Replacing ManagedType bean definition %s.", beanName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ private ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
if (beanDefinition.hasConstructorArgumentValues()) {

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

if (value instanceof Collection<?> values && values.stream().allMatch(it -> it instanceof Class)) {
if (indexedArgumentValue != null && indexedArgumentValue.getValue() instanceof Collection<?> values
&& values.stream().allMatch(it -> it instanceof Class)) {
return ManagedTypes.fromIterable((Collection<Class<?>>) values);
}
}
Expand Down Expand Up @@ -115,7 +115,6 @@ private ManagedTypes resolveManagedTypes(RegisteredBean registeredBean) {
* @param managedTypes never {@literal null}.
* @return new instance of {@link BeanRegistrationAotContribution} or {@literal null} if nothing to do.
*/
@Nullable
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes,
RegisteredBean registeredBean) {
return new ManagedTypesRegistrationAotContribution(managedTypes, registeredBean, this::contributeType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.springframework.data.aot;

import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -69,8 +68,8 @@
* @author John Blum
* @author Christoph Strobl
* @author Mark Paluch
* @see org.springframework.beans.factory.aot.BeanRegistrationAotContribution
* @since 3.0
* @see org.springframework.beans.factory.aot.BeanRegistrationAotContribution
*/
class ManagedTypesRegistrationAotContribution implements RegisteredBeanAotContribution {

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

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

this.sourceTypes = sourceTypes;
this.source = source;
this.instanceMethod = Lazy.of(() -> findInstanceFactory(source.getBeanClass()));
}

@Override
Expand Down Expand Up @@ -230,15 +230,15 @@ private CodeBlock toCodeBlock(List<Class<?>> values, boolean allPublic) {
}

@Nullable
private Method findInstanceFactory() {
private static Method findInstanceFactory(Class<?> beanClass) {

for (Method beanMethod : ReflectionUtils.getDeclaredMethods(source.getBeanClass())) {
for (Method beanMethod : ReflectionUtils.getDeclaredMethods(beanClass)) {

if (!isInstanceFactory(beanMethod)) {
continue;
}

ResolvableType parameterType = ResolvableType.forMethodParameter(beanMethod, 0, source.getBeanClass());
ResolvableType parameterType = ResolvableType.forMethodParameter(beanMethod, 0, beanClass);

if (parameterType.isAssignableFrom(LIST_TYPE) || parameterType.isAssignableFrom(MANAGED_TYPES_TYPE)) {
return beanMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.aot.hint.annotation.SimpleReflectiveProcessor;

/**
* Extension to {@link SimpleReflectiveProcessor} to register reflective hints for invoking public methods.
*
* @author Christoph Strobl
*/
public class PublicMethodReflectiveProcessor extends SimpleReflectiveProcessor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.springframework.beans.factory.support.RegisteredBean;

/**
* Extension to {@link BeanRegistrationAotContribution} that bases its contribution on a {@link RegisteredBean}. This
* Extension to {@link BeanRegistrationAotContribution} that bases its contribution to a {@link RegisteredBean}. This
* interface exposes its {@link #getSource() source}.
*
* @author Christoph Strobl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.time.temporal.TemporalAccessor;
import java.util.Optional;

import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;

/**
* Interface to abstract the ways setting the auditing information can be implemented.
*
Expand All @@ -31,7 +34,9 @@ public interface AuditableBeanWrapper<T> {
*
* @param value
*/
Object setCreatedBy(Object value);
@Nullable
@Contract("null -> null; !null -> !null")
Object setCreatedBy(@Nullable Object value);

/**
* Set the date the object was created.
Expand All @@ -45,7 +50,9 @@ public interface AuditableBeanWrapper<T> {
*
* @param value
*/
Object setLastModifiedBy(Object value);
@Nullable
@Contract("null -> null; !null -> !null")
Object setLastModifiedBy(@Nullable Object value);

/**
* Returns the date of the last modification date of the backing bean.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ Auditor<?> getAuditor() {
.orElse(Auditor.none());
}

@Override
public void afterPropertiesSet() {

if (!auditorAware.isPresent()) {
if (auditorAware.isEmpty()) {
logger.debug("No AuditorAware set; Auditing will not be applied");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ private <T> T touch(Auditor<?> auditor, T target, boolean isNew) {

/**
* Sets modifying and creating auditor. Creating auditor is only set on new auditables.
*
* @param auditor
* @param wrapper
* @param isNew
* @return
*/
private void touchAuditor(Auditor<?> auditor, AuditableBeanWrapper<?> wrapper, boolean isNew) {

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

/**
* Touches the auditable regarding modification and creation date. Creation date is only set on new auditables.
*
* @param wrapper
* @param isNew
* @return
*/
private Optional<TemporalAccessor> touchDate(AuditableBeanWrapper<?> wrapper, boolean isNew) {

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/springframework/data/auditing/Auditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
class Auditor<T> {

private static final Auditor NONE = new Auditor(null) {
private static final Auditor<Object> NONE = new Auditor<>(null) {

@Override
public boolean isPresent() {
Expand Down Expand Up @@ -59,6 +59,7 @@ public T getValue() {
* @param <T>
* @return {@link Auditor#none()} if the given {@literal source} is {@literal null}. }
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Auditor<T> of(@Nullable T source) {

if (source instanceof Auditor) {
Expand All @@ -77,7 +78,7 @@ public static <T> Auditor<T> of(@Nullable T source) {
* @param <T>
* @return {@link Auditor#none()} if the given {@literal source} is {@literal null}. }
*/
public static <T> Auditor<T> ofOptional(@Nullable Optional<T> source) {
public static <T> Auditor<T> ofOptional(Optional<T> source) {
return Auditor.of(source.orElse(null));
}

Expand All @@ -87,8 +88,9 @@ public static <T> Auditor<T> ofOptional(@Nullable Optional<T> source) {
* @param <T>
* @return never {@literal null}.
*/
@SuppressWarnings("unchecked")
public static <T> Auditor<T> none() {
return NONE;
return (Auditor<T>) NONE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public AuditableInterfaceBeanWrapper(ConversionService conversionService,
}

@Override
public Object setCreatedBy(Object value) {
public Object setCreatedBy(@Nullable Object value) {

auditable.setCreatedBy(value);
return value;
Expand Down Expand Up @@ -267,7 +267,7 @@ public ReflectionAuditingBeanWrapper(ConversionService conversionService, T targ
}

@Override
public Object setCreatedBy(Object value) {
public Object setCreatedBy(@Nullable Object value) {
return setField(metadata.getCreatedByField(), value);
}

Expand All @@ -277,7 +277,7 @@ public TemporalAccessor setCreatedDate(TemporalAccessor value) {
}

@Override
public Object setLastModifiedBy(Object value) {
public Object setLastModifiedBy(@Nullable Object value) {
return setField(metadata.getLastModifiedByField(), value);
}

Expand Down Expand Up @@ -308,7 +308,8 @@ public T getBean() {
* @param field
* @param value
*/
private <S> S setField(Optional<Field> field, S value) {
@Nullable
private <S> S setField(Optional<Field> field, @Nullable S value) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.util.Lazy;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -203,7 +205,7 @@ public TemporalAccessor setCreatedDate(TemporalAccessor value) {
}

@Override
public Object setLastModifiedBy(Object value) {
public Object setLastModifiedBy(@Nullable Object value) {
return setProperty(metadata.lastModifiedByPaths, value);
}

Expand All @@ -226,8 +228,10 @@ public T getBean() {
return accessor.getBean();
}

@Nullable
@Contract("_, null -> null; _, !null -> !null")
private <S> S setProperty(
PersistentPropertyPaths<?, ? extends PersistentProperty<?>> paths, S value) {
PersistentPropertyPaths<?, ? extends PersistentProperty<?>> paths, @Nullable S value) {

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

Expand Down
Loading