Skip to content

Commit

Permalink
Apply cursor ancestry validation to more visit calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Oct 11, 2024
1 parent 637266b commit f50d4ae
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions rewrite-core/src/main/java/org/openrewrite/TreeVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public final Cursor updateCursor(T currentValue) {
}

public @Nullable T visit(@Nullable Tree tree, P p, Cursor parent) {
if (parent.getValue() instanceof Tree && ((Tree) parent.getValue()).isScope(tree)) {
throw new IllegalArgumentException(
"The `parent` cursor must not point to the same `tree` as the tree to be visited. " +
"This usually indicates that you have used getCursor() where getCursor().getParent() is appropriate."
);
}
this.cursor = parent;
return visit(tree, p);
}
Expand All @@ -165,12 +171,7 @@ public final Cursor updateCursor(T currentValue) {
return t;
}

public T visitNonNull(Tree tree, P p, Cursor parent) {
if (parent.getValue() instanceof Tree && ((Tree) parent.getValue()).isScope(tree)) {
throw new IllegalArgumentException(
"The `parent` cursor must not point to the same `tree` as the tree to be visited"
);
}
public @NonNull T visitNonNull(Tree tree, P p, Cursor parent) {
T t = visit(tree, p, parent);
assert t != null;
return t;
Expand Down

0 comments on commit f50d4ae

Please sign in to comment.