[java] XPath more powerful support for comparing types of nodes then typeIs function
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 5.5k
- Forks
- 1.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 54
Description
Is your feature request related to a problem? Please describe.
pmd-java typeIs() function is very useful to determine the type of a node. However, it is limited to comparing one node with a literal. Sometimes you want to compare types of two nodes, or types of two list of nodes, which is not possible now.
Describe the solution you'd like
We'd like to have more powerful support for comparing types of nodes.
Describe alternatives you've considered
Use of typeIs() is too limited.
Examples
We created a rule AvoidInfiniteRecursion, however it was really complicated to write one without being able to compare types of the formal parameters and the method call arguments, and it has false positives.
With additional attributes this would be much more easy.
Proposed new attributes: @ Type and @ MemberOfType
Example XPath:
//MethodDeclaration
(: call same method :)
//MethodCall[@MethodName = ancestor::MethodDeclaration/@Name and @MemberOfType = ancestor::ClassDeclaration[1]/@Type
and deep-equal(ArgumentList/VariableAccess/@Type = ancestor::MethodDeclaration/FormalParameters/FormalParameter/@Type)]
Difficulty to consider: the difference between typeIs and typeIsExactly. @ Type attribute should be the exact type (maybe named @ ExactType), and typeIs(node/@ Type) could be used to include super types.
So simplified like:
ArgumentList/VariableAccess[pmd-java:typeIs(FormalParameter/@Type)]
or maybe even better would be including the deep-equals and simplified usage, something like:
pmd-java:typesAre(ArgumentList/VariableAccess, ancestor::MethodDeclaration/FormalParameters/FormalParameter)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the existing pmd-java typeIs() and typeIsExactly() entry points and compare their semantics with the proposed @Type, @MemberOfType, and typesAre() forms. Use the AvoidInfiniteRecursion XPath example to define the required node and list comparisons, including exact types versus supertypes, and confirm the intended behavior before implementation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100