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