checks for whitespace after COMMA and TYPECAST

This commit is contained in:
Rick Giles 2002-11-11 11:48:45 +00:00
parent 7e083cf6cb
commit 288d01d86c
2 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,82 @@
////////////////////////////////////////////////////////////////////////////////
// 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.api.Check;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.Java14TokenTypes;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
/**
* Checks for whitespace after a token.
*
* @author <a href="mailto:checkstyle@puppycrawl.com">Oliver Burn</a>
* @author Rick Giles
* @version 1.0
*/
public class WhitespaceAfterCheck
extends Check
{
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getDefaultTokens()
{
return new int[] {
TokenTypes.COMMA, // ','
};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public int[] getAcceptableTokens()
{
return new int[] {
TokenTypes.COMMA, // ','
TokenTypes.TYPECAST,
};
}
/** @see com.puppycrawl.tools.checkstyle.api.Check */
public void visitToken(DetailAST aAST)
{
final String[] lines = getLines();
final Object[] message;
final DetailAST targetAST;
if (aAST.getType() == TokenTypes.TYPECAST) {
targetAST = aAST.findFirstToken(TokenTypes.RPAREN);
// TODO: i18n
message = new Object[]{"cast"};
}
else {
targetAST = aAST;
message = new Object[]{aAST.getText()};
}
final String line = lines[targetAST.getLineNo() - 1];
final int after =
targetAST.getColumnNo() + targetAST.getText().length();
if ((after < line.length())
&& !Character.isWhitespace(line.charAt(after)))
{
log(targetAST.getLineNo(),
targetAST.getColumnNo() + targetAST.getText().length(),
"ws.notFollowed",
message);
}
}
}

View File

@ -0,0 +1,41 @@
package com.puppycrawl.tools.checkstyle;
import com.puppycrawl.tools.checkstyle.checks.WhitespaceAfterCheck;
public class WhitespaceAfterCheckTest
extends BaseCheckTestCase
{
private CheckConfiguration checkConfig;
public WhitespaceAfterCheckTest(String aName)
{
super(aName);
}
public void setUp() {
checkConfig = new CheckConfiguration();
checkConfig.setClassname(WhitespaceAfterCheck.class.getName());
}
public void testDefault() throws Exception
{
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputSimple.java");
final String[] expected = {
"42:40: ',' is not followed by whitespace.",
"71:30: ',' is not followed by whitespace.",
};
verify(c, fname, expected);
}
public void testCast() throws Exception
{
checkConfig.addTokens("TYPECAST");
final Checker c = createChecker(checkConfig);
final String fname = getPath("InputWhitespace.java");
final String[] expected = {
"88:21: 'cast' is not followed by whitespace.",
};
verify(c, fname, expected);
}
}