While reading the source code of SpringRetry, I come across this code snippet:
private static class AnnotationMethodsResolver {
private Class<? extends Annotation> annotationType;
public AnnotationMethodsResolver(Class<? extends Annotation> annotationType) {
this.annotationType = annotationType;
}
public boolean hasAnnotatedMethods(Class<?> clazz) {
final AtomicBoolean found = new AtomicBoolean(false);
ReflectionUtils.doWithMethods(clazz,
new MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException,
IllegalAccessException {
if (found.get()) {
return;
}
Annotation annotation = AnnotationUtils.findAnnotation(method,
annotationType);
if (annotation != null) { found.set(true); }
}
});
return found.get();
}
}
My question is, why using AtomicBoolean as a local variable here? I have checked the source code of RelfectionUtils.doWithMethods() and didn't find any concurrency invocation there.
Each invocation of hasAnnotatedMethods gets its own instance of found, so the context from which hasAnnotatedMethods is called doesn't matter.
It is possible that ReflectionUtils.doWithMethods calls the doWith method from multiple threads, which would require doWith to be thread safe.
I suspect that AtomicBoolean is just being used to return a value from the callback, and that boolean[] found = new boolean[1]; would do just as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With