Implemented the parameter number check.

Also fixed more Javadoc.
This commit is contained in:
Oliver Burn 2002-10-14 23:36:11 +00:00
parent 253994d210
commit 696e7f063f
5 changed files with 171 additions and 1 deletions

View File

@ -51,10 +51,20 @@ public class FullIdent
return mColNo;
}
/**
* Append the specified text.
* @param aText the text to append
*/
public void append(String aText)
{
mBuffer.append(".");
}
/**
* Append the specified token and also recalibrate the first line and
* column.
* @param aAST the token to append
*/
public void append(DetailAST aAST)
{
mBuffer.append(aAST.getText());
@ -73,6 +83,11 @@ public class FullIdent
}
}
/**
* Creates a new FullIdent starting from the specified node.
* @param aAST the node to start from
* @return a <code>FullIdent</code> value
*/
public static FullIdent createFullIdent(DetailAST aAST)
{
final FullIdent fi = new FullIdent();
@ -80,6 +95,12 @@ public class FullIdent
return fi;
}
/**
* Recursively extract a FullIdent.
*
* @param aFull the FullIdent to add to
* @param aAST the node to recurse from
*/
public static void extractFullIdent(FullIdent aFull, DetailAST aAST)
{
// A guard to be paranoid

View File

@ -202,4 +202,39 @@ public final class Utils
}
return (DetailAST) retVal;
}
/**
* Returns the first sibling token that makes a specified type.
* @param aFrom the token to search from
* @param aType the token type to match
* @return the matching token, or null if no match
*/
public static DetailAST findFirstToken(AST aFrom, int aType)
{
DetailAST retVal = null;
for (AST i = aFrom; i != null; i = i.getNextSibling()) {
if (i.getType() == aType) {
retVal = (DetailAST) i;
break;
}
}
return retVal;
}
/**
* Returns the number of sibling tokens that are of a specified type.
* @param aFrom the token to search from
* @param aType the token type to match
* @return the number of matching token
*/
public static int countTokens(AST aFrom, int aType)
{
int count = 0;
for (AST i = aFrom; i != null; i = i.getNextSibling()) {
if (i.getType() == aType) {
count++;
}
}
return count;
}
}

View File

@ -0,0 +1,69 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2002 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.JavaTokenTypes;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.Utils;
/**
* Check the number of parameters a method or constructor has.
*
* @author Oliver Burn
* @version 1.0
*/
public class ParameterNumberCheck
extends Check
{
/** the maximum number of allowed parameters */
private int mMax = 7;
/**
* Sets the maximum number of allowed parameters
* @param aMax the max allowed parameters
*/
public void setMax(int aMax)
{
mMax = aMax;
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {JavaTokenTypes.METHOD_DEF, JavaTokenTypes.CTOR_DEF};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final DetailAST params =
Utils.findFirstToken(aAST.getFirstChild(),
JavaTokenTypes.PARAMETERS);
final int count = Utils.countTokens(params.getFirstChild(),
JavaTokenTypes.PARAMETER_DEF);
if (count > mMax) {
final DetailAST name = Utils.findFirstToken(aAST.getFirstChild(),
JavaTokenTypes.IDENT);
log(name.getLineNo(), name.getColumnNo(),
"maxParam", new Integer(mMax));
}
}
}

View File

@ -16,13 +16,18 @@
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.Java14TokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
/**
* Checks for whitespace around a token.
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @version 1.0
*/
public class WhitespaceAroundCheck
extends Check
implements Java14TokenTypes

View File

@ -0,0 +1,40 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.ParameterNumberCheck;
public class ParameterNumberCheckTest
extends BaseCheckTestCase
{
public ParameterNumberCheckTest(String aName)
{
super(aName);
}
public void testDefault()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(ParameterNumberCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
"194:10: More than 7 parameters.",
};
verify(c, fname, expected);
}
public void testNum()
throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(ParameterNumberCheck.class.getName());
checkConfig.addProperty("max", "2");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
"71:9: More than 2 parameters.",
"194:10: More than 2 parameters.",
};
verify(c, fname, expected);
}
}