Integrated: 8155591: Misleading warning when not overriding close method in interface extending AutoCloseable
Archie Cobbs
acobbs at openjdk.org
Thu Nov 6 15:31:29 UTC 2025
On Tue, 2 Sep 2025 23:35:52 GMT, Archie Cobbs <acobbs at openjdk.org> wrote:
> The compiler generates a warning if you declare a `close()` method that throws any supertype of `InterruptedException` in a class or interface extending `AutoCloseable`. This is because suppressed `InterruptedException`'s are likely to lead to misbehavior at runtime.
>
> However, this feature also generates a warning due to `AutoCloseable.close()` itself, if you simply inherit it. For example:
>
> $ cat MyClass.java
> public interface MyClass extends AutoCloseable {
> }
> $ javac -Xlint:try MyClass.java
> MyClass.java:1: warning: [try] auto-closeable resource MyClass has a member method close() that could throw InterruptedException
> public interface MyClass extends AutoCloseable {
> ^
> 1 warning
>
> The warning is not useful because `AutoCloseable.close()` is abstract and so it's going to be some other class' job to deal with the throws clause.
>
> This change prevents `AutoCloseable.close()` itself from triggering the warning in a class declaration. We preserve the existing behavior in which a warning is generated at the use site (i.e., at the try-with-resources statement).
This pull request has now been integrated.
Changeset: a5864582
Author: Archie Cobbs <acobbs at openjdk.org>
URL: https://git.openjdk.org/jdk/commit/a5864582da7e19b941bf55c294a414bc1a0c7a84
Stats: 306 lines in 6 files changed: 300 ins; 0 del; 6 mod
8155591: Misleading warning when not overriding close method in interface extending AutoCloseable
Reviewed-by: jlahoda
-------------
PR: https://git.openjdk.org/jdk/pull/27062
More information about the compiler-dev
mailing list