Force warning when class is not closed

醉酒当歌 提交于 2019-12-10 12:53:33

问题


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.


回答1:


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
    }


来源:https://stackoverflow.com/questions/31921293/force-warning-when-class-is-not-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!