diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FullIdent.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FullIdent.java index 21b3e25ce..e5968d891 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FullIdent.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FullIdent.java @@ -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 FullIdent 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 diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java index e1394c0a4..a14a80110 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Utils.java @@ -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; + } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParameterNumberCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParameterNumberCheck.java new file mode 100644 index 000000000..a1371122b --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParameterNumberCheck.java @@ -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)); + } + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAroundCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAroundCheck.java index a0d1bd06d..322327194 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAroundCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAroundCheck.java @@ -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 Oliver Burn + * @version 1.0 + */ public class WhitespaceAroundCheck extends Check implements Java14TokenTypes diff --git a/src/tests/com/puppycrawl/tools/checkstyle/ParameterNumberCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/ParameterNumberCheckTest.java new file mode 100644 index 000000000..a30e08dd0 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/ParameterNumberCheckTest.java @@ -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); + } +}