Skip to content

Commit

Permalink
Avoid infinite loop in transitive super annotation calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Jan 10, 2024
1 parent bd027b3 commit d4cf735
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2022 Pivotal, Inc.
* Copyright (c) 2017, 2024 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -82,16 +82,16 @@ public static Set<String> getTransitiveSuperAnnotations(ITypeBinding typeBinding
}
}

public static Stream<ITypeBinding> findTransitiveSupers(ITypeBinding typeBinding, Set<String> seen) {
private static Stream<ITypeBinding> findTransitiveSupers(ITypeBinding typeBinding, Set<String> seen) {
synchronized(lock) {
String qname = typeBinding.getQualifiedName();
if (seen.add(qname)) {
return Stream.concat(
Stream.of(typeBinding),
getDirectSuperAnnotations(typeBinding).stream().flatMap(superBinding ->
findTransitiveSupers(superBinding, seen)
)
);
if (typeBinding != null) {
String qname = typeBinding.getQualifiedName();
if (seen.add(qname)) {
return Stream.concat(
Stream.of(typeBinding),
getDirectSuperAnnotations(typeBinding).stream().flatMap(superBinding -> findTransitiveSupers(superBinding, seen))
);
}
}
return Stream.empty();
}
Expand Down Expand Up @@ -153,9 +153,20 @@ public static Collection<IAnnotationBinding> getDirectSuperAnnotationBindings(IA

public static Stream<IAnnotationBinding> findTransitiveSuperAnnotationBindings(
IAnnotationBinding annotationBinding) {
return internalFindTransitiveSuperAnnotationBindings(annotationBinding, new HashSet<>());
}

public static Stream<IAnnotationBinding> internalFindTransitiveSuperAnnotationBindings(
IAnnotationBinding annotationBinding, Set<String> seen) {
synchronized (lock) {
return Stream.concat(Stream.of(annotationBinding), getDirectSuperAnnotationBindings(annotationBinding)
.stream().flatMap(superBinding -> findTransitiveSuperAnnotationBindings(superBinding)));
if (annotationBinding.getAnnotationType() != null) {
seen.add(annotationBinding.getAnnotationType().getQualifiedName());
return Stream.concat(Stream.of(annotationBinding), getDirectSuperAnnotationBindings(annotationBinding)
.stream()
.filter(superBinding -> !seen.contains(superBinding.getAnnotationType().getQualifiedName()))
.flatMap(superBinding -> internalFindTransitiveSuperAnnotationBindings(superBinding, seen)));
}
return Stream.empty();
}
}

Expand Down

0 comments on commit d4cf735

Please sign in to comment.