Class StringToStringRule

  • All Implemented Interfaces:
    JavaParserVisitor, ImmutableLanguage, PropertySource, Rule

    public class StringToStringRule
    extends AbstractJavaRule
    Finds toString() call on String object. Note: due to an issue with type resolution, this implementation doesn't detect cases when toString() call is chained to a method returning String which is not declared in the class having the call or the call arguments are not of the exact same type as method parameters are, excluding the case when method name and number of it's parameters is enough to identify the method. Example:
    
        class A {
             public String str() {
                return "exampleStr";
             }
        }
        class B {
            public void foo() {
                String s = new A().str().toString(); // not detected because str() is from another class
                s = getString().toString(); // detected
                s = getData(new FileInputStream()).toString(); // detected because of argument type
                s = getData(new Integer(4), new Integer(5)).toString(); // detected because of unique args count
            }
            public String getString() {
                return "exampleStr";
            }
            public String getData(InputStream is) {
                return "argsResolutionIssueExample";
            }
            public int getData(String s) {
                return 0;
            }
            public String getData(Number a, Number b) {
                return "uniqueArgsCountExample";
            }
        }