Class ForbidThrowAnonymousExceptionsCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.ForbidThrowAnonymousExceptionsCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class ForbidThrowAnonymousExceptionsCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheckThis Check warns on throwing anonymous exception.
Examples:catch (Exception e) { throw new RuntimeException() { //WARNING //some code }; }The distinguishing of exception types occurs by analyzing variable's class's name.
catch (Exception e) { RuntimeException run = new RuntimeException() { //some code }; throw run; //WARNING }
Check has an option which contains the regular expression for exception class name matching
Default value is "^.*Exception" because usually exception type ends with suffix "Exception".
Then, if we have an ObjBlock (distinguished by curly braces), it's anonymous
exception definition. It could be defined in throw statement immediately.
In that case, after literal new, there would be an expression type finishing with and ObjBlock.
- Since:
- 1.11.0
- Author:
- Aleksey Nesterenko, Max Vetrenko
-
-
Constructor Summary
Constructors Constructor Description ForbidThrowAnonymousExceptionsCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]getAcceptableTokens()int[]getDefaultTokens()int[]getRequiredTokens()voidsetExceptionClassNameRegex(String exceptionClassNameRegex)Setter for pattern.voidvisitToken(com.puppycrawl.tools.checkstyle.api.DetailAST literalThrowOrVariableDefAst)-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
-
-
-
Field Detail
-
MSG_KEY
public static final String MSG_KEY
Warning message key.- See Also:
- Constant Field Values
-
-
Method Detail
-
setExceptionClassNameRegex
public void setExceptionClassNameRegex(String exceptionClassNameRegex)
Setter for pattern.- Parameters:
exceptionClassNameRegex- The regular expression to set.
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST literalThrowOrVariableDefAst)
- Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-