In Eclipse Neon, if I write this Java code:
Stream<Object> stream = Stream.builder().build();
I get no leak warnings, but if I implement Stream
, such as
public class MyStream<T> implements Stream<T> {
// implementation
}
and I write similar code
Stream<Object> stream = new MyStream<>();
I get a Resource leak: 'stream' is never closed
warning. This happens only in Eclipse, while compiling with javac
does not issue any warning.
Note I'm not looking for an answer on how to close the stream and such, but for an answer which explains the reason of this different behavior for the same interface.
Eclipse has a whitelist of types that do not require cleanup, because they don't actually refer to system resources. Core Java types are listed here, but your custom types are not. See the help for more information.
In the first case you are not creating the instance of the resource. In the second case, you are.
The eclipse documentation states the following:
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. [...]
- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With