未关闭课程时发出警告 [英] Force warning when class is not closed

查看:77
本文介绍了未关闭课程时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用java.util.Scannerjava.io.BufferedReader之类的IO类时,Eclipse将显示警告Resource leak: 'suchAndSuch' is never closed.在未关闭的情况下,如何使Eclipse在我自己的类中显示此警告?

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?

我想知道的是是否存在接口或我需要实现的东西,以使我的类像IO类一样对待,以便它独立于任何一个IDE,例如当未关闭资源时,命令行javac也会显示警告.

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.

推荐答案

Eclipse对实现java.io.Closeable的任何类给出警告永不关闭"

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
    }

这篇关于未关闭课程时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆