From 4c384214a90272fe239b76e35ec5bcb87ef4b080 Mon Sep 17 00:00:00 2001 From: ychulovskyy Date: Fri, 21 Nov 2014 19:02:43 +0100 Subject: [PATCH] InnerTypeLast within methods #6 --- .../checks/design/InnerTypeLastCheck.java | 11 ++++--- .../checks/design/InnerTypeLastCheckTest.java | 11 ++----- .../design/InputInnerClassCheck.java | 29 ++++++++++++++----- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java index 1ac07e41e..c31767b54 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks.design; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.ScopeUtils; import com.puppycrawl.tools.checkstyle.api.TokenTypes; /** @@ -50,12 +51,10 @@ public class InnerTypeLastCheck extends Check } else { DetailAST nextSibling = aAST.getNextSibling(); - while (null != nextSibling - && ((nextSibling.getType() != TokenTypes.CLASS_DEF) - || (nextSibling.getType() != TokenTypes.INTERFACE_DEF))) - { - if (nextSibling.getType() == TokenTypes.VARIABLE_DEF - || nextSibling.getType() == TokenTypes.METHOD_DEF) + while (null != nextSibling) { + if (!ScopeUtils.inCodeBlock(aAST) + && (nextSibling.getType() == TokenTypes.VARIABLE_DEF + || nextSibling.getType() == TokenTypes.METHOD_DEF)) { log(nextSibling.getLineNo(), nextSibling.getColumnNo(), "arrangement.members.before.inner"); diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckTest.java index 895317fd8..f49fc7898 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckTest.java @@ -20,9 +20,10 @@ package com.puppycrawl.tools.checkstyle.checks.design; import com.puppycrawl.tools.checkstyle.BaseCheckTestSupport; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; -import java.io.File; import org.junit.Test; +import java.io.File; + public class InnerTypeLastCheckTest extends BaseCheckTestSupport { @Test @@ -31,17 +32,11 @@ public class InnerTypeLastCheckTest extends BaseCheckTestSupport final DefaultConfiguration checkConfig = createCheckConfig(InnerTypeLastCheck.class); final String[] expected = { - "15:17: Fields and methods should be before inner classes.", - "25:17: Fields and methods should be before inner classes.", - "26:17: Fields and methods should be before inner classes.", - "39:25: Fields and methods should be before inner classes.", - "40:25: Fields and methods should be before inner classes.", "44:9: Fields and methods should be before inner classes.", - "60:25: Fields and methods should be before inner classes.", - "61:25: Fields and methods should be before inner classes.", "65:9: Fields and methods should be before inner classes.", "69:9: Fields and methods should be before inner classes.", "78:5: Fields and methods should be before inner classes.", + "95:9: Fields and methods should be before inner classes.", }; verify(checkConfig, getPath("design" + File.separator + "InputInnerClassCheck.java"), expected); diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputInnerClassCheck.java b/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputInnerClassCheck.java index fdc92c786..ffb376d78 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputInnerClassCheck.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputInnerClassCheck.java @@ -12,7 +12,7 @@ public class InputInnerClassCheck { } } - int test3 = 300; //error + int test3 = 300; } public void methodTestInner2() { @@ -22,8 +22,8 @@ public class InputInnerClassCheck { int test6 = 500; } - int test6 = 600; //error - int test8 = 800; //error + int test6 = 600; + int test8 = 800; } class Inner1 { @@ -36,8 +36,8 @@ public class InputInnerClassCheck { int test10 = 500; } - int test11 = 600; //error - int test12 = 800; //error + int test11 = 600; + int test12 = 800; } } @@ -57,8 +57,8 @@ class Temp2 { int test10 = 500; } - int test11 = 600; //error - int test12 = 800; //error + int test11 = 600; + int test12 = 800; } } @@ -80,3 +80,18 @@ class Temp3 { return new int[]{1, }; } } + +class Temp4 { + + class InnerCheck { + class InnerInnerCheck { + private int a = 0; + } + + class InnerInnerCheck2 { + private int a = 0; + } + + private int I = 0; // error + } +}