Fixed bug in MethodLengthCheck where it was assuming that a method definition

had to have braces. Not the case for interfaces and abstract methods. Need to
make the same change to the other method check.
This commit is contained in:
Oliver Burn 2002-11-03 02:14:07 +00:00
parent ce177456e7
commit ba8a851e90
3 changed files with 28 additions and 16 deletions

View File

@ -28,10 +28,12 @@
<!--
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodLeftCurlyCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodLengthCheck"/>
-->
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodLengthCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.MethodNameCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.ModifierCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.NeedBracesCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.NoWhiteSpaceAroundCheck"/>
<check classname="com.puppycrawl.tools.checkstyle.checks.OtherLeftCurlyCheck">
<set-property name="option" value="nlow"/>

View File

@ -29,19 +29,19 @@ public class MethodLengthCheck extends Check
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
if (aAST.getType() != TokenTypes.METHOD_DEF) {
return;
}
DetailAST openingBrace = aAST.getLastChild();
DetailAST closingBrace = openingBrace.getLastChild();
int methodBodyStart = openingBrace.getLineNo();
int methodBodyEnd = closingBrace.getLineNo();
int length = methodBodyEnd - methodBodyStart + 1;
if (length > mMax) {
// TODO: This is old style but shouldn'r we use aAST.getLineNo() ?
log(openingBrace.getLineNo(), "maxLen.method",
new Integer(length), new Integer(mMax));
final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
if (openingBrace != null) {
final DetailAST closingBrace =
openingBrace.findFirstToken(TokenTypes.RCURLY);
final int length =
closingBrace.getLineNo() - openingBrace.getLineNo() + 1;
if (length > mMax) {
log(aAST.getLineNo(),
aAST.getColumnNo(),
"maxLen.method",
new Integer(length),
new Integer(mMax));
}
}
}

View File

@ -16,7 +16,6 @@
// 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;
import com.puppycrawl.tools.checkstyle.checks.MethodLengthCheck;
@ -36,7 +35,18 @@ public class MethodLengthCheckTest extends BaseCheckTestCase
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
"80: Method length is 20 lines (max allowed is 19)."
"79:5: Method length is 20 lines (max allowed is 19)."
};
verify(c, fname, expected);
}
public void testAbstract() throws Exception
{
final CheckConfiguration checkConfig = new CheckConfiguration();
checkConfig.setClassname(MethodLengthCheck.class.getName());
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputModifier.java");
final String[] expected = {
};
verify(c, fname, expected);
}