12

When I work with IO classes like java.util.Scanner or java.io.BufferedReader, Eclipse displays a warning Resource leak: 'suchAndSuch' is never closed. How can I make Eclipse display this warning for my own class when it is not closed?

What I want to know is if there is an Interface or something I need to implement to make my class be treated like the IO classes, so that it's independent of any one IDE, e.g. the command line javac also displays a warning when a resource isn't closed.

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • 1
    use Pluggable Annotation Processor API http://stackoverflow.com/a/24981511/3651739 – Jishnu Prathap Aug 10 '15 at 13:50
  • 2
    Have you tried implementing the `java.io.Closeable` interface in the class that you want Eclipse to warn you about? – JonK Aug 10 '15 at 13:51
  • Is it sufficient to have your class implement `java.io.Closeable`? See http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-avoiding_resource_leaks.htm . – Andy Thomas Aug 10 '15 at 13:52
  • @JonK I did once, but for whatever reason it didn't work. I guess Eclipse doesn't check if the object is closed if it's passed to another method. – jpyams Aug 10 '15 at 14:06

1 Answers1

4

Eclipse gives warning "never closed" for any class which implements java.io.Closeable

class X implements Closeable {
    @Override
    public void close() throws IOException {
    }
}

...
    public static void main(String[] args) throws IOException {
        X x = new X(); <-- Eclipse warning
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275