Support for $closeResource synthesized by java 9 compiler
Pilot implementation of rewriting $closeResource(...) methods
synthesized by java 9 compiler for some cases of try-with-resources.
The problem with current implementation is that on pre-19 devices
even though java.lang.AutoCloseable is available in platform (not in
android.jar), is not fully supported in a sense that not all classes
implement it properly.
We have to replace it with our version of $closeResource(...) which
takes the resource as java.lang.Object and checks if it can cast
it to AutoCloseable.
To avoid duplicating the methods we also use one single method defined
in an utility class '$r8$twr$utility' created when needed. This class
needs to be deduped for incremental dexing scenarios.
Bug: 70292540
Change-Id: I4da89a8482ab6895267e58c3debc7ad4e12a3dfe
diff --git a/src/test/examplesJava9/twrcloseresource/TwrCloseResourceTest.java b/src/test/examplesJava9/twrcloseresource/TwrCloseResourceTest.java
new file mode 100644
index 0000000..d4dbf5b
--- /dev/null
+++ b/src/test/examplesJava9/twrcloseresource/TwrCloseResourceTest.java
@@ -0,0 +1,139 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package twrcloseresource;
+
+import java.util.jar.JarFile;
+
+public class TwrCloseResourceTest implements Iface {
+ public static void main(String[] args) {
+ TwrCloseResourceTest o = new TwrCloseResourceTest();
+ o.foo(args[0]);
+ o.iFoo(args[0]);
+ bar(args[0]);
+ Iface.iBar(args[0]);
+ }
+
+ synchronized void foo(String arg) {
+ try {
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("A");
+ } catch (Exception e) {
+ System.out.println("B");
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("C");
+ }
+ System.out.println("D");
+ throw new RuntimeException();
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("E");
+ }
+ } catch (Exception e) {
+ System.out.println("F");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("G");
+ try (JarFile b = new JarFile(arg)) {
+ System.out.println("H");
+ } finally {
+ System.out.println("I");
+ throw new RuntimeException();
+ }
+ } catch (Exception e) {
+ System.out.println("J");
+ }
+ System.out.println("K");
+ }
+
+ static synchronized void bar(String arg) {
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("1");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("2");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("3");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("4");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("5");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("6");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("7");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("8");
+ }
+ System.out.println("99");
+ }
+}
+
+interface Iface {
+ default void iFoo(String arg) {
+ try {
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("iA");
+ } catch (Exception e) {
+ System.out.println("iB");
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("iC");
+ }
+ System.out.println("iD");
+ throw new RuntimeException();
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("iE");
+ }
+ } catch (Exception e) {
+ System.out.println("iF");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("iG");
+ try (JarFile b = new JarFile(arg)) {
+ System.out.println("iH");
+ } finally {
+ System.out.println("iI");
+ throw new RuntimeException();
+ }
+ } catch (Exception e) {
+ System.out.println("iJ");
+ }
+ System.out.println("iK");
+ }
+
+ static void iBar(String arg) {
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("i1");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("i2");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("i3");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("i4");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("i5");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("i6");
+ }
+ try (JarFile a = new JarFile(arg)) {
+ System.out.println("i7");
+ throw new RuntimeException();
+ } catch (Exception e) {
+ System.out.println("i8");
+ }
+ System.out.println("i99");
+ }
+}