Class 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
  • Field Details

  • Constructor Details

  • Method Details

    • getDefaultTokens

      public int[] getDefaultTokens()
      Description copied from class: AbstractCheck
      Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.
      Specified by:
      getDefaultTokens in class AbstractCheck
      Returns:
      the default tokens
      See Also:
    • getAcceptableTokens

      public int[] getAcceptableTokens()
      Description copied from class: AbstractCheck
      The 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:
      getAcceptableTokens in class AbstractCheck
      Returns:
      the token set this check is designed for.
      See Also:
    • getRequiredTokens

      public int[] getRequiredTokens()
      Description copied from class: AbstractCheck
      The tokens that this check must be registered for.
      Specified by:
      getRequiredTokens in class AbstractCheck
      Returns:
      the token set this must be registered for.
      See Also:
    • visitToken

      public void visitToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called to process a token.
      Overrides:
      visitToken in class AbstractCheck
      Parameters:
      ast - the token to process
    • getThrownType

      private static String getThrownType(DetailAST throwAst)
      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

      private static boolean hasViolatingGetMessageCall(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.
      Parameters:
      ast - the AST node to search
      thrownType - the simple type name of the thrown exception
      Returns:
      true if a violating getMessage() call is found
    • isMatchingGetMessage

      private static boolean isMatchingGetMessage(DetailAST node, String thrownType)
      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 check
      thrownType - the simple type name of the thrown exception
      Returns:
      true if this is a matching getMessage() call
    • findCatchForVariable

      private static DetailAST findCatchForVariable(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.
      Parameters:
      ast - the starting AST node
      varName - the variable name to look for
      Returns:
      the LITERAL_CATCH node that declares the variable, or null
    • matchesCaughtType

      private static boolean matchesCaughtType(DetailAST paramDef, String typeName)
      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 node
      typeName - the type name to match against
      Returns:
      true if the type matches any of the caught types
    • containsTypeName

      private static boolean containsTypeName(DetailAST ast, String typeName)
      Checks if the given TYPE AST's child matches the specified type name.
      Parameters:
      ast - the TYPE AST node
      typeName - the type name to match
      Returns:
      true if the type matches