GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] StringToString can not detect the case: getStringMethod().toString() by Drofff · Pull Request #2659 · pmd/pmd

Conversation

@Drofff

@Drofff Drofff commented Jul 23, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Describe the PR

I've added a visit to the ASTPrimaryExpression node checking whether call to a method returning String is followed by toString(). Method call arguments are checked in order to get the actual method's return type and avoid false positives like in the example below:

public String getId(String name) {...}

public Integer getId(int position) {...}

public void foo() {
  String a = getId(0).toString(); // <-- avoiding false-positive here
}

Related issues

  • Fixes [java] StringToString can not detect the case: getStringMethod().toString() #2530

Ready?

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ghost

ghost commented Jul 23, 2020 •
edited by ghost
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
1 Message
📖 This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report
✅ This changeset introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report
✅ This changeset introduces 0 new violations, 1 new errors and 0 new configuration errors,
removes 0 violations, 2 errors and 2 configuration errors.
Full report

Generated by 🚫 Danger

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel requested changes Jul 30, 2020
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Drofff Thanks for the PR!
Could you please fix the merge conflict by rebasing or merging master into your branch? Then we get our regression tester running and see, if we introduced with these change any new false positives/false negatives.

Regarding the issue you are fixing: Be aware, that you are fixing only the case, where the called method is defined within the same class, e.g. the following case would not be covered:

public class A {
    public String methodReturningString() { return "foo"; }
}

And in another file:

public class StringToStringFN {
    public static void main(String[] args) {
        A a = new A();
        String s = a.methodReturningString().toString(); // <--- that is technically also a StringToString violation
    }
}

I don't think, we can report this case yet. It would be a a lot more effort to cover this as well (in theory, the type resolution should give you the return type of the method call, but that's not working in PMD 6.x, maybe it'll be improved with PMD 7).

I don't want to say, that you should fix this as well, but just wanting to make sure, that you are aware of this. We probably should mention this on the fixed issue, as to avoid the expectation, that all possible cases are fixed, when we close the issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Drofff
[java] StringToString can not detect the case: getStringMethod().toSt…
b48da45
…ring()
@Drofff
Drofff force-pushed the str_to_str branch 2 times, most recently from ba790dd to b48da45 Compare July 31, 2020 08:25
@adangel

adangel commented Jul 31, 2020

Copy link
Copy Markdown
Member

@Drofff There seems to be a NPE, e.g. https://travis-ci.org/github/pmd/pmd/jobs/713606711#L13483

Jul 31, 2020 8:47:58 AM net.sourceforge.pmd.RuleSet apply

WARNING: Exception applying rule StringToString on file /home/travis/build/pmd/target/repositories/spring-framework/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SessionAttributeMethodArgumentResolver.java, continuing with next rule

java.lang.NullPointerException

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.argMatchesMethodParam(StringToStringRule.java:179)

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.argNotMatchesMethodParam(StringToStringRule.java:173)

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.argsMatchMethodParams(StringToStringRule.java:165)

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.areArgsValidForMethod(StringToStringRule.java:156)

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.doesSrcMethodReturnString(StringToStringRule.java:131)

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.callsToStringOnMethodReturningString(StringToStringRule.java:122)

	at net.sourceforge.pmd.lang.java.rule.performance.StringToStringRule.visit(StringToStringRule.java:115)

	at net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression.jjtAccept(ASTPrimaryExpression.java:25)

	at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.visit(AbstractJavaRule.java:219)

	at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.visit(AbstractJavaRule.java:506)

	at net.sourceforge.pmd.lang.java.ast.ASTExpression.jjtAccept(ASTExpression.java:37)

Can you have a look? That's the reason, why the regression tester still has no results....

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Drofff
StringToString: NPE at method param type fix
32a8c04
@Drofff

Drofff commented Aug 1, 2020

Copy link
Copy Markdown
Contributor Author

@adangel thanks for your time!
I am aware of the limitations of this implementation and moreover there is an issue regarding imported classes as method parameters, which caused the NPE. I have added a javadoc comment describing these issues to the class and I hope it is clear and concise. Is it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Drofff
StringToString: return type null check
33e0239
@adangel adangel self-assigned this Aug 6, 2020
@adangel adangel added this to the 6.27.0 milestone Aug 6, 2020
@adangel adangel mentioned this pull request Aug 6, 2020
[java] StringToString can not detect the case: getStringMethod().toString() #2530
Closed
@adangel
[java] StringToString - compare subtypes of arguments/parameters
5631a72
@adangel

adangel commented Aug 6, 2020

Copy link
Copy Markdown
Member

Hi @Drofff ,
I've pushed some changes to your branch:

  • By using type resolution instead of string comparisons, we can indeed figure out, that "getData(InputStream)" is called with an argument of type "FileInputStream".
  • I've removed the collection of declared variables - all tests are still running, so I don't know the reason, why this was needed. I assume, this is not necessary. Otherwise, please add a test case.
  • I've commented the issue [java] StringToString can not detect the case: getStringMethod().toString() #2530, because I don't expect that users will read javadoc. The limitations should be mentioned where users potentially read it - and that is - if they read the release notes (where we mention the fixed issue), they might go to the issue on github and read it. But they never will look into the implementation/source code.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Drofff

Drofff commented Aug 6, 2020 •
edited by adangel
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Hello, @adangel. Thank you for your time! Your changes are constructive and meaningful. And in order to explain my previous commit, please, take a look at the test cases below:

         <test-code>
              <description>external class as a method parameter NullPointerException test</description>
              <expected-problems>1</expected-problems>
              <expected-linenumbers>8</expected-linenumbers>
              <code><![CDATA[
         import org.springframework.context.MessageSourceResolvable;
         public class Foo {
             public Long getData(User usr) {
                 return usr.getId();
             }
             public String getData(MessageSourceResolvable msr) {
                return "MSR: " + msr.toString();
             }
             public void bar() {
                 String s = getData(new MessageSourceResolvable()).toString();
             }
          }
              ]]></code>
        </test-code>

         <test-code>
              <description>variable as method call argument</description>
              <expected-problems>1</expected-problems>
              <code><![CDATA[
          public class Foo {
             public void bar() {
                User user = new User();
                String s = getId(user).toString();
             }
             public String getId(User user) {
                return "";
             }
             public int getId(Car car) {
                return 0;
             }
          }
              ]]></code>
         </test-code>

         <test-code>
              <description>method call as method call argument</description>
              <expected-problems>1</expected-problems>
              <code><![CDATA[
          public class Foo {
             public void bar() {
                String s = getData(getLong()).toString();
             }
             public long getLong() {
                return 0;
             }
             public String getData(long d) {
                return "";
             }
             public long getData(String d) {
                return 0;
             }
          }
              ]]></code>
         </test-code>

I'm sorry that I hadn't added these cases before. All the code from commit had been written to cover them. Are they correct?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Drofff and others added 2 commits August 13, 2020 15:50
@Drofff @adangel
[java] StringToString: Add more tests
7765ee1
@adangel
[java] StringToString: fix impl for method calls as args
c48f407
@adangel

adangel commented Aug 13, 2020

Copy link
Copy Markdown
Member

Thanks for the test cases, I've integrated them and fixed the implementation accordingly.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@adangel
Fix pmd, add missing test classes for typeres
278e4f3
adangel added a commit that referenced this pull request Aug 13, 2020
@adangel
[doc] Update release notes, refs #2659, fixes #2530
dbf3bbe
adangel added a commit that referenced this pull request Aug 13, 2020
@adangel
Merge branch 'pr-2659' into master
575d126
[java] StringToString can not detect the case: getStringMethod().toString() #2659
@adangel
adangel merged commit 278e4f3 into pmd:master Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel requested changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adangel adangel

Labels

None yet

Projects

None yet

Milestone

6.27.0

Development

Successfully merging this pull request may close these issues.

[java] StringToString can not detect the case: getStringMethod().toString()

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@Drofff @adangel
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.