KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > UnusedParameterCheck


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.usage;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24
25 /**
26  * <p>Checks that a parameter is used.
27  * </p>
28  * <p>
29  * An example of how to configure the check is:
30  * </p>
31  * <pre>
32  * &lt;module name="usage.UnusedParameter"/&gt;
33  * </pre>
34  *
35  * @author Rick Giles
36  */

37 public class UnusedParameterCheck extends AbstractUsageCheck
38 {
39     /** controls checking of catch clause parameter */
40     private boolean mIgnoreCatch = true;
41     /** controls checking of public/protected/package methods */
42     private boolean mIgnoreNonLocal;
43
44     /** @see com.puppycrawl.tools.checkstyle.api.Check */
45     public int[] getDefaultTokens()
46     {
47         return new int[] {
48             TokenTypes.PARAMETER_DEF,
49         };
50     }
51
52     /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
53     public String JavaDoc getErrorKey()
54     {
55         return "unused.parameter";
56     }
57
58     /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
59     public boolean mustCheckReferenceCount(DetailAST aAST)
60     {
61         boolean result = false;
62         final DetailAST parent = aAST.getParent();
63         if (parent != null) {
64             if (parent.getType() == TokenTypes.PARAMETERS) {
65                 final DetailAST grandparent = parent.getParent();
66                 if (grandparent != null) {
67                     result = hasBody(grandparent)
68                         && (!mIgnoreNonLocal || isLocal(grandparent));
69                 }
70             }
71             else if (parent.getType() == TokenTypes.LITERAL_CATCH) {
72                 result = !mIgnoreCatch;
73             }
74         }
75         return result;
76     }
77
78     /**
79      * Determines whether an AST is a method definition with a body, or is
80      * a constructor definition.
81      * @param aAST the AST to check.
82      * @return if AST has a body.
83      */

84     private boolean hasBody(DetailAST aAST)
85     {
86         if (aAST.getType() == TokenTypes.METHOD_DEF) {
87             return aAST.branchContains(TokenTypes.SLIST);
88         }
89         else if (aAST.getType() == TokenTypes.CTOR_DEF) {
90             return true;
91         }
92         return false;
93     }
94
95     /**
96      * Checks if a given method is local, i.e. either static or private.
97      * @param aAST method def for check
98      * @return true if a given method is iether static or private.
99      */

100     private boolean isLocal(DetailAST aAST)
101     {
102         if (aAST.getType() == TokenTypes.METHOD_DEF) {
103             final DetailAST modifiers =
104                 aAST.findFirstToken(TokenTypes.MODIFIERS);
105             return (modifiers == null)
106                 || modifiers.branchContains(TokenTypes.LITERAL_STATIC)
107                 || modifiers.branchContains(TokenTypes.LITERAL_PRIVATE);
108         }
109         return true;
110     }
111
112     /**
113      * Control whether unused catch clause parameters are flagged.
114      * @param aIgnoreCatch whether unused catch clause parameters
115      * should be flagged.
116      */

117     public void setIgnoreCatch(boolean aIgnoreCatch)
118     {
119         mIgnoreCatch = aIgnoreCatch;
120     }
121
122     /**
123      * Controls whether public/protected/paskage methods shouldn't be checked.
124      * @param aIgnoreNonLocal whether we should check any other methods
125      * except static and private should be checked.
126      */

127     public void setIgnoreNonLocal(boolean aIgnoreNonLocal)
128     {
129         mIgnoreNonLocal = aIgnoreNonLocal;
130     }
131
132 }
133
Popular Tags