KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > design > ThrowsCountCheck


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.design;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.Check;
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 heirachy 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 statments */
49     private int mMax;
50
51     /** Creates new instance of the check. */
52     public ThrowsCountCheck()
53     {
54         setMax(DEFAULT_MAX);
55     }
56
57     /** {@inheritDoc} */
58     public int[] getDefaultTokens()
59     {
60         return new int[] {
61             TokenTypes.LITERAL_THROWS,
62         };
63     }
64
65     /** {@inheritDoc} */
66     public int[] getRequiredTokens()
67     {
68         return getDefaultTokens();
69     }
70
71     /**
72      * Getter for max property.
73      * @return maximum allowed throws statements.
74      */

75     public int getMax()
76     {
77         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         mMax = aMax;
87     }
88
89     /** {@inheritDoc} */
90     public void visitToken(DetailAST aAST)
91     {
92         switch (aAST.getType()) {
93         case TokenTypes.LITERAL_THROWS:
94             visitLiteralThrows(aAST);
95             break;
96         default:
97             throw new IllegalStateException JavaDoc(aAST.toString());
98         }
99     }
100
101     /**
102      * Checks number of throws statments.
103      * @param aAST throws for check.
104      */

105     private void visitLiteralThrows(DetailAST aAST)
106     {
107         // Account for all the commas!
108
final int count = (aAST.getChildCount() + 1) / 2;
109         if (count > getMax()) {
110             log(aAST.getLineNo(), aAST.getColumnNo(), "throws.count",
111                 new Integer JavaDoc(count), new Integer JavaDoc(getMax()));
112         }
113     }
114 }
115
Popular Tags