Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java


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