Source code: com/puppycrawl/tools/checkstyle/checks/RightCurlyCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2002 Oliver Burn
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 package com.puppycrawl.tools.checkstyle.checks;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25 * <p>
26 * Checks the placement of right curly braces.
27 * The policy to verify is specified using the {@link RightCurlyOption} class
28 * and defaults to {@link RightCurlyOption#SAME}.
29 * </p>
30 * <p> By default the check will check the following tokens:
31 * {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH},
32 * {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
33 * {@link TokenTypes#LITERAL_TRY LITERAL_TRY}.
34 * </p>
35 * <p>
36 * An example of how to configure the check is:
37 * </p>
38 * <pre>
39 * <module name="RightCurly"/>
40 * </pre>
41 * <p>
42 * An example of how to configure the check with policy
43 * {@link RightCurlyOption#ALONE} for <code>else</code> tokens is:
44 * </p>
45 * <pre>
46 * <module name="RightCurly">
47 * <property name="tokens" value="LITERAL_ELSE"/>
48 * <property name="option" value="alone"/>
49 * </module>
50 * </pre>
51 * @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
52 * @author lkuehne
53 * @version 1.0
54 */
55 public class RightCurlyCheck
56 extends AbstractOptionCheck
57 {
58 /**
59 * Sets the right curly option to same.
60 */
61 public RightCurlyCheck()
62 {
63 super(RightCurlyOption.SAME);
64 }
65
66 /** @see com.puppycrawl.tools.checkstyle.api.Check */
67 public int[] getDefaultTokens()
68 {
69 return new int[] {TokenTypes.LITERAL_TRY,
70 TokenTypes.LITERAL_CATCH,
71 TokenTypes.LITERAL_ELSE};
72 }
73
74 /** @see com.puppycrawl.tools.checkstyle.api.Check */
75 public void visitToken(DetailAST aAST)
76 {
77 // Attempt to locate the tokens to do the check
78 DetailAST rcurly = null;
79 DetailAST nextToken = null;
80 if (aAST.getType() == TokenTypes.LITERAL_ELSE) {
81 nextToken = aAST;
82 final DetailAST thenAST = aAST.getPreviousSibling();
83 rcurly = thenAST.getLastChild();
84 }
85 else if (aAST.getType() == TokenTypes.LITERAL_CATCH) {
86 nextToken = (DetailAST) aAST.getNextSibling();
87 rcurly = aAST.getLastChild().getLastChild();
88 }
89 else if (aAST.getType() == TokenTypes.LITERAL_TRY) {
90 DetailAST firstChild = (DetailAST) aAST.getFirstChild();
91 nextToken = (DetailAST) firstChild.getNextSibling();
92 rcurly = firstChild.getLastChild();
93 }
94
95 // If have both tokens, perform the check
96 if ((rcurly != null) && (nextToken != null)) {
97 if ((getAbstractOption() == RightCurlyOption.SAME)
98 && (rcurly.getLineNo() != nextToken.getLineNo()))
99 {
100 log(rcurly.getLineNo(), rcurly.getColumnNo(),
101 "line.same", "}");
102 }
103 else if ((getAbstractOption() == RightCurlyOption.ALONE)
104 && (rcurly.getLineNo() == nextToken.getLineNo()))
105 {
106 log(rcurly.getLineNo(), rcurly.getColumnNo(),
107 "line.alone", "}");
108 }
109 }
110 }
111 }