Skip to content

[JAVA][FR]Support pattern matching in generated Parser code #4891

@pheonixblade9

Description

@pheonixblade9

In generated Parser code, explicit casts are used, when pattern matching has been available since JDK16: https://openjdk.org/jeps/394

For example:

    @Override
    public void enterRule(ParseTreeListener listener) {
      if (listener instanceof MyListener) ((MyListener) listener).enterSomeRule(this);
    }

could be changed to

    @Override
    public void enterRule(ParseTreeListener listener) {
      if (listener instanceof MyListener listener1) listener.enterSomeRule(this);
    }

to avoid the extra downcast.

This would definitely require testing to determine if this is actually a performance benefit, but if it's a wash, it seems reasonable to avoid extra casting when you are already checking the type via instanceof.

If nothing else, it simplifies the generated code slightly!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions