KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 /**
25  * Restricts nested try-catch-finally blocks to a specified depth (default = 1).
26  * @author <a HREF="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
27  */

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

73     private void visitLiteralTry(DetailAST aTry)
74     {
75         nestIn(aTry, "nested.try.depth");
76     }
77
78     /** Decreases current nesting depth */
79     private void leaveLiteralTry()
80     {
81         nestOut();
82     }
83 }
84
Popular Tags