Issue #3323: added try-with-resources to RedundantModifierCheck

This commit is contained in:
rnveach 2016-07-18 20:06:15 -04:00 committed by Roman Ivanov
parent 7a8b92371b
commit b415ce007b
4 changed files with 77 additions and 2 deletions

View File

@ -132,6 +132,7 @@ public class RedundantModifierCheck
TokenTypes.CTOR_DEF,
TokenTypes.CLASS_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.RESOURCE,
};
}
@ -157,6 +158,9 @@ public class RedundantModifierCheck
else if (ast.getType() == TokenTypes.METHOD_DEF) {
processMethods(ast);
}
else if (ast.getType() == TokenTypes.RESOURCE) {
processResources(ast);
}
}
/**
@ -290,6 +294,26 @@ public class RedundantModifierCheck
}
}
/**
* Checks if given resource has redundant modifiers.
* @param ast ast
*/
private void processResources(DetailAST ast) {
final DetailAST modifiers = ast.findFirstToken(TokenTypes.MODIFIERS);
DetailAST modifier = modifiers.getFirstChild();
while (modifier != null) {
final int type = modifier.getType();
if (type == TokenTypes.FINAL) {
log(modifier.getLineNo(), modifier.getColumnNo(), MSG_KEY, modifier.getText());
break;
}
modifier = modifier.getNextSibling();
}
}
/**
* Checks if given ast has redundant public modifier.
* @param ast ast

View File

@ -164,6 +164,7 @@ public class RedundantModifierCheckTest
TokenTypes.CTOR_DEF,
TokenTypes.CLASS_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.RESOURCE,
};
Assert.assertArrayEquals(expected, actual);
}
@ -198,4 +199,15 @@ public class RedundantModifierCheckTest
};
verify(checkConfig, getPath("InputFinalInAnonymousClass.java"), expected);
}
@Test
public void testFinalInTryWithResource() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(RedundantModifierCheck.class);
final String[] expected = {
"22:14: " + getCheckMessage(MSG_KEY, "final"),
"27:14: " + getCheckMessage(MSG_KEY, "final"),
"28:17: " + getCheckMessage(MSG_KEY, "final"),
};
verify(checkConfig, getPath("InputFinalInTryWithResource.java"), expected);
}
}

View File

@ -0,0 +1,37 @@
package com.puppycrawl.tools.checkstyle.checks.modifier;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
public class InputFinalInTryWithResource {
public static void test() {
try {
}
catch (RuntimeException e) {
}
try (@NotNull BufferedReader br = new BufferedReader(new FileReader(""))) {
}
catch (IOException e) {
}
try (final BufferedReader br = new BufferedReader(new FileReader(""))) {
}
catch (IOException e) {
}
try (final BufferedReader br = new BufferedReader(new FileReader(""));
final BufferedReader br2 = new BufferedReader(new FileReader(""))) {
}
catch (IOException e) {
}
}
}
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
@interface NotNull {
}

View File

@ -246,7 +246,8 @@
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">INTERFACE_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">CTOR_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">CLASS_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>.
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_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#METHOD_DEF">METHOD_DEF</a>,
@ -255,7 +256,8 @@
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INTERFACE_DEF">INTERFACE_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CTOR_DEF">CTOR_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#CLASS_DEF">CLASS_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>.
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ENUM_DEF">ENUM_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RESOURCE">RESOURCE</a>.
</td>
</tr>
</table>