Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
Issue checkstyle#3348: add validation of try with resources to the Lo…
Browse files Browse the repository at this point in the history
…calFinalVariableName check
  • Loading branch information
vasilyeva committed May 7, 2017
1 parent 5e0ab75 commit fc7b934
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 6 deletions.
Expand Up @@ -27,8 +27,8 @@
/**
* <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>
Expand Down Expand Up @@ -67,6 +67,7 @@ public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.VARIABLE_DEF,
TokenTypes.PARAMETER_DEF,
TokenTypes.RESOURCE
};
}

Expand All @@ -79,7 +80,8 @@ public int[] getRequiredTokens() {
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);
}
}
Expand Up @@ -263,6 +263,10 @@ public static boolean isLocalVariableDef(DetailAST node) {
final DetailAST parent = node.getParent();
localVariableDef = parent.getType() == TokenTypes.LITERAL_CATCH;
}

if (node.getType() == TokenTypes.RESOURCE) {
localVariableDef = true;
}
return localVariableDef;
}

Expand Down
Expand Up @@ -95,7 +95,26 @@ public void testGetAcceptableTokens() {
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);
}
}
@@ -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);
}
}
}
8 changes: 5 additions & 3 deletions src/xdocs/config_naming.xml
Expand Up @@ -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>

Expand All @@ -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>
Expand Down

0 comments on commit fc7b934

Please sign in to comment.