Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ThrowsCountCheck |
|
| 1.4285714285714286;1.429 |
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.design; | |
20 | ||
21 | import com.puppycrawl.tools.checkstyle.api.Check; | |
22 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
23 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
24 | ||
25 | /** | |
26 | * <p> | |
27 | * Restricts throws statements to a specified count (default = 1). | |
28 | * </p> | |
29 | * <p> | |
30 | * Rationale: | |
31 | * Exceptions form part of a methods interface. Declaring | |
32 | * a method to throw too many differently rooted | |
33 | * exceptions makes exception handling onerous and leads | |
34 | * to poor programming practices such as catch | |
35 | * (Exception). This check forces developers to put | |
36 | * exceptions into a hierarchy such that in the simplest | |
37 | * case, only one type of exception need be checked for by | |
38 | * a caller but allows any sub-classes to be caught | |
39 | * specifically if necessary. | |
40 | * </p> | |
41 | * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> | |
42 | */ | |
43 | public final class ThrowsCountCheck extends Check | |
44 | { | |
45 | /** default value of max property */ | |
46 | private static final int DEFAULT_MAX = 1; | |
47 | ||
48 | /** maximum allowed throws statements */ | |
49 | private int mMax; | |
50 | ||
51 | /** Creates new instance of the check. */ | |
52 | public ThrowsCountCheck() | |
53 | 2 | { |
54 | 2 | setMax(DEFAULT_MAX); |
55 | 2 | } |
56 | ||
57 | @Override | |
58 | public int[] getDefaultTokens() | |
59 | { | |
60 | 2 | return new int[] { |
61 | TokenTypes.LITERAL_THROWS, | |
62 | }; | |
63 | } | |
64 | ||
65 | @Override | |
66 | public int[] getRequiredTokens() | |
67 | { | |
68 | 0 | return getDefaultTokens(); |
69 | } | |
70 | ||
71 | /** | |
72 | * Getter for max property. | |
73 | * @return maximum allowed throws statements. | |
74 | */ | |
75 | public int getMax() | |
76 | { | |
77 | 14 | return mMax; |
78 | } | |
79 | ||
80 | /** | |
81 | * Setter for max property. | |
82 | * @param aMax maximum allowed throws statements. | |
83 | */ | |
84 | public void setMax(int aMax) | |
85 | { | |
86 | 3 | mMax = aMax; |
87 | 3 | } |
88 | ||
89 | @Override | |
90 | public void visitToken(DetailAST aAST) | |
91 | { | |
92 | 10 | switch (aAST.getType()) { |
93 | case TokenTypes.LITERAL_THROWS: | |
94 | 10 | visitLiteralThrows(aAST); |
95 | 10 | break; |
96 | default: | |
97 | 0 | throw new IllegalStateException(aAST.toString()); |
98 | } | |
99 | 10 | } |
100 | ||
101 | /** | |
102 | * Checks number of throws statements. | |
103 | * @param aAST throws for check. | |
104 | */ | |
105 | private void visitLiteralThrows(DetailAST aAST) | |
106 | { | |
107 | // Account for all the commas! | |
108 | 10 | final int count = (aAST.getChildCount() + 1) / 2; |
109 | 10 | if (count > getMax()) { |
110 | 4 | log(aAST.getLineNo(), aAST.getColumnNo(), "throws.count", |
111 | count, getMax()); | |
112 | } | |
113 | 10 | } |
114 | } |