From 9324fdcf34b60f100b43dd18f52898c6418893eb Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 11 Aug 2014 15:42:21 +0400 Subject: [PATCH] VariableDeclarationUsageDistanceCheck was update. Create new extended message in case ignoreFinal=true #239 --- ...VariableDeclarationUsageDistanceCheck.java | 15 ++++- .../checks/coding/messages.properties | 1 + ...ableDeclarationUsageDistanceCheckTest.java | 58 ++++++++++--------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java index 9655dd9a8..fbae952d1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java @@ -172,6 +172,11 @@ public class VariableDeclarationUsageDistanceCheck extends Check */ public static final String MSG_KEY = "variable.declaration.usage.distance"; + /** + * Warning message key. + */ + public static final String MSG_KEY_EXT = "variable.declaration.usage.distance.extend"; + /** * Default value of distance between declaration of variable and its first * usage. @@ -275,8 +280,14 @@ public class VariableDeclarationUsageDistanceCheck extends Check if (dist > mAllowedDistance && !isInitializationSequence(variableUsageAst, variable.getText())) { - log(variable.getLineNo(), - MSG_KEY, variable.getText(), dist, mAllowedDistance); + if (mIgnoreFinal) { + log(variable.getLineNo(), + MSG_KEY_EXT, variable.getText(), dist, mAllowedDistance); + } + else { + log(variable.getLineNo(), + MSG_KEY, variable.getText(), dist, mAllowedDistance); + } } } } diff --git a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties index 1abd0aa50..b9047f154 100644 --- a/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties +++ b/src/main/resources/com/puppycrawl/tools/checkstyle/checks/coding/messages.properties @@ -64,4 +64,5 @@ unnecessary.paren.return=Unnecessary parentheses around return value. unnecessary.paren.string=Unnecessary parentheses around string {0}. package.dir.mismatch=Package declaration does not match directory ''{0}''. variable.declaration.usage.distance=Distance between variable ''{0}'' declaration and its first usage is {1}, but allowed {2}. +variable.declaration.usage.distance.extend=Distance between variable ''{0}'' declaration and its first usage is {1}, but allowed {2}. Consider to make that variable as final if you still need to store its value in advance (before method calls that might do side effect on original value). overload.methods.declaration=Overload methods should not be split. Previous overloaded method located at line ''{0}''. diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java index b7b41f549..a9db2bee0 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java @@ -19,6 +19,8 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY; +import static com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.MSG_KEY_EXT; + import static java.text.MessageFormat.format; import org.junit.Test; @@ -171,34 +173,34 @@ public class VariableDeclarationUsageDistanceCheckTest extends checkConfig.addAttribute("validateBetweenScopes", "true"); checkConfig.addAttribute("ignoreFinal", "true"); final String[] expected = { - "30: " + getCheckMessage(MSG_KEY, "a", 2, 1), - "38: " + getCheckMessage(MSG_KEY, "temp", 2, 1), - "44: " + getCheckMessage(MSG_KEY, "temp", 2, 1), - "57: " + getCheckMessage(MSG_KEY, "count", 2, 1), - "71: " + getCheckMessage(MSG_KEY, "count", 4, 1), - "96: " + getCheckMessage(MSG_KEY, "arg", 2, 1), - "144: " + getCheckMessage(MSG_KEY, "m", 3, 1), - "145: " + getCheckMessage(MSG_KEY, "n", 2, 1), - "184: " + getCheckMessage(MSG_KEY, "result", 2, 1), - "219: " + getCheckMessage(MSG_KEY, "t", 5, 1), - "222: " + getCheckMessage(MSG_KEY, "c", 3, 1), - "223: " + getCheckMessage(MSG_KEY, "d2", 3, 1), - "260: " + getCheckMessage(MSG_KEY, "selected", 2, 1), - "261: " + getCheckMessage(MSG_KEY, "model", 2, 1), - "287: " + getCheckMessage(MSG_KEY, "sw", 2, 1), - "300: " + getCheckMessage(MSG_KEY, "wh", 2, 1), - "343: " + getCheckMessage(MSG_KEY, "green", 2, 1), - "344: " + getCheckMessage(MSG_KEY, "blue", 3, 1), - "454: " + getCheckMessage(MSG_KEY, "aOpt", 3, 1), - "455: " + getCheckMessage(MSG_KEY, "bOpt", 2, 1), - "471: " + getCheckMessage(MSG_KEY, "l1", 3, 1), - "471: " + getCheckMessage(MSG_KEY, "l2", 2, 1), - "479: " + getCheckMessage(MSG_KEY, "myOption", 7, 1), - "491: " + getCheckMessage(MSG_KEY, "myOption", 6, 1), - "504: " + getCheckMessage(MSG_KEY, "count", 4, 1), - "505: " + getCheckMessage(MSG_KEY, "files", 2, 1), - "540: " + getCheckMessage(MSG_KEY, "id", 2, 1), - "542: " + getCheckMessage(MSG_KEY, "parentId", 3, 1), + "30: " + getCheckMessage(MSG_KEY_EXT, "a", 2, 1), + "38: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1), + "44: " + getCheckMessage(MSG_KEY_EXT, "temp", 2, 1), + "57: " + getCheckMessage(MSG_KEY_EXT, "count", 2, 1), + "71: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1), + "96: " + getCheckMessage(MSG_KEY_EXT, "arg", 2, 1), + "144: " + getCheckMessage(MSG_KEY_EXT, "m", 3, 1), + "145: " + getCheckMessage(MSG_KEY_EXT, "n", 2, 1), + "184: " + getCheckMessage(MSG_KEY_EXT, "result", 2, 1), + "219: " + getCheckMessage(MSG_KEY_EXT, "t", 5, 1), + "222: " + getCheckMessage(MSG_KEY_EXT, "c", 3, 1), + "223: " + getCheckMessage(MSG_KEY_EXT, "d2", 3, 1), + "260: " + getCheckMessage(MSG_KEY_EXT, "selected", 2, 1), + "261: " + getCheckMessage(MSG_KEY_EXT, "model", 2, 1), + "287: " + getCheckMessage(MSG_KEY_EXT, "sw", 2, 1), + "300: " + getCheckMessage(MSG_KEY_EXT, "wh", 2, 1), + "343: " + getCheckMessage(MSG_KEY_EXT, "green", 2, 1), + "344: " + getCheckMessage(MSG_KEY_EXT, "blue", 3, 1), + "454: " + getCheckMessage(MSG_KEY_EXT, "aOpt", 3, 1), + "455: " + getCheckMessage(MSG_KEY_EXT, "bOpt", 2, 1), + "471: " + getCheckMessage(MSG_KEY_EXT, "l1", 3, 1), + "471: " + getCheckMessage(MSG_KEY_EXT, "l2", 2, 1), + "479: " + getCheckMessage(MSG_KEY_EXT, "myOption", 7, 1), + "491: " + getCheckMessage(MSG_KEY_EXT, "myOption", 6, 1), + "504: " + getCheckMessage(MSG_KEY_EXT, "count", 4, 1), + "505: " + getCheckMessage(MSG_KEY_EXT, "files", 2, 1), + "540: " + getCheckMessage(MSG_KEY_EXT, "id", 2, 1), + "542: " + getCheckMessage(MSG_KEY_EXT, "parentId", 3, 1), }; verify(checkConfig, getPath("coding/InputVariableDeclarationUsageDistanceCheck.java"), expected); }