Fix code size for FileChannel
Change-Id: I63697f083fde05b0577d797450946c41f3882ac4
diff --git a/src/test/java/com/android/tools/r8/L8TestBuilder.java b/src/test/java/com/android/tools/r8/L8TestBuilder.java
index 2a1d0f5..190e87b 100644
--- a/src/test/java/com/android/tools/r8/L8TestBuilder.java
+++ b/src/test/java/com/android/tools/r8/L8TestBuilder.java
@@ -191,7 +191,7 @@
ImmutableList.<String>builder()
.addAll(keepRules)
.addAll(
- generatedKeepRules != null
+ generatedKeepRules != null && !generatedKeepRules.isEmpty()
? ImmutableList.of(generatedKeepRules)
: Collections.emptyList())
.build();
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java
index 40942f7..116b62a 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/DesugaredLibraryWarningTest.java
@@ -14,8 +14,10 @@
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.android.tools.r8.errors.UnusedProguardKeepRuleDiagnostic;
import com.google.common.collect.ImmutableList;
import java.util.List;
+import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -95,7 +97,11 @@
} else {
diagnosticsHandler.assertNoWarnings();
}
- diagnosticsHandler.assertNoInfos();
+ // The L8 compilation has always the extra keep rules from the json files, which may
+ // be unused, leading to infos.
+ Assert.assertTrue(
+ diagnosticsHandler.getInfos().stream()
+ .allMatch(info -> info instanceof UnusedProguardKeepRuleDiagnostic));
});
}
}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/NoDesugaredLibraryDexFileTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/NoDesugaredLibraryDexFileTest.java
index 0cf1b01..9cd3753 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/NoDesugaredLibraryDexFileTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/NoDesugaredLibraryDexFileTest.java
@@ -19,7 +19,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
-import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -58,7 +57,7 @@
.addKeepClassAndMembersRules(Executor.class)
.compile()
.inspect(this::assertNoForwardingStreamMethod)
- .inspectKeepRules(Assert::assertNull)
+ .inspectKeepRules(kr -> assertTrue(kr == null || kr.isEmpty()))
.run(parameters.getRuntime(), Executor.class)
.assertSuccessWithOutputLines("1", "0");
}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/ProgramRewritingTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/ProgramRewritingTest.java
index 5e6c723..2faea77 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/ProgramRewritingTest.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/ProgramRewritingTest.java
@@ -109,7 +109,7 @@
if (parameters.getApiLevel().getLevel() < AndroidApiLevel.N.getLevel()) {
keepRules.set(String.join("\n", kr));
} else {
- assert kr == null;
+ assert kr == null || kr.isEmpty();
keepRules.set("");
}
})
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelSizeTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelSizeTest.java
new file mode 100644
index 0000000..651f108
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/FileChannelSizeTest.java
@@ -0,0 +1,109 @@
+// Copyright (c) 2023, 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 com.android.tools.r8.desugar.desugaredlibrary.jdk11;
+
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.DEFAULT_SPECIFICATIONS;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK11_PATH;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper.DexVm.Version;
+import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class FileChannelSizeTest extends DesugaredLibraryTestBase {
+
+ private static final String EXPECTED_RESULT =
+ StringUtils.lines("class java.io.FileNotFoundException");
+ private static final String EXPECTED_KEEP_RULES =
+ StringUtils.lines(
+ "-keep class j$.nio.channels.DesugarChannels {",
+ " java.nio.channels.FileChannel"
+ + " convertMaybeLegacyFileChannelFromLibrary(java.nio.channels.FileChannel);",
+ "}");
+
+ private final TestParameters parameters;
+ private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+ private final CompilationSpecification compilationSpecification;
+
+ @Parameters(name = "{0}, spec: {1}, {2}")
+ public static List<Object[]> data() {
+ return buildParameters(
+ // Skip Android 4.4.4 due to missing libjavacrypto.
+ getTestParameters()
+ .withDexRuntime(Version.V4_0_4)
+ .withDexRuntimesStartingFromIncluding(Version.V5_1_1)
+ .withAllApiLevels()
+ .build(),
+ ImmutableList.of(JDK11_PATH),
+ DEFAULT_SPECIFICATIONS);
+ }
+
+ public FileChannelSizeTest(
+ TestParameters parameters,
+ LibraryDesugaringSpecification libraryDesugaringSpecification,
+ CompilationSpecification compilationSpecification) {
+ this.parameters = parameters;
+ this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+ this.compilationSpecification = compilationSpecification;
+ }
+
+ @Test
+ public void test() throws Throwable {
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+ .addInnerClasses(getClass())
+ .addKeepMainRule(TestClass.class)
+ .compile()
+ .inspectKeepRules(
+ kr -> {
+ if (parameters.getApiLevel().isLessThan(AndroidApiLevel.N)) {
+ assertEquals(EXPECTED_KEEP_RULES, kr.get(0));
+ }
+ })
+ .inspectL8(
+ i -> {
+ if (compilationSpecification.isL8Shrink()) {
+ assertTrue(i.allClasses().size() <= 6);
+ if (parameters.getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.N)) {
+ assertEquals(0, i.allClasses().size());
+ }
+ }
+ })
+ .withArt6Plus64BitsLib()
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutput(EXPECTED_RESULT);
+ }
+
+ public static class TestClass {
+
+ public static void main(String[] args) throws IOException {
+ try {
+ String toWrite = "Hello World! ";
+ ByteBuffer byteBuffer = ByteBuffer.allocate(toWrite.length());
+ FileInputStream fileInputStream = new FileInputStream(new File("notexisting.txt"));
+ fileInputStream.getChannel().read(byteBuffer);
+ fileInputStream.close();
+ System.out.println(new String(byteBuffer.array()));
+ } catch (IOException e) {
+ System.out.println(e.getClass());
+ }
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/LibraryDesugaringSpecification.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/LibraryDesugaringSpecification.java
index 3d76b78..42ade2a 100644
--- a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/LibraryDesugaringSpecification.java
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/test/LibraryDesugaringSpecification.java
@@ -269,10 +269,10 @@
.applyIf(
l8Shrink,
builder -> {
- if (keepRule != null && !keepRule.trim().isEmpty()) {
- String totalKeepRules = keepRule + "\n" + getExtraKeepRules();
- builder.addGeneratedKeepRules(totalKeepRules);
- }
+ assert keepRule != null;
+ String totalKeepRules =
+ keepRule + (getExtraKeepRules().isEmpty() ? "" : ("\n" + getExtraKeepRules()));
+ builder.addGeneratedKeepRules(totalKeepRules);
},
L8TestBuilder::setDebug);
}
diff --git a/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1 b/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1
index d52bbc5..247f833 100644
--- a/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1
+++ b/third_party/openjdk/desugar_jdk_libs_11.tar.gz.sha1
@@ -1 +1,2 @@
-024c0b5aa78dba1442189dc55367551825cc4085
\ No newline at end of file
+aa4b6c72e66addc2d7453eedc62a6d810994af6d
+