简体   繁体   English

工厂创建的实例的AutoCloseable“资源泄漏”警告?

[英]AutoCloseable “resource leak” warning for factory created instances?

These "resource leak" warnings I'm getting in Eclipse for AutoCloseable s seem to be a life-saver. 我在Eclipse中为AutoCloseable获取的这些“资源泄漏”警告似乎可以节省生命。

However, how do I get them to work for factory created instances? 但是,如何让它们为工厂创建的实例工作?

For example ( a works, but b doesn't): 例如( a作品,但是b没有):

public static void main(String[] args) {
    // a) This emits a warning
    new AutoCloseable() {
        @Override
        public void close() throws Exception {}
    };

    // b) But this doesn't!
    newResource();
}

public static AutoCloseable newResource() {
    return new AutoCloseable() {
        @Override
        public void close() throws Exception {}
    };
}

Is there an annotation I can stick on newResource() or something I can do to let the compiler (or is it Eclipse?) know of the ownership change? 是否有一个注释我可以坚持使用newResource()或者我可以做些什么让编译器(或者它是Eclipse?)知道所有权的变化?

The Neon Eclipse documentation on "resource leak" detection explains what is going on; 关于“资源泄漏”检测的Neon Eclipse文档解释了正在发生的事情; see "avoiding resource leaks" . “避免资源泄漏” It states: 它指出:

Ownership / responsibility 所有权/责任

The above diagnostics basically assume that a method that creates an instance of a resource type is also responsible for closing this resource. 上述诊断基本上假设创建资源类型实例的方法也负责关闭此资源。 However, some resources will be shared among several methods. 但是,一些资源将在几种方法之间共享。 Here the analysis makes the following assumptions: 这里的分析做出以下假设:

  1. If a method returns a resource to its caller, it is not responsible for closing; 如果方法将资源返回给其调用者,则它不负责关闭; no problem is reported. 没有问题报道。
  2. If a resource is stored in a field, no single method is considered as responsible for closing; 如果资源存储在字段中,则没有一种方法被视为负责关闭; no problem is reported. 没有问题报道。
  3. If a method obtains a resource via a method call rather than by a new expression, it may or may not be responsible; 如果方法通过方法调用而不是通过新表达式获取资源,则它可能负责也可能不负责; any problems are only flagged as potential resource leaks. 任何问题都只被标记为潜在的资源泄漏。
  4. If a resource is passed as an argument in a method call or constructor call, the current method may or may not be responsible; 如果资源在方法调用或构造函数调用中作为参数传递,则当前方法可能负责也可能不负责; any problems are only flagged as potential resource leaks. 任何问题都只被标记为潜在的资源泄漏。

Point #1 explains why there is no "resource leak" warning for the return statement in the newResource method. 第1点解释了为什么newResource方法中的return语句没有“资源泄漏”警告。

Point #3 explains why there is no "resource leak" warning for the newResource() call. 第3点解释了为什么newResource()调用没有“资源泄漏”警告。 At best, it would be a "potential resource leak" warning. 充其量,这将是一个“潜在的资源泄漏”警告。 Either you have those warnings disabled, or the previous warning is inhibiting it. 要么禁用这些警告,要么先前警告禁止警告。


Q: Is there an annotation to tell Eclipse about transfer of resource ownership? 问:是否有一个注释告诉Eclipse有关资源所有权的转移?

A: The Neon Eclipse documentation doesn't mention any such annotation. 答:Neon Eclipse文档没有提到任何这样的注释。 (And it does go into detail about the annotations for null checking!) (它确实详细介绍了空检查的注释!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM