New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try-with-resources and java9 shows bugs #493

Open
ptahchiev opened this Issue Nov 3, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@ptahchiev

ptahchiev commented Nov 3, 2017

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] 

KengoTODA added a commit that referenced this issue Nov 5, 2017

@KengoTODA

This comment has been minimized.

Member

KengoTODA commented Nov 5, 2017

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 (Exception e) {
    t.addSuppressed(e);
  }
}

KengoTODA added a commit that referenced this issue Nov 16, 2017

@KengoTODA

This comment has been minimized.

Member

KengoTODA commented Nov 16, 2017

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

I expected that change like 138729b 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.

@tokuhirom

This comment has been minimized.

tokuhirom commented Nov 12, 2018

What's the current status of this issue?

@KengoTODA

This comment has been minimized.

Member

KengoTODA commented Nov 12, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment