Coverage Report - com.puppycrawl.tools.checkstyle.checks.indentation.SynchronizedHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SynchronizedHandler
100%
17/17
100%
4/4
1.2
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 // checkstyle: Checks Java source code for adherence to a set of rules.
 3  
 // Copyright (C) 2001-2017 the original author or authors.
 4  
 //
 5  
 // This library is free software; you can redistribute it and/or
 6  
 // modify it under the terms of the GNU Lesser General Public
 7  
 // License as published by the Free Software Foundation; either
 8  
 // version 2.1 of the License, or (at your option) any later version.
 9  
 //
 10  
 // This library is distributed in the hope that it will be useful,
 11  
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
 // Lesser General Public License for more details.
 14  
 //
 15  
 // You should have received a copy of the GNU Lesser General Public
 16  
 // License along with this library; if not, write to the Free Software
 17  
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  
 ////////////////////////////////////////////////////////////////////////////////
 19  
 
 20  
 package com.puppycrawl.tools.checkstyle.checks.indentation;
 21  
 
 22  
 import com.puppycrawl.tools.checkstyle.api.DetailAST;
 23  
 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
 24  
 
 25  
 /**
 26  
  * Handler for synchronized statements.
 27  
  *
 28  
  * @author liscju piotr.listkiewicz@gmail.com
 29  
  */
 30  
 public class SynchronizedHandler extends BlockParentHandler {
 31  
 
 32  
     /**
 33  
      * Determine that "synchronized" token used as modifier of method.
 34  
      */
 35  
     private final boolean methodModifier;
 36  
 
 37  
     /**
 38  
      * Construct an instance of this handler with the given indentation check,
 39  
      * name, abstract syntax tree, and parent handler.
 40  
      *
 41  
      * @param indentCheck the indentation check
 42  
      * @param ast         the abstract syntax tree
 43  
      * @param parent      the parent handler
 44  
      */
 45  
     public SynchronizedHandler(IndentationCheck indentCheck, DetailAST ast,
 46  
                                AbstractExpressionHandler parent) {
 47  2
         super(indentCheck, "synchronized", ast, parent);
 48  2
         methodModifier = isMethodModifier(ast);
 49  2
     }
 50  
 
 51  
     @Override
 52  
     public void checkIndentation() {
 53  2
         if (!methodModifier) {
 54  1
             super.checkIndentation();
 55  1
             checkSynchronizedExpr();
 56  2
             checkWrappingIndentation(getMainAst(),
 57  1
                     getSynchronizedStatementRightParen(getMainAst()));
 58  
         }
 59  2
     }
 60  
 
 61  
     /**
 62  
      * Check indentation of expression we synchronized on.
 63  
      */
 64  
     private void checkSynchronizedExpr() {
 65  1
         final DetailAST syncAst = getMainAst().findFirstToken(TokenTypes.LPAREN)
 66  1
                 .getNextSibling();
 67  1
         final IndentLevel expected =
 68  1
                 new IndentLevel(getIndent(), getBasicOffset());
 69  1
         checkExpressionSubtree(syncAst, expected, false, false);
 70  1
     }
 71  
 
 72  
     /**
 73  
      * Checks if given synchronized is modifier of method.
 74  
      * @param ast synchronized(TokenTypes.LITERAL_SYNCHRONIZED) to check
 75  
      * @return true if synchronized only modifies method
 76  
      */
 77  
     private static boolean isMethodModifier(DetailAST ast) {
 78  2
         return ast.getParent().getType() == TokenTypes.MODIFIERS;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Returns right parenthesis of synchronized statement.
 83  
      * @param syncStatementAST ast node(TokenTypes.LITERAL_SYNCHRONIZED)
 84  
      * @return right parenthesis of synchronized statement.
 85  
      */
 86  
     private static DetailAST getSynchronizedStatementRightParen(DetailAST syncStatementAST) {
 87  1
         return syncStatementAST.findFirstToken(TokenTypes.RPAREN);
 88  
     }
 89  
 }