Class NoGetMessageInThrowCheck
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.NoGetMessageInThrowCheck
- 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:
- 13.3.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static booleancontainsTypeName(DetailAST ast, String typeName) Checks if the given TYPE AST's child matches the specified type name.private static DetailASTfindCatchForVariable(DetailAST ast, String varName) Walks up the AST from the given node to find an enclosing catch block that declares a parameter with the specified variable 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 static booleanhasViolatingGetMessageCall(DetailAST ast, String thrownType) Recursively checks if the AST subtree contains a getMessage() call on a caught exception variable whose type matches the thrown type.private static booleanisMatchingGetMessage(DetailAST node, String thrownType) Checks if a node represents a getMessage() call on a caught exception variable whose catch type matches the thrown type.private static booleanmatchesCaughtType(DetailAST paramDef, String typeName) Checks if the caught exception type in a parameter definition matches the given type name.voidvisitToken(DetailAST ast) Called to process a token.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, 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:
-
-
Constructor Details
-
NoGetMessageInThrowCheck
public NoGetMessageInThrowCheck()
-
-
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:
-
visitToken
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
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
-
hasViolatingGetMessageCall
Recursively checks if the AST subtree contains a getMessage() call on a caught exception variable whose 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 getMessage() call is found
-
isMatchingGetMessage
Checks if a node represents a getMessage() call on a caught exception variable whose catch 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 getMessage() call
-
findCatchForVariable
Walks up the AST from the given node to find an enclosing catch block that declares a parameter with the specified variable name.- Parameters:
ast- the starting AST nodevarName- the variable name to look for- Returns:
- the LITERAL_CATCH node that declares the variable, or null
-
matchesCaughtType
Checks if the caught exception type in a parameter definition matches the given type name. Handles both simple types and multi-catch types.- Parameters:
paramDef- the PARAMETER_DEF AST nodetypeName- the type name to match against- Returns:
- true if the type matches any of the caught types
-
containsTypeName
Checks if the given TYPE AST's child matches the specified type name.- Parameters:
ast- the TYPE AST nodetypeName- the type name to match- Returns:
- true if the type matches
-