Class SimpleStringSplitCheck

java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.qulice.checkstyle.SimpleStringSplitCheck
All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

public final class SimpleStringSplitCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
Checks that String.split is only invoked with regex arguments that the JDK handles via its fastpath.

For anything beyond the fastpath, String.split builds a fresh Pattern on every call, which is wasteful in tight loops. Extract the regex into a private static final Pattern field and use Pattern.split(CharSequence) instead.

The JDK fastpath accepts only a one-char string whose sole character is not a regex meta character, or a two-char string whose first character is a backslash and whose second character is not an ASCII letter or digit.

Examples that are flagged:

 "abxxdexxzy".split("xx");
 "abxxdexxzy".split("xx", 1);
 "abxxdexxzy".split(".");
 

Examples that are accepted:

 "abxdexzy".split("x");
 "abxdexzy".split("x", 2);
 "abxdexzy".split("\n");
 "ab.ex.zy".split("\\.");
 

The check only reports calls whose first argument is a string literal: when the regex is a variable the optimization cannot be determined from the AST alone.

Since:
0.24
  • Nested Class Summary

    Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

    com.puppycrawl.tools.checkstyle.AbstractAutomaticBean.OutputStreamOptions
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int[]
     
    int[]
     
    int[]
     
    void
    visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
     

    Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck

    beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens

    Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

    finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity

    Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean

    configure, contextualize, getConfiguration, setupChild

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SimpleStringSplitCheck

      public SimpleStringSplitCheck()
  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Specified by:
      getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Specified by:
      getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • getRequiredTokens

      public int[] getRequiredTokens()
      Specified by:
      getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
    • visitToken

      public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
      Overrides:
      visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck