Skip to content

Commit

Permalink
Cleanup visitors doc for rewrite8
Browse files Browse the repository at this point in the history
I didn't really see many changes that needed to be made here and
I'm unsure if that's because none of this stuff changed or if we
are missing some key pieces.

I did find a few minor cleanup pieces and I did call out that one
of the examples doesn't work. I think I'll need some help on that
front.
  • Loading branch information
mike-solomon committed Jun 21, 2023
1 parent 5ce6120 commit a19195a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions concepts-and-explanations/visitors.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ As an example of how the `Cursor` can be helpful, imagine a visitor that is task

```java
@Override
public J visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext context) {
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext context) {
// The base class provides the language-specific navigation of sub-elements
// Without this invocation, sub-elements such as inner classes will never be visited
J.ClassDeclaration classDeclaration = (J.ClassDeclaration) super.visitClassDeclaration(cd, context);
Expand Down Expand Up @@ -80,9 +80,9 @@ Each `Cursor` within the stack has a `Map` into which arbitrary data may be read
For example, imagine you wanted to make a visitor which needs to change to a class declaration based on something it finds within a method declaration. You could freely add information to the cursor without worry that this would affect any other visitors or recipes:

```java
public class ChangesClassBasedOnMethod extends JavaVisitor<ExecutionContext> {
public class ChangesClassBasedOnMethod extends JavaIsoVisitor<ExecutionContext> {
@Override
public J visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext ctx) {
// Traverses down into sub-elements of the current class declaration
J.ClassDeclaration classDeclaration = (J.ClassDeclaration) super.visitClassDeclaration(cd, ctx);

Expand All @@ -95,7 +95,7 @@ public class ChangesClassBasedOnMethod extends JavaVisitor<ExecutionContext> {
}

@Override
public J visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) {
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) {
if (/* methodDeclaration meets some criteria */) {
// Puts the message on the cursor corresponding to the element this message will be read from
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "FOUND_METHOD", methodDeclaration);
Expand Down Expand Up @@ -164,6 +164,8 @@ There are some cases, though, where you may want to share other types of informa

In those cases, you can specify a different, mutable type for your visitor such as in this visitor which counts the number of method invocations in a Java LST:

TODO: This does not work for me. I get errors about `static` declarations not being allowed in inner classes and also that the visitor can't be referenced from a static context.

```java
public class MethodCountVisitor extends JavaIsoVisitor<AtomicInteger> {
@Override
Expand Down

0 comments on commit a19195a

Please sign in to comment.