Boolean Expression Complexity Check, fixed NPE, issue #654

This commit is contained in:
alexkravin 2015-02-20 22:41:56 +04:00
parent 5ede09997c
commit 84115fe9bf
3 changed files with 42 additions and 1 deletions

View File

@ -125,10 +125,14 @@ public final class BooleanExpressionComplexityCheck extends Check
case TokenTypes.EXPR:
visitExpr();
break;
case TokenTypes.BOR:
if (!isPipeOperator(ast)) {
context.visitBooleanOperator();
}
break;
case TokenTypes.LAND:
case TokenTypes.BAND:
case TokenTypes.LOR:
case TokenTypes.BOR:
case TokenTypes.BXOR:
context.visitBooleanOperator();
break;
@ -137,6 +141,19 @@ public final class BooleanExpressionComplexityCheck extends Check
}
}
/**
* Checks if {@link TokenTypes#BOR binary OR} is applied to exceptions
* in
* <a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.20">
* multi-catch</a> (pipe-syntax).
* @param binaryOr {@link TokenTypes#BOR binary or}
* @return true if binary or is applied to exceptions in multi-catch.
*/
private static boolean isPipeOperator(DetailAST binaryOr)
{
return binaryOr.getParent().getType() == TokenTypes.TYPE;
}
@Override
public void leaveToken(DetailAST ast)
{

View File

@ -55,4 +55,16 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestSupport
verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected);
}
@Test
public void testNPE() throws Exception
{
DefaultConfiguration checkConfig =
createCheckConfig(BooleanExpressionComplexityCheck.class);
String[] expected = {
};
verify(checkConfig, getPath("metrics" + File.separator + "InputBooleanExpressionComplexityNPE.java"), expected);
}
}

View File

@ -0,0 +1,12 @@
package com.puppycrawl.tools.checkstyle.metrics;
public class InputBooleanExpressionComplexityNPE
{
static {
try {
System.out.println("a");
} catch (IllegalStateException | IllegalArgumentException e) {
throw new RuntimeException(e);
}
}
}