Strings Rules

These rules deal with different problems that can occur with String manipulation.

AvoidDuplicateLiterals

Code containing duplicate String literals can usually be improved by declaring the String as a constant field.

Here's an example of code that would trigger this rule:

 

public class Foo {
 private void bar() {
    buz("Howdy");
    buz("Howdy");
    buz("Howdy");
    buz("Howdy");
 }
 private void buz(String x) {}
}

     

StringInstantiation

Avoid instantiating String objects; this is usually unnecessary.

This rule is defined by the following XPath expression:


                   
    //AllocationExpression[Name/@Image='String'][count(.//Expression) < 2][not(ArrayDimsAndInits)]
                    
               

Here's an example of code that would trigger this rule:

 

public class Foo {
 private String bar = new String("bar"); // just do a String bar = "bar";
}

     

StringToString

Avoid calling toString() on String objects; this is unnecessary

Here's an example of code that would trigger this rule:

 

public class Foo {
 private String baz() {
  String bar = "howdy";
  return bar.toString();
 }
}