Skip to content

[apex] Make apex visitor generic#2661

Merged
oowekyala merged 7 commits into
pmd:pmd/7.0.xfrom
oowekyala:apex-generic-visitor
Aug 23, 2020
Merged

[apex] Make apex visitor generic#2661
oowekyala merged 7 commits into
pmd:pmd/7.0.xfrom
oowekyala:apex-generic-visitor

Conversation

@oowekyala

@oowekyala oowekyala commented Jul 23, 2020

Copy link
Copy Markdown
Member

Describe the PR

Make the apex visitor generic. This also changes some conventions:

  • The method visit(ApexNode<?> node, P data) is now called visitApexNode. This avoids overloading all the other visit methods, so that it is clearer what method gets called.

    • For example, you don't need to upcast the node to select that overload. Previously you had to call visit((ApexNode<?>) node, data), now you can call visitApexNode(node, data). You can't mess it up anymore, and mistakenly forget the cast, which would select another overload
    • Another this it's good for, is that the implementations of acceptVisitor cannot select visitApexNode by mistake now. This spots cases, where we add a node and forget to add a method to the visitor. This mistake was present for ASTApexFile (refs [apex] Make apex have a single RootNode  #2491):
      • The code for jjtAccept was copypasted and called visitor.visit(node, data).
      • There was no overload specific to ASTApexFile, so the selected overload was the default visitApexNode behavior, but this was silent.
  • Similarly, the implementation of acceptVisitor with the more specific ApexVisitor parameter is named acceptApexVisitor to avoid overload selection conflicts, that need to be resolved through casts

  • the umbrella visit methods, eg the common method to which visit(ASTUserClass and visit(ASTUserInterface delegate, is also renamed so as not to overload other visit methods. Eg here it's called visitTypeDecl.

With this scheme

  • only concrete node types have a method named exactly visit
  • for each concrete node type there's exactly one of those methods, that is applicable. So there can be no overload conflicts that need casts to be resolved
  • if there is none, and we copy-paste the acceptVisitor code, then it will fail to compile and warn us that the visitor needs a new method
  • selecting another compatible method is clear at the call site visitTypeDecl, visitApexNode, visitNode

I think this naming scheme should be used in other modules too. Especially with the newer java grammar, where there are a few more instances of visitor method delegation (eg for expressions, etc)

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by travis)
  • Added (in-code) documentation (if needed)

@oowekyala oowekyala added this to the 7.0.0 milestone Jul 23, 2020
@ghost

ghost commented Jul 24, 2020

Copy link
Copy Markdown
1 Message
📖 No java rules are changed!

Generated by 🚫 Danger

@oowekyala oowekyala marked this pull request as ready for review August 7, 2020 00:21
@adangel adangel self-requested a review August 22, 2020 11:04
/**
* @author Clément Fournier
*/
public class ApexParserVisitorReducedAdapter extends ApexParserVisitorAdapter {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we deleting this on pmd7, should we deprecate this on pmd6?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the equivalent is already deprecated in the Java module

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

adangel added a commit to adangel/pmd that referenced this pull request Aug 22, 2020
@oowekyala oowekyala merged commit da3d063 into pmd:pmd/7.0.x Aug 23, 2020
@oowekyala oowekyala deleted the apex-generic-visitor branch August 23, 2020 14:54
@adangel adangel added the in:ast About the AST structure or API, the parsing step label Jan 12, 2023
@adangel adangel mentioned this pull request Jan 23, 2023
55 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:ast About the AST structure or API, the parsing step

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants