Skip to content

Commit e3639ea

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

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
@@ -116,7 +116,6 @@ public <T> T getDelegate() {
116116
return (T) entity;
117117
}
118118

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

122121
return Lazy.of(() -> {
@@ -125,7 +124,7 @@ private static <T> Lazy<Optional<T>> detectAnnotation(Object entity, Class<? ext
125124
annotationType);
126125
ReflectionUtils.doWithMethods(entity.getClass(), methodCallback);
127126
if (methodCallback.getMethod() != null) {
128-
return Optional.ofNullable((T) ReflectionUtils.invokeMethod(methodCallback.getRequiredMethod(), entity));
127+
return Optional.ofNullable(methodCallback.invoke(entity));
129128
}
130129

131130
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

@@ -59,8 +60,6 @@ public void doWith(Field field) throws IllegalArgumentException, IllegalAccessEx
5960
}
6061

6162
if (AnnotatedElementUtils.findMergedAnnotation(field, annotationType) != null) {
62-
63-
ReflectionUtils.makeAccessible(field);
6463
this.field = field;
6564
}
6665
}
@@ -133,6 +132,7 @@ public <T> T getValue(Object source) {
133132
return null;
134133
}
135134

135+
ReflectionUtils.makeAccessible(field);
136136
return (T) ReflectionUtils.getField(field, source);
137137
}
138138
}

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

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

129129
this.annotation = foundAnnotation;
130-
131-
ReflectionUtils.makeAccessible(method);
132130
this.foundMethod = method;
133131
}
134132
}
133+
134+
/**
135+
* Invokes the method using reflection.
136+
*
137+
* @param target can be {@literal null} for static method invocations.
138+
* @param args method arguments.
139+
* @return
140+
* @since 2.7
141+
*/
142+
@Nullable
143+
@SuppressWarnings("unchecked")
144+
public <T> T invoke(@Nullable Object target, Object... args) {
145+
146+
Method method = this.foundMethod;
147+
148+
if (method == null) {
149+
return null;
150+
}
151+
152+
ReflectionUtils.makeAccessible(method);
153+
return (T) ReflectionUtils.invokeMethod(method, target, args);
154+
}
135155
}

0 commit comments

Comments
 (0)