From aede54ee77fb0887148db30be804b36758111698 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Sun, 13 Oct 2002 14:05:42 +0000 Subject: [PATCH] Fully implemented the paren pad check --- .../checkstyle/checks/ParenPadCheck.java | 60 ++++++++++++++++- .../tools/checkstyle/ParenPadCheckTest.java | 65 +++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/ParenPadCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java index 654339e60..549c6878f 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/ParenPadCheck.java @@ -20,12 +20,17 @@ package com.puppycrawl.tools.checkstyle.checks; import com.puppycrawl.tools.checkstyle.JavaTokenTypes; +import com.puppycrawl.tools.checkstyle.PadOption; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.Utils; +import org.apache.commons.beanutils.ConversionException; public class ParenPadCheck extends Check { + private PadOption mOption = PadOption.NOSPACE; + /** @see com.puppycrawl.tools.checkstyle.api.Check */ public int[] getDefaultTokens() { @@ -41,6 +46,59 @@ public class ParenPadCheck /** @see com.puppycrawl.tools.checkstyle.api.Check */ public void visitToken(DetailAST aAST) { - // TODO: implement + if (aAST.getType() == JavaTokenTypes.RPAREN) { + processRight(aAST); + } + else { + processLeft(aAST); + } + } + + public void setOption(String aOption) + { + mOption = PadOption.decode(aOption); + if (mOption == null) { + throw new ConversionException("unable to parse " + aOption); + } + } + + private void processLeft(DetailAST aAST) + { + final String line = getLines()[aAST.getLineNo() - 1]; + final int after = aAST.getColumnNo() + 1; + if (after < line.length()) { + if ((PadOption.NOSPACE == mOption) + && (Character.isWhitespace(line.charAt(after)))) + { + log(aAST.getLineNo(), after, "ws.followed", "("); + } + else if ((PadOption.SPACE == mOption) + && !Character.isWhitespace(line.charAt(after)) + && (line.charAt(after) != ')')) + { + log(aAST.getLineNo(), after, "ws.notFollowed", "("); + } + } + } + + private void processRight(DetailAST aAST) + { + final String line = getLines()[aAST.getLineNo() - 1]; + final int before = aAST.getColumnNo() - 1; + if (before >= 0) { + if ((PadOption.NOSPACE == mOption) + && Character.isWhitespace(line.charAt(before)) + && !Utils.whitespaceBefore(before, line)) + { + log(aAST.getLineNo(), before, "ws.preceeded", ")"); + } + else if ((PadOption.SPACE == mOption) + && !Character.isWhitespace(line.charAt(before)) + && (line.charAt(before) != '(')) + { + log(aAST.getLineNo(), aAST.getColumnNo(), + "ws.notPreceeded", ")"); + } + } } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/ParenPadCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/ParenPadCheckTest.java new file mode 100644 index 000000000..e21ea5f76 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/ParenPadCheckTest.java @@ -0,0 +1,65 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.ParenPadCheck; + +public class ParenPadCheckTest + extends BaseCheckTestCase +{ + public ParenPadCheckTest(String aName) + { + super(aName); + } + + public void testDefault() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(ParenPadCheck.class.getName()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputWhitespace.java"); + final String[] expected = { + "58:12: '(' is followed by whitespace.", + "58:36: ')' is preceeded with whitespace.", + "74:13: '(' is followed by whitespace.", + "74:18: ')' is preceeded with whitespace.", + }; + verify(c, fname, expected); + } + + public void testSpace() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(ParenPadCheck.class.getName()); + checkConfig.addProperty("option", PadOption.SPACE.toString()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputWhitespace.java"); + final String[] expected = { + "29:20: '(' is not followed by whitespace.", + "29:23: ')' is not preceeded with whitespace.", + "37:22: '(' is not followed by whitespace.", + "37:26: ')' is not preceeded with whitespace.", + "41:15: '(' is not followed by whitespace.", + "41:33: ')' is not preceeded with whitespace.", + "76:20: '(' is not followed by whitespace.", + "76:21: ')' is not preceeded with whitespace.", + "87:21: '(' is not followed by whitespace.", + "87:27: ')' is not preceeded with whitespace.", + "88:14: '(' is not followed by whitespace.", + "88:20: ')' is not preceeded with whitespace.", + "89:14: '(' is not followed by whitespace.", + "89:20: ')' is not preceeded with whitespace.", + "90:14: '(' is not followed by whitespace.", + "90:20: ')' is not preceeded with whitespace.", + "97:22: '(' is not followed by whitespace.", + "97:28: ')' is not preceeded with whitespace.", + "98:14: '(' is not followed by whitespace.", + "98:18: ')' is not preceeded with whitespace.", + "150:28: '(' is not followed by whitespace.", + "150:32: ')' is not preceeded with whitespace.", + "153:16: '(' is not followed by whitespace.", + "153:20: ')' is not preceeded with whitespace.", + }; + verify(c, fname, expected); + } +}