Class AvoidNotShortCircuitOperatorsForBooleanCheck

  • All Implemented Interfaces:
    com.puppycrawl.tools.checkstyle.api.Configurable, com.puppycrawl.tools.checkstyle.api.Contextualizable

    public class AvoidNotShortCircuitOperatorsForBooleanCheck
    extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
    This check limits using of not short-circuit operators ("|", "&", "|=", "&=") in boolean expressions.
    Reason:
          Short-circuit operators ("||", "&&") are more safer and can accelerate the evaluation of complex boolean expressions. Check identifies an expression as a boolean if it contains at least one boolean operand or if result of expression evaluation sets the value of a boolean variable.

         Using boolean variables that do not belong to the current class and all calls to boolean methods are not handled by this check.

    Examples:

    1. Using of not short-circuit operators while determining a Boolean variable
     boolean x = true;
     boolean result = true | x || false; // a warning here
     
    2. Using of not short-circuit operators while overriding a Boolean variable.
     boolean x = true;
     boolean result = false;
     // any code
     result &= true | x || false; // a warning here
     
    3. Expression calculated with not short-circuit operators contains at least one boolean operand.
     public boolean isTrue() {
         return this.z | MyObject.is() // no warnings here
                 || isModifier() && isNotTrue();
     }
     ...
     boolean r=true;
     public boolean isTrue() {
         return this.z | true && r // a warning here
                 || isModifier() && isNotTrue();
     }
     
    Since:
    1.8.0
    Author:
    Daniil Yaroslavtsev
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

        com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String MSG_KEY
      A key to search the warning message text in "messages.properties" file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getAcceptableTokens()  
      int[] getDefaultTokens()  
      int[] getRequiredTokens()  
      List<String> getSupportedOperandsNames​(com.puppycrawl.tools.checkstyle.api.DetailAST exprParentAST)
      Searches for all supported operands names in current expression.
      boolean hasTrueOrFalseLiteral​(com.puppycrawl.tools.checkstyle.api.DetailAST parentAST)
      Checks is the current expression has keywords "true" or "false".
      boolean isBooleanExpression​(com.puppycrawl.tools.checkstyle.api.DetailAST node)
      Checks that current expression is calculated using "|", "&", "|=", "&=" operators contains at least one Boolean operand.
      void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)  
      • 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.api.AutomaticBean

        configure, contextualize, getConfiguration, setupChild
    • Field Detail

      • MSG_KEY

        public static final String MSG_KEY
        A key to search the warning message text in "messages.properties" file.
        See Also:
        Constant Field Values
    • Constructor Detail

      • AvoidNotShortCircuitOperatorsForBooleanCheck

        public AvoidNotShortCircuitOperatorsForBooleanCheck()
    • Method Detail

      • getDefaultTokens

        public final 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 final void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST detailAST)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • isBooleanExpression

        public final boolean isBooleanExpression​(com.puppycrawl.tools.checkstyle.api.DetailAST node)
        Checks that current expression is calculated using "|", "&", "|=", "&=" operators contains at least one Boolean operand.
        Parameters:
        node - - current TokenTypes.EXPR node to check.
        Returns:
        "true" if current expression is calculated using "|", "&", "|=". "&=" operators contains at least one Boolean operand or false otherwise.
      • getSupportedOperandsNames

        public final List<String> getSupportedOperandsNames​(com.puppycrawl.tools.checkstyle.api.DetailAST exprParentAST)
        Searches for all supported operands names in current expression. When checking, treatments to external class variables, method calls, etc are not considered as expression operands.
        Parameters:
        exprParentAST - - the current TokenTypes.EXPR parent node.
        Returns:
        List of supported operands contained in current expression.
      • hasTrueOrFalseLiteral

        public final boolean hasTrueOrFalseLiteral​(com.puppycrawl.tools.checkstyle.api.DetailAST parentAST)
        Checks is the current expression has keywords "true" or "false".
        Parameters:
        parentAST - - the current TokenTypes.EXPR parent node.
        Returns:
        true if the current processed expression contains "true" or "false" keywords and false otherwise.