Skip to content

Commit

Permalink
Issue checkstyle#6125: Ignore variable reference as resource statement
Browse files Browse the repository at this point in the history
  • Loading branch information
strkkk committed May 9, 2019
1 parent 4a7977c commit c25b39d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
Expand Up @@ -113,6 +113,11 @@ public final class OneStatementPerLineCheck extends AbstractCheck {
*/
private int lambdaStatementEnd = -1;

/**
* Hold the line-number where the last resource variable statement ended.
*/
private int lastVariableResourceStatementEnd = -1;

@Override
public int[] getDefaultTokens() {
return getRequiredTokens();
Expand Down Expand Up @@ -196,22 +201,48 @@ private void checkIfSemicolonIsInDifferentLineThanPrevious(DetailAST ast) {
currentStatement = ast.getPreviousSibling();
}
if (isInLambda) {
int countOfSemiInCurrentLambda = countOfSemiInLambda.pop();
countOfSemiInCurrentLambda++;
countOfSemiInLambda.push(countOfSemiInCurrentLambda);
if (!inForHeader && countOfSemiInCurrentLambda > 1
&& isOnTheSameLine(currentStatement,
lastStatementEnd, forStatementEnd,
lambdaStatementEnd)) {
log(ast, MSG_KEY);
}
checkLambda(ast, currentStatement);
}
else if (isResource(ast.getParent())) {
checkResourceVariable(ast);
}
else if (!inForHeader && isOnTheSameLine(currentStatement, lastStatementEnd,
forStatementEnd, lambdaStatementEnd)) {
log(ast, MSG_KEY);
}
}

private void checkLambda(DetailAST ast, DetailAST currentStatement) {
int countOfSemiInCurrentLambda = countOfSemiInLambda.pop();
countOfSemiInCurrentLambda++;
countOfSemiInLambda.push(countOfSemiInCurrentLambda);
if (!inForHeader && countOfSemiInCurrentLambda > 1
&& isOnTheSameLine(currentStatement,
lastStatementEnd, forStatementEnd,
lambdaStatementEnd)) {
log(ast, MSG_KEY);
}
}

private static boolean isResource(DetailAST ast) {
return ast != null
&& (ast.getType() == TokenTypes.RESOURCES
|| ast.getType() == TokenTypes.RESOURCE_SPECIFICATION);
}

private void checkResourceVariable(DetailAST currentStatement) {
DetailAST ast = currentStatement.getPreviousSibling();
if (currentStatement.getNextSibling().getType() == TokenTypes.RPAREN) {
ast = ast.getLastChild();
}
if (ast.getType() != TokenTypes.IDENT) {
if (ast.getLineNo() == lastVariableResourceStatementEnd) {
log(currentStatement, MSG_KEY);
}
lastVariableResourceStatementEnd = ast.getLineNo();
}
}

/**
* Checks whether two statements are on the same line.
* @param ast token for the current statement.
Expand Down
Expand Up @@ -94,4 +94,16 @@ public void oneStatementNonCompilableInputTest() throws Exception {
verify(checkConfig, getNonCompilablePath("InputOneStatementPerLine.java"), expected);
}

@Test
public void testResourceReferenceVariableIgnored() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(OneStatementPerLineCheck.class);
final String[] expected = {
"22:95: " + getCheckMessage(MSG_KEY),
};

verify(checkConfig,
getNonCompilablePath("InputOneStatementPerLineTryWithResources.java"),
expected);
}

}
@@ -0,0 +1,33 @@
//non-compiled with javac: Compilable with Java9
package com.puppycrawl.tools.checkstyle.checks.coding.onestatementperline;

import java.io.IOException;
import java.io.PipedOutputStream;
import java.io.OutputStream;

public class InputOneStatementPerLineTryWithResources {

void method() throws IOException {
OutputStream s1 = new PipedOutputStream();
OutputStream s2 = new PipedOutputStream();
try (s1; s2; OutputStream s3 = new PipedOutputStream();) {
}
try (s1; OutputStream s4 = new PipedOutputStream(); s2;) {
}
try (s1; s2; OutputStream s5 = new PipedOutputStream()) {
}
try (s1; OutputStream s6 = new PipedOutputStream(); s2) {
}
try (
OutputStream s7 = new PipedOutputStream();OutputStream s8 = new PipedOutputStream();
s2;
) {
}
try (s1; OutputStream s9 = new PipedOutputStream();
s2;) {
}
try (OutputStream s10 = new PipedOutputStream();
OutputStream s11 = new PipedOutputStream(); s2;) {
}
}
}

0 comments on commit c25b39d

Please sign in to comment.