Class NoCauseGetMessageInExceptionConstructorCheck
java.lang.Object
com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
com.puppycrawl.tools.checkstyle.api.AbstractCheck
com.puppycrawl.tools.checkstyle.checks.coding.NoCauseGetMessageInExceptionConstructorCheck
- All Implemented Interfaces:
Configurable,Contextualizable
Checks that throw statements within catch blocks do not use
the caught exception's getMessage() call when the thrown exception
is the same type as the caught exception, as rethrowing the same
exception type with its own message is redundant.
Rationale: When throwing an exception of the same type as the caught exception and including the caught exception's message via getMessage(), the information is redundant. The original exception should be rethrown directly, or a different exception type should be used.
Example of violations:
catch (IOException ex) {
throw new IOException("Error: " + ex.getMessage()); // violation
}
Correct usage:
catch (IOException ex) {
throw new RuntimeException("Error: " + ex.getMessage(), ex); // OK, different type
}
catch (IOException ex) {
throw new IOException("Error processing file", ex); // OK, no getMessage()
}
- Since:
- 14.4.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
FieldsModifier and TypeFieldDescriptionStack of catch blocks currently in scope during traversal.static final StringA key is pointing to the warning message text in "messages.properties" file. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newNoCauseGetMessageInExceptionConstructorCheckinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled before the starting to process a tree.private voidcheckThrow(DetailAST throwAst) Reports the throw statement if it references a matching caught exception'sgetMessage()inside its expression.private static booleancontainsTypeName(DetailAST ast, String typeName) Recursively checks if the given AST contains a type matching the specified name.int[]The configurable token set.int[]Returns the default token a check is interested in.int[]The tokens that this check must be registered for.private static StringgetThrownType(DetailAST throwAst) Gets the simple type name of the exception being thrown via a new expression.private booleanhasViolatingGetMessage(DetailAST ast, String thrownType) Recursively checks if the AST subtree contains agetMessage()call on an enclosing caught exception whose declared type matches the thrown type.private booleanisViolatingGetMessage(DetailAST node, String thrownType) Checks if a single node is<var>.getMessage()wherevaris declared by any enclosing catch whose type matches the thrown type.voidleaveToken(DetailAST ast) Called after all the child nodes have been process.private static booleanmatchesCatchScope(DetailAST catchBlock, String varName, String thrownType) Checks whether the given catch block declares the specified variable and catches a type matching the thrown type.voidvisitToken(DetailAST ast) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Field Details
-
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
-
catchScopes
Stack of catch blocks currently in scope during traversal.
-
-
Constructor Details
-
NoCauseGetMessageInExceptionConstructorCheck
Creates a newNoCauseGetMessageInExceptionConstructorCheckinstance.
-
-
Method Details
-
getDefaultTokens
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
-
getAcceptableTokens
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
-
getRequiredTokens
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
-
beginTree
Description copied from class:AbstractCheckCalled before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTreein classAbstractCheck- Parameters:
rootAST- the root of the tree
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
leaveToken
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
checkThrow
Reports the throw statement if it references a matching caught exception'sgetMessage()inside its expression.- Parameters:
throwAst- the LITERAL_THROW AST node
-
getThrownType
Gets the simple type name of the exception being thrown via a new expression.- Parameters:
throwAst- the LITERAL_THROW AST node- Returns:
- the simple type name, or null if the throw does not use a new expression
-
hasViolatingGetMessage
Recursively checks if the AST subtree contains agetMessage()call on an enclosing caught exception whose declared type matches the thrown type.- Parameters:
ast- the AST node to searchthrownType- the simple type name of the thrown exception- Returns:
- true if a violating call is found
-
isViolatingGetMessage
Checks if a single node is<var>.getMessage()wherevaris declared by any enclosing catch whose type matches the thrown type.- Parameters:
node- the AST node to checkthrownType- the simple type name of the thrown exception- Returns:
- true if this is a matching call
-
matchesCatchScope
Checks whether the given catch block declares the specified variable and catches a type matching the thrown type.- Parameters:
catchBlock- the LITERAL_CATCH AST nodevarName- the caught-exception variable name to matchthrownType- the simple type name of the thrown exception- Returns:
- true if the catch matches both the variable and the type
-
containsTypeName
Recursively checks if the given AST contains a type matching the specified name. Handles IDENT (simple names), DOT (qualified names), and BOR (multi-catch) nodes.- Parameters:
ast- the AST node to searchtypeName- the type name to match- Returns:
- true if a matching type is found
-