From f9b6da329f47d1b4fa6becd259f1c5aaa96cd540 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 19 Jun 2014 17:37:46 +0400 Subject: [PATCH] NoLineWrap #173 --- .../checks/whitespace/NoLineWrapCheck.java | 95 +++++++++++++++++++ .../checkstyle/checks/messages.properties | 1 + .../checks/whitespace/messages.properties | 2 + .../whitespace/NoLineWrapCheckTest.java | 61 ++++++++++++ .../whitespace/NoLineWrapBadInput.java | 27 ++++++ .../whitespace/NoLineWrapGoodInput.java | 13 +++ src/xdocs/availablechecks.xml | 5 + src/xdocs/config_whitespace.xml | 91 ++++++++++++++++++ 8 files changed, 295 insertions(+) create mode 100644 src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheck.java create mode 100644 src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheckTest.java create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapBadInput.java create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapGoodInput.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheck.java new file mode 100644 index 000000000..46154170b --- /dev/null +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheck.java @@ -0,0 +1,95 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2014 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.whitespace; + +import com.puppycrawl.tools.checkstyle.api.Check; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; + +/** + *

Checks that chosen statements are not line-wrapped. + * By default this Check restricts wrapping import and package statements, + * but it's possible to check any statement. + *

+ *

Examples

+ *

+ * + * Examples of line-wrapped statements (bad case): + *

 package com.puppycrawl.
+ *    tools.checkstyle.checks;
+ *
+ * import com.puppycrawl.tools.
+ *    checkstyle.api.Check;
+ * 
+ * + *

+ * To configure the check to force no line-wrapping + * in package and import statements (default values): + *

+ *
+ * <module name="NoLineWrap"/>
+ * 
+ * + *

+ * To configure the check to force no line-wrapping only + * in import statements: + *

+ *
+ * <module name="NoLineWrap">
+ *     <property name="tokens" value="IMPORT">
+ * </module>
+ * 
+ * + * Examples of not line-wrapped statements (good case): + *
 import com.puppycrawl.tools.checkstyle.api.Check;
+ * 
+ * + * @author maxvetrenko + */ +public class NoLineWrapCheck extends Check +{ + + @Override + public int[] getDefaultTokens() + { + return new int[] {TokenTypes.PACKAGE_DEF, TokenTypes.IMPORT}; + } + + @Override + public int[] getAcceptableTokens() + { + return new int[] { + TokenTypes.IMPORT, + TokenTypes.PACKAGE_DEF, + TokenTypes.CLASS_DEF, + TokenTypes.METHOD_DEF, + TokenTypes.CTOR_DEF, + TokenTypes.ENUM_DEF, + TokenTypes.INTERFACE_DEF, + }; + } + + @Override + public void visitToken(DetailAST aAST) + { + if (aAST.getLineNo() != aAST.getLastChild().getLineNo()) { + log(aAST.getLineNo(), "no.line.wrap", aAST.getText()); + } + } +} diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties index 4e297ad1d..db1070429 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -30,3 +30,4 @@ type.file.mismatch=The name of the outer type and the file do not match. properties.duplicateproperty=Duplicated property ''{0}'' ({1} occurrence(s)). unable.open.cause=Unable to open ''{0}'': {1}. + diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/messages.properties index 0b3e65839..40dbc49da 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/whitespace/messages.properties @@ -6,6 +6,8 @@ line.new=''{0}'' should be on a new line. line.previous=''{0}'' should be on the previous line. line.same=''{0}'' should be on the same line. +no.line.wrap={0} statement should not be line-wrapped. + ws.followed=''{0}'' is followed by whitespace. ws.notFollowed=''{0}'' is not followed by whitespace. ws.notPreceded=''{0}'' is not preceded with whitespace. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheckTest.java new file mode 100644 index 000000000..a4562d119 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoLineWrapCheckTest.java @@ -0,0 +1,61 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2014 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.whitespace; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import org.junit.Test; + +public class NoLineWrapCheckTest + extends BaseCheckTestSupport +{ + @Test + public void testCaseWithoutLineWrapping() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); + final String[] expected = {}; + verify(checkConfig, getPath("whitespace/NoLineWrapGoodInput.java"), expected); + } + + @Test + public void testDefaultTokensLineWrapping() throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); + final String[] expected = { + "1: package statement should not be line-wrapped.", + "6: import statement should not be line-wrapped.", + }; + verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected); + } + + @Test + public void testCustomTokensLineWrapping() + throws Exception + { + final DefaultConfiguration checkConfig = createCheckConfig(NoLineWrapCheck.class); + checkConfig.addAttribute("tokens", "IMPORT, CLASS_DEF, METHOD_DEF, ENUM_DEF"); + final String[] expected = { + "6: import statement should not be line-wrapped.", + "10: CLASS_DEF statement should not be line-wrapped.", + "13: METHOD_DEF statement should not be line-wrapped.", + "20: ENUM_DEF statement should not be line-wrapped.", + }; + verify(checkConfig, getPath("whitespace/NoLineWrapBadInput.java"), expected); + } +} diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapBadInput.java b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapBadInput.java new file mode 100644 index 000000000..85457e179 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapBadInput.java @@ -0,0 +1,27 @@ +package com.puppycrawl.tools. + checkstyle; + +import com.google.common.annotations.Beta; + +import javax.accessibility. + AccessibleAttributeSequence; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +public class + BadLineWrapInput { + + public void + fooMethod() { + final int + foo = 0; + } +} + +enum + FooFoo { +} + +interface + InterFoo {} + + diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapGoodInput.java b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapGoodInput.java new file mode 100644 index 000000000..d78bd4fe1 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/whitespace/NoLineWrapGoodInput.java @@ -0,0 +1,13 @@ +package com.puppycrawl.tools.checkstyle; + +import com.google.common.annotations.Beta; + +import javax.accessibility.AccessibleAttributeSequence; +import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; + +public class GoodLineWrapInput { + + public void fooMethod() { + // + } +} diff --git a/src/xdocs/availablechecks.xml b/src/xdocs/availablechecks.xml index b3f590680..f75d26716 100644 --- a/src/xdocs/availablechecks.xml +++ b/src/xdocs/availablechecks.xml @@ -461,6 +461,11 @@ Checks that no method having zero parameters is defined using the name finalize. + + NoLineWrap + + Checks that chosen statements are not line-wrapped. + NoWhitespaceAfter diff --git a/src/xdocs/config_whitespace.xml b/src/xdocs/config_whitespace.xml index fd4de0b6e..37a7fcea8 100644 --- a/src/xdocs/config_whitespace.xml +++ b/src/xdocs/config_whitespace.xml @@ -1171,5 +1171,96 @@ public void func() {} // empty method

+ +
+ +

+ Checks that chosen statements are not line-wrapped. By default this + Check restricts wrapping import and package statements, but it's possible to check + any statement. +

+
+ + + + + + + + + + + + + + + +
namedescriptiontypedefault value
tokensassignments to checksubset of tokens PACKAGE_DEF, + IMPORT, + CLASS_DEF, + ENUM_DEF + INTERFACE_DEF, + CTOR_DEF, + METHOD_DEF, + PACKAGE_DEF, + IMPORT
+
+ + +

+ Examples of line-wrapped statements (bad case): +

+ +package com.puppycrawl. + tools.checkstyle.checks; + +import com.puppycrawl.tools. + checkstyle.api.Check; + +

+ To configure the check to force no line-wrapping + in package and import statements (default values): +

+ +<module name="NoLineWrap"/> + +

+ To configure the check to force no line-wrapping only + in import statements: +

+ +<module name="NoLineWrap"> + <property name="tokens" value="IMPORT"> +</module> + +

+ Examples of not line-wrapped statements (good case): +

+ +import com.puppycrawl.tools.checkstyle.api.Check; + +
+ + +

+ com.puppycrawl.tools.checkstyle.checks +

+
+ + +

+ TreeWalker +

+
+