KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > coding > NestedIfDepthCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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.coding;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
24
25 /**
26  * Restricts nested if-else blocks to a specified depth (default = 1).
27  *
28  * @author <a HREF="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
29  */

30 public final class NestedIfDepthCheck extends AbstractNestedDepthCheck
31 {
32     /** default allowed nesting depth. */
33     private static final int DEFAULT_MAX = 1;
34
35     /** Creates new check instance with default allowed nesting depth. */
36     public NestedIfDepthCheck()
37     {
38         super(DEFAULT_MAX);
39     }
40
41     /** {@inheritDoc} */
42     public int[] getDefaultTokens()
43     {
44         return new int[] {TokenTypes.LITERAL_IF};
45     }
46
47     /** {@inheritDoc} */
48     public void visitToken(DetailAST aAST)
49     {
50         switch (aAST.getType()) {
51         case TokenTypes.LITERAL_IF:
52             visitLiteralIf(aAST);
53             break;
54         default:
55             throw new IllegalStateException JavaDoc(aAST.toString());
56         }
57     }
58
59     /** {@inheritDoc} */
60     public void leaveToken(DetailAST aAST)
61     {
62         switch (aAST.getType()) {
63         case TokenTypes.LITERAL_IF:
64             leaveLiteralIf(aAST);
65             break;
66         default:
67             throw new IllegalStateException JavaDoc(aAST.toString());
68         }
69     }
70
71     /**
72      * Increases current nesting depth.
73      * @param aIf node for if.
74      */

75     private void visitLiteralIf(DetailAST aIf)
76     {
77         if (!CheckUtils.isElseIf(aIf)) {
78             nestIn(aIf, "nested.if.depth");
79         }
80     }
81
82     /**
83      * Decreases current nesting depth.
84      * @param aIf node for if.
85      */

86     private void leaveLiteralIf(DetailAST aIf)
87     {
88         if (!CheckUtils.isElseIf(aIf)) {
89             nestOut();
90         }
91     }
92 }
93
Popular Tags