ThrowCountCheck should have option to skip private methods, #1136
This commit is contained in:
parent
e6f4dad587
commit
5f047efa20
|
|
@ -41,6 +41,10 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
|||
* a caller but allows any sub-classes to be caught
|
||||
* specifically if necessary.
|
||||
* </p>
|
||||
* <p>
|
||||
* <b>ignorePrivateMethods</b> - allows to skip private methods as they do
|
||||
* not cause problems for other classes.
|
||||
* </p>
|
||||
* @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
|
||||
*/
|
||||
public final class ThrowsCountCheck extends Check {
|
||||
|
|
@ -54,6 +58,9 @@ public final class ThrowsCountCheck extends Check {
|
|||
/** default value of max property */
|
||||
private static final int DEFAULT_MAX = 1;
|
||||
|
||||
/** whether private methods must be ignored **/
|
||||
private boolean ignorePrivateMethods = true;
|
||||
|
||||
/** maximum allowed throws statements */
|
||||
private int max;
|
||||
|
||||
|
|
@ -89,6 +96,14 @@ public final class ThrowsCountCheck extends Check {
|
|||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether private methods must be ignored.
|
||||
* @param ignorePrivateMethods whether private methods must be ignored.
|
||||
*/
|
||||
public void setIgnorePrivateMethods(boolean ignorePrivateMethods) {
|
||||
this.ignorePrivateMethods = ignorePrivateMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for max property.
|
||||
* @param max maximum allowed throws statements.
|
||||
|
|
@ -112,7 +127,8 @@ public final class ThrowsCountCheck extends Check {
|
|||
* @param ast throws for check.
|
||||
*/
|
||||
private void visitLiteralThrows(DetailAST ast) {
|
||||
if (!isOverriding(ast)) {
|
||||
if ((!ignorePrivateMethods || !isInPrivateMethod(ast))
|
||||
&& !isOverriding(ast)) {
|
||||
// Account for all the commas!
|
||||
final int count = (ast.getChildCount() + 1) / 2;
|
||||
if (count > getMax()) {
|
||||
|
|
@ -159,4 +175,14 @@ public final class ThrowsCountCheck extends Check {
|
|||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if method, which throws an exception is private.
|
||||
* @param ast throws, which is being checked.
|
||||
* @return true, if method, which throws an exception is private.
|
||||
*/
|
||||
private static boolean isInPrivateMethod(DetailAST ast) {
|
||||
final DetailAST methodModifiers = ast.getParent().findFirstToken(TokenTypes.MODIFIERS);
|
||||
return methodModifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public class ThrowsCountCheckTest extends BaseCheckTestSupport {
|
|||
"14:20: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
"18:20: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
"22:20: " + getCheckMessage(MSG_KEY, 3, 1),
|
||||
"45:43: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
"48:43: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
};
|
||||
|
||||
verify(checkConfig, getPath("design" + File.separator + "InputThrowsCount.java"), expected);
|
||||
|
|
@ -90,4 +90,18 @@ public class ThrowsCountCheckTest extends BaseCheckTestSupport {
|
|||
assertEquals(ast.toString(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotIgnorePrivateMethod() throws Exception {
|
||||
DefaultConfiguration checkConfig = createCheckConfig(ThrowsCountCheck.class);
|
||||
checkConfig.addAttribute("ignorePrivateMethods", "false");
|
||||
String[] expected = {
|
||||
"14:20: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
"18:20: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
"22:20: " + getCheckMessage(MSG_KEY, 3, 1),
|
||||
"29:28: " + getCheckMessage(MSG_KEY, 3, 1),
|
||||
"48:43: " + getCheckMessage(MSG_KEY, 2, 1),
|
||||
};
|
||||
verify(checkConfig, getPath("design" + File.separator + "InputThrowsCount.java"), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,9 @@ public class InputThrowsCount {
|
|||
|
||||
void method6() {
|
||||
}
|
||||
|
||||
private void method7() throws Exception, AWTException, Throwable {
|
||||
}
|
||||
}
|
||||
|
||||
class SubClass extends InputThrowsCount {
|
||||
|
|
|
|||
|
|
@ -591,6 +591,11 @@ public class StringUtils // not final to allow subclassing
|
|||
type of exception need be checked for by a caller but any
|
||||
subclasses can be caught specifically if necessary.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<b>ignorePrivateMethods</b> - allows to skip private methods as they do
|
||||
not cause problems for other classes.
|
||||
</p>
|
||||
</subsection>
|
||||
|
||||
<subsection name="Properties">
|
||||
|
|
@ -607,6 +612,12 @@ public class StringUtils // not final to allow subclassing
|
|||
<td><a href="property_types.html#integer">Integer</a></td>
|
||||
<td><code>1</code></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ignorePrivateMethods</td>
|
||||
<td>whether private methods must be ignored</td>
|
||||
<td><a href="property_types.html#boolean">Boolean</a></td>
|
||||
<td><code>true</code></td>
|
||||
</tr>
|
||||
</table>
|
||||
</subsection>
|
||||
|
||||
|
|
@ -618,6 +629,15 @@ public class StringUtils // not final to allow subclassing
|
|||
<source>
|
||||
<module name="ThrowsCount">
|
||||
<property name="max" value="2"/>
|
||||
</module>
|
||||
</source>
|
||||
|
||||
<p>
|
||||
To configure the check so that it doesn't skip private methods:
|
||||
</p>
|
||||
<source>
|
||||
<module name="ThrowsCount">
|
||||
<property name="ignorePrivateMethods" value="false"/>
|
||||
</module>
|
||||
</source>
|
||||
</subsection>
|
||||
|
|
|
|||
Loading…
Reference in New Issue