From 288d01d86c8ce976ef8a448b8d6aaa2aa3584825 Mon Sep 17 00:00:00 2001 From: Rick Giles Date: Mon, 11 Nov 2002 11:48:45 +0000 Subject: [PATCH] checks for whitespace after COMMA and TYPECAST --- .../checks/WhitespaceAfterCheck.java | 82 +++++++++++++++++++ .../checkstyle/WhitespaceAfterCheckTest.java | 41 ++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAfterCheck.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/WhitespaceAfterCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAfterCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAfterCheck.java new file mode 100644 index 000000000..9a020f336 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/WhitespaceAfterCheck.java @@ -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 Oliver Burn + * @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); + } + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/WhitespaceAfterCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/WhitespaceAfterCheckTest.java new file mode 100644 index 000000000..cfd2670b7 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/WhitespaceAfterCheckTest.java @@ -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); + } +}