Class UnusedTryResourceShouldBeUnnamedCheck

All Implemented Interfaces:
Configurable, Contextualizable

public class UnusedTryResourceShouldBeUnnamedCheck extends AbstractCheck
Ensures that try-with-resources resource variables that are not used are declared as an unnamed variable.

Rationale:

  • Improves code readability by clearly indicating which resources are unused.
  • Follows Java conventions for denoting unused variables with an underscore (_).

Only declared resources inside the try-with-resources parentheses are checked (i.e. var a = lock() or AutoCloseable a = lock()). Resources that are referenced but not declared inside the try (e.g. try (releaser) { }) are never flagged, because those resources cannot be replaced with _.

See the Java Language Specification for more information about unnamed variables.

Attention: This check should be activated only on source code that is compiled by jdk21 or higher; unnamed variables came out as a preview feature in Java 21 and became a standard part of the language in Java 22.

Since:
13.5.0
  • Field Details

    • MSG_UNUSED_TRY_RESOURCE

      public static final String MSG_UNUSED_TRY_RESOURCE
      A key pointing to the warning message text in "messages.properties" file.
      See Also:
    • UNNAMED_VARIABLE_IDENTIFIER

      private static final String UNNAMED_VARIABLE_IDENTIFIER
      The unnamed variable identifier introduced in Java 21.
      See Also:
    • INVALID_RESOURCE_IDENT_PARENTS

      private static final int[] INVALID_RESOURCE_IDENT_PARENTS
      Parent token types for an TokenTypes.IDENT that indicate the identifier is not a plain variable reference and should therefore be excluded from "used" detection.
    • tryResources

      A stack of per-try resource-detail lists.
  • Constructor Details

    • UnusedTryResourceShouldBeUnnamedCheck

      public UnusedTryResourceShouldBeUnnamedCheck()
  • 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:
    • beginTree

      public void beginTree(DetailAST rootAST)
      Description copied from class: AbstractCheck
      Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.
      Overrides:
      beginTree in class AbstractCheck
      Parameters:
      rootAST - the root of the tree
    • 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
    • leaveToken

      public void leaveToken(DetailAST ast)
      Description copied from class: AbstractCheck
      Called after all the child nodes have been process.
      Overrides:
      leaveToken in class AbstractCheck
      Parameters:
      ast - the token leaving
    • collectTrackedResources

      private static Deque<UnusedTryResourceShouldBeUnnamedCheck.TryResourceDetails> collectTrackedResources(DetailAST tryAst)
      Collects all tracked resources from the RESOURCE_SPECIFICATION of a try-with-resources statement.
      Parameters:
      tryAst - the TokenTypes.LITERAL_TRY token
      Returns:
      a deque of UnusedTryResourceShouldBeUnnamedCheck.TryResourceDetails for trackable resources; never null, but may be empty for plain try statements
    • isResourceUsageCandidate

      private static boolean isResourceUsageCandidate(DetailAST identAst)
      Determines whether an TokenTypes.IDENT token is a candidate for being a use of a tracked try resource.
      Parameters:
      identAst - the TokenTypes.IDENT token to inspect
      Returns:
      true if the token could represent a reference to a resource variable
    • isShadowedByCatchParameter

      private static boolean isShadowedByCatchParameter(DetailAST identAst)
      Returns true when identAst is shadowed by a catch parameter of an immediately enclosing TokenTypes.LITERAL_CATCH block.
      Parameters:
      identAst - the TokenTypes.IDENT token to inspect
      Returns:
      true if a catch parameter with the same name is in scope
    • isResourceDeclarationIdent

      private static boolean isResourceDeclarationIdent(DetailAST identAst)
      Returns true when identAst is the variable-name token inside a TokenTypes.RESOURCE node (i.e. the declaration site, not a use).
      Parameters:
      identAst - the TokenTypes.IDENT token
      Returns:
      true if this IDENT is the name in a resource declaration/reference
    • isObjectReferenceInDot

      private static boolean isObjectReferenceInDot(DetailAST identAst)
      Returns true when identAst is the first child of a TokenTypes.DOT node, meaning it is the object reference in an expression such as a.close() — a genuine use of the variable.
      Parameters:
      identAst - the TokenTypes.IDENT token
      Returns:
      true if the IDENT is the left-hand operand of a dot expression