Skip to content

Commit

Permalink
Fix StackOverflow on cyclic references involving collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
xRodney committed Oct 20, 2022
1 parent 3365ffc commit 9c75743
Showing 1 changed file with 7 additions and 6 deletions.
Expand Up @@ -33,7 +33,7 @@ public class ClassDependenciesVisitor extends TypedVisitor<TypeDefBuilder> {
private static final Map<String, Set<String>> traversedClasses = new HashMap<>();
private static final Map<String, Set<String>> crdNameToCrClass = new HashMap<>();
private final Set<String> classesForCR;
private final Set<String> processed = new HashSet<>();
private final Set<ClassRef> processed = new HashSet<>();

public ClassDependenciesVisitor(String crClassName, String crdName) {
// need to record all classes associated with the different versions of the CR (not the CRD spec)
Expand All @@ -49,7 +49,7 @@ public void visit(TypeDefBuilder builder) {
// note that we cannot simply check the traversed class set to know if a class has been processed because classes
// are usually added to the traversed set before they're looked at in depth
final String className = type.getFullyQualifiedName();
if (ignore(className) || processed.contains(className)) {
if (ignore(className)) {
return;
}

Expand All @@ -74,9 +74,6 @@ public void visit(TypeDefBuilder builder) {

// add classes from extends list
type.getExtendsList().forEach(this::processTypeRef);

// add class to the processed classes
processed.add(className);
}

private boolean ignore(String className) {
Expand All @@ -86,7 +83,11 @@ private boolean ignore(String className) {
private void processTypeRef(TypeRef t) {
if (t instanceof ClassRef) {
ClassRef classRef = (ClassRef) t;
visit(new TypeDefBuilder(Types.typeDefFrom(classRef)));
// only process the class reference if we haven't already
// note that the references are stored in the set including type arguments, so List<A> and List<B> are not the same
if (processed.add(classRef)) {
visit(new TypeDefBuilder(Types.typeDefFrom(classRef)));
}
}
}

Expand Down

0 comments on commit 9c75743

Please sign in to comment.