Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] XPath "//MethodCall/AmbiguousName" not match #4946

Closed
koalalam opened this issue Apr 10, 2024 · 8 comments
Closed

[java] XPath "//MethodCall/AmbiguousName" not match #4946

koalalam opened this issue Apr 10, 2024 · 8 comments
Labels
a:question Request for information that doesn't necessarily entail changes to the code base

Comments

@koalalam
Copy link

Affects PMD Version: 7.0.0

Rule: Custom XPath Rule to capture the use of old version of Apache Commons Collections

XPath 3.1

//ImportDeclaration[starts-with(@ImportedName,'org.apache.commons.collections.')]
| //MethodCall/AmbiguousName[starts-with(@Image,'org.apache.commons.collections.')]

Description:

The XPath expression works in Designer, but not in PMD.

Code Sample demonstrating the issue:

import org.apache.commons.collections.CollectionUtils;

public class Test {
  public void Test() {
    Collection<String> emptyCollection = new ArrayList<>();
    boolean good4 = org.apache.commons.collections.CollectionUtils.isEmpty(emptyCollection);  // Line 6
  }
}

Expected outcome:

PMD should report a violation at line 6 but doesn't. This is a false-negative.

Running PMD through: Gradle

@koalalam koalalam added the a:false-negative PMD doesn't flag a problematic piece of code label Apr 10, 2024
@oowekyala
Copy link
Member

You should probably write

//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.collections.CollectionUtils')]

This should also work in eg just CollectionUtils.isEmpty(...) without qualification. AmbiguousName nodes are rewritten to TypeExpression if your analysis classpath is complete (ie includes this apache library). This is probably what happens to make your expression with AmbiguousName not match.

@jsotuyod
Copy link
Member

You can set the auxclasspath on the designer to test this out from the View -> Analysis Classpath menu.

@koalalam
Copy link
Author

You should probably write

//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.collections.CollectionUtils')]

How about a prefix match against a package name?

Below works in PMD v6.x:

//ImportDeclaration[starts-with(@ImportedName,'org.apache.commons.collections.')]
| //PrimaryPrefix/Name[starts-with(@Image, 'org.apache.commons.collections.')]

Is there a way to do it in PMD v7? Thank you very much!

@oowekyala
Copy link
Member

Currently you can do that in XPath for the import but not for the method call. You can do it with a java rule though. methodCall.getOverloadSelectionInfo().getMethodType().getSymbol().getPackageName()

@koalalam
Copy link
Author

Thanks @oowekyala. Any chance that we can have function e.g. pmd-java:packageStartsWith that we can use to build xpath rule?

@oowekyala
Copy link
Member

I don't think a function would be useful here. We can easily add an attribute @PackageQualifier to ASTClassType though.

@oowekyala oowekyala changed the title XPath "//MethodCall/AmbiguousName" not match [java] XPath "//MethodCall/AmbiguousName" not match Apr 16, 2024
@oowekyala oowekyala added a:question Request for information that doesn't necessarily entail changes to the code base and removed a:false-negative PMD doesn't flag a problematic piece of code labels Apr 16, 2024
@koalalam
Copy link
Author

@oowekyala Great! I assume we can then use starts-with(@PackageQualifier, 'org.apache.commons.collections.') like we did in PMD v6.

Please advise if raising a new issue for this feature request is needed. Thanks again!

@jsotuyod
Copy link
Member

Yes, I'd recommend creating a new separate issue for that. Keeping track of all issues, when the details go into long threads is really difficult.

@jsotuyod jsotuyod closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
oowekyala added a commit to oowekyala/pmd that referenced this issue May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:question Request for information that doesn't necessarily entail changes to the code base
Projects
None yet
Development

No branches or pull requests

3 participants