Skip to content

Commit 302bcca

Browse files
mp911deschauder
authored andcommitted
Consider annotated methods in AnnotationRevisionMetadata.
We now detect annotated methods. Closes #2569
1 parent 52012f9 commit 302bcca

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Optional;
2424

2525
import org.springframework.data.util.AnnotationDetectionFieldCallback;
26+
import org.springframework.data.util.AnnotationDetectionMethodCallback;
2627
import org.springframework.data.util.Lazy;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.ReflectionUtils;
@@ -34,6 +35,7 @@
3435
*
3536
* @author Oliver Gierke
3637
* @author Jens Schauder
38+
* @author Mark Paluch
3739
*/
3840
public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implements RevisionMetadata<N> {
3941

@@ -114,10 +116,18 @@ public <T> T getDelegate() {
114116
return (T) entity;
115117
}
116118

119+
@SuppressWarnings("unchecked")
117120
private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? extends Annotation> annotationType) {
118121

119122
return Lazy.of(() -> {
120123

124+
AnnotationDetectionMethodCallback<? extends Annotation> methodCallback = new AnnotationDetectionMethodCallback<>(
125+
annotationType);
126+
ReflectionUtils.doWithMethods(entity.getClass(), methodCallback);
127+
if (methodCallback.getMethod() != null) {
128+
return Optional.ofNullable((T) ReflectionUtils.invokeMethod(methodCallback.getRequiredMethod(), entity));
129+
}
130+
121131
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(annotationType);
122132
ReflectionUtils.doWithFields(entity.getClass(), callback);
123133
return Optional.ofNullable(callback.getValue(entity));

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import org.springframework.core.annotation.AnnotatedElementUtils;
2222
import org.springframework.lang.Nullable;
2323
import org.springframework.util.Assert;
24+
import org.springframework.util.ReflectionUtils;
2425
import org.springframework.util.ReflectionUtils.MethodCallback;
2526

2627
/**
2728
* {@link MethodCallback} to find annotations of a given type.
2829
*
2930
* @author Oliver Gierke
3031
* @author Christoph Strobl
32+
* @author Mark Paluch
3133
*/
3234
public class AnnotationDetectionMethodCallback<A extends Annotation> implements MethodCallback {
3335

@@ -125,6 +127,8 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
125127
}
126128

127129
this.annotation = foundAnnotation;
130+
131+
ReflectionUtils.makeAccessible(method);
128132
this.foundMethod = method;
129133
}
130134
}

src/test/java/org/springframework/data/history/AnnotationRevisionMetadataUnitTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*
3434
* @author Oliver Gierke
3535
* @author Jens Schauder
36+
* @author Mark Paluch
3637
*/
3738
class AnnotationRevisionMetadataUnitTests {
3839

@@ -83,6 +84,21 @@ void exposesRevisionDateAndInstantForLocalDateTime() {
8384
softly.assertAll();
8485
}
8586

87+
@Test // GH-2569
88+
void exposesRevisionMetadataUsingMethodAccessors() {
89+
90+
SampleWithMethodAnnotations sample = new SampleWithMethodAnnotations();
91+
sample.revisionNumber = 1L;
92+
sample.revisionDate = Instant.now();
93+
94+
RevisionMetadata<Long> metadata = getMetadata(sample);
95+
96+
softly.assertThat(metadata.getRevisionNumber()).hasValue(1L);
97+
softly.assertThat(metadata.getRevisionInstant()).hasValue(sample.revisionDate);
98+
99+
softly.assertAll();
100+
}
101+
86102
@Test // DATACMNS-1251
87103
void exposesRevisionDateAndInstantForInstant() {
88104

@@ -149,6 +165,22 @@ static class Sample {
149165
@Reference LocalDateTime revisionDate;
150166
}
151167

168+
static class SampleWithMethodAnnotations {
169+
170+
Long revisionNumber;
171+
Instant revisionDate;
172+
173+
@Autowired
174+
public Long getRevisionNumber() {
175+
return revisionNumber;
176+
}
177+
178+
@Reference
179+
public Instant getRevisionDate() {
180+
return revisionDate;
181+
}
182+
}
183+
152184
static class SampleWithInstant {
153185

154186
@Autowired Long revisionNumber;

0 commit comments

Comments
 (0)