Fixed bug #1579227: Fixed docs, added unit tests and changed the check implementation to allow turning off the | and & operators
This commit is contained in:
parent
0bb5ca0b5c
commit
9d41bddb46
|
|
@ -27,7 +27,7 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils;
|
|||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Restricts nested boolean operators (&&, || and ^) to
|
||||
* Restricts nested boolean operators (&&, ||, &, | and ^) to
|
||||
* a specified depth (default = 3).
|
||||
*
|
||||
* @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
|
||||
|
|
@ -69,7 +69,11 @@ public final class BooleanExpressionComplexityCheck extends Check
|
|||
/** {@inheritDoc} */
|
||||
public int[] getRequiredTokens()
|
||||
{
|
||||
return getDefaultTokens();
|
||||
return new int[] {
|
||||
TokenTypes.CTOR_DEF,
|
||||
TokenTypes.METHOD_DEF,
|
||||
TokenTypes.EXPR,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -24,6 +24,11 @@ public class BooleanExpressionComplexityCheckTestInput {
|
|||
}
|
||||
|
||||
public boolean equals(Object object) {
|
||||
return (((_a && (_b & _c)) || (_c ^ _d)));
|
||||
return (((_a && (_b & _c)) || (_c ^ _d) || (_a && _d)));
|
||||
}
|
||||
|
||||
public boolean bitwise()
|
||||
{
|
||||
return (((_a & (_b & _c)) | (_c ^ _d) | (_a & _d)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,20 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestCase
|
|||
createCheckConfig(BooleanExpressionComplexityCheck.class);
|
||||
|
||||
String[] expected = {
|
||||
"13:9: Boolean expression complexity is 4 (max allowed is 3).",
|
||||
"13:9: Boolean expression complexity is 4 (max allowed is 3).",
|
||||
"32:9: Boolean expression complexity is 6 (max allowed is 3).",
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected);
|
||||
}
|
||||
|
||||
public void testNoBitwise() throws Exception {
|
||||
DefaultConfiguration checkConfig =
|
||||
createCheckConfig(BooleanExpressionComplexityCheck.class);
|
||||
checkConfig.addAttribute("max", "5");
|
||||
checkConfig.addAttribute("tokens", "BXOR,LAND,LOR");
|
||||
|
||||
String[] expected = {
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@
|
|||
<subsection name="Description">
|
||||
<p>
|
||||
Restrict the number of number of <span
|
||||
class="code">&&</span>, <span class="code">||</span>
|
||||
class="code">&&</span>, <span class="code">||</span>,
|
||||
<span class="code">&</span>, <span class="code">|</span>
|
||||
and <span class="code">^</span> in an expression.
|
||||
</p>
|
||||
|
||||
|
|
@ -21,6 +22,14 @@
|
|||
Rationale: Too many conditions leads to code that is difficult
|
||||
to read and hence debug and maintain.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Note that the operators <span class="code">&</span> and
|
||||
<span class="code">|</span> are not only integer bitwise operators, they are also the
|
||||
<a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.2">
|
||||
non-shortcut versions</a> of the boolean operators
|
||||
<span class="code">&&</span> and <span class="code">||</span>.
|
||||
</p>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Properties">
|
||||
|
|
@ -40,6 +49,25 @@
|
|||
<td><a href="property_types.html#integer">integer</a></td>
|
||||
<td><span class="default">3</span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>tokens</td>
|
||||
<td>tokens to check</td>
|
||||
<td>
|
||||
subset of tokens
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">LAND</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BAND">BAND</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">LOR</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BOR">BOR</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BXOR">BXOR</a>
|
||||
</td>
|
||||
<td>
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LAND">LAND</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BAND">BAND</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LOR">LOR</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BOR">BOR</a>,
|
||||
<a href="api/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#BXOR">BXOR</a>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</subsection>
|
||||
|
||||
|
|
@ -60,6 +88,17 @@
|
|||
<property name="max" value="7"/>
|
||||
</module>
|
||||
</source>
|
||||
|
||||
<p>
|
||||
To configure the check to ignore <span class="code">&</span> and
|
||||
<span class="code">|</span>:
|
||||
</p>
|
||||
<source>
|
||||
<module name="BooleanExpressionComplexity">
|
||||
<property name="tokens" value="BXOR,LAND,LOR"/>
|
||||
</module>
|
||||
</source>
|
||||
|
||||
</subsection>
|
||||
|
||||
<subsection name="Package">
|
||||
|
|
|
|||
|
|
@ -25,10 +25,19 @@
|
|||
The calculated value of NPath complexity was subject to integer overflow,
|
||||
so complex code was not always flagged (bug 1654769).
|
||||
</li>
|
||||
<li>
|
||||
Fixed misleading documentation for BooleanExpressionComplexity check (bug 1579227).
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>New features:</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
Allow to control the operator token types in BooleanExpressionComplexity
|
||||
check (in response to bug 1579227).
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
<section name="Release 4.3">
|
||||
|
|
|
|||
Loading…
Reference in New Issue