KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > FinalParametersCheck


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;
20
21 import com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24
25 /**
26  * Check that method/constructor/catch/foreach parameters are final.
27  * The user can set the token set to METHOD_DEF, CONSTRUCTOR_DEF,
28  * LITERAL_CATCH, FOR_EACH_CLAUSE or any combination of these token
29  * types, to control the scope of this check.
30  * Default scope is both METHOD_DEF and CONSTRUCTOR_DEF.
31  *
32  * @author lkuehne
33  * @author o_sukhodolsky
34  * @author Michael Studman
35  */

36 public class FinalParametersCheck extends Check
37 {
38     /** {@inheritDoc} */
39     public int[] getDefaultTokens()
40     {
41         return new int[] {
42             TokenTypes.METHOD_DEF,
43             TokenTypes.CTOR_DEF,
44         };
45     }
46
47     /** {@inheritDoc} */
48     public int[] getAcceptableTokens()
49     {
50         return new int[] {
51             TokenTypes.METHOD_DEF,
52             TokenTypes.CTOR_DEF,
53             TokenTypes.LITERAL_CATCH,
54             TokenTypes.FOR_EACH_CLAUSE,
55         };
56     }
57
58     /** {@inheritDoc} */
59     public void visitToken(DetailAST aAST)
60     {
61         // don't flag interfaces
62
final DetailAST container = aAST.getParent().getParent();
63         if (container.getType() == TokenTypes.INTERFACE_DEF) {
64             return;
65         }
66
67         if (aAST.getType() == TokenTypes.LITERAL_CATCH) {
68             visitCatch(aAST);
69         }
70         else if (aAST.getType() == TokenTypes.FOR_EACH_CLAUSE) {
71             visitForEachClause(aAST);
72         }
73         else {
74             visitMethod(aAST);
75         }
76     }
77
78     /**
79      * Checks parameters of the method or ctor.
80      * @param aMethod method or ctor to check.
81      */

82     private void visitMethod(final DetailAST aMethod)
83     {
84         // exit on fast lane if there is nothing to check here
85
if (!aMethod.branchContains(TokenTypes.PARAMETER_DEF)) {
86             return;
87         }
88
89         // ignore abstract method
90
final DetailAST modifiers =
91             aMethod.findFirstToken(TokenTypes.MODIFIERS);
92         if (modifiers.branchContains(TokenTypes.ABSTRACT)) {
93             return;
94         }
95
96         // we can now be sure that there is at least one parameter
97
final DetailAST parameters =
98             aMethod.findFirstToken(TokenTypes.PARAMETERS);
99         DetailAST child = (DetailAST) parameters.getFirstChild();
100         while (child != null) {
101             // childs are PARAMETER_DEF and COMMA
102
if (child.getType() == TokenTypes.PARAMETER_DEF) {
103                 checkParam(child);
104             }
105             child = (DetailAST) child.getNextSibling();
106         }
107     }
108
109     /**
110      * Checks parameter of the catch block.
111      * @param aCatch catch block to check.
112      */

113     private void visitCatch(final DetailAST aCatch)
114     {
115         checkParam(aCatch.findFirstToken(TokenTypes.PARAMETER_DEF));
116     }
117
118     /**
119      * Checks parameter of the for each clause.
120      * @param aForEachClause for each clause to check.
121      */

122     private void visitForEachClause(final DetailAST aForEachClause)
123     {
124         checkParam(aForEachClause.findFirstToken(TokenTypes.VARIABLE_DEF));
125     }
126
127     /**
128      * Checks if the given parameter is final.
129      * @param aParam parameter to check.
130      */

131     private void checkParam(final DetailAST aParam)
132     {
133         if (!aParam.branchContains(TokenTypes.FINAL)) {
134             final DetailAST paramName = aParam.findFirstToken(TokenTypes.IDENT);
135             final DetailAST firstNode = CheckUtils.getFirstNode(aParam);
136             log(firstNode.getLineNo(), firstNode.getColumnNo(),
137                 "final.parameter", paramName.getText());
138         }
139     }
140 }
141
Popular Tags