001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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.blocks;
021
022import java.util.Locale;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
029
030/**
031 * <p>
032 * Checks the placement of left curly braces.
033 * The policy to verify is specified using the {@link LeftCurlyOption} class
034 * and the default one being {@link LeftCurlyOption#EOL}.
035 * </p>
036 * <p>
037 * By default the following tokens are checked:
038 *  {@link TokenTypes#LAMBDA LAMBDA},
039 *  {@link TokenTypes#LITERAL_CASE LITERAL_CASE},
040 *  {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH},
041 *  {@link TokenTypes#LITERAL_DEFAULT LITERAL_DEFAULT},
042 *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
043 *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
044 *  {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY},
045 *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
046 *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
047 *  {@link TokenTypes#LITERAL_SWITCH LITERAL_SWITCH},
048 *  {@link TokenTypes#LITERAL_SYNCHRONIZED LITERAL_SYNCHRONIZED},
049 *  {@link TokenTypes#LITERAL_TRY LITERAL_TRY},
050 *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE},
051 *  {@link TokenTypes#STATIC_INIT STATIC_INIT}.
052 * </p>
053 *
054 * <p>
055 * The policy to verify is specified using the {@link LeftCurlyOption} class and
056 * defaults to {@link LeftCurlyOption#EOL}.
057 * </p>
058 * <p>
059 * An example of how to configure the check is:
060 * </p>
061 * <pre>
062 * &lt;module name="LeftCurly"/&gt;
063 * </pre>
064 * <p>
065 * An example of how to configure the check with policy
066 * {@link LeftCurlyOption#NLOW} is:
067 * </p>
068 * <pre>
069 * &lt;module name="LeftCurly"&gt;
070 *      &lt;property name="option" value="nlow"/&gt;
071 * &lt;/module&gt;
072 * </pre>
073 * <p>
074 * An example of how to configure the check to validate enum definitions:
075 * </p>
076 * <pre>
077 * &lt;module name="LeftCurly"&gt;
078 *      &lt;property name="ignoreEnums" value="false"/&gt;
079 * &lt;/module&gt;
080 * </pre>
081 *
082 */
083@StatelessCheck
084public class LeftCurlyCheck
085    extends AbstractCheck {
086
087    /**
088     * A key is pointing to the warning message text in "messages.properties"
089     * file.
090     */
091    public static final String MSG_KEY_LINE_NEW = "line.new";
092
093    /**
094     * A key is pointing to the warning message text in "messages.properties"
095     * file.
096     */
097    public static final String MSG_KEY_LINE_PREVIOUS = "line.previous";
098
099    /**
100     * A key is pointing to the warning message text in "messages.properties"
101     * file.
102     */
103    public static final String MSG_KEY_LINE_BREAK_AFTER = "line.break.after";
104
105    /** Open curly brace literal. */
106    private static final String OPEN_CURLY_BRACE = "{";
107
108    /** If true, Check will ignore enums. */
109    private boolean ignoreEnums = true;
110
111    /** The policy to enforce. */
112    private LeftCurlyOption option = LeftCurlyOption.EOL;
113
114    /**
115     * Set the option to enforce.
116     * @param optionStr string to decode option from
117     * @throws IllegalArgumentException if unable to decode
118     */
119    public void setOption(String optionStr) {
120        option = LeftCurlyOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
121    }
122
123    /**
124     * Sets whether check should ignore enums when left curly brace policy is EOL.
125     * @param ignoreEnums check's option for ignoring enums.
126     */
127    public void setIgnoreEnums(boolean ignoreEnums) {
128        this.ignoreEnums = ignoreEnums;
129    }
130
131    @Override
132    public int[] getDefaultTokens() {
133        return getAcceptableTokens();
134    }
135
136    @Override
137    public int[] getAcceptableTokens() {
138        return new int[] {
139            TokenTypes.ANNOTATION_DEF,
140            TokenTypes.CLASS_DEF,
141            TokenTypes.CTOR_DEF,
142            TokenTypes.ENUM_CONSTANT_DEF,
143            TokenTypes.ENUM_DEF,
144            TokenTypes.INTERFACE_DEF,
145            TokenTypes.LAMBDA,
146            TokenTypes.LITERAL_CASE,
147            TokenTypes.LITERAL_CATCH,
148            TokenTypes.LITERAL_DEFAULT,
149            TokenTypes.LITERAL_DO,
150            TokenTypes.LITERAL_ELSE,
151            TokenTypes.LITERAL_FINALLY,
152            TokenTypes.LITERAL_FOR,
153            TokenTypes.LITERAL_IF,
154            TokenTypes.LITERAL_SWITCH,
155            TokenTypes.LITERAL_SYNCHRONIZED,
156            TokenTypes.LITERAL_TRY,
157            TokenTypes.LITERAL_WHILE,
158            TokenTypes.METHOD_DEF,
159            TokenTypes.OBJBLOCK,
160            TokenTypes.STATIC_INIT,
161        };
162    }
163
164    @Override
165    public int[] getRequiredTokens() {
166        return CommonUtil.EMPTY_INT_ARRAY;
167    }
168
169    @Override
170    public void visitToken(DetailAST ast) {
171        final DetailAST startToken;
172        DetailAST brace;
173
174        switch (ast.getType()) {
175            case TokenTypes.CTOR_DEF:
176            case TokenTypes.METHOD_DEF:
177                startToken = skipModifierAnnotations(ast);
178                brace = ast.findFirstToken(TokenTypes.SLIST);
179                break;
180            case TokenTypes.INTERFACE_DEF:
181            case TokenTypes.CLASS_DEF:
182            case TokenTypes.ANNOTATION_DEF:
183            case TokenTypes.ENUM_DEF:
184            case TokenTypes.ENUM_CONSTANT_DEF:
185                startToken = skipModifierAnnotations(ast);
186                final DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);
187                brace = objBlock;
188
189                if (objBlock != null) {
190                    brace = objBlock.getFirstChild();
191                }
192                break;
193            case TokenTypes.LITERAL_WHILE:
194            case TokenTypes.LITERAL_CATCH:
195            case TokenTypes.LITERAL_SYNCHRONIZED:
196            case TokenTypes.LITERAL_FOR:
197            case TokenTypes.LITERAL_TRY:
198            case TokenTypes.LITERAL_FINALLY:
199            case TokenTypes.LITERAL_DO:
200            case TokenTypes.LITERAL_IF:
201            case TokenTypes.STATIC_INIT:
202            case TokenTypes.LAMBDA:
203                startToken = ast;
204                brace = ast.findFirstToken(TokenTypes.SLIST);
205                break;
206            case TokenTypes.LITERAL_ELSE:
207                startToken = ast;
208                brace = getBraceAsFirstChild(ast);
209                break;
210            case TokenTypes.LITERAL_CASE:
211            case TokenTypes.LITERAL_DEFAULT:
212                startToken = ast;
213                brace = getBraceAsFirstChild(ast.getNextSibling());
214                break;
215            default:
216                // ATTENTION! We have default here, but we expect case TokenTypes.METHOD_DEF,
217                // TokenTypes.LITERAL_FOR, TokenTypes.LITERAL_WHILE, TokenTypes.LITERAL_DO only.
218                // It has been done to improve coverage to 100%. I couldn't replace it with
219                // if-else-if block because code was ugly and didn't pass pmd check.
220
221                startToken = ast;
222                brace = ast.findFirstToken(TokenTypes.LCURLY);
223                break;
224        }
225
226        if (brace != null) {
227            verifyBrace(brace, startToken);
228        }
229    }
230
231    /**
232     * Gets a SLIST if it is the first child of the AST.
233     * @param ast {@code DetailAST}.
234     * @return {@code DetailAST} if the first child is {@code TokenTypes.SLIST},
235     *     {@code null} otherwise.
236     */
237    private static DetailAST getBraceAsFirstChild(DetailAST ast) {
238        DetailAST brace = null;
239        if (ast != null) {
240            final DetailAST candidate = ast.getFirstChild();
241            if (candidate != null && candidate.getType() == TokenTypes.SLIST) {
242                brace = candidate;
243            }
244        }
245        return brace;
246    }
247
248    /**
249     * Skip all {@code TokenTypes.ANNOTATION}s to the first non-annotation.
250     * @param ast {@code DetailAST}.
251     * @return {@code DetailAST}.
252     */
253    private static DetailAST skipModifierAnnotations(DetailAST ast) {
254        DetailAST resultNode = ast;
255        final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
256
257        if (modifiers != null) {
258            final DetailAST lastAnnotation = findLastAnnotation(modifiers);
259
260            if (lastAnnotation != null) {
261                if (lastAnnotation.getNextSibling() == null) {
262                    resultNode = modifiers.getNextSibling();
263                }
264                else {
265                    resultNode = lastAnnotation.getNextSibling();
266                }
267            }
268        }
269        return resultNode;
270    }
271
272    /**
273     * Find the last token of type {@code TokenTypes.ANNOTATION}
274     * under the given set of modifiers.
275     * @param modifiers {@code DetailAST}.
276     * @return {@code DetailAST} or null if there are no annotations.
277     */
278    private static DetailAST findLastAnnotation(DetailAST modifiers) {
279        DetailAST annotation = modifiers.findFirstToken(TokenTypes.ANNOTATION);
280        while (annotation != null && annotation.getNextSibling() != null
281               && annotation.getNextSibling().getType() == TokenTypes.ANNOTATION) {
282            annotation = annotation.getNextSibling();
283        }
284        return annotation;
285    }
286
287    /**
288     * Verifies that a specified left curly brace is placed correctly
289     * according to policy.
290     * @param brace token for left curly brace
291     * @param startToken token for start of expression
292     */
293    private void verifyBrace(final DetailAST brace,
294                             final DetailAST startToken) {
295        final String braceLine = getLine(brace.getLineNo() - 1);
296
297        // Check for being told to ignore, or have '{}' which is a special case
298        if (braceLine.length() <= brace.getColumnNo() + 1
299                || braceLine.charAt(brace.getColumnNo() + 1) != '}') {
300            if (option == LeftCurlyOption.NL) {
301                if (!CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
302                    log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
303                }
304            }
305            else if (option == LeftCurlyOption.EOL) {
306                validateEol(brace, braceLine);
307            }
308            else if (startToken.getLineNo() != brace.getLineNo()) {
309                validateNewLinePosition(brace, startToken, braceLine);
310            }
311        }
312    }
313
314    /**
315     * Validate EOL case.
316     * @param brace brace AST
317     * @param braceLine line content
318     */
319    private void validateEol(DetailAST brace, String braceLine) {
320        if (CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
321            log(brace, MSG_KEY_LINE_PREVIOUS, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
322        }
323        if (!hasLineBreakAfter(brace)) {
324            log(brace, MSG_KEY_LINE_BREAK_AFTER, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
325        }
326    }
327
328    /**
329     * Validate token on new Line position.
330     * @param brace brace AST
331     * @param startToken start Token
332     * @param braceLine content of line with Brace
333     */
334    private void validateNewLinePosition(DetailAST brace, DetailAST startToken, String braceLine) {
335        // not on the same line
336        if (startToken.getLineNo() + 1 == brace.getLineNo()) {
337            if (CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
338                log(brace, MSG_KEY_LINE_PREVIOUS, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
339            }
340            else {
341                log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
342            }
343        }
344        else if (!CommonUtil.hasWhitespaceBefore(brace.getColumnNo(), braceLine)) {
345            log(brace, MSG_KEY_LINE_NEW, OPEN_CURLY_BRACE, brace.getColumnNo() + 1);
346        }
347    }
348
349    /**
350     * Checks if left curly has line break after.
351     * @param leftCurly
352     *        Left curly token.
353     * @return
354     *        True, left curly has line break after.
355     */
356    private boolean hasLineBreakAfter(DetailAST leftCurly) {
357        DetailAST nextToken = null;
358        if (leftCurly.getType() == TokenTypes.SLIST) {
359            nextToken = leftCurly.getFirstChild();
360        }
361        else {
362            if (!ignoreEnums
363                    && leftCurly.getParent().getParent().getType() == TokenTypes.ENUM_DEF) {
364                nextToken = leftCurly.getNextSibling();
365            }
366        }
367        return nextToken == null
368                || nextToken.getType() == TokenTypes.RCURLY
369                || leftCurly.getLineNo() != nextToken.getLineNo();
370    }
371
372}