Don't allow infinite recursive annotations (#258)

This commit is contained in:
JOO200
2020-04-19 00:41:22 +02:00
committed by GitHub
parent 5d6892dbd4
commit 81dc6e267b
@@ -27,6 +27,8 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
@@ -50,7 +52,7 @@ class Annotations <M extends CommandManager> extends AnnotationLookups {
}
String getAnnotationValue(AnnotatedElement object, Class<? extends Annotation> annoClass, int options) {
Annotation annotation = getAnnotationRecursive(object, annoClass);
Annotation annotation = getAnnotationRecursive(object, annoClass, new HashSet<>());
String value = null;
if (annotation != null) {
@@ -103,13 +105,15 @@ class Annotations <M extends CommandManager> extends AnnotationLookups {
return value;
}
private static Annotation getAnnotationRecursive(AnnotatedElement object, Class<? extends Annotation> annoClass) {
private static Annotation getAnnotationRecursive(AnnotatedElement object, Class<? extends Annotation> annoClass, Collection<Annotation> checked) {
if (object.isAnnotationPresent(annoClass)) {
return object.getAnnotation(annoClass);
} else {
for (Annotation otherAnnotation : object.getDeclaredAnnotations()) {
if (!otherAnnotation.annotationType().getPackage().getName().startsWith("java.")) {
final Annotation foundAnnotation = getAnnotationRecursive(otherAnnotation.annotationType(), annoClass);
if (checked.contains(otherAnnotation)) return null;
checked.add(otherAnnotation);
final Annotation foundAnnotation = getAnnotationRecursive(otherAnnotation.annotationType(), annoClass, checked);
if (foundAnnotation != null) {
return foundAnnotation;
}