diff options
author | vasilyeva <valeria.vasylieva@gmail.com> | 2017-05-07 22:46:37 +0300 |
---|---|---|
committer | Roman Ivanov <romani@users.noreply.github.com> | 2017-05-07 14:49:37 -0700 |
commit | a6d025d0c72777b4dab2a5f8b27638b644a4fa72 (patch) | |
tree | f33db4bee5e2636b9b2e0e1fb43d3a262a758fed | |
parent | b666a23cc595af7cc1a8bf4d4d49744025adfa11 (diff) | |
download | checkstyle-a6d025d0c72777b4dab2a5f8b27638b644a4fa72.tar.gz |
Issue #3348: add validation of try with resources to the LocalFinalVariableName check
5 files changed, 118 insertions, 6 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheck.java index c2ce576c3..558ab2eb9 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheck.java @@ -27,8 +27,8 @@ import com.puppycrawl.tools.checkstyle.utils.ScopeUtils; /** * <p> * Checks that local final variable names conform to a format specified - * by the format property. A catch parameter is considered to be - * a local variable.The format is a + * by the format property. A catch parameter and resources in try statements + * are considered to be a local variables.The format is a * {@link java.util.regex.Pattern regular expression} and defaults to * <strong>^[a-z][a-zA-Z0-9]*$</strong>. * </p> @@ -67,6 +67,7 @@ public class LocalFinalVariableNameCheck return new int[] { TokenTypes.VARIABLE_DEF, TokenTypes.PARAMETER_DEF, + TokenTypes.RESOURCE, }; } @@ -79,7 +80,8 @@ public class LocalFinalVariableNameCheck protected final boolean mustCheckName(DetailAST ast) { final DetailAST modifiersAST = ast.findFirstToken(TokenTypes.MODIFIERS); - final boolean isFinal = modifiersAST.branchContains(TokenTypes.FINAL); + final boolean isFinal = ast.getType() == TokenTypes.RESOURCE + || modifiersAST.branchContains(TokenTypes.FINAL); return isFinal && ScopeUtils.isLocalVariableDef(ast); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/utils/ScopeUtils.java b/src/main/java/com/puppycrawl/tools/checkstyle/utils/ScopeUtils.java index f9c0eaa9c..05b902bad 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/ScopeUtils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/ScopeUtils.java @@ -263,6 +263,10 @@ public final class ScopeUtils { final DetailAST parent = node.getParent(); localVariableDef = parent.getType() == TokenTypes.LITERAL_CATCH; } + + if (node.getType() == TokenTypes.RESOURCE) { + localVariableDef = true; + } return localVariableDef; } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java index e0ddbfec9..c9d6c0fdc 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/naming/LocalFinalVariableNameCheckTest.java @@ -95,7 +95,26 @@ public class LocalFinalVariableNameCheckTest final int[] expected = { TokenTypes.VARIABLE_DEF, TokenTypes.PARAMETER_DEF, + TokenTypes.RESOURCE, }; assertArrayEquals(expected, actual); } + + @Test + public void testTryWithResources() throws Exception { + final DefaultConfiguration checkConfig = + createCheckConfig(LocalFinalVariableNameCheck.class); + checkConfig.addAttribute("format", "[A-Z]+"); + + final String pattern = "[A-Z]+"; + + final String[] expected = { + "23:30: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern), + "33:29: " + getCheckMessage(MSG_INVALID_PATTERN, "br", pattern), + "53:22: " + getCheckMessage(MSG_INVALID_PATTERN, "zf", pattern), + "71:30: " + getCheckMessage(MSG_INVALID_PATTERN, "fis8859_1", pattern), + "73:32: " + getCheckMessage(MSG_INVALID_PATTERN, "isrutf8", pattern), + }; + verify(checkConfig, getPath("InputLocalFinalVariableNameTryResources.java"), expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/InputLocalFinalVariableNameTryResources.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/InputLocalFinalVariableNameTryResources.java new file mode 100644 index 000000000..f9ab90a51 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/naming/localfinalvariablename/InputLocalFinalVariableNameTryResources.java @@ -0,0 +1,85 @@ +package com.puppycrawl.tools.checkstyle.checks.naming.localfinalvariablename; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.PropertyResourceBundle; +import java.util.zip.ZipFile; + +/** + * Contains test cases regarding checking local + * final variable name in try-with-resources statement: + * + * @author Valeria Vasylieva + **/ +public class InputLocalFinalVariableNameTryResources { + + void method() throws Exception { + final String fileName = "Test"; + final BufferedReader br = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + try { + } finally { + br.close(); + } + } + + void method2() throws Exception { + final String fileName = "Test"; + try (BufferedReader br = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8))) { + } finally { + + } + } + + void method3() throws Exception { + final String fileName = "Test"; + try (final BufferedReader BR = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8))) { + } finally { + + } + } + + void method4() throws Exception { + final String fileName = "Test"; + try (BufferedReader BR = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + ZipFile zf = new ZipFile(fileName)) { + } finally { + + } + } + + void method5() throws Exception { + final String fileName = "Test"; + try (BufferedReader BR = new BufferedReader(new InputStreamReader( + new FileInputStream(fileName), StandardCharsets.UTF_8)); + ZipFile ZF = new ZipFile(fileName)) { + } finally { + + } + } + + void method6() throws Exception { + String srcDir = System.getProperty("test.src", "."); + try (FileInputStream fis8859_1 = new FileInputStream(new File(srcDir, "Bug6204853.properties")); + FileInputStream fisUTF8 = new FileInputStream(new File(srcDir, "Bug6204853_Utf8.properties")); + InputStreamReader isrutf8 = new InputStreamReader(fisUTF8, "UTF-8")) { + PropertyResourceBundle bundleUtf8 = new PropertyResourceBundle(isrutf8); + PropertyResourceBundle bundle = new PropertyResourceBundle(fis8859_1); + String[] arrayUtf8 = {"1", "2", "3"}; + String[] array = {"key1", "key2"}; + if (!Arrays.equals(arrayUtf8, array)) { + throw new RuntimeException("Error message"); + } + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + } +} diff --git a/src/xdocs/config_naming.xml b/src/xdocs/config_naming.xml index bb27871cf..6b301fb07 100644 --- a/src/xdocs/config_naming.xml +++ b/src/xdocs/config_naming.xml @@ -608,7 +608,7 @@ <subsection name="Description"> <p> Validates identifiers for local, <code>final</code> variables, including - <code>catch</code> parameters. + <code>catch</code> parameters and resources in <code>try</code> statements. </p> </subsection> @@ -632,11 +632,13 @@ <td> subset of tokens <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>, - <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>. + <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>, + <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">RESOURCE</a>. </td> <td> <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">VARIABLE_DEF</a>, - <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>. + <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">PARAMETER_DEF</a>, + <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">RESOURCE</a>. </td> </tr> </table> |