001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.indentation;
021
022import com.puppycrawl.tools.checkstyle.api.DetailAST;
023import com.puppycrawl.tools.checkstyle.api.TokenTypes;
024
025/**
026 * Handler for synchronized statements.
027 *
028 * @author liscju piotr.listkiewicz@gmail.com
029 */
030public class SynchronizedHandler extends BlockParentHandler {
031
032    /**
033     * Determine that "synchronized" token used as modifier of method.
034     */
035    private final boolean methodModifier;
036
037    /**
038     * Construct an instance of this handler with the given indentation check,
039     * name, abstract syntax tree, and parent handler.
040     *
041     * @param indentCheck the indentation check
042     * @param ast         the abstract syntax tree
043     * @param parent      the parent handler
044     */
045    public SynchronizedHandler(IndentationCheck indentCheck, DetailAST ast,
046                               AbstractExpressionHandler parent) {
047        super(indentCheck, "synchronized", ast, parent);
048        methodModifier = isMethodModifier(ast);
049    }
050
051    @Override
052    public void checkIndentation() {
053        if (!methodModifier) {
054            super.checkIndentation();
055            checkSynchronizedExpr();
056            final LineWrappingHandler lineWrap =
057                    new LineWrappingHandler(getIndentCheck(), getMainAst(),
058                            getSynchronizedStatementRightParen(getMainAst()));
059            lineWrap.checkIndentation();
060        }
061    }
062
063    /**
064     * Check indentation of expression we synchronized on.
065     */
066    private void checkSynchronizedExpr() {
067        final DetailAST syncAst = getMainAst().findFirstToken(TokenTypes.LPAREN)
068                .getNextSibling();
069        final IndentLevel expected =
070                new IndentLevel(getLevel(), getBasicOffset());
071        checkExpressionSubtree(syncAst, expected, false, false);
072    }
073
074    /**
075     * Checks if given synchronized is modifier of method*
076     * @param ast synchronized(TokenTypes.LITERAL_SYNCHRONIZED) to check
077     * @return true if synchronized only modifies method
078     */
079    private static boolean isMethodModifier(DetailAST ast) {
080        return ast.getParent().getType() == TokenTypes.MODIFIERS;
081    }
082
083    /**
084     * Returns right parenthesis of synchronized statement.
085     * @param syncStatementAST ast node(TokenTypes.LITERAL_SYNCHRONIZED)
086     * @return right parenthesis of synchronized statement.
087     */
088    private static DetailAST getSynchronizedStatementRightParen(DetailAST syncStatementAST) {
089        return syncStatementAST.findFirstToken(TokenTypes.RPAREN);
090    }
091}