KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > j2ee > MethodChecker


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.j2ee;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25  * Root class for method checks for a client Check.
26  * @author Rick Giles
27  */

28 public abstract class MethodChecker
29 {
30     /** the client Check */
31     private final AbstractJ2eeCheck mCheck;
32
33     /**
34      * Constructs a <code>MethodChecker</code>.
35      * @param aCheck the client Check.
36      */

37     public MethodChecker(AbstractJ2eeCheck aCheck)
38     {
39         mCheck = aCheck;
40     }
41
42     /**
43      * Gets the client Check.
44      * @return the client Check.
45      */

46     protected AbstractJ2eeCheck getCheck()
47     {
48         return mCheck;
49     }
50
51     /**
52      * Checks that the methods of a component satisfy requirements.
53      * @param aAST the AST for the component definition.
54      */

55     public void checkMethods(DetailAST aAST)
56     {
57         final DetailAST objBlock = aAST.findFirstToken(TokenTypes.OBJBLOCK);
58         if (objBlock != null) {
59             DetailAST child = (DetailAST) objBlock.getFirstChild();
60             while (child != null) {
61                 if (child.getType() == TokenTypes.METHOD_DEF) {
62                     checkMethod(child);
63                 }
64                 child = (DetailAST) child.getNextSibling();
65             }
66         }
67     }
68
69     /**
70      * Checks whether a method satisfies component requirements.
71      * @param aMethodAST the AST for the method definition.
72      */

73     public abstract void checkMethod(DetailAST aMethodAST);
74
75     /**
76      * Checks whether a method satisfies public, non-static, and final
77      * requirements.
78      * @param aMethodAST AST for the method definition.
79      * @param aAllowFinal true if the method may be final.
80      */

81     public void checkMethod(
82         DetailAST aMethodAST,
83         boolean aAllowFinal)
84     {
85         // must be declared as public
86
if (!Utils.isPublic(aMethodAST)) {
87             logName(aMethodAST, "nonpublicmethod.bean", new Object JavaDoc[] {});
88         }
89
90         // declared as final?
91
if (!aAllowFinal && Utils.isFinal(aMethodAST)) {
92             logName(aMethodAST, "illegalmodifiermethod.bean",
93                 new Object JavaDoc[] {"final"});
94         }
95
96         // must not be declared as static
97
if (Utils.isStatic(aMethodAST)) {
98             logName(aMethodAST, "illegalmodifiermethod.bean",
99                 new Object JavaDoc[] {"static"});
100         }
101     }
102
103     /**
104      * Checks that the throws clause of a method definition includes an
105      * Exception.
106      * @param aMethodAST the AST for the method definition.
107      * @param aException the name of the Exception to check.
108      */

109     protected void checkThrows(DetailAST aMethodAST, String JavaDoc aException)
110     {
111         if (!Utils.hasThrows(aMethodAST, aException)) {
112             logName(aMethodAST, "missingthrows.bean",
113                 new Object JavaDoc[] {aException});
114         }
115     }
116
117     /**
118      * Checks that the throws clause of a method definition does no include an
119      * Exception.
120      * @param aMethodAST the AST for the method definition.
121      * @param aException the name of the Exception to check.
122      */

123     protected void checkNotThrows(DetailAST aMethodAST, String JavaDoc aException)
124     {
125         if (Utils.hasThrows(aMethodAST, aException)) {
126             logName(aMethodAST, "hasthrows.bean", new Object JavaDoc[] {aException});
127         }
128     }
129
130     /**
131      * Logs an error message for a method.
132      * @param aMethodAST the AST for the method definition.
133      * @param aKey key for message.
134      * @param aArgs message arguments.
135      */

136     protected void log(DetailAST aMethodAST, String JavaDoc aKey, Object JavaDoc[] aArgs)
137     {
138         mCheck.log(aMethodAST, aKey, aArgs);
139     }
140
141     /**
142      * Logs an error message for a method, including the method name.
143      * @param aMethodAST the AST for the method definition.
144      * @param aKey key for message.
145      * @param aArgs message arguments.
146      */

147     protected void logName(DetailAST aMethodAST, String JavaDoc aKey, Object JavaDoc[] aArgs)
148     {
149         mCheck.logName(aMethodAST, aKey, aArgs);
150     }
151 }
152
Popular Tags