Polishing.
Lazily make methods and fields accessible. Introduce invoke method to AnnotationDetectionMethodCallback. See #2569
This commit is contained in:
committed by
Jens Schauder
parent
6bc28c118c
commit
8b3e4d44f0
@@ -100,7 +100,6 @@ 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(() -> {
|
||||
@@ -109,7 +108,7 @@ public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implem
|
||||
annotationType);
|
||||
ReflectionUtils.doWithMethods(entity.getClass(), methodCallback);
|
||||
if (methodCallback.getMethod() != null) {
|
||||
return Optional.ofNullable((T) ReflectionUtils.invokeMethod(methodCallback.getRequiredMethod(), entity));
|
||||
return Optional.ofNullable(methodCallback.invoke(entity));
|
||||
}
|
||||
|
||||
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(annotationType);
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.util.ReflectionUtils.FieldCallback;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class AnnotationDetectionFieldCallback implements FieldCallback {
|
||||
|
||||
@@ -55,8 +56,6 @@ public class AnnotationDetectionFieldCallback implements FieldCallback {
|
||||
}
|
||||
|
||||
if (AnnotatedElementUtils.findMergedAnnotation(field, annotationType) != null) {
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
this.field = field;
|
||||
}
|
||||
}
|
||||
@@ -129,6 +128,7 @@ public class AnnotationDetectionFieldCallback implements FieldCallback {
|
||||
return null;
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
return (T) ReflectionUtils.getField(field, source);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,9 +123,29 @@ public class AnnotationDetectionMethodCallback<A extends Annotation> implements
|
||||
}
|
||||
|
||||
this.annotation = foundAnnotation;
|
||||
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
this.foundMethod = method;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes the method using reflection.
|
||||
*
|
||||
* @param target can be {@literal null} for static method invocations.
|
||||
* @param args method arguments.
|
||||
* @return
|
||||
* @since 2.7
|
||||
*/
|
||||
@Nullable
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T invoke(@Nullable Object target, Object... args) {
|
||||
|
||||
Method method = this.foundMethod;
|
||||
|
||||
if (method == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
return (T) ReflectionUtils.invokeMethod(method, target, args);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user