From f9131f9536c71d963c83aab1550b7a0371aa9ff5 Mon Sep 17 00:00:00 2001 From: HuGanghui Date: Thu, 19 Mar 2020 11:39:00 +0800 Subject: [PATCH 01/17] doc: fix typo in config_whitespace.xml --- src/xdocs/config_whitespace.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xdocs/config_whitespace.xml b/src/xdocs/config_whitespace.xml index 65292f052f76..12ac8424575b 100644 --- a/src/xdocs/config_whitespace.xml +++ b/src/xdocs/config_whitespace.xml @@ -1280,7 +1280,7 @@ public void foo(final char @NotNull [] param) {} // No violation GENERIC_END, ELLIPSIS, - + METHOD_REF. From 38a84fc4152f5a60db2161332c8df35ce7375d61 Mon Sep 17 00:00:00 2001 From: Gaurav Punjabi Date: Thu, 19 Mar 2020 14:25:46 +0530 Subject: [PATCH 02/17] Issue #7750: Update AbstractChecks to log DetailAST - NeedBraces --- .../rule411bracesareused/NeedBracesTest.java | 76 +++---- .../XpathRegressionNeedBracesTest.java | 127 ++++++++++++ ...uppressionXpathRegressionNeedBracesDo.java | 15 ++ ...pathRegressionNeedBracesEmptyLoopBody.java | 11 ++ ...onXpathRegressionNeedBracesSingleLine.java | 20 ++ ...hRegressionNeedBracesSingleLineLambda.java | 6 + .../checks/blocks/NeedBracesCheck.java | 2 +- .../filters/SuppressionXpathFilter.java | 3 - .../checks/blocks/NeedBracesCheckTest.java | 186 +++++++++--------- .../internal/XpathRegressionTest.java | 1 - src/xdocs/config_filters.xml | 1 - 11 files changed, 311 insertions(+), 137 deletions(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNeedBracesTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesDo.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesEmptyLoopBody.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLine.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLineLambda.java diff --git a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule411bracesareused/NeedBracesTest.java b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule411bracesareused/NeedBracesTest.java index 62b75cee9314..ff9a84374628 100644 --- a/src/it/java/com/google/checkstyle/test/chapter4formatting/rule411bracesareused/NeedBracesTest.java +++ b/src/it/java/com/google/checkstyle/test/chapter4formatting/rule411bracesareused/NeedBracesTest.java @@ -38,44 +38,44 @@ public void testNeedBraces() throws Exception { final String messageKey = "needBraces"; final String[] expected = { - "29: " + getCheckMessage(clazz, messageKey, "do"), - "41: " + getCheckMessage(clazz, messageKey, "while"), - "42: " + getCheckMessage(clazz, messageKey, "while"), - "44: " + getCheckMessage(clazz, messageKey, "while"), - "45: " + getCheckMessage(clazz, messageKey, "if"), - "58: " + getCheckMessage(clazz, messageKey, "for"), - "59: " + getCheckMessage(clazz, messageKey, "for"), - "61: " + getCheckMessage(clazz, messageKey, "for"), - "63: " + getCheckMessage(clazz, messageKey, "if"), - "82: " + getCheckMessage(clazz, messageKey, "if"), - "83: " + getCheckMessage(clazz, messageKey, "if"), - "85: " + getCheckMessage(clazz, messageKey, "if"), - "87: " + getCheckMessage(clazz, messageKey, "else"), - "89: " + getCheckMessage(clazz, messageKey, "if"), - "97: " + getCheckMessage(clazz, messageKey, "else"), - "99: " + getCheckMessage(clazz, messageKey, "if"), - "100: " + getCheckMessage(clazz, messageKey, "if"), - "126: " + getCheckMessage(clazz, messageKey, "while"), - "129: " + getCheckMessage(clazz, messageKey, "do"), - "135: " + getCheckMessage(clazz, messageKey, "if"), - "138: " + getCheckMessage(clazz, messageKey, "if"), - "139: " + getCheckMessage(clazz, messageKey, "else"), - "144: " + getCheckMessage(clazz, messageKey, "for"), - "147: " + getCheckMessage(clazz, messageKey, "for"), - "157: " + getCheckMessage(clazz, messageKey, "while"), - "160: " + getCheckMessage(clazz, messageKey, "do"), - "166: " + getCheckMessage(clazz, messageKey, "if"), - "169: " + getCheckMessage(clazz, messageKey, "if"), - "170: " + getCheckMessage(clazz, messageKey, "else"), - "175: " + getCheckMessage(clazz, messageKey, "for"), - "178: " + getCheckMessage(clazz, messageKey, "for"), - "189: " + getCheckMessage(clazz, messageKey, "while"), - "192: " + getCheckMessage(clazz, messageKey, "do"), - "198: " + getCheckMessage(clazz, messageKey, "if"), - "201: " + getCheckMessage(clazz, messageKey, "if"), - "202: " + getCheckMessage(clazz, messageKey, "else"), - "207: " + getCheckMessage(clazz, messageKey, "for"), - "210: " + getCheckMessage(clazz, messageKey, "for"), + "29:9: " + getCheckMessage(clazz, messageKey, "do"), + "41:9: " + getCheckMessage(clazz, messageKey, "while"), + "42:9: " + getCheckMessage(clazz, messageKey, "while"), + "44:9: " + getCheckMessage(clazz, messageKey, "while"), + "45:13: " + getCheckMessage(clazz, messageKey, "if"), + "58:9: " + getCheckMessage(clazz, messageKey, "for"), + "59:9: " + getCheckMessage(clazz, messageKey, "for"), + "61:9: " + getCheckMessage(clazz, messageKey, "for"), + "63:13: " + getCheckMessage(clazz, messageKey, "if"), + "82:9: " + getCheckMessage(clazz, messageKey, "if"), + "83:9: " + getCheckMessage(clazz, messageKey, "if"), + "85:9: " + getCheckMessage(clazz, messageKey, "if"), + "87:9: " + getCheckMessage(clazz, messageKey, "else"), + "89:9: " + getCheckMessage(clazz, messageKey, "if"), + "97:9: " + getCheckMessage(clazz, messageKey, "else"), + "99:9: " + getCheckMessage(clazz, messageKey, "if"), + "100:13: " + getCheckMessage(clazz, messageKey, "if"), + "126:9: " + getCheckMessage(clazz, messageKey, "while"), + "129:9: " + getCheckMessage(clazz, messageKey, "do"), + "135:9: " + getCheckMessage(clazz, messageKey, "if"), + "138:9: " + getCheckMessage(clazz, messageKey, "if"), + "139:9: " + getCheckMessage(clazz, messageKey, "else"), + "144:9: " + getCheckMessage(clazz, messageKey, "for"), + "147:9: " + getCheckMessage(clazz, messageKey, "for"), + "157:13: " + getCheckMessage(clazz, messageKey, "while"), + "160:13: " + getCheckMessage(clazz, messageKey, "do"), + "166:13: " + getCheckMessage(clazz, messageKey, "if"), + "169:13: " + getCheckMessage(clazz, messageKey, "if"), + "170:13: " + getCheckMessage(clazz, messageKey, "else"), + "175:13: " + getCheckMessage(clazz, messageKey, "for"), + "178:13: " + getCheckMessage(clazz, messageKey, "for"), + "189:13: " + getCheckMessage(clazz, messageKey, "while"), + "192:13: " + getCheckMessage(clazz, messageKey, "do"), + "198:13: " + getCheckMessage(clazz, messageKey, "if"), + "201:13: " + getCheckMessage(clazz, messageKey, "if"), + "202:13: " + getCheckMessage(clazz, messageKey, "else"), + "207:13: " + getCheckMessage(clazz, messageKey, "for"), + "210:13: " + getCheckMessage(clazz, messageKey, "for"), }; final Configuration checkConfig = getModuleConfig("NeedBraces"); diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNeedBracesTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNeedBracesTest.java new file mode 100644 index 000000000000..5fc571acd9c0 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionNeedBracesTest.java @@ -0,0 +1,127 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2020 the original author or authors. +// +// 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 org.checkstyle.suppressionxpathfilter; + +import static com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck.MSG_KEY_NEED_BRACES; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck; + +public class XpathRegressionNeedBracesTest extends AbstractXpathTestSupport { + private final String checkName = NeedBracesCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testDo() throws Exception { + final File fileToProcess = new File(getPath( + "SuppressionXpathRegressionNeedBracesDo.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(NeedBracesCheck.class); + + final String[] expectedViolation = { + "13:9: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "do"), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNeedBracesDo']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_DO" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testSingleLine() throws Exception { + final File fileToProcess = new File(getPath( + "SuppressionXpathRegressionNeedBracesSingleLine.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(NeedBracesCheck.class); + moduleConfig.addAttribute("allowSingleLineStatement", "true"); + + final String[] expectedViolation = { + "16:9: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "if"), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNeedBracesSingleLine']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testSingleLineLambda() throws Exception { + final File fileToProcess = new File(getPath( + "SuppressionXpathRegressionNeedBracesSingleLineLambda.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(NeedBracesCheck.class); + moduleConfig.addAttribute("tokens", "LAMBDA"); + moduleConfig.addAttribute("allowSingleLineStatement", "true"); + + final String[] expectedViolation = { + "4:29: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "->"), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNeedBracesSingleLineLambda']]" + + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='r3']]/ASSIGN/LAMBDA" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testEmptyLoopBody() throws Exception { + final File fileToProcess = new File(getPath( + "SuppressionXpathRegressionNeedBracesEmptyLoopBody.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(NeedBracesCheck.class); + + final String[] expectedViolation = { + "9:9: " + getCheckMessage(NeedBracesCheck.class, MSG_KEY_NEED_BRACES, "while"), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionNeedBracesEmptyLoopBody']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_WHILE" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesDo.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesDo.java new file mode 100644 index 000000000000..c210f82cc86e --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesDo.java @@ -0,0 +1,15 @@ +package org.checkstyle.suppressionxpathfilter.needbraces; + +public class SuppressionXpathRegressionNeedBracesDo { + /** @return helper func **/ + boolean condition() + { + return false; + } + + /** Test do/while loops **/ + public void test() { + // Invalid + do test(); while (condition()); // warn + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesEmptyLoopBody.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesEmptyLoopBody.java new file mode 100644 index 000000000000..185130872fb8 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesEmptyLoopBody.java @@ -0,0 +1,11 @@ +package org.checkstyle.suppressionxpathfilter.needbraces; + +public class SuppressionXpathRegressionNeedBracesEmptyLoopBody { + private int incrementValue() { + return 1; + } + + public void test() { + while(incrementValue() < 5);; // warn + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLine.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLine.java new file mode 100644 index 000000000000..ae011c01eb78 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLine.java @@ -0,0 +1,20 @@ +package org.checkstyle.suppressionxpathfilter.needbraces; + +import com.puppycrawl.tools.checkstyle.checks.blocks.needbraces.InputNeedBracesSingleLineStatements; + +public class SuppressionXpathRegressionNeedBracesSingleLine { + private static class SomeClass { + boolean flag = true; + private static boolean test(boolean k) { + return k; + } + } + + /** Test do/while loops **/ + public int test() { + // Invalid + if (SomeClass.test(true) == true) // warn + return 4; + return 0; + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLineLambda.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLineLambda.java new file mode 100644 index 000000000000..f81b3480f80c --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/needbraces/SuppressionXpathRegressionNeedBracesSingleLineLambda.java @@ -0,0 +1,6 @@ +package org.checkstyle.suppressionxpathfilter.needbraces; + +public class SuppressionXpathRegressionNeedBracesSingleLineLambda { + static Runnable r3 = () -> // warn + String.CASE_INSENSITIVE_ORDER.equals("Hello world two!"); +} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheck.java index 74413839122f..d5f47e38b429 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheck.java @@ -217,7 +217,7 @@ public int[] getRequiredTokens() { public void visitToken(DetailAST ast) { final boolean hasNoSlist = ast.findFirstToken(TokenTypes.SLIST) == null; if (hasNoSlist && !isSkipStatement(ast) && isBracesNeeded(ast)) { - log(ast.getLineNo(), MSG_KEY_NEED_BRACES, ast.getText()); + log(ast, MSG_KEY_NEED_BRACES, ast.getText()); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java index 0bb08412d1ed..de6a89628c69 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java @@ -101,9 +101,6 @@ * MissingJavadocType * *
  • - * NeedBraces - *
  • - *
  • * OverloadMethodsDeclarationOrder *
  • *
  • diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheckTest.java index ded7ecdc9fce..2865ec36b689 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/blocks/NeedBracesCheckTest.java @@ -39,29 +39,29 @@ public void testIt() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(NeedBracesCheck.class); final String[] expected = { - "29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), - "41: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "42: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "44: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "45: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "58: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "59: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "61: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "63: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "82: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "83: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "85: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "87: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), - "89: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), - "99: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "100: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "103: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "104: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "105: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "106: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), - "107: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "108: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "29:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), + "41:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "42:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "44:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "45:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "58:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "59:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "61:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "63:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "82:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "83:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "85:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "87:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), + "89:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "97:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), + "99:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "100:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "103:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "104:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "105:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "106:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), + "107:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "108:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), }; verify(checkConfig, getPath("InputNeedBraces.java"), expected); } @@ -75,24 +75,24 @@ public void testItWithAllowsOn() throws Exception { checkConfig.addAttribute("tokens", "LITERAL_DO, LITERAL_ELSE, LITERAL_FOR, LITERAL_IF, " + "LITERAL_WHILE, LITERAL_CASE, LITERAL_DEFAULT, LAMBDA"); final String[] expected = { - "42: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "45: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "59: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "61: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "63: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "83: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "85: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "87: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), - "89: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), - "99: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "100: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "103: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "104: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "105: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "106: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), - "107: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "108: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "42:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "45:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "59:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "61:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "63:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "83:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "85:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "87:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), + "89:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "97:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), + "99:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "100:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "103:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "104:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "105:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "106:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), + "107:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "108:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), }; verify(checkConfig, getPath("InputNeedBraces.java"), expected); } @@ -103,17 +103,17 @@ public void testSingleLineStatements() throws Exception { createModuleConfig(NeedBracesCheck.class); checkConfig.addAttribute("allowSingleLineStatement", "true"); final String[] expected = { - "23: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "38: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "46: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "53: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), - "56: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "62: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "91: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "95: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), - "107: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "114: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "23:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "29:43: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "38:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "46:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "53:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "do"), + "56:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "62:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "91:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "95:11: " + getCheckMessage(MSG_KEY_NEED_BRACES, "else"), + "107:47: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "114:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), }; verify(checkConfig, getPath("InputNeedBracesSingleLineStatements.java"), expected); } @@ -125,10 +125,10 @@ public void testSingleLineLambda() throws Exception { checkConfig.addAttribute("tokens", "LAMBDA"); checkConfig.addAttribute("allowSingleLineStatement", "true"); final String[] expected = { - "7: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "10: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "15: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "16: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "7:29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "10:22: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "15:60: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "16:27: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), }; verify(checkConfig, getPath("InputNeedBracesSingleLineLambda.java"), expected); } @@ -139,12 +139,12 @@ public void testDoNotAllowSingleLineLambda() throws Exception { createModuleConfig(NeedBracesCheck.class); checkConfig.addAttribute("tokens", "LAMBDA"); final String[] expected = { - "5: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "6: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "7: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "10: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "15: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), - "16: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "5:28: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "6:29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "7:29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "10:22: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "15:60: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), + "16:27: " + getCheckMessage(MSG_KEY_NEED_BRACES, "->"), }; verify(checkConfig, getPath("InputNeedBracesSingleLineLambda.java"), expected); } @@ -156,10 +156,10 @@ public void testSingleLineCaseDefault() throws Exception { checkConfig.addAttribute("tokens", "LITERAL_CASE, LITERAL_DEFAULT"); checkConfig.addAttribute("allowSingleLineStatement", "true"); final String[] expected = { - "72: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "75: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "122: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "124: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), + "72:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "75:13: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "122:17: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "124:17: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), }; verify(checkConfig, getPath("InputNeedBracesSingleLineStatements.java"), expected); } @@ -180,12 +180,12 @@ public void testSingleLineCaseDefaultNoSingleLine() throws Exception { createModuleConfig(NeedBracesCheck.class); checkConfig.addAttribute("tokens", "LITERAL_CASE, LITERAL_DEFAULT"); final String[] expected = { - "14: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "15: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "18: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), - "21: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), - "29: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "30: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), + "14:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "15:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "18:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), + "21:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), + "29:17: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "30:17: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), }; verify(checkConfig, getPath("InputNeedBracesCaseDefaultNoSingleLine.java"), expected); } @@ -205,9 +205,9 @@ public void testConditions() throws Exception { checkConfig.addAttribute("tokens", "LITERAL_ELSE, LITERAL_CASE, LITERAL_DEFAULT"); checkConfig.addAttribute("allowSingleLineStatement", "true"); final String[] expected = { - "41: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "44: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), - "56: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), + "41:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "44:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "case"), + "56:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "default"), }; verify(checkConfig, getPath("InputNeedBracesConditional.java"), expected); } @@ -218,7 +218,7 @@ public void testAllowEmptyLoopBodyTrue() throws Exception { createModuleConfig(NeedBracesCheck.class); checkConfig.addAttribute("allowEmptyLoopBody", "true"); final String[] expected = { - "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "97:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), }; verify(checkConfig, getPath("InputNeedBracesNoBodyLoops.java"), expected); } @@ -228,23 +228,23 @@ public void testAllowEmptyLoopBodyFalse() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(NeedBracesCheck.class); final String[] expected = { - "10: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "14: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "18: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "19: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "23: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "28: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "33: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "39: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "45: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "50: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "54: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "60: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "67: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "89: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), - "93: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), - "97: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), - "108: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "10:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "14:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "18:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "19:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "23:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "28:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "33:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "39:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "45:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "50:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "54:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "60:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "67:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "89:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), + "93:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "for"), + "97:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "if"), + "108:9: " + getCheckMessage(MSG_KEY_NEED_BRACES, "while"), }; verify(checkConfig, getPath("InputNeedBracesNoBodyLoops.java"), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java index 890700c9ab8f..c2aea64610d8 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -72,7 +72,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "MissingJavadocMethod", "MissingJavadocPackage", "MissingJavadocType", - "NeedBraces", "OverloadMethodsDeclarationOrder", "PackageDeclaration", "Regexp", diff --git a/src/xdocs/config_filters.xml b/src/xdocs/config_filters.xml index 3acd41bbcd04..137313ad2c53 100644 --- a/src/xdocs/config_filters.xml +++ b/src/xdocs/config_filters.xml @@ -921,7 +921,6 @@ public class UserService {
  • MissingJavadocMethod
  • MissingJavadocPackage
  • MissingJavadocType
  • -
  • NeedBraces
  • OverloadMethodsDeclarationOrder
  • PackageDeclaration
  • Regexp
  • From 1b6408dcc486eba28ece010f6c58c874b2e911a3 Mon Sep 17 00:00:00 2001 From: Abhishek-kumar09 Date: Sat, 21 Mar 2020 18:45:15 +0530 Subject: [PATCH 03/17] Issue #7914: add spoon project to no-exception testing --- .ci/wercker.sh | 14 ++++++++++++++ wercker.yml | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/.ci/wercker.sh b/.ci/wercker.sh index ffcaac9ed8c0..bb7056556a14 100755 --- a/.ci/wercker.sh +++ b/.ci/wercker.sh @@ -316,6 +316,20 @@ no-exception-spotbugs) rm -rf contribution ;; +no-exception-spoon) + CS_POM_VERSION=$(mvn -e -q -Dexec.executable='echo' -Dexec.args='${project.version}' \ + --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec) + echo CS_version: ${CS_POM_VERSION} + checkout_from https://github.com/checkstyle/contribution.git + cd .ci-temp/contribution/checkstyle-tester + sed -i.'' 's/^guava/#guava/' projects-to-test-on.properties + sed -i.'' 's/#spoon/spoon/' projects-to-test-on.properties + groovy ./launch.groovy --listOfProjects projects-for-wercker.properties \ + --config checks-nonjavadoc-error.xml --checkstyleVersion ${CS_POM_VERSION} + cd ../../ + rm -rf contribution + ;; + no-exception-spring-framework) CS_POM_VERSION=$(mvn -e -q -Dexec.executable='echo' -Dexec.args='${project.version}' \ --non-recursive org.codehaus.mojo:exec-maven-plugin:1.3.1:exec) diff --git a/wercker.yml b/wercker.yml index be6f59bb5497..0c630269ca15 100644 --- a/wercker.yml +++ b/wercker.yml @@ -255,6 +255,16 @@ build: echo "build is skipped ..." fi + - script: + name: NoExceptiontest - spoon + code: | + if [[ $RUN_JOB == 1 ]]; then + echo "Command: ./.ci/wercker.sh no-exception-spoon" + ./.ci/wercker.sh no-exception-spoon + else + echo "build is skipped ..." + fi + - script: name: NoExceptiontest - spring-framework code: | From 9b94d177d01b1d2c71f79b01a66359caa2f874b8 Mon Sep 17 00:00:00 2001 From: Akhil singh Date: Fri, 20 Mar 2020 21:07:53 +0530 Subject: [PATCH 04/17] Issue #7854: Resolve Pitest Issues - IllegalImportCheck (1) --- .ci/pitest.sh | 1 - .../checks/imports/IllegalImportCheckTest.java | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/.ci/pitest.sh b/.ci/pitest.sh index 690bf9e82f88..665d6d42463d 100755 --- a/.ci/pitest.sh +++ b/.ci/pitest.sh @@ -72,7 +72,6 @@ pitest-imports) mvn -e -P$1 clean test org.pitest:pitest-maven:mutationCoverage; declare -a ignoredItems=( "CustomImportOrderCheck.java.html:
            if (bestMatch.group.equals(NON_GROUP_RULE_GROUP)) {
    " - "IllegalImportCheck.java.html:
            if (regexp) {
    " "IllegalImportCheck.java.html:
            if (!result && illegalClasses != null) {
    " "IllegalImportCheck.java.html:
            if (!result) {
    " "ImportControlLoader.java.html:
            else if (ALLOW_ELEMENT_NAME.equals(qName) || "disallow".equals(qName)) {
    " diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/IllegalImportCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/IllegalImportCheckTest.java index 80221d5d2dea..9936aa36be47 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/IllegalImportCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/imports/IllegalImportCheckTest.java @@ -93,6 +93,20 @@ public void testIllegalClasses() verify(checkConfig, getNonCompilablePath("InputIllegalImportDefault.java"), expected); } + @Test + public void testIllegalClassesStarImport() + throws Exception { + final DefaultConfiguration checkConfig = + createModuleConfig(IllegalImportCheck.class); + checkConfig.addAttribute("illegalClasses", "java.io.*"); + final String[] expected = { + "9:1: " + getCheckMessage(MSG_KEY, "java.io.*"), + "15:1: " + getCheckMessage(MSG_KEY, "sun.applet.*"), + "28:1: " + getCheckMessage(MSG_KEY, "sun.*"), + }; + verify(checkConfig, getNonCompilablePath("InputIllegalImportDefault.java"), expected); + } + @Test public void testIllegalPackagesRegularExpression() throws Exception { From fed7f9b8fcf3ecb8ed167bc3fcd1bf7095d64edf Mon Sep 17 00:00:00 2001 From: Gaurav Punjabi Date: Tue, 3 Mar 2020 21:33:01 +0530 Subject: [PATCH 05/17] Issue #7693: Update doc for JavaNCSS --- .../checks/metrics/JavaNCSSCheck.java | 77 ++++++++++++++++- src/xdocs/config_metrics.xml | 86 ++++++++++++++++++- 2 files changed, 158 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java index 6330f79cf564..b818ac25ca49 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java @@ -75,15 +75,90 @@ *
      * <module name="JavaNCSS"/>
      * 
    + *

    Example:

    + *
    + * public void test() {
    + *   System.out.println("Line 1");
    + *   // another 48 lines of code
    + *   System.out.println("Line 50") // OK
    + *   System.out.println("Line 51") // violation, the method crosses 50 non commented lines
    + * }
    + * 
    *

    - * To configure the check with 40 allowed non commenting lines for a method: + * To configure the check with 40 allowed non commented lines for a method: *

    *
      * <module name="JavaNCSS">
      *   <property name="methodMaximum" value="40"/>
      * </module>
      * 
    + *

    Example:

    + *
    + * public void test() {
    + *   System.out.println("Line 1");
    + *   // another 38 lines of code
    + *   System.out.println("Line 40") // OK
    + *   System.out.println("Line 41") // violation, the method crosses 40 non commented lines
    + * }
    + * 
    + *

    + * To configure the check to set limit of non commented lines in class to 100: + *

    + *
    + * <module name="JavaNCSS">
    + *   <property name="classMaximum" value="100"/>
    + * </module>
    + * 
    + *

    Example:

    + *
    + * public class Test {
    + *   public void test() {
    + *       System.out.println("Line 1");
    + *       // another 47 lines of code
    + *       System.out.println("Line 49");
    + *   }
      *
    + *   public void test1() {
    + *       System.out.println("Line 50"); // OK
    + *       // another 47 lines of code
    + *       System.out.println("Line 98"); // violation
    + *   }
    + * }
    + * 
    + *

    + * To configure the check to set limit of non commented lines in file to 200: + *

    + *
    + * <module name="JavaNCSS">
    + *   <property name="fileMaximum" value="200"/>
    + * </module>
    + * 
    + *

    Example:

    + *
    + * public class Test1 {
    + *   public void test() {
    + *       System.out.println("Line 1");
    + *       // another 48 lines of code
    + *       System.out.println("Line 49");
    + *   }
    + *
    + *   public void test1() {
    + *       System.out.println("Line 50");
    + *       // another 47 lines of code
    + *       System.out.println("Line 98"); // OK
    + *   }
    + * }
    + *
    + * class Test2 {
    + *   public void test() {
    + *       System.out.println("Line 150"); // OK
    + *   }
    + *
    + *   public void test1() {
    + *       System.out.println("Line 200"); // violation
    + *   }
    + * }
    + * 
    * @since 3.5 */ // -@cs[AbbreviationAsWordInName] We can not change it as, diff --git a/src/xdocs/config_metrics.xml b/src/xdocs/config_metrics.xml index 365429ee00be..8793089ced73 100644 --- a/src/xdocs/config_metrics.xml +++ b/src/xdocs/config_metrics.xml @@ -1060,18 +1060,96 @@ class SwitchExample { <module name="JavaNCSS"/> -

    - To configure the check with 40 allowed non commenting lines - for a method: + Example: +

    + +public void test() { + System.out.println("Line 1"); + // another 48 lines of code + System.out.println("Line 50") // OK + System.out.println("Line 51") // violation, the method crosses 50 non commented lines +} + +

    + To configure the check with 40 allowed non commented lines for a method:

    <module name="JavaNCSS"> <property name="methodMaximum" value="40"/> </module> - +

    + Example: +

    + +public void test() { + System.out.println("Line 1"); + // another 38 lines of code + System.out.println("Line 40") // OK + System.out.println("Line 41") // violation, the method crosses 40 non commented lines +} + +

    + To configure the check to set limit of non commented lines in class to 100: +

    + +<module name="JavaNCSS"> + <property name="classMaximum" value="100"/> +</module> + +

    Example:

    + +public class Test { + public void test() { + System.out.println("Line 1"); + // another 47 lines of code + System.out.println("Line 49"); + } + + public void test1() { + System.out.println("Line 50"); // OK + // another 47 lines of code + System.out.println("Line 98"); // violation + } +} + +

    + To configure the check to set limit of non commented lines in file to 200: +

    + +<module name="JavaNCSS"> + <property name="fileMaximum" value="200"/> +</module> + +

    Example:

    + +public class Test1 { + public void test() { + System.out.println("Line 1"); + // another 48 lines of code + System.out.println("Line 49"); + } + public void test1() { + System.out.println("Line 50"); + // another 47 lines of code + System.out.println("Line 98"); // OK + } +} + +class Test2 { + public void test() { + System.out.println("Line 150"); // OK + } + + public void test1() { + System.out.println("Line 200"); // violation + } +} + + +
    • From 42cae924c259c95cd4d0fa1e448d00eee6bc97ed Mon Sep 17 00:00:00 2001 From: Shrey Kumar Singh Date: Sat, 21 Mar 2020 14:04:58 +0530 Subject: [PATCH 06/17] Issue #7737: Update AbstractChecks to log DetailAST - InvalidJavadocPosition --- ...hRegressionInvalidJavadocPositionTest.java | 172 ++++++++++++++++++ ...hRegressionInvalidJavadocPositionFive.java | 9 + ...hRegressionInvalidJavadocPositionFour.java | 7 + ...thRegressionInvalidJavadocPositionOne.java | 8 + ...thRegressionInvalidJavadocPositionSix.java | 8 + ...RegressionInvalidJavadocPositionThree.java | 9 + ...thRegressionInvalidJavadocPositionTwo.java | 7 + .../filters/SuppressionXpathFilter.java | 3 - .../internal/XpathRegressionTest.java | 1 - src/xdocs/config_filters.xml | 1 - 10 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInvalidJavadocPositionTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFive.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFour.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionOne.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionSix.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionThree.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionTwo.java diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInvalidJavadocPositionTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInvalidJavadocPositionTest.java new file mode 100644 index 000000000000..d1e4311a1aa5 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionInvalidJavadocPositionTest.java @@ -0,0 +1,172 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2020 the original author or authors. +// +// 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 org.checkstyle.suppressionxpathfilter; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.javadoc.InvalidJavadocPositionCheck; + +public class XpathRegressionInvalidJavadocPositionTest extends AbstractXpathTestSupport { + + private final String checkName = InvalidJavadocPositionCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testOne() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInvalidJavadocPositionOne.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(InvalidJavadocPositionCheck.class); + + final String[] expectedViolation = { + "4:1: " + getCheckMessage(InvalidJavadocPositionCheck.class, + InvalidJavadocPositionCheck.MSG_KEY), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionInvalidJavadocPositionOne']]" + + "/MODIFIERS/BLOCK_COMMENT_BEGIN" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testTwo() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInvalidJavadocPositionTwo.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(InvalidJavadocPositionCheck.class); + + final String[] expectedViolation = { + "5:1: " + getCheckMessage(InvalidJavadocPositionCheck.class, + InvalidJavadocPositionCheck.MSG_KEY), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionInvalidJavadocPositionTwo']]" + + "/OBJBLOCK/BLOCK_COMMENT_BEGIN" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testThree() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInvalidJavadocPositionThree.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(InvalidJavadocPositionCheck.class); + + final String[] expectedViolation = { + "6:5: " + getCheckMessage(InvalidJavadocPositionCheck.class, + InvalidJavadocPositionCheck.MSG_KEY), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionInvalidJavadocPositionThree']]" + + "/OBJBLOCK/BLOCK_COMMENT_BEGIN" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testFour() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInvalidJavadocPositionFour.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(InvalidJavadocPositionCheck.class); + + final String[] expectedViolation = { + "4:5: " + getCheckMessage(InvalidJavadocPositionCheck.class, + InvalidJavadocPositionCheck.MSG_KEY), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionInvalidJavadocPositionFour']]" + + "/OBJBLOCK/BLOCK_COMMENT_BEGIN" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testFive() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInvalidJavadocPositionFive.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(InvalidJavadocPositionCheck.class); + + final String[] expectedViolation = { + "5:9: " + getCheckMessage(InvalidJavadocPositionCheck.class, + InvalidJavadocPositionCheck.MSG_KEY), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionInvalidJavadocPositionFive']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]" + + "/SLIST/BLOCK_COMMENT_BEGIN" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testSix() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionInvalidJavadocPositionSix.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(InvalidJavadocPositionCheck.class); + + final String[] expectedViolation = { + "5:5: " + getCheckMessage(InvalidJavadocPositionCheck.class, + InvalidJavadocPositionCheck.MSG_KEY), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionInvalidJavadocPositionSix']]" + + "/OBJBLOCK/BLOCK_COMMENT_BEGIN" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFive.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFive.java new file mode 100644 index 000000000000..2cb5606536cb --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFive.java @@ -0,0 +1,9 @@ +package org.checkstyle.suppressionxpathfilter.invalidjavadocposition; + +public class SuppressionXpathRegressionInvalidJavadocPositionFive { + public void foo() { + /** // warn + * Javadoc comment + */ + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFour.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFour.java new file mode 100644 index 000000000000..5d65ecc150ad --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionFour.java @@ -0,0 +1,7 @@ +package org.checkstyle.suppressionxpathfilter.invalidjavadocposition; + +public class SuppressionXpathRegressionInvalidJavadocPositionFour { + /** // warn + * Javadoc Comment + */ +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionOne.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionOne.java new file mode 100644 index 000000000000..89405e83077b --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionOne.java @@ -0,0 +1,8 @@ +package org.checkstyle.suppressionxpathfilter.invalidjavadocposition; + +@SuppressWarnings("serial") +/** // warn + * Javadoc Comment + */ +public class SuppressionXpathRegressionInvalidJavadocPositionOne { +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionSix.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionSix.java new file mode 100644 index 000000000000..03a330ee4660 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionSix.java @@ -0,0 +1,8 @@ +package org.checkstyle.suppressionxpathfilter.invalidjavadocposition; + +public class SuppressionXpathRegressionInvalidJavadocPositionSix { + int a; + /** // warn + * Javadoc Comment + */ +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionThree.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionThree.java new file mode 100644 index 000000000000..6e64de7f644f --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionThree.java @@ -0,0 +1,9 @@ +package org.checkstyle.suppressionxpathfilter.invalidjavadocposition; + +public class SuppressionXpathRegressionInvalidJavadocPositionThree { + public void foo(){ + } + /** // warn + * Javadoc comment + */ +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionTwo.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionTwo.java new file mode 100644 index 000000000000..bf9852b86aff --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/invalidjavadocposition/SuppressionXpathRegressionInvalidJavadocPositionTwo.java @@ -0,0 +1,7 @@ +package org.checkstyle.suppressionxpathfilter.invalidjavadocposition; + +public class SuppressionXpathRegressionInvalidJavadocPositionTwo { +} +/** // warn + * Javadoc comment + */ diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java index de6a89628c69..a365a6c572ca 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java @@ -74,9 +74,6 @@ * InterfaceMemberImpliedModifier *
    • *
    • - * InvalidJavadocPosition - *
    • - *
    • * JavadocContentLocation *
    • *
    • diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java index c2aea64610d8..b4ee06b8b9b6 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -63,7 +63,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "Indentation", "InterfaceIsType", "InterfaceMemberImpliedModifier", - "InvalidJavadocPosition", "JavadocContentLocation", "JavadocMethod", "JavadocType", diff --git a/src/xdocs/config_filters.xml b/src/xdocs/config_filters.xml index 137313ad2c53..e5f5de8f2388 100644 --- a/src/xdocs/config_filters.xml +++ b/src/xdocs/config_filters.xml @@ -912,7 +912,6 @@ public class UserService {
    • Indentation
    • InterfaceIsType
    • InterfaceMemberImpliedModifier
    • -
    • InvalidJavadocPosition
    • JavadocContentLocation
    • JavadocMethod
    • JavadocType
    • From ee34c13b9e2cd3a69cf2e8ef6dab4d3000261a77 Mon Sep 17 00:00:00 2001 From: sulkykookie Date: Fri, 20 Mar 2020 20:38:53 +0530 Subject: [PATCH 07/17] Issue #7732: Update AbstractChecks to log DetailAST - IllegalCatch --- .../XpathRegressionIllegalCatchTest.java | 85 +++++++++++++++++++ ...ressionXpathRegressionIllegalCatchOne.java | 9 ++ ...ressionXpathRegressionIllegalCatchTwo.java | 19 +++++ .../filters/SuppressionXpathFilter.java | 3 - .../internal/XpathRegressionTest.java | 1 - src/xdocs/config_filters.xml | 1 - 6 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionIllegalCatchTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchOne.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchTwo.java diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionIllegalCatchTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionIllegalCatchTest.java new file mode 100644 index 000000000000..f35c1787b668 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionIllegalCatchTest.java @@ -0,0 +1,85 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2020 the original author or authors. +// +// 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 org.checkstyle.suppressionxpathfilter; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck; + +public class XpathRegressionIllegalCatchTest extends AbstractXpathTestSupport { + + private final String checkName = IllegalCatchCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testOne() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionIllegalCatchOne.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(IllegalCatchCheck.class); + + final String[] expectedViolation = { + "6:11: " + getCheckMessage(IllegalCatchCheck.class, + IllegalCatchCheck.MSG_KEY, "RuntimeException"), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionIllegalCatchOne']]/OBJBLOCK" + + "/METHOD_DEF[./IDENT[@text='fun']]/SLIST" + + "/LITERAL_TRY/LITERAL_CATCH" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testTwo() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionIllegalCatchTwo.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(IllegalCatchCheck.class); + + final String[] expectedViolation = { + "16:11: " + getCheckMessage(IllegalCatchCheck.class, + IllegalCatchCheck.MSG_KEY, "java.lang.RuntimeException"), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionIllegalCatchTwo']]/OBJBLOCK" + + "/METHOD_DEF[./IDENT[@text='methodTwo']]/SLIST" + + "/LITERAL_TRY/LITERAL_CATCH" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchOne.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchOne.java new file mode 100644 index 000000000000..7f39b3a5fac5 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchOne.java @@ -0,0 +1,9 @@ +package org.checkstyle.suppressionxpathfilter.illegalcatch; + +public class SuppressionXpathRegressionIllegalCatchOne { + public void fun() { + try { + } catch (RuntimeException e) { // warn + } + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchTwo.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchTwo.java new file mode 100644 index 000000000000..109e09b7dd79 --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/illegalcatch/SuppressionXpathRegressionIllegalCatchTwo.java @@ -0,0 +1,19 @@ +package org.checkstyle.suppressionxpathfilter.illegalcatch; + +public class SuppressionXpathRegressionIllegalCatchTwo { + public void methodOne() { + try { + foo(); + } catch (java.sql.SQLException e) { + } + } + + private void foo() throws java.sql.SQLException { + } + + public void methodTwo() { + try { + } catch (java.lang.RuntimeException e) { // warn + } + } +} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java index a365a6c572ca..ef7b20d5b187 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java @@ -62,9 +62,6 @@ * EmptyLineSeparator * *
    • - * IllegalCatch - *
    • - *
    • * Indentation *
    • *
    • diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java index b4ee06b8b9b6..0844ac435937 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -59,7 +59,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "CommentsIndentation", "CustomImportOrder", "EmptyLineSeparator", - "IllegalCatch", "Indentation", "InterfaceIsType", "InterfaceMemberImpliedModifier", diff --git a/src/xdocs/config_filters.xml b/src/xdocs/config_filters.xml index e5f5de8f2388..350080625060 100644 --- a/src/xdocs/config_filters.xml +++ b/src/xdocs/config_filters.xml @@ -908,7 +908,6 @@ public class UserService {
    • CommentsIndentation
    • CustomImportOrder
    • EmptyLineSeparator
    • -
    • IllegalCatch
    • Indentation
    • InterfaceIsType
    • InterfaceMemberImpliedModifier
    • From 3828fb1efece4f88445cfdf70b27e90abd0c3ef7 Mon Sep 17 00:00:00 2001 From: Parag Pachpute Date: Tue, 17 Mar 2020 22:01:47 -0400 Subject: [PATCH 08/17] Issue #7648: add doc for magic number --- .../checks/coding/MagicNumberCheck.java | 23 +++++++++++++++++++ src/xdocs/config_coding.xml | 21 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java index 16bb3064068b..dca11d92db81 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java @@ -134,6 +134,10 @@ * int i = i + 1; // no violation * int j = j + 8; // violation * } + * + * public int hashCode() { + * return 10; // violation + * } * } * @interface anno { * int value() default 10; // no violation @@ -212,6 +216,25 @@ * } * * + *

      + * Config example of ignoreHashCodeMethod option: + *

      + *
      + * <module name="MagicNumber">
      + *   <property name="ignoreHashCodeMethod" value="true"/>
      + * </module>
      + * 
      + *

      + * result is no violation: + *

      + *
      + * class TestHashCode {
      + *     public int hashCode() {
      + *         return 10;       // OK
      + *     }
      + * }
      + * 
      + * * @since 3.1 */ @StatelessCheck diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml index 8a663c472ce3..336f2644bbfc 100644 --- a/src/xdocs/config_coding.xml +++ b/src/xdocs/config_coding.xml @@ -3015,6 +3015,10 @@ class MyClass { int i = i + 1; // no violation int j = j + 8; // violation } + + public int hashCode() { + return 10; // violation + } } @interface anno { int value() default 10; // no violation @@ -3092,6 +3096,23 @@ class TestMethodCall { } +

      + Config example of ignoreHashCodeMethod option: +

      + +<module name="MagicNumber"> + <property name="ignoreHashCodeMethod" value="true"/> +</module> + +

      result is no violation:

      + +class TestHashCode { + public int hashCode() { + return 10; // OK + } +} + + From 4f1ff633a373a08d5d002c15c746402ee42b0499 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 23 Mar 2020 07:09:42 +0000 Subject: [PATCH 09/17] dependency: bump junit.version from 5.6.0 to 5.6.1 Bumps `junit.version` from 5.6.0 to 5.6.1. Updates `junit-jupiter-api` from 5.6.0 to 5.6.1 - [Release notes](https://github.com/junit-team/junit5/releases) - [Commits](https://github.com/junit-team/junit5/compare/r5.6.0...r5.6.1) Updates `junit-jupiter-engine` from 5.6.0 to 5.6.1 - [Release notes](https://github.com/junit-team/junit5/releases) - [Commits](https://github.com/junit-team/junit5/compare/r5.6.0...r5.6.1) Updates `junit-vintage-engine` from 5.6.0 to 5.6.1 - [Release notes](https://github.com/junit-team/junit5/releases) - [Commits](https://github.com/junit-team/junit5/compare/r5.6.0...r5.6.1) Signed-off-by: dependabot-preview[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index fd876efd4676..71a82eb0dbf6 100644 --- a/pom.xml +++ b/pom.xml @@ -219,7 +219,7 @@ 4 0.10 **/test/resources/**/*,**/it/resources/**/* - 5.6.0 + 5.6.1 From e13352f1a8e85a54a29b3bdcc3cc24d24c14059a Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 23 Mar 2020 07:09:56 +0000 Subject: [PATCH 10/17] dependency: bump powermock.version from 2.0.5 to 2.0.6 Bumps `powermock.version` from 2.0.5 to 2.0.6. Updates `powermock-api-mockito2` from 2.0.5 to 2.0.6 - [Release notes](https://github.com/powermock/powermock/releases) - [Changelog](https://github.com/powermock/powermock/blob/release/2.x/docs/changelog.txt) - [Commits](https://github.com/powermock/powermock/compare/powermock-2.0.5...powermock-2.0.6) Updates `powermock-module-junit4` from 2.0.5 to 2.0.6 - [Release notes](https://github.com/powermock/powermock/releases) - [Changelog](https://github.com/powermock/powermock/blob/release/2.x/docs/changelog.txt) - [Commits](https://github.com/powermock/powermock/compare/powermock-2.0.5...powermock-2.0.6) Signed-off-by: dependabot-preview[bot] --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 71a82eb0dbf6..257c4ce40670 100644 --- a/pom.xml +++ b/pom.xml @@ -204,7 +204,7 @@ 3.13.0 6.21.0 0.8.5 - 2.0.5 + 2.0.6 9.9.1-7 3.1.1 1.37.1 From d74da922b0c4c33b7fde1ec366f70c38241b86b8 Mon Sep 17 00:00:00 2001 From: Gaurab Dasgupta Date: Mon, 23 Mar 2020 14:19:04 +0530 Subject: [PATCH 11/17] Issue #7746: Update AbstractChecks to log DetailAST- MissingJavadocPackage --- ...thRegressionMissingJavadocPackageTest.java | 81 +++++++++++++++++++ .../blockcomment/package-info.java | 4 + .../nojavadoc/package-info.java | 1 + .../javadoc/MissingJavadocPackageCheck.java | 2 +- .../filters/SuppressionXpathFilter.java | 3 - .../MissingJavadocPackageCheckTest.java | 16 ++-- .../internal/XpathRegressionTest.java | 1 - src/xdocs/config_filters.xml | 1 - 8 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionMissingJavadocPackageTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/blockcomment/package-info.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/nojavadoc/package-info.java diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionMissingJavadocPackageTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionMissingJavadocPackageTest.java new file mode 100644 index 000000000000..8feaff1f8080 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionMissingJavadocPackageTest.java @@ -0,0 +1,81 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2020 the original author or authors. +// +// 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 org.checkstyle.suppressionxpathfilter; + +import java.io.File; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocPackageCheck; + +public class XpathRegressionMissingJavadocPackageTest extends AbstractXpathTestSupport { + + private final String checkName = MissingJavadocPackageCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testBlockComment() throws Exception { + final File fileToProcess = new File(getPath( + "blockcomment/package-info.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(MissingJavadocPackageCheck.class); + + final String[] expectedViolation = { + "4:1: " + getCheckMessage(MissingJavadocPackageCheck.class, + MissingJavadocPackageCheck.MSG_PKG_JAVADOC_MISSING), + }; + final List expectedXpathQueries = Collections.singletonList( + "/PACKAGE_DEF" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testNoJavadoc() throws Exception { + final File fileToProcess = new File(getPath( + "nojavadoc/package-info.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(MissingJavadocPackageCheck.class); + + final String[] expectedViolation = { + "1:1: " + getCheckMessage(MissingJavadocPackageCheck.class, + MissingJavadocPackageCheck.MSG_PKG_JAVADOC_MISSING), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/PACKAGE_DEF" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/blockcomment/package-info.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/blockcomment/package-info.java new file mode 100644 index 000000000000..f27a70334b3f --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/blockcomment/package-info.java @@ -0,0 +1,4 @@ +/* + * block comment + */ +package org.checkstyle.suppressionxpathfilter.missingjavadocpackage.blockcomment; //warn diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/nojavadoc/package-info.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/nojavadoc/package-info.java new file mode 100644 index 000000000000..b5573db8b14c --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/missingjavadocpackage/nojavadoc/package-info.java @@ -0,0 +1 @@ +package org.checkstyle.suppressionxpathfilter.missingjavadocpackage.nojavadoc; //warn diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheck.java index a5ce51acb7cc..2930ba97cb0c 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheck.java @@ -92,7 +92,7 @@ public boolean isCommentNodesRequired() { public void visitToken(DetailAST ast) { final FileContents contents = getFileContents(); if (contents.inPackageInfo() && !hasJavadoc(ast)) { - log(ast.getLineNo(), MSG_PKG_JAVADOC_MISSING); + log(ast, MSG_PKG_JAVADOC_MISSING); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java index ef7b20d5b187..9ddc613823e1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java @@ -89,9 +89,6 @@ * MissingJavadocMethod * *
    • - * MissingJavadocPackage - *
    • - *
    • * MissingJavadocType *
    • *
    • diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheckTest.java index c448df8cebe9..ac356c2f365d 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/MissingJavadocPackageCheckTest.java @@ -68,7 +68,7 @@ public void testPackageJavadocPresentWithBlankLines() throws Exception { public void testPackageJavadocMissing() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "1:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/package-info.java"), expected); } @@ -77,7 +77,7 @@ public void testPackageJavadocMissing() throws Exception { public void testBlockCommentInsteadOfJavadoc() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "4: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "4:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/blockcomment/package-info.java"), expected); } @@ -86,7 +86,7 @@ public void testBlockCommentInsteadOfJavadoc() throws Exception { public void testSinglelineCommentInsteadOfJavadoc() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "2: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "2:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/singleline/package-info.java"), expected); } @@ -95,7 +95,7 @@ public void testSinglelineCommentInsteadOfJavadoc() throws Exception { public void testSinglelineCommentInsteadOfJavadoc2() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "2: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "2:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/single/package-info.java"), expected); } @@ -104,7 +104,7 @@ public void testSinglelineCommentInsteadOfJavadoc2() throws Exception { public void testPackageJavadocMissingWithAnnotation() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "2: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "2:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/annotation/package-info.java"), expected); } @@ -113,7 +113,7 @@ public void testPackageJavadocMissingWithAnnotation() throws Exception { public void testPackageJavadocMissingWithAnnotationAndBlockComment() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "6: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "6:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/annotation/blockcomment/package-info.java"), expected); } @@ -122,7 +122,7 @@ public void testPackageJavadocMissingWithAnnotationAndBlockComment() throws Exce public void testPackageJavadocMissingDetachedJavadoc() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "5: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "5:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/detached/package-info.java"), expected); } @@ -138,7 +138,7 @@ public void testPackageJavadocPresentWithHeader() throws Exception { public void testPackageJavadocMissingWithBlankLines() throws Exception { final DefaultConfiguration config = createModuleConfig(MissingJavadocPackageCheck.class); final String[] expected = { - "2: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), + "2:1: " + getCheckMessage(MSG_PKG_JAVADOC_MISSING), }; verify(config, getPath("nojavadoc/blank/package-info.java"), expected); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java index 0844ac435937..3a0b7e5f4e20 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -68,7 +68,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "LambdaParameterName", "MethodCount", "MissingJavadocMethod", - "MissingJavadocPackage", "MissingJavadocType", "OverloadMethodsDeclarationOrder", "PackageDeclaration", diff --git a/src/xdocs/config_filters.xml b/src/xdocs/config_filters.xml index 350080625060..38c0c9a80cd1 100644 --- a/src/xdocs/config_filters.xml +++ b/src/xdocs/config_filters.xml @@ -917,7 +917,6 @@ public class UserService {
    • LambdaParameterName
    • MethodCount
    • MissingJavadocMethod
    • -
    • MissingJavadocPackage
    • MissingJavadocType
    • OverloadMethodsDeclarationOrder
    • PackageDeclaration
    • From d900b13ad9bb3f2eebba48a2734be6d8093c5cd0 Mon Sep 17 00:00:00 2001 From: Anubhav Date: Sun, 8 Mar 2020 19:12:38 +0800 Subject: [PATCH 12/17] Issue #7666: Add documentation examples for InnerAssignment check --- .../checks/coding/InnerAssignmentCheck.java | 44 +++++++++++++++++++ src/xdocs/config_coding.xml | 43 ++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java index 3528aa6246b2..68fb0b54acdf 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/InnerAssignmentCheck.java @@ -57,6 +57,50 @@ *
        * <module name="InnerAssignment"/>
        * 
      + *

      Example:

      + *
      + * class MyClass {
      + *
      + *   void foo() {
      + *     int a, b;
      + *     a = b = 5; // violation, assignment to each variable should be in a separate statement
      + *     a = b += 5; // violation
      + *
      + *     a = 5; // OK
      + *     b = 5; // OK
      + *     a = 5; b = 5; // OK
      + *
      + *     double myDouble;
      + *     double[] doubleArray = new double[] {myDouble = 4.5, 15.5}; // violation
      + *
      + *     String nameOne;
      + *     List<String> myList = new ArrayList<String>();
      + *     myList.add(nameOne = "tom"); // violation
      +
      + *     for (int k = 0; k < 10; k = k + 2) { // OK
      + *       // some code
      + *     }
      + *
      + *     boolean someVal;
      + *     if (someVal = true) { // violation
      + *       // some code
      + *     }
      + *
      + *     while (someVal = false) {} // violation
      + *
      + *     InputStream is = new FileInputStream("textFile.txt");
      + *     while ((b = is.read()) != -1) { // OK, this is a common idiom
      + *       // some code
      + *     }
      + *
      + *   }
      + *
      + *   boolean testMethod() {
      + *     boolean val;
      + *     return val = true; // violation
      + *   }
      + * }
      + * 
      * * @since 3.0 */ diff --git a/src/xdocs/config_coding.xml b/src/xdocs/config_coding.xml index 336f2644bbfc..954155e4b094 100644 --- a/src/xdocs/config_coding.xml +++ b/src/xdocs/config_coding.xml @@ -2820,6 +2820,49 @@ while ((line = bufferedReader.readLine()) != null) { <module name="InnerAssignment"/> +

      Example:

      + +class MyClass { + + void foo() { + int a, b; + a = b = 5; // violation, assignment to each variable should be in a separate statement + a = b += 5; // violation + + a = 5; // OK + b = 5; // OK + a = 5; b = 5; // OK + + double myDouble; + double[] doubleArray = new double[] {myDouble = 4.5, 15.5}; // violation + + String nameOne; + List<String> myList = new ArrayList<String>(); + myList.add(nameOne = "tom"); // violation + + for (int k = 0; k < 10; k = k + 2) { // OK + // some code + } + + boolean someVal; + if (someVal = true) { // violation + // some code + } + + while (someVal = false) {} // violation + + InputStream is = new FileInputStream("textFile.txt"); + while ((b = is.read()) != -1) { // OK, this is a common idiom + // some code + } + } + + boolean testMethod() { + boolean val; + return val = true; // violation + } +} +
      From 611a9632b6bec63a53f00293f4539fce442d2a76 Mon Sep 17 00:00:00 2001 From: HuGanghui Date: Sun, 22 Mar 2020 15:36:27 +0800 Subject: [PATCH 13/17] Issue #7612: Update doc for InterfaceIsType --- .../checks/design/InterfaceIsTypeCheck.java | 33 +++++++++++++++++++ src/xdocs/config_design.xml | 33 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InterfaceIsTypeCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InterfaceIsTypeCheck.java index 14536ab05733..5034c4af70c3 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InterfaceIsTypeCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InterfaceIsTypeCheck.java @@ -53,7 +53,40 @@ *
        * <module name="InterfaceIsType"/>
        * 
      + *

      Example:

      + *
      + * public interface Test1 { // violation
      + *     int a = 3;
      + *
      + * }
      + *
      + * public interface Test2 { // OK
      + *
      + * }
        *
      + * public interface Test3 { // OK
      + *     int a = 3;
      + *     void test();
      + * }
      + * 
      + *

      + * To configure the check to report violation so that it doesn't allow Marker Interfaces: + *

      + *
      + * <module name="InterfaceIsType">
      + *   <property name="allowMarkerInterfaces" value="false"/>
      + * </module>
      + * 
      + *

      Example:

      + *
      + * public interface Test1 { // violation
      + *     int a = 3;
      + * }
      + *
      + * public interface Test2 { // violation
      + *
      + * }
      + * 
      * @since 3.1 */ @StatelessCheck diff --git a/src/xdocs/config_design.xml b/src/xdocs/config_design.xml index 38226ea2917d..66931354d50b 100644 --- a/src/xdocs/config_design.xml +++ b/src/xdocs/config_design.xml @@ -510,6 +510,39 @@ public class StringUtils // not final to allow subclassing <module name="InterfaceIsType"/> +

      Example:

      + +public interface Test1 { // violation + int a = 3; +} + +public interface Test2 { // OK + +} + +public interface Test3 { // OK + int a = 3; + void test(); +} + +

      + To configure the check to report violation so that it doesn't allow Marker Interfaces: +

      + +<module name="InterfaceIsType"> + <property name="allowMarkerInterfaces" value="false"/> +</module> + +

      Example:

      + +public interface Test1 { // violation + int a = 3; +} + +public interface Test2 { // violation + +} +
      From 18b792ebc5b6d5a2d427ccf61a45b122ba90ff53 Mon Sep 17 00:00:00 2001 From: Nick Mancuso Date: Fri, 20 Mar 2020 01:21:40 -0400 Subject: [PATCH 14/17] minor: add "cd -" to removeFolderWithProtectedFiles --- .ci/travis/travis.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.ci/travis/travis.sh b/.ci/travis/travis.sh index 2ec1a15977ae..d725266c6f9c 100755 --- a/.ci/travis/travis.sh +++ b/.ci/travis/travis.sh @@ -3,10 +3,7 @@ set -e removeFolderWithProtectedFiles() { - cd "$1" - find . -delete - cd .. - rmdir "$1" + find $1 -delete } case $1 in From b850cbac297f6e5a5ca82beb8520896551e87453 Mon Sep 17 00:00:00 2001 From: Abhishek-kumar09 Date: Tue, 24 Mar 2020 00:25:08 +0530 Subject: [PATCH 15/17] Issue #7742: Update AbstractChecks to log DetailAST - LambdaParameterName --- ...pathRegressionLambdaParameterNameTest.java | 111 ++++++++++++++++++ ...onXpathRegressionLambdaParameterName1.java | 9 ++ ...onXpathRegressionLambdaParameterName2.java | 9 ++ .../filters/SuppressionXpathFilter.java | 3 - .../internal/XpathRegressionTest.java | 1 - src/xdocs/config_filters.xml | 1 - 6 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionLambdaParameterNameTest.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName1.java create mode 100644 src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName2.java diff --git a/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionLambdaParameterNameTest.java b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionLambdaParameterNameTest.java new file mode 100644 index 000000000000..431ecf6c5980 --- /dev/null +++ b/src/it/java/org/checkstyle/suppressionxpathfilter/XpathRegressionLambdaParameterNameTest.java @@ -0,0 +1,111 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2020 the original author or authors. +// +// 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 org.checkstyle.suppressionxpathfilter; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; +import com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck; +import com.puppycrawl.tools.checkstyle.checks.naming.LambdaParameterNameCheck; + +public class XpathRegressionLambdaParameterNameTest extends AbstractXpathTestSupport { + + private final String checkName = LambdaParameterNameCheck.class.getSimpleName(); + + @Override + protected String getCheckName() { + return checkName; + } + + @Test + public void testOne() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionLambdaParameterName1.java")); + + final DefaultConfiguration moduleConfig = + createModuleConfig(LambdaParameterNameCheck.class); + final String defaultPattern = "^[a-z][a-zA-Z0-9]*$"; + + final String[] expectedViolation = { + "7:44: " + getCheckMessage(LambdaParameterNameCheck.class, + AbstractNameCheck.MSG_INVALID_PATTERN, "S", defaultPattern), + }; + + final List expectedXpathQueries = Collections.singletonList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionLambdaParameterName1']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[" + + "./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/IDENT[@text='S']" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } + + @Test + public void testTwo() throws Exception { + final File fileToProcess = + new File(getPath("SuppressionXpathRegressionLambdaParameterName2.java")); + + final String nonDefaultPattern = "^_[a-zA-Z0-9]*$"; + + final DefaultConfiguration moduleConfig = + createModuleConfig(LambdaParameterNameCheck.class); + moduleConfig.addAttribute("format", nonDefaultPattern); + + final String[] expectedViolation = { + "7:45: " + getCheckMessage(LambdaParameterNameCheck.class, + AbstractNameCheck.MSG_INVALID_PATTERN, "s", nonDefaultPattern), + }; + + final List expectedXpathQueries = Arrays.asList( + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionLambdaParameterName2']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/" + + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS", + + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionLambdaParameterName2']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/" + + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS" + + "/PARAMETER_DEF[./IDENT[@text='s']]", + + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionLambdaParameterName2']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST" + + "/VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS" + + "/PARAMETER_DEF[./IDENT[@text='s']]/MODIFIERS", + + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionLambdaParameterName2']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/" + + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS" + + "/PARAMETER_DEF[./IDENT[@text='s']]/TYPE", + + "/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionLambdaParameterName2']]" + + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/" + + "VARIABLE_DEF[./IDENT[@text='trimmer']]/ASSIGN/LAMBDA/PARAMETERS" + + "/PARAMETER_DEF/IDENT[@text='s']" + ); + + runVerifications(moduleConfig, fileToProcess, expectedViolation, + expectedXpathQueries); + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName1.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName1.java new file mode 100644 index 000000000000..f720010fe9de --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName1.java @@ -0,0 +1,9 @@ +package org.checkstyle.suppressionxpathfilter.lambdaparametername; + +import java.util.function.Function; + +public class SuppressionXpathRegressionLambdaParameterName1 { + void test() { + Function trimmer = S -> S.trim(); // warn + } +} diff --git a/src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName2.java b/src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName2.java new file mode 100644 index 000000000000..8b818ed11e0b --- /dev/null +++ b/src/it/resources/org/checkstyle/suppressionxpathfilter/lambdaparametername/SuppressionXpathRegressionLambdaParameterName2.java @@ -0,0 +1,9 @@ +package org.checkstyle.suppressionxpathfilter.lambdaparametername; + +import java.util.function.Function; + +public class SuppressionXpathRegressionLambdaParameterName2 { + void test() { + Function trimmer = (s) -> s.trim(); // warn + } +} diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java index 9ddc613823e1..22ada2fcb849 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathFilter.java @@ -80,9 +80,6 @@ * JavadocType * *
    • - * LambdaParameterName - *
    • - *
    • * MethodCount *
    • *
    • diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java index 3a0b7e5f4e20..827ce740de90 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XpathRegressionTest.java @@ -65,7 +65,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport { "JavadocContentLocation", "JavadocMethod", "JavadocType", - "LambdaParameterName", "MethodCount", "MissingJavadocMethod", "MissingJavadocType", diff --git a/src/xdocs/config_filters.xml b/src/xdocs/config_filters.xml index 38c0c9a80cd1..d23cc40e221b 100644 --- a/src/xdocs/config_filters.xml +++ b/src/xdocs/config_filters.xml @@ -914,7 +914,6 @@ public class UserService {
    • JavadocContentLocation
    • JavadocMethod
    • JavadocType
    • -
    • LambdaParameterName
    • MethodCount
    • MissingJavadocMethod
    • MissingJavadocType
    • From 3b3f7240264dfb7045fc08ab28c5d731ee989ca7 Mon Sep 17 00:00:00 2001 From: Gaurav Punjabi Date: Sat, 21 Mar 2020 16:42:44 +0530 Subject: [PATCH 16/17] Issue #7920: Resolve Pitest Issues - RedundantImportCheck --- .ci/pitest.sh | 1 - .../tools/checkstyle/checks/imports/RedundantImportCheck.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.ci/pitest.sh b/.ci/pitest.sh index 665d6d42463d..6632c1d5c96c 100755 --- a/.ci/pitest.sh +++ b/.ci/pitest.sh @@ -86,7 +86,6 @@ pitest-imports) "PkgImportControl.java.html:
              if (regex || parent.regex) {
      " "PkgImportControl.java.html:
              if (regex) {
      " "PkgImportRule.java.html:
              if (isRegExp()) {
      " - "RedundantImportCheck.java.html:
                  else if (pkgName != null && isFromPackage(imp.getText(), pkgName)) {
      " "UnusedImportsCheck.java.html:
                  if (collect) {
      " ); checkPitestReport "${ignoredItems[@]}" diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java index 4c71e4eeb688..e35c3a95a4a7 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/RedundantImportCheck.java @@ -150,7 +150,7 @@ private static boolean isFromPackage(String importName, String pkg) { // So '.' must be present in member name and we are not checking for it final int index = importName.lastIndexOf('.'); final String front = importName.substring(0, index); - return front.equals(pkg); + return pkg.equals(front); } } From dabbebac4b52591a8689b6f2e1b81d98e58d0a8e Mon Sep 17 00:00:00 2001 From: liach Date: Thu, 19 Mar 2020 00:07:23 -0500 Subject: [PATCH 17/17] Issue #5969: Respect and expect tab width in column number check for lambda indentations --- .../checks/indentation/LambdaHandler.java | 14 +-- .../indentation/IndentationCheckTest.java | 41 +++++++++ .../indentation/InputIndentationLambda3.java | 89 +++++++++++++++++++ .../indentation/InputIndentationLambda4.java | 64 +++++++++++++ .../indentation/InputIndentationLambda5.java | 17 ++++ 5 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda3.java create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda4.java create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda5.java diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java index d465ce4b3bbc..15d80f95e21e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LambdaHandler.java @@ -66,7 +66,7 @@ protected IndentLevel getIndentImpl() { // If the start of the lambda is the first element on the line; // assume line wrapping with respect to its parent. final DetailAST firstChild = getMainAst().getFirstChild(); - if (getLineStart(firstChild) == firstChild.getColumnNo()) { + if (getLineStart(firstChild) == expandedTabsColumnNo(firstChild)) { level = new IndentLevel(level, getIndentCheck().getLineWrappingIndentation()); } @@ -77,19 +77,19 @@ protected IndentLevel getIndentImpl() { public void checkIndentation() { // If the argument list is the first element on the line final DetailAST firstChild = getMainAst().getFirstChild(); - if (getLineStart(firstChild) == firstChild.getColumnNo()) { + if (getLineStart(firstChild) == expandedTabsColumnNo(firstChild)) { final IndentLevel level = getIndent(); - if (!level.isAcceptable(firstChild.getColumnNo())) { - logError(firstChild, "arguments", firstChild.getColumnNo(), level); + if (!level.isAcceptable(expandedTabsColumnNo(firstChild))) { + logError(firstChild, "arguments", expandedTabsColumnNo(firstChild), level); } } // If the "->" is the first element on the line, assume line wrapping. - if (getLineStart(getMainAst()) == getMainAst().getColumnNo()) { + if (getLineStart(getMainAst()) == expandedTabsColumnNo(getMainAst())) { final IndentLevel level = new IndentLevel(getIndent(), getIndentCheck().getLineWrappingIndentation()); - if (!level.isAcceptable(getMainAst().getColumnNo())) { - logError(getMainAst(), "", getMainAst().getColumnNo(), level); + if (!level.isAcceptable(expandedTabsColumnNo(getMainAst()))) { + logError(getMainAst(), "", expandedTabsColumnNo(getMainAst()), level); } } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index a39ea6f6fdbd..d46810a21341 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -1759,6 +1759,47 @@ public void testLambda2() throws Exception { verifyWarns(checkConfig, getPath("InputIndentationLambda2.java"), expected); } + @Test + public void testLambda3() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + checkConfig.addAttribute("basicOffset", "4"); + checkConfig.addAttribute("lineWrappingIndentation", "8"); + final String[] expected = { + "15: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8), + "23: " + getCheckMessage(MSG_ERROR_MULTI, "lambda arguments", 20, "12, 16"), + "29: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8), + "30: " + getCheckMessage(MSG_CHILD_ERROR, "block", 12, 16), + "31: " + getCheckMessage(MSG_ERROR, "block rcurly", 8, 12), + "43: " + getCheckMessage(MSG_ERROR, "lambda arguments", 20, 16), + "65: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 12, 8), + "67: " + getCheckMessage(MSG_ERROR, "lambda", 28, 24), + "87: " + getCheckMessage(MSG_ERROR, "method def rcurly", 12, 8), + }; + verifyWarns(checkConfig, getPath("InputIndentationLambda3.java"), expected); + } + + @Test + public void testLambda4() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "4"); + checkConfig.addAttribute("basicOffset", "4"); + checkConfig.addAttribute("lineWrappingIndentation", "8"); + final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; + verifyWarns(checkConfig, getPath("InputIndentationLambda4.java"), expected); + } + + @Test + public void testLambda5() throws Exception { + final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "3"); + checkConfig.addAttribute("basicOffset", "3"); + checkConfig.addAttribute("caseIndent", "0"); + checkConfig.addAttribute("lineWrappingIndentation", "6"); + final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; + verifyWarns(checkConfig, getPath("InputIndentationLambda5.java"), expected); + } + @Test public void testSeparatedStatements() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(IndentationCheck.class); diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda3.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda3.java new file mode 100644 index 000000000000..87b928b03f1a --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda3.java @@ -0,0 +1,89 @@ +//a comment //indent:0 exp:0 +package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0 + +import java.util.Arrays; //indent:0 exp:0 +import java.util.List; //indent:0 exp:0 +import java.util.Optional; //indent:0 exp:0 +import java.util.function.BinaryOperator; //indent:0 exp:0 +import java.util.function.Consumer; //indent:0 exp:0 +import java.util.stream.Collectors; //indent:0 exp:0 +import java.util.stream.Stream; //indent:0 exp:0 + + +public class InputIndentationLambda3 { //indent:0 exp:0 + public Consumer par(Consumer f1, Consumer f2) { //indent:4 exp:4 + return f2; //indent:12 exp:8 warn + } //indent:4 exp:4 + + private void print(int i) { //indent:4 exp:4 + } //indent:4 exp:4 + + public Consumer returnFunctionOfLambda() { //indent:4 exp:4 + return par( //indent:8 exp:8 + (x) -> print(x * 1), //indent:20 exp:12,16 warn + (x) -> print(x * 2) //indent:16 exp:16 + ); //indent:8 exp:8 + } //indent:4 exp:4 + + public static BinaryOperator returnLambda() { //indent:4 exp:4 + return (t1, t2) -> { //indent:12 exp:8 warn + return t1; //indent:12 exp:16 warn + }; //indent:8 exp:12 warn + } //indent:4 exp:4 + + class TwoParams { //indent:4 exp:4 + TwoParams(Consumer c1, Consumer c2) { //indent:8 exp:8 + } //indent:8 exp:8 + } //indent:4 exp:4 + + public void makeTwoParams() { //indent:4 exp:4 + TwoParams t0 = new TwoParams( //indent:8 exp:8 + intValueA //indent:16 exp:16 + -> print(intValueA * 1), //indent:24 exp:24 + (x) -> //indent:20 exp:16 warn + print(x * 2) //indent:24 exp:24 + ); //indent:8 exp:8 + + TwoParams t1 = new TwoParams( //indent:8 exp:8 + x //indent:16 exp:16 + -> print(x * 1), //indent:24 exp:24 + (aggregateValue) -> //indent:16 exp:16 + print(aggregateValue * 2)); //indent:24 exp:24 + } //indent:4 exp:4 + + // see https://github.com/checkstyle/checkstyle/issues/5969 //indent:4 exp:4 + List test(List input) { //indent:4 exp:4 + return input.stream() //indent:8 exp:8 + .flatMap(each -> Arrays.stream(each.split(""))) //indent:16 exp:16 + .flatMap(in -> Arrays.stream(in.split(""))) //indent:16 exp:16 + // only 2 char parameter has violation //indent:16 exp:16 + .map(Integer::valueOf) //indent:16 exp:16 + .collect(Collectors.toList()); //indent:16 exp:16 + } //indent:4 exp:4 + + String test2(String input) { //indent:4 exp:4 + return Optional.ofNullable(input) //indent:12 exp:8 warn + .filter(in //indent:16 exp:16 + -> //indent:28 exp:24 warn + in.contains("e")) //indent:20 exp:20 + .filter(inp -> inp.contains("e")) //indent:12 exp:12 + // only 3 char parameter has violation //indent:16 exp:16 + .orElse(null); //indent:16 exp:16 + } //indent:4 exp:4 + + // see https://github.com/checkstyle/checkstyle/issues/7675 //indent:4 exp:4 + public void test(Stream> stream) { //indent:4 exp:4 + stream //indent:8 exp:8 +.sorted() //indent:0 exp:0 +.filter(ps -> ps instanceof Inner) //indent:0 exp:0 + .map(ps -> ((Inner) ps).getPropertyNames()) //indent:16 exp:16 + // This line above originally breaks //indent:16 exp:16 + .forEach(System.out::println); //indent:16 exp:16 + } //indent:4 exp:4 + + private static class Inner { //indent:4 exp:4 + String[] getPropertyNames() { //indent:8 exp:8 + return new String[] {"a", "b"}; //indent:12 exp:12 + } //indent:12 exp:8 warn + } //indent:4 exp:4 +} //indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda4.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda4.java new file mode 100644 index 000000000000..3bcbaf2ffc84 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda4.java @@ -0,0 +1,64 @@ +//a comment //indent:0 exp:0 +package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0 + +import java.util.Arrays; //indent:0 exp:0 +import java.util.List; //indent:0 exp:0 +import java.util.Optional; //indent:0 exp:0 +import java.util.function.BinaryOperator; //indent:0 exp:0 +import java.util.function.Consumer; //indent:0 exp:0 +import java.util.stream.Collectors; //indent:0 exp:0 + + +public class InputIndentationLambda4 { //indent:0 exp:0 + public Consumer par(Consumer f1, Consumer f2) { //indent:4 exp:4 + return f2; //indent:8 exp:8 + } //indent:4 exp:4 + + private void print(int i) { //indent:4 exp:4 + } //indent:4 exp:4 + + public Consumer returnFunctionOfLambda() { //indent:4 exp:4 + return par( //indent:8 exp:8 + (x) -> print(x * 1), //indent:16 exp:16 + (x) -> print(x * 2) //indent:16 exp:16 + ); //indent:8 exp:8 + } //indent:4 exp:4 + + public static BinaryOperator returnLambda() { //indent:4 exp:4 + return (t1, t2) -> { //indent:8 exp:8 + return t1; //indent:12 exp:12 + }; //indent:8 exp:8 + } //indent:4 exp:4 + + class TwoParams { //indent:4 exp:4 + TwoParams(Consumer c1, Consumer c2) { //indent:8 exp:8 + } //indent:8 exp:8 + } //indent:4 exp:4 + + public void makeTwoParams() { //indent:4 exp:4 + TwoParams t0 = new TwoParams( //indent:8 exp:8 + (x) -> print(x * 1), //indent:16 exp:16 + (x) -> print(x * 2) //indent:16 exp:16 + ); //indent:8 exp:8 + + TwoParams t1 = new TwoParams( //indent:8 exp:8 + (x) -> print(x * 1), //indent:16 exp:16 + (x) -> print(x * 2)); //indent:16 exp:16 + } //indent:4 exp:4 + + // see https://github.com/checkstyle/checkstyle/issues/5969 //indent:4 exp:4 + List test(List input) { //indent:4 exp:4 + return input.stream() //indent:8 exp:8 + .flatMap(in -> Arrays.stream(in.split(""))) //indent:16 exp:16 + // only 2 char parameter has violation //indent:16 exp:16 + .map(Integer::valueOf) //indent:16 exp:16 + .collect(Collectors.toList()); //indent:16 exp:16 + } //indent:4 exp:4 + + String test2(String input) { //indent:4 exp:4 + return Optional.ofNullable(input) //indent:8 exp:8 + .filter(inp -> inp.contains("e")) //indent:16 exp:16 + // only 3 char parameter has violation //indent:16 exp:16 + .orElse(null); //indent:16 exp:16 + } //indent:4 exp:4 +} //indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda5.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda5.java new file mode 100644 index 000000000000..3b93ad5aa15a --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/indentation/InputIndentationLambda5.java @@ -0,0 +1,17 @@ +package com.puppycrawl.tools.checkstyle.checks.indentation.indentation; //indent:0 exp:0 +// see https://github.com/checkstyle/checkstyle/issues/7675 //indent:0 exp:0 +import java.util.stream.Stream; //indent:0 exp:0 +public class InputIndentationLambda5 { //indent:0 exp:0 + public void test(Stream> stream) { //indent:3 exp:3 + stream //indent:6 exp:6 + .filter(ps -> ps instanceof Inner) //indent:12 exp:12 + .map(ps -> ((Inner) ps).getPropertyNames()) //indent:12 exp:12 + .forEach(System.out::println); //indent:12 exp:12 + } //indent:3 exp:3 + + private static class Inner { //indent:3 exp:3 + String[] getPropertyNames() { //indent:6 exp:6 + return new String[] {"a", "b"}; //indent:9 exp:9 + } //indent:6 exp:6 + } //indent:3 exp:3 +} //indent:0 exp:0