001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2005 Oliver Burn
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: ////////////////////////////////////////////////////////////////////////////////
019: package com.puppycrawl.tools.checkstyle.checks.usage;
020:
021: import com.puppycrawl.tools.checkstyle.api.DetailAST;
022: import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023:
024: /**
025: * <p>Checks that a parameter is used.
026: * </p>
027: * <p>
028: * An example of how to configure the check is:
029: * </p>
030: * <pre>
031: * <module name="usage.UnusedParameter"/>
032: * </pre>
033: *
034: * @author Rick Giles
035: */
036: public class UnusedParameterCheck extends AbstractUsageCheck {
037: /** controls checking of catch clause parameter */
038: private boolean mIgnoreCatch = true;
039: /** controls checking of public/protected/package methods */
040: private boolean mIgnoreNonLocal;
041:
042: /** @see com.puppycrawl.tools.checkstyle.api.Check */
043: public int[] getDefaultTokens() {
044: return new int[] { TokenTypes.PARAMETER_DEF, };
045: }
046:
047: /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
048: public String getErrorKey() {
049: return "unused.parameter";
050: }
051:
052: /** @see com.puppycrawl.tools.checkstyle.checks.usage.AbstractUsageCheck */
053: public boolean mustCheckReferenceCount(DetailAST aAST) {
054: boolean result = false;
055: final DetailAST parent = aAST.getParent();
056: if (parent != null) {
057: if (parent.getType() == TokenTypes.PARAMETERS) {
058: final DetailAST grandparent = parent.getParent();
059: if (grandparent != null) {
060: result = hasBody(grandparent)
061: && (!mIgnoreNonLocal || isLocal(grandparent));
062: }
063: } else if (parent.getType() == TokenTypes.LITERAL_CATCH) {
064: result = !mIgnoreCatch;
065: }
066: }
067: return result;
068: }
069:
070: /**
071: * Determines whether an AST is a method definition with a body, or is
072: * a constructor definition.
073: * @param aAST the AST to check.
074: * @return if AST has a body.
075: */
076: private boolean hasBody(DetailAST aAST) {
077: if (aAST.getType() == TokenTypes.METHOD_DEF) {
078: return aAST.branchContains(TokenTypes.SLIST);
079: } else if (aAST.getType() == TokenTypes.CTOR_DEF) {
080: return true;
081: }
082: return false;
083: }
084:
085: /**
086: * Checks if a given method is local, i.e. either static or private.
087: * @param aAST method def for check
088: * @return true if a given method is iether static or private.
089: */
090: private boolean isLocal(DetailAST aAST) {
091: if (aAST.getType() == TokenTypes.METHOD_DEF) {
092: final DetailAST modifiers = aAST
093: .findFirstToken(TokenTypes.MODIFIERS);
094: return (modifiers == null)
095: || modifiers
096: .branchContains(TokenTypes.LITERAL_STATIC)
097: || modifiers
098: .branchContains(TokenTypes.LITERAL_PRIVATE);
099: }
100: return true;
101: }
102:
103: /**
104: * Control whether unused catch clause parameters are flagged.
105: * @param aIgnoreCatch whether unused catch clause parameters
106: * should be flagged.
107: */
108: public void setIgnoreCatch(boolean aIgnoreCatch) {
109: mIgnoreCatch = aIgnoreCatch;
110: }
111:
112: /**
113: * Controls whether public/protected/paskage methods shouldn't be checked.
114: * @param aIgnoreNonLocal whether we should check any other methods
115: * except static and private should be checked.
116: */
117: public void setIgnoreNonLocal(boolean aIgnoreNonLocal) {
118: mIgnoreNonLocal = aIgnoreNonLocal;
119: }
120:
121: }
|