Support Path.of at all api levels
Change-Id: I56ba39c617aa76de966a413946bca9b87143b3f0
diff --git a/src/library_desugar/jdk11/desugar_jdk_libs_nio.json b/src/library_desugar/jdk11/desugar_jdk_libs_nio.json
index dfdb60d..740060c 100644
--- a/src/library_desugar/jdk11/desugar_jdk_libs_nio.json
+++ b/src/library_desugar/jdk11/desugar_jdk_libs_nio.json
@@ -7,6 +7,13 @@
"common_flags": [
{
"api_level_below_or_equal": 10000,
+ "amend_library_method": [
+ "public static java.nio.file.Path java.nio.file.Path#of(java.lang.String, java.lang.String[])",
+ "public static java.nio.file.Path java.nio.file.Path#of(java.net.URI)"
+ ]
+ },
+ {
+ "api_level_below_or_equal": 10000,
"api_level_greater_or_equal": 24,
"rewrite_prefix": {
"java.util.stream.DesugarDoubleStream": "j$.util.stream.DesugarDoubleStream",
@@ -405,6 +412,14 @@
]
},
{
+ "api_level_below_or_equal": 10000,
+ "api_level_greater_or_equal": 26,
+ "retarget_method": {
+ "java.nio.file.Path java.nio.file.Path#of(java.lang.String, java.lang.String[])": "java.nio.file.Path java.nio.file.Paths#get(java.lang.String, java.lang.String[])",
+ "java.nio.file.Path java.nio.file.Path#of(java.net.URI)": "java.nio.file.Path java.nio.file.Paths#get(java.net.URI)"
+ }
+ },
+ {
"api_level_below_or_equal": 33,
"amend_library_method": [
"public java.lang.Object[] java.util.Collection#toArray(java.util.function.IntFunction)"
diff --git a/src/test/examplesJava11/path/PathExample.java b/src/test/examplesJava11/path/PathExample.java
new file mode 100644
index 0000000..8b17da2
--- /dev/null
+++ b/src/test/examplesJava11/path/PathExample.java
@@ -0,0 +1,16 @@
+// Copyright (c) 2022, 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 path;
+
+import java.nio.file.Path;
+
+public class PathExample {
+
+ public static void main(String[] args) {
+ Path thePath = Path.of("foo", "bar");
+ System.out.println(thePath);
+ System.out.println(Path.of(thePath.toUri()).getFileName());
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/PathOfTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/PathOfTest.java
new file mode 100644
index 0000000..33fa6ab
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/PathOfTest.java
@@ -0,0 +1,61 @@
+// Copyright (c) 2022, 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 com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper;
+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.StringUtils;
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+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 PathOfTest extends DesugaredLibraryTestBase {
+
+ private final TestParameters parameters;
+ private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+ private final CompilationSpecification compilationSpecification;
+
+ private static final Path INPUT_JAR = Paths.get(ToolHelper.EXAMPLES_JAVA11_JAR_DIR + "path.jar");
+ private static final String EXPECTED_OUTPUT = StringUtils.lines("foo/bar", "bar");
+ private static final String MAIN_CLASS = "path.PathExample";
+
+ @Parameters(name = "{0}, spec: {1}, {2}")
+ public static List<Object[]> data() {
+ return buildParameters(
+ getTestParameters().withDexRuntimes().withAllApiLevels().build(),
+ ImmutableList.of(JDK11_PATH),
+ DEFAULT_SPECIFICATIONS);
+ }
+
+ public PathOfTest(
+ TestParameters parameters,
+ LibraryDesugaringSpecification libraryDesugaringSpecification,
+ CompilationSpecification compilationSpecification) {
+ this.parameters = parameters;
+ this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+ this.compilationSpecification = compilationSpecification;
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+ .addProgramFiles(INPUT_JAR)
+ .addKeepMainRule(MAIN_CLASS)
+ .run(parameters.getRuntime(), MAIN_CLASS)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+}