boolean (ClassNode root, ClassNode child) { Set<String> setEnclosing = child.enclosingClasses; LinkedList<ClassNode> stack = new LinkedList<>(); stack.add(root); while (!stack.isEmpty()) { ClassNode node = stack.removeFirst(); if (setEnclosing.contains(node.classStruct.qualifiedName)) { node.nested.add(child); child.parent = node; return true; } // note: ordered list stack.addAll(node.nested); } return false; }