Spotbugs: Try-with-resources and java9 shows bugs

Created on 3 Nov 2017  路  7Comments  路  Source: spotbugs/spotbugs

This is the code:
try (Connection cnn = platformFlyway.getDataSource().getConnection(); Statement st = cnn.createStatement(); ResultSet rs = st.executeQuery(query)) { return processQueryResult(rs); }
Works fine with spotbugs and java8.
With java9 I get this:

````
[INFO] com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl.executeDatabaseQuery(String, boolean) may fail to clean up java.sql.ResultSet [com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl, com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl, com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl, com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl] Obligation to clean up resource created at PlatformServiceImpl.java:[line 641] is not dischargedPath continues at PlatformServiceImpl.java:[line 642]Path continues at PlatformServiceImpl.java:[line 643]Path continues at PlatformServiceImpl.java:[line 642] OBL_UNSATISFIED_OBLIGATION
[INFO] com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl.executeDatabaseQuery(String, boolean) may fail to clean up java.sql.Statement [com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl, com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl, com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl, com.nemesis.platform.core.service.platform.impl.PlatformServiceImpl] Obligation to clean up resource created at PlatformServiceImpl.java:[line 641] is not dischargedPath continues at PlatformServiceImpl.java:[line 642]Path continues at PlatformServiceImpl.java:[line 643]Path continues at PlatformServiceImpl.java:[line 642] OBL_UNSATISFIED_OBLIGATION
[INFO]

````

Java-9 bug help wanted

Most helpful comment

No update. Currently nobody can propose solution for this issue.

Can a solution be influenced from jacoco? https://github.com/jacoco/jacoco/pull/669

All 7 comments

Reproduced this problem by unit test. I will attach result of javap:

It seems that java9 uses local method Issue493.$closeResource:(Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V to close AutoCloseable instances.
SpotBugs should handle the second parameter of this method as @WillClose.

  private static void $closeResource(java.lang.Throwable, java.lang.AutoCloseable);
    descriptor: (Ljava/lang/Throwable;Ljava/lang/AutoCloseable;)V
    flags: (0x100a) ACC_PRIVATE, ACC_STATIC, ACC_SYNTHETIC
    Code:
      stack=2, locals=3, args_size=2
         0: aload_0
         1: ifnull        22
         4: aload_1
         5: invokeinterface #1,  1            // InterfaceMethod java/lang/AutoCloseable.close:()V
        10: goto          28
        13: astore_2
        14: aload_0
        15: aload_2
        16: invokevirtual #3                  // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
        19: goto          28
        22: aload_1
        23: invokeinterface #1,  1            // InterfaceMethod java/lang/AutoCloseable.close:()V
        28: return
private static void $closeResource(java.lang.Throwable t, java.lang.AutoCloseable a)
    throws Exception {
  if (t == null) {
    a.close();
    return;
  }

  try {
    a.close();
  } catch (Throwable e) {
    t.addSuppressed(e);
  }
}

I investigated implementation of BuildObligationPolicyDatabase, and found that we cannot support this case $closeResource.

I expected that change like 138729ba91b77d9690588c0e2c0213f570ccbc2a can treat the parameter of $closeResource as annotated with @WillClose. But I found that this change does not as I expected; it mark variable closed only when variable's type is same with method parameter's type. In other words, type of Obligation#className is now java.lang.AutoCloseable but type of variables are java.sql.Connection etc. In my understanding, there is no prepared way to fill this gap.

What's the current status of this issue?

No update. Currently nobody can propose solution for this issue.

No update. Currently nobody can propose solution for this issue.

Can a solution be influenced from jacoco? https://github.com/jacoco/jacoco/pull/669

Are there any news on this ?

note: The class file built by Java 11.0.7 (AdoptOpenJDK 11.0.7+10). As described at https://github.com/jacoco/jacoco/pull/669 we have no $closeResource method.

I guess that this issue does not exist in the latest Java 11 release.

Was this page helpful?
0 / 5 - 0 ratings