Source code: com/puppycrawl/tools/checkstyle/checks/usage/UnusedParameterCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 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 * <module name="usage.UnusedParameter"/>
33 * </pre>
34 * <p>
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
42 /** @see com.puppycrawl.tools.checkstyle.api.Check */
43 public int[] getDefaultTokens()
44 {
45 return new int[] {
46 TokenTypes.PARAMETER_DEF,
47 };
48 }
49
50 /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
51 public String getErrorKey()
52 {
53 return "unused.parameter";
54 }
55
56 /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
57 public boolean mustCheckReferenceCount(DetailAST aAST)
58 {
59 boolean result = false;
60 final DetailAST parent = aAST.getParent();
61 if (parent != null) {
62 if (parent.getType() == TokenTypes.PARAMETERS) {
63 final DetailAST grandparent = parent.getParent();
64 if (grandparent != null) {
65 result = hasBody(grandparent);
66 }
67 }
68 else if (parent.getType() == TokenTypes.LITERAL_CATCH) {
69 result = !mIgnoreCatch;
70 }
71 }
72 return result;
73 }
74
75 /**
76 * Determines whether an AST is a method definition with a body, or is
77 * a constructor definition.
78 * @param aAST the AST to check.
79 * @return if AST has a body.
80 */
81 private boolean hasBody(DetailAST aAST)
82 {
83 if (aAST.getType() == TokenTypes.METHOD_DEF) {
84 return aAST.branchContains(TokenTypes.SLIST);
85 }
86 else if (aAST.getType() == TokenTypes.CTOR_DEF) {
87 return true;
88 }
89 return false;
90 }
91
92 /**
93 * Control whether unused catch clause parameters are flagged.
94 * @param aIgnoreCatch whether unused catch clause parameters
95 * should be flagged.
96 */
97 public void setIgnoreCatch(boolean aIgnoreCatch)
98 {
99 mIgnoreCatch = aIgnoreCatch;
100 }
101
102 }