Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
NestedTryDepthCheck |
|
| 1.6666666666666667;1.667 |
1 | //////////////////////////////////////////////////////////////////////////////// | |
2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
3 | // Copyright (C) 2001-2014 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 | 2 | super(DEFAULT_MAX); |
37 | 2 | } |
38 | ||
39 | @Override | |
40 | public int[] getDefaultTokens() | |
41 | { | |
42 | 2 | return new int[] {TokenTypes.LITERAL_TRY}; |
43 | } | |
44 | ||
45 | @Override | |
46 | public void visitToken(DetailAST aAST) | |
47 | { | |
48 | 20 | switch (aAST.getType()) { |
49 | case TokenTypes.LITERAL_TRY: | |
50 | 20 | visitLiteralTry(aAST); |
51 | 20 | break; |
52 | default: | |
53 | 0 | throw new IllegalStateException(aAST.toString()); |
54 | } | |
55 | 20 | } |
56 | ||
57 | @Override | |
58 | public void leaveToken(DetailAST aAST) | |
59 | { | |
60 | 20 | switch (aAST.getType()) { |
61 | case TokenTypes.LITERAL_TRY: | |
62 | 20 | leaveLiteralTry(); |
63 | 20 | break; |
64 | default: | |
65 | 0 | throw new IllegalStateException(aAST.toString()); |
66 | } | |
67 | 20 | } |
68 | ||
69 | /** | |
70 | * Increases current nesting depth. | |
71 | * @param aTry node for try. | |
72 | */ | |
73 | private void visitLiteralTry(DetailAST aTry) | |
74 | { | |
75 | 20 | nestIn(aTry, "nested.try.depth"); |
76 | 20 | } |
77 | ||
78 | /** Decreases current nesting depth */ | |
79 | private void leaveLiteralTry() | |
80 | { | |
81 | 20 | nestOut(); |
82 | 20 | } |
83 | } |