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/NeedBracesCheck.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  
20  package com.puppycrawl.tools.checkstyle.checks.blocks;
21  
22  
23  import com.puppycrawl.tools.checkstyle.api.Check;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  
27  /**
28   * <p>
29   * Checks for braces around code blocks.
30   * </p>
31   * <p> By default the check will check the following blocks:
32   *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
33   *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
34   *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
35   *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
36   *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE}.
37   * </p>
38   * <p>
39   * An example of how to configure the check is:
40   * </p>
41   * <pre>
42   * &lt;module name="NeedBraces"/&gt;
43   * </pre>
44   * <p> An example of how to configure the check for <code>if</code> and
45   * <code>else</code> blocks is:
46   * <pre>
47   * &lt;module name="NeedBraces"&gt;
48   *     &lt;property name="tokens" value="LITERAL_IF, LITERAL_ELSE"/&gt;
49   * &lt;/module&gt;
50   * </pre>
51   * @author Rick Giles
52   * @version 1.0
53   */
54  public class NeedBracesCheck
55      extends Check
56  {
57      /** @see com.puppycrawl.tools.checkstyle.api.Check */
58      public int[] getDefaultTokens()
59      {
60          return new int[] {
61              TokenTypes.LITERAL_DO,
62              TokenTypes.LITERAL_ELSE,
63              TokenTypes.LITERAL_FOR,
64              TokenTypes.LITERAL_IF,
65              TokenTypes.LITERAL_WHILE,
66          };
67      }
68  
69      /** @see com.puppycrawl.tools.checkstyle.api.Check */
70      public void visitToken(DetailAST aAST)
71      {
72          final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);
73          boolean isElseIf = false;
74          if ((aAST.getType() == TokenTypes.LITERAL_ELSE)
75              && (aAST.findFirstToken(TokenTypes.LITERAL_IF) != null))
76          {
77              isElseIf = true;
78          }
79          if ((slistAST == null) && !isElseIf) {
80              log(aAST.getLineNo(), "needBraces", aAST.getText());
81          }
82      }
83  }