Class ConsecutiveLiteralAppendsRule

  • All Implemented Interfaces:
    AstVisitor<Object,​Object>, JavaParserVisitor, JavaVisitor<Object,​Object>, PropertySource, Rule

    public class ConsecutiveLiteralAppendsRule
    extends AbstractJavaRulechainRule
    This rule finds concurrent calls to StringBuffer/Builder.append where String literals are used. It would be much better to make these calls using one call to .append.

    Example:

     StringBuilder buf = new StringBuilder();
     buf.append("Hello");
     buf.append(" ").append("World");
     

    This would be more eloquently put as:

     StringBuilder buf = new StringBuilder();
     buf.append("Hello World");
     

    The rule takes one parameter, threshold, which defines the lower limit of consecutive appends before a violation is created. The default is 1.