pmd / pmd/pmd

[java] CloseResource: Disallow allowedResourceTypes classes returned by certain methods

Open
#6,562 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

an:enhancement
Dominant language
Java
Stars
5.5k
Forks
1.6k
Avg merge
1d 19h
Merged PRs (30d)
54

Description

Is your feature request related to a problem? Please describe.
By default, java.util.stream.Stream is part of the default allowedResourceTypes, which makes a lot of sense as the vast majority of Stream instances never need to be closed. However, there are some cases, where streams really should be closed - the reason why I'm opening this one is because I found some resource leaks in JPA/Hibernate code. It called TypedQuery#getResultStream(). This returns a Stream backed by a ResultSet, which needs to be closed, which happens when the Stream is closed

Describe the solution you'd like
A new config option, basically the inverse of allowedResourceMethodPatterns, which allows me to list some method calls returning Closeables, which are listed in allowedResourceTypes, but should result in a warning nonetheless

Describe alternatives you've considered
I briefly considered removing Stream from allowedResourceTypes, but quickly stopped considering it, after I did it and saw the very, very long list of warnings this produced :)

Additional context
This is basically the inverse of https://github.com/pmd/pmd/pull/6437

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the CloseResource rule and its existing allowedResourceMethodPatterns configuration, then trace how returned resource types are exempted. Add the requested inverse configuration for selected method calls and verify that those calls produce warnings while ordinary allowed Stream returns remain exempt.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.