Skip to content

Commit 8b3e4d4

Browse files
mp911deschauder
authored andcommitted
Polishing.
Lazily make methods and fields accessible. Introduce invoke method to AnnotationDetectionMethodCallback. See #2569
1 parent 6bc28c1 commit 8b3e4d4

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ public <T> T getDelegate() {
100100
return (T) entity;
101101
}
102102

103-
@SuppressWarnings("unchecked")
104103
private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? extends Annotation> annotationType) {
105104

106105
return Lazy.of(() -> {
@@ -109,7 +108,7 @@ private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? ext
109108
annotationType);
110109
ReflectionUtils.doWithMethods(entity.getClass(), methodCallback);
111110
if (methodCallback.getMethod() != null) {
112-
return Optional.ofNullable((T) ReflectionUtils.invokeMethod(methodCallback.getRequiredMethod(), entity));
111+
return Optional.ofNullable(methodCallback.invoke(entity));
113112
}
114113

115114
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(annotationType);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*
3131
* @author Oliver Gierke
3232
* @author Christoph Strobl
33+
* @author Mark Paluch
3334
*/
3435
public class AnnotationDetectionFieldCallback implements FieldCallback {
3536

@@ -55,8 +56,6 @@ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessEx
5556
}
5657

5758
if (AnnotatedElementUtils.findMergedAnnotation(field, annotationType) != null) {
58-
59-
ReflectionUtils.makeAccessible(field);
6059
this.field = field;
6160
}
6261
}
@@ -129,6 +128,7 @@ public <T> T getValue(Object source) {
129128
return null;
130129
}
131130

131+
ReflectionUtils.makeAccessible(field);
132132
return (T) ReflectionUtils.getField(field, source);
133133
}
134134
}

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,29 @@ public void doWith(Method method) throws IllegalArgumentException, IllegalAccess
123123
}
124124

125125
this.annotation = foundAnnotation;
126-
127-
ReflectionUtils.makeAccessible(method);
128126
this.foundMethod = method;
129127
}
130128
}
129+
130+
/**
131+
* Invokes the method using reflection.
132+
*
133+
* @param target can be {@literal null} for static method invocations.
134+
* @param args method arguments.
135+
* @return
136+
* @since 2.7
137+
*/
138+
@Nullable
139+
@SuppressWarnings("unchecked")
140+
public <T> T invoke(@Nullable Object target, Object... args) {
141+
142+
Method method = this.foundMethod;
143+
144+
if (method == null) {
145+
return null;
146+
}
147+
148+
ReflectionUtils.makeAccessible(method);
149+
return (T) ReflectionUtils.invokeMethod(method, target, args);
150+
}
131151
}

0 commit comments

Comments
 (0)