diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java b/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java index 47061b95d..89133fbdb 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/ScopeUtils.java @@ -43,20 +43,17 @@ public final class ScopeUtils { */ public static Scope getScopeFromMods(DetailAST aMods) { Scope retVal = Scope.PACKAGE; // default scope - for (AST token = aMods.getFirstChild(); - token != null; - token = token.getNextSibling()) { + for (AST token = aMods.getFirstChild(); token != null + && retVal == Scope.PACKAGE; + token = token.getNextSibling()) { if ("public".equals(token.getText())) { retVal = Scope.PUBLIC; - break; } else if ("protected".equals(token.getText())) { retVal = Scope.PROTECTED; - break; } else if ("private".equals(token.getText())) { retVal = Scope.PRIVATE; - break; } } return retVal; @@ -105,20 +102,19 @@ public final class ScopeUtils { // Loop up looking for a containing interface block for (DetailAST token = aAST.getParent(); - token != null; + token != null && !retVal; token = token.getParent()) { + final int type = token.getType(); - if (type == TokenTypes.CLASS_DEF - || type == TokenTypes.ENUM_DEF - || type == TokenTypes.ANNOTATION_DEF) { - break; // in a class, enum or annotation - } - else if (type == TokenTypes.LITERAL_NEW) { - break; // inner implementation - } - else if (type == TokenTypes.INTERFACE_DEF) { + + if (type == TokenTypes.INTERFACE_DEF) { retVal = true; - break; + } + else if (type == TokenTypes.CLASS_DEF + || type == TokenTypes.ENUM_DEF + || type == TokenTypes.ANNOTATION_DEF + || type == TokenTypes.LITERAL_NEW) { + break; // in a class, enum or annotation } } @@ -137,21 +133,19 @@ public final class ScopeUtils { // Loop up looking for a containing interface block for (DetailAST token = aAST.getParent(); - token != null; + token != null && !retVal; token = token.getParent()) { final int type = token.getType(); - if (type == TokenTypes.CLASS_DEF + if (type == TokenTypes.ANNOTATION_DEF) { + retVal = true; + } + else if (type == TokenTypes.CLASS_DEF || type == TokenTypes.ENUM_DEF - || type == TokenTypes.INTERFACE_DEF) { + || type == TokenTypes.INTERFACE_DEF + || type == TokenTypes.LITERAL_NEW) { break; // in a class, enum or interface } - else if (type == TokenTypes.LITERAL_NEW) { - break; // inner implementation - } - else if (type == TokenTypes.ANNOTATION_DEF) { - retVal = true; - break; - } + } return retVal; @@ -181,20 +175,17 @@ public final class ScopeUtils { // Loop up looking for a containing interface block for (DetailAST token = aAST.getParent(); - token != null; + token != null && !retVal; token = token.getParent()) { final int type = token.getType(); - if (type == TokenTypes.INTERFACE_DEF - || type == TokenTypes.ANNOTATION_DEF - || type == TokenTypes.CLASS_DEF) { - break; // in an interface, annotation or class - } - else if (type == TokenTypes.LITERAL_NEW) { - break; // inner implementation, enums can't be inner classes - } - else if (type == TokenTypes.ENUM_DEF) { + if (type == TokenTypes.ENUM_DEF) { retVal = true; - break; + } + else if (type == TokenTypes.INTERFACE_DEF + || type == TokenTypes.ANNOTATION_DEF + || type == TokenTypes.CLASS_DEF + || type == TokenTypes.LITERAL_NEW) { + break; // in an interface, annotation or class } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java index 63a3a52f2..044c57dc0 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/imports/CustomImportOrderCheck.java @@ -671,10 +671,7 @@ public class CustomImportOrderCheck extends Check { int result = 0; final String[] import1Tokens = import1.split("\\."); final String[] import2Tokens = import2.split("\\."); - for (int i = 0; i < import1Tokens.length; i++) { - if (i == import2Tokens.length) { - break; - } + for (int i = 0; i < import1Tokens.length && i != import2Tokens.length; i++) { final String import1Token = import1Tokens[i]; final String import2Token = import2Tokens[i]; result = import1Token.compareTo(import2Token); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/ScopeUtilsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/ScopeUtilsTest.java index cb1d7b3cf..db3fa7475 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/ScopeUtilsTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/ScopeUtilsTest.java @@ -51,6 +51,20 @@ public class ScopeUtilsTest { Assert.assertFalse(ScopeUtils.inEnumBlock(ast2)); } + @Test + public void testInEnumBlockWithEnum() throws ReflectiveOperationException { + DetailAST ast0 = new DetailAST(); + ast0.setType(TokenTypes.OBJBLOCK); + DetailAST ast1 = new DetailAST(); + ast1.setType(TokenTypes.ENUM_DEF); + ast0.addChild(ast1); + DetailAST ast2 = new DetailAST(); + ast2.setType(TokenTypes.MODIFIERS); + ast1.addChild(ast2); + + Assert.assertTrue(ScopeUtils.inEnumBlock(ast2)); + } + @Test public void testInEnumBlockInInterface() throws ReflectiveOperationException { DetailAST ast = new DetailAST(); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckInput3.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckInput3.java new file mode 100644 index 000000000..4b1d6aa08 --- /dev/null +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckInput3.java @@ -0,0 +1,38 @@ +package com.puppycrawl.tools.checkstyle.checks.sizes; + +public class MethodCountCheckInput3 { + + /** + * Dummy inner class to check that the inner-classes methods are not counted for the outer class. + */ + /** + * Dummy method doing nothing + */ + void doNothing50() { + } + + /** + * Dummy method doing nothing + */ + void doNothing51() { + } + + /** + * Dummy method doing nothing + */ + void doNothing52() { + } + + /** + * Dummy method doing nothing + */ + void doNothing53() { + } + + /** + * Dummy method doing nothing + */ + void doNothing54() { + } + +} \ No newline at end of file diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckTest.java index 46a08fde8..04ef74f9c 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/sizes/MethodCountCheckTest.java @@ -102,4 +102,18 @@ public class MethodCountCheckTest extends BaseCheckTestSupport { verify(checkConfig, getSrcPath("checks/sizes/MethodCountCheckInput2.java"), expected); } + + @Test + public void testWithPackageModifier() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(MethodCountCheck.class); + checkConfig.addAttribute("maxPrivate", "0"); + checkConfig.addAttribute("maxTotal", "2"); + + final String[] expected = { + "3: " + getCheckMessage(MSG_MANY_METHODS, 5, 2), + }; + + verify(checkConfig, + getSrcPath("checks/sizes/MethodCountCheckInput3.java"), expected); + } }