Consider annotated methods in AnnotationRevisionMetadata.
We now detect annotated methods. Closes #2569
This commit is contained in:
committed by
Jens Schauder
parent
4788b63268
commit
536d542cce
@@ -23,6 +23,7 @@ import java.util.Date;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.data.util.AnnotationDetectionFieldCallback;
|
import org.springframework.data.util.AnnotationDetectionFieldCallback;
|
||||||
|
import org.springframework.data.util.AnnotationDetectionMethodCallback;
|
||||||
import org.springframework.data.util.Lazy;
|
import org.springframework.data.util.Lazy;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
@@ -34,6 +35,7 @@ import org.springframework.util.ReflectionUtils;
|
|||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author Jens Schauder
|
* @author Jens Schauder
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implements RevisionMetadata<N> {
|
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;
|
return (T) entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? extends Annotation> annotationType) {
|
private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? extends Annotation> annotationType) {
|
||||||
|
|
||||||
return Lazy.of(() -> {
|
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);
|
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(annotationType);
|
||||||
ReflectionUtils.doWithFields(entity.getClass(), callback);
|
ReflectionUtils.doWithFields(entity.getClass(), callback);
|
||||||
return Optional.ofNullable(callback.getValue(entity));
|
return Optional.ofNullable(callback.getValue(entity));
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
|
|||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.ReflectionUtils.MethodCallback;
|
import org.springframework.util.ReflectionUtils.MethodCallback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,6 +29,7 @@ import org.springframework.util.ReflectionUtils.MethodCallback;
|
|||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author Christoph Strobl
|
* @author Christoph Strobl
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
public class AnnotationDetectionMethodCallback<A extends Annotation> implements MethodCallback {
|
public class AnnotationDetectionMethodCallback<A extends Annotation> implements MethodCallback {
|
||||||
|
|
||||||
@@ -125,6 +127,8 @@ public class AnnotationDetectionMethodCallback<A extends Annotation> implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.annotation = foundAnnotation;
|
this.annotation = foundAnnotation;
|
||||||
|
|
||||||
|
ReflectionUtils.makeAccessible(method);
|
||||||
this.foundMethod = method;
|
this.foundMethod = method;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import org.springframework.data.annotation.Reference;
|
|||||||
*
|
*
|
||||||
* @author Oliver Gierke
|
* @author Oliver Gierke
|
||||||
* @author Jens Schauder
|
* @author Jens Schauder
|
||||||
|
* @author Mark Paluch
|
||||||
*/
|
*/
|
||||||
class AnnotationRevisionMetadataUnitTests {
|
class AnnotationRevisionMetadataUnitTests {
|
||||||
|
|
||||||
@@ -83,6 +84,21 @@ class AnnotationRevisionMetadataUnitTests {
|
|||||||
softly.assertAll();
|
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
|
@Test // DATACMNS-1251
|
||||||
void exposesRevisionDateAndInstantForInstant() {
|
void exposesRevisionDateAndInstantForInstant() {
|
||||||
|
|
||||||
@@ -149,6 +165,22 @@ class AnnotationRevisionMetadataUnitTests {
|
|||||||
@Reference LocalDateTime revisionDate;
|
@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 {
|
static class SampleWithInstant {
|
||||||
|
|
||||||
@Autowired Long revisionNumber;
|
@Autowired Long revisionNumber;
|
||||||
|
|||||||
Reference in New Issue
Block a user