A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/1966 below:

[java] CloseResource false positive if Stream is passed as method parameter · Issue #1966 · pmd/pmd · GitHub

Affects PMD Version: 6.16.0

Rule: CloseResource

Description:

If a IO stream is passed into a method as a method parameter and then assigned to a local variable, the rule should not be triggered. In that case, the stream has not be created locally, so the caller of the method is responsible to close it.

Code Sample demonstrating the issue:

import java.io.*;

public class CloseResourceFP {
    public void check(InputStream in) {
        if (in instanceof FileInputStream) {
            FileInputStream fin = (FileInputStream) in;
            doCheck(fin);
        } else if (in instanceof ByteArrayInputStream) {
            ByteArrayInputStream bin = (ByteArrayInputSream) in;
            doCheck(bin);
        } else {
            BufferedInputStream buf = new BufferedInputStream(in);
            doCheck(buf);
        }
    }

    public void dump(final Writer writer) {
        final PrintWriter printWriter = writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter(writer);
        printWriter.println(this);
    }
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4