Skip to content

[java] Rework AccessNode#2259

Merged
oowekyala merged 4 commits into
pmd:java-grammarfrom
oowekyala:grammar-modifier-node
Feb 7, 2020
Merged

[java] Rework AccessNode#2259
oowekyala merged 4 commits into
pmd:java-grammarfrom
oowekyala:grammar-modifier-node

Conversation

@oowekyala

Copy link
Copy Markdown
Member
  • Introduce an enum to represent modifiers (JModifier)
  • Introduce a ModifierList node, that ranges over the modifiers of a declaration (including annotations). This has methods to query which JModifiers were present, and which are here implicitly.
  • Declarations always have a ModifierList as a child, even if no explicit modifiers are mentioned. The node is implicit in these cases, which is why this PR needed [core] Merge JavaCC build scripts #2211
  • This allows removing the methods isSyntactically* on ASTFieldDeclaration, ASTMethodDeclaration, which were inconsistent and don't account for all modifiers. Now you can just call eg node.hasExplicitModifier(JModifier.PUBLIC). Things like field.isInterfaceMember() can be replaced with field.getEnclosingType().isInterface().
  • This also makes AccessNode trivial to implement, so we can remove some base classes. ASTAnonymousClassDeclaration can also finally extend AbstractAnyTypeDeclaration, which fixes [java] Add new node for anonymous class declaration #905 for good

The implementation of AST-based symbols depends on this PR.

Introduce a ModifierList node, that ranges
 over the modifiers of a declaration (including
annotations).

This is a combination of a few old commits:

Figure out modifiers

Fix tests

Remove AccessTypeNode

Document

Remove specific methods

Fix symboltable test

Fix tests

Rename to JModifier

Fix copypaste default/abstract

Improve doc

Test anon classes

Remove useless impl

Static modifier should not be present on toplevel classes

Simplify impl

Add visibility enum

Port some tests

Fix test ranges

Fix modifier ordering

Cleanup

Fix unnecessary modifier rule

Rename to use plural

Improve visibility doc

Simplify some things

Checkstyle

Remove some usages of typekind

Fix missing import

Remove irrelevant method

REmove some duplication

Replace AccessNode with ModifierOwner

Rename to AccessNode to reduce diff

Remove changes to rules

Add convenience methods

Make VariableDeclaratorId a ModifierOwner

Fix variable name decl

Make enum constant an implicit AccessNode

Fix compil

Checkstyle

Cleanup

Deprecate TypeKind

Cleanup

Remove TypeKind

Revert "Remove TypeKind"

This reverts commit 222c169c3401a01507726f339ae9f9b2b20dc69a.

Fix doc

Fix UnnecessaryModifierRule

Use special node instead of ModifierSet

Remove useless tests

Fix tests WIP

Work should be resumed when pmd#2211 is merged into java-grammar

Fix some tests

Doc
@oowekyala oowekyala added this to the 7.0.0 milestone Jan 28, 2020
Javacc nodes now use this convention,
and since this PR updates the test
module to take implicit nodes into
account, the scala module needs to
be ported too.

Refs pmd#2021
@ghost

ghost commented Jan 28, 2020

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

Generated by 🚫 Danger

@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. 👍

Two points:

return super.isPublic();
}

public boolean isSyntacticallyStatic() {

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.

Is there anything we can provide on master? The methods isSyntactically* will be gone, but deprecation doesn't make sense IMHO unless we can tell, what to do instead....

@oowekyala

Copy link
Copy Markdown
Member Author

Does this solve/address #1307 already?

Not really, though it gives a clearer path to address it.

Basically, the point of #1307 is that nodes like FormalParameter should not have methods like isPublic/isSynchronized, which are meaningless. This PR gives an ASTModifierList to every "declaration" node, including FormalParameter, LocalVariableDeclaration, etc. So it looks like it has exactly the same problem... Except we don't have to keep the method isPublic on those nodes, even though it's technically possible to write hasModifier(PUBLIC). This would also remove the XPath attributes

So, to me, #1307 will be fixed when those methods of AccessNode are removed, and their usages replaced with calls to hasModifiers. For many of them we can readily do so, but I excluded them from the PR to reduce the diff.

Note that it probably would be nice to keep shorthands like isPublic, isPrivate, for the nodes for which it makes sense. We don't need those in the XPath API though, and they could be tagged with @NoAttribute.

About migration from master:

We could move JModifier to master, and add the methods hasModifier and hasExplicitModifier to AccessNode. This would work, but we have to also take care of the XPath API, otherwise XPath users would see deprecated attributes only

Ideally in the XPath API, AccessNodes have the attributes @Modifiers and @ExplicitModifiers, whose value is a sequence of strings, eg ('public', 'static'). But they don't have boolean attributes like @Public, @Static, @SyntacticallyPublic, etc. To do so, ideally we'd start by implementing #2172 (comment) on master

@oowekyala

Copy link
Copy Markdown
Member Author

We probably should add an example to https://github.com/pmd/pmd/wiki/Java_clean_changes to show, how the modifiers are now represented in the AST (via a own virtual node).

We also need to update https://github.com/pmd/pmd/wiki/Java_clean_changes#annotation-nesting because now annotations are a child of the ModifierList node

@oowekyala oowekyala merged commit e3200a8 into pmd:java-grammar Feb 7, 2020
@oowekyala oowekyala deleted the grammar-modifier-node branch February 7, 2020 13:36
@adangel adangel added the in:ast About the AST structure or API, the parsing step label Jan 13, 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