001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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            checkWrappingIndentation(getMainAst(),
057                    getSynchronizedStatementRightParen(getMainAst()));
058        }
059    }
060
061    /**
062     * Check indentation of expression we synchronized on.
063     */
064    private void checkSynchronizedExpr() {
065        final DetailAST syncAst = getMainAst().findFirstToken(TokenTypes.LPAREN)
066                .getNextSibling();
067        final IndentLevel expected =
068                new IndentLevel(getIndent(), getBasicOffset());
069        checkExpressionSubtree(syncAst, expected, false, false);
070    }
071
072    /**
073     * Checks if given synchronized is modifier of method.
074     * @param ast synchronized(TokenTypes.LITERAL_SYNCHRONIZED) to check
075     * @return true if synchronized only modifies method
076     */
077    private static boolean isMethodModifier(DetailAST ast) {
078        return ast.getParent().getType() == TokenTypes.MODIFIERS;
079    }
080
081    /**
082     * Returns right parenthesis of synchronized statement.
083     * @param syncStatementAST ast node(TokenTypes.LITERAL_SYNCHRONIZED)
084     * @return right parenthesis of synchronized statement.
085     */
086    private static DetailAST getSynchronizedStatementRightParen(DetailAST syncStatementAST) {
087        return syncStatementAST.findFirstToken(TokenTypes.RPAREN);
088    }
089
090}