From 21c76786c66c9a688e8a71898258ae61bcaec39d Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Sun, 13 Oct 2002 12:08:58 +0000 Subject: [PATCH] Implemented Other Left Curly check --- .../checks/OtherLeftCurlyCheck.java | 86 +++++++++++++++++++ .../checkstyle/OtherLeftCurlyCheckTest.java | 60 +++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/OtherLeftCurlyCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java new file mode 100644 index 000000000..56785bb85 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/OtherLeftCurlyCheck.java @@ -0,0 +1,86 @@ +//////////////////////////////////////////////////////////////////////////////// +// 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; + +public class OtherLeftCurlyCheck + extends LeftCurlyCheck +{ + /** @see Check */ + public int[] getDefaultTokens() + { + return new int[] {JavaTokenTypes.LITERAL_while, + JavaTokenTypes.LITERAL_try, + JavaTokenTypes.LITERAL_catch, + JavaTokenTypes.LITERAL_finally, + JavaTokenTypes.LITERAL_synchronized, + JavaTokenTypes.LITERAL_switch, + JavaTokenTypes.LITERAL_do, + JavaTokenTypes.LITERAL_if, + JavaTokenTypes.LITERAL_else, + }; + // STATIC_INIT + // "for" + } + + /** @see Check */ + public void visitToken(DetailAST aAST) + { + final DetailAST startToken = aAST; + final DetailAST brace; + + switch (aAST.getType()) { + case JavaTokenTypes.LITERAL_while: + case JavaTokenTypes.LITERAL_catch: + case JavaTokenTypes.LITERAL_synchronized: + brace = Utils.getLastSibling(aAST.getFirstChild()); + break; + case JavaTokenTypes.LITERAL_try: + case JavaTokenTypes.LITERAL_finally: + case JavaTokenTypes.LITERAL_do: + brace = (DetailAST) aAST.getFirstChild(); + break; + case JavaTokenTypes.LITERAL_else: + final DetailAST candidate = (DetailAST) aAST.getFirstChild(); + if (candidate.getType() == JavaTokenTypes.SLIST) { + brace = candidate; + } + else { + // silently ignore + brace = null; + } + break; + case JavaTokenTypes.LITERAL_switch: + case JavaTokenTypes.LITERAL_if: + brace = (DetailAST) aAST.getFirstChild().getNextSibling(); + break; + default: + brace = null; + } + + if (brace != null) { + verifyBrace(brace, startToken); + } + } +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/OtherLeftCurlyCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/OtherLeftCurlyCheckTest.java new file mode 100644 index 000000000..30dbc21ee --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/OtherLeftCurlyCheckTest.java @@ -0,0 +1,60 @@ +package com.puppycrawl.tools.checkstyle; + +import com.puppycrawl.tools.checkstyle.checks.OtherLeftCurlyCheck; + +public class OtherLeftCurlyCheckTest + extends BaseCheckTestCase +{ + public OtherLeftCurlyCheckTest(String aName) + { + super(aName); + } + + public void testDefault() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(OtherLeftCurlyCheck.class.getName()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputLeftCurlyOther.java"); + final String[] expected = { + "19:9: '{' should be on the previous line.", + "21:13: '{' should be on the previous line.", + "23:17: '{' should be on the previous line.", + "30:17: '{' should be on the previous line.", + "34:17: '{' should be on the previous line.", + "42:13: '{' should be on the previous line.", + "46:13: '{' should be on the previous line.", + "52:9: '{' should be on the previous line.", + "54:13: '{' should be on the previous line.", + }; + verify(c, fname, expected); + } + + public void testNL() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(OtherLeftCurlyCheck.class.getName()); + checkConfig.addProperty("option", LeftCurlyOption.NL.toString()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputLeftCurlyOther.java"); + final String[] expected = { + "26:33: '{' should be on a new line." + }; + verify(c, fname, expected); + } + + public void testIgnore() + throws Exception + { + final CheckConfiguration checkConfig = new CheckConfiguration(); + checkConfig.setClassname(OtherLeftCurlyCheck.class.getName()); + checkConfig.addProperty("option", LeftCurlyOption.IGNORE.toString()); + final Checker c = createChecker(checkConfig); + final String fname = getPath("InputLeftCurlyOther.java"); + final String[] expected = { + }; + verify(c, fname, expected); + } +}