Skip to content

[java] Updates to AST nodes#4768

Merged
adangel merged 26 commits into
pmd:masterfrom
adangel:java-ast-updates
Jan 26, 2024
Merged

[java] Updates to AST nodes#4768
adangel merged 26 commits into
pmd:masterfrom
adangel:java-ast-updates

Conversation

@adangel

@adangel adangel commented Dec 11, 2023

Copy link
Copy Markdown
Member

Describe the PR

Finish the remaining open tasks for Java (#1307, #3751).

Note: I don't want to remove any methods for now, just deprecate them, if not already. I think, we should remove the deprecated methods only after a RC5, to have some deprecation notice. However, renaming classes definitely breaks...
Not sure, if it's worth to wait. I'll see, once I proceed with this PR.

In this case, I decided now to remove the method ASTClassDeclaration#isPackagePrivate since we renamed the class from ASTClassOrInterfaceDeclaration. So, deprecating isn't that helpful.

Tasks:

  • push down isStatic to just ASTMethodOrConstructorDeclaration, ASTFieldDeclaration and ASTAnyTypeDeclaration as it's convenient (like isAbstract)
    • Note: moved isStatic to ASTMethodDeclaration instead, as constructors can't be static
  • add a hasVisibility(Visibility) method to AccessNode, to replace the isPublic/Private/*, etc, for the convenience of java rules
  • Remove FinalizableNode, as it's not a solid abstraction, and just push down isFinal to ASTVariableDeclaratorId, ASTAnyTypeDeclaration, ASTFieldDeclaration, and ASTMethodOrConstructorDeclaration for convenience
    • Note: added to ASTAnyTypeDeclaration, ASTCatchParameter, ASTFormalParameter, ASTLambdaParameter, ASTLocalVariableDeclaration, ASTVariableDeclaratorId, ASTMethodDeclaration
  • Rename AccessNode -> ModifierOwner, including API doc in release notes
  • Rename ASTClassOrInterfaceType -> ASTClassType, including API doc in release notes
  • Rename ASTClassOrInterfaceDeclaration -> ASTClassDeclaration, including API doc in release notes
  • Rename ASTAnyTypeDeclaration -> ASTTypeDeclaration (ASTTypeDeclaration is a node in the current tree but was removed), including API doc in release notes
  • Rename ASTMethodOrConstructorDeclaration -> ASTExecutableDeclaration
    • including AbstractMethodOrConstructorDeclaration -> AbstractExecutableDeclaration
  • Rename ASTVariableDeclaratorId -> ASTVariableId
  • Rename ASTClassOrInterfaceBody -> ASTClassBody
  • Fix deprecated API usages in rules (e.g. addViolation calls, deprecated getter usages)
  • Integrate some parts of [java] Remove usages of getImage in java module #4352 - notably:
    • ASTLiteral improvements and introducing getLiteralText(). Note: Using Long.parseUnsignedLong rather than a custom implementation.
    • Expose Chars-typed attributes in XPath, so that @LiteralText is available
    • The goal is, that we don't use deprecated methods anymore, so we can delete them. It's not the goal to improve rule implementations in general.

Related issues

Ready?

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

Only on ASTMethodDeclaration, ASTFieldDeclaration and ASTAnyTypeDeclaration.
This makes it possible to remove it
from AccessNode.
Deprecate ASTClassOrInterfaceDeclaration#isPackagePrivate().
Use #hasVisibility(Visibility) instead, which can correctly
differentiate between local and package private classes.
* @PackagePrivate is deprecated now and gone
* @Static is no more deprecated for types, methods, fields
@adangel adangel added the in:ast About the AST structure or API, the parsing step label Dec 11, 2023
@adangel adangel added this to the 7.0.0 milestone Dec 11, 2023
@adangel adangel mentioned this pull request Dec 11, 2023
55 tasks
@adangel adangel changed the title [jaa] Updates to AST nodes [java] Updates to AST nodes Dec 11, 2023
@ghost

ghost commented Dec 11, 2023

Copy link
Copy Markdown
1 Message
📖 Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 10 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 10 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 10 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact
Compared to master:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
Download full report as build artifact

Generated by 🚫 Danger

@oowekyala

Copy link
Copy Markdown
Member

Thanks for starting this! I would also suggest renaming VariableDeclaratorId to VariableId since it's used relatively often in the codebase.

@adangel

adangel commented Dec 12, 2023

Copy link
Copy Markdown
Member Author

I guess, we should also rename ASTClassOrInterfaceBody to ASTClassBody. It looks odd, if we leave ASTClassOrInterfaceBody... (it's the only one with "ClassOrInterface")

adangel and others added 11 commits December 13, 2023 08:54
Also rename AbstractAnyTypeDeclaration to AbstractTypeDeclaration
…ration

Also rename AbstractMethodOrConstructorDeclaration to AbstractExecutableDeclaration
- getName() instead of getMethodName() or getVariableName()
- firstChild() instead of getFirstChildOfType()
- getRoot() instead of getFirstParentOfType(ASTCompilationUnit.class)
- children() instead of findChildrenOfType()
- no more addRuleChainVisit()
- ancestors() instead of getNthParent()
- descendants() instead of findDescendantsOfType()
- firstChild() instead of getFirstChildOfType()
- descendants() instead of findDescendantsOfType()
- ancestors() instead of getFirstParentOfType()
- This improves ASTLiteral implementation
- Adds ASTLiteral#getLiteralText() - not yet exposed as XPath attribute

Co-authored-by: Clément Fournier <clement.fournier76@gmail.com>
- taken from pmd#4352 (avoid getImage())
- Exposes @LiteralText for ASTLiteral

Co-authored-by: Clément Fournier <clement.fournier76@gmail.com>
@adangel adangel marked this pull request as ready for review December 14, 2023 14:00
@adangel

adangel commented Dec 14, 2023

Copy link
Copy Markdown
Member Author

I think, this is ready now.
It's unfortunately quite big, but the task list gives a good overview I think.

Many deprecated API usages are resolved. I integrated parts of #4352 (ASTLiteral improvements, XPath Attribute improvements).
I kept out changing getImage in general (which was started in #4352) and only fixed usage, when there is a direct
replacement (e.g. getName). Reasons:

  • I don't want to make this PR even bigger.
  • I think, we should back out the deprecation of Node::getImage and improve it later - see [core] Remove Node::getImage #4318 for that.
    There are still some issues, e.g. XPathRule requires something to get the message for reporting violations...
    without that, we can't finish it.
  • Node::getImage is not deprecated on PMD6, it was only deprecated on the PMD7 branch
  • Similar case is PropertySource::dysfunctionReason - I think, it's better to un-deprecate it and deprecate it
    again only when we have a replacement for it.

Deleting deprecations needs to be done in a separate PR (I don't want to make this even bigger).

Update: Except for release notes :) fixed issues needs to be updated Done

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

Thanks, this looks good

adangel and others added 2 commits January 12, 2024 10:30
…nal/PrettyPrintingUtil.java

Co-authored-by: Clément Fournier <clement.fournier@tu-dresden.de>
@adangel adangel merged commit 0c4b4f4 into pmd:master Jan 26, 2024
@adangel adangel deleted the java-ast-updates branch January 26, 2024 14:45
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.

[java] Rename some node types [java] AccessNode API changes

2 participants