Consider annotated methods in AnnotationRevisionMetadata.

We now detect annotated methods.

Closes #2569
This commit is contained in:
Mark Paluch
2022-03-02 14:22:30 +01:00
committed by Jens Schauder
parent 4788b63268
commit 536d542cce
3 changed files with 46 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Date;
import java.util.Optional;
import org.springframework.data.util.AnnotationDetectionFieldCallback;
import org.springframework.data.util.AnnotationDetectionMethodCallback;
import org.springframework.data.util.Lazy;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
@@ -34,6 +35,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Mark Paluch
*/
public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implements RevisionMetadata<N> {
@@ -114,10 +116,18 @@ public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implem
return (T) entity;
}
@SuppressWarnings("unchecked")
private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? extends Annotation> annotationType) {
return Lazy.of(() -> {
AnnotationDetectionMethodCallback<? extends Annotation> methodCallback = new AnnotationDetectionMethodCallback<>(
annotationType);
ReflectionUtils.doWithMethods(entity.getClass(), methodCallback);
if (methodCallback.getMethod() != null) {
return Optional.ofNullable((T) ReflectionUtils.invokeMethod(methodCallback.getRequiredMethod(), entity));
}
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(annotationType);
ReflectionUtils.doWithFields(entity.getClass(), callback);
return Optional.ofNullable(callback.getValue(entity));

View File

@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.MethodCallback;
/**
@@ -28,6 +29,7 @@ import org.springframework.util.ReflectionUtils.MethodCallback;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
*/
public class AnnotationDetectionMethodCallback<A extends Annotation> implements MethodCallback {
@@ -125,6 +127,8 @@ public class AnnotationDetectionMethodCallback<A extends Annotation> implements
}
this.annotation = foundAnnotation;
ReflectionUtils.makeAccessible(method);
this.foundMethod = method;
}
}

View File

@@ -33,6 +33,7 @@ import org.springframework.data.annotation.Reference;
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Mark Paluch
*/
class AnnotationRevisionMetadataUnitTests {
@@ -83,6 +84,21 @@ class AnnotationRevisionMetadataUnitTests {
softly.assertAll();
}
@Test // GH-2569
void exposesRevisionMetadataUsingMethodAccessors() {
SampleWithMethodAnnotations sample = new SampleWithMethodAnnotations();
sample.revisionNumber = 1L;
sample.revisionDate = Instant.now();
RevisionMetadata<Long> metadata = getMetadata(sample);
softly.assertThat(metadata.getRevisionNumber()).hasValue(1L);
softly.assertThat(metadata.getRevisionInstant()).hasValue(sample.revisionDate);
softly.assertAll();
}
@Test // DATACMNS-1251
void exposesRevisionDateAndInstantForInstant() {
@@ -149,6 +165,22 @@ class AnnotationRevisionMetadataUnitTests {
@Reference LocalDateTime revisionDate;
}
static class SampleWithMethodAnnotations {
Long revisionNumber;
Instant revisionDate;
@Autowired
public Long getRevisionNumber() {
return revisionNumber;
}
@Reference
public Instant getRevisionDate() {
return revisionDate;
}
}
static class SampleWithInstant {
@Autowired Long revisionNumber;