Test java stub remove verification error
Bug: 172796269
Change-Id: I3840e569da224883a97de3738fa12b5473a6b3fe
diff --git a/src/test/java/com/android/tools/r8/desugar/softverificationerrorremoval/GetDeclaredMethodsErrorRemovalTest.java b/src/test/java/com/android/tools/r8/desugar/softverificationerrorremoval/GetDeclaredMethodsErrorRemovalTest.java
new file mode 100644
index 0000000..2b72a6f
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/softverificationerrorremoval/GetDeclaredMethodsErrorRemovalTest.java
@@ -0,0 +1,95 @@
+// Copyright (c) 2020, 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.softverificationerrorremoval;
+
+import static com.android.tools.r8.TestRuntime.CfRuntime.getCheckedInJdk8;
+import static org.hamcrest.CoreMatchers.containsString;
+
+import com.android.tools.r8.D8TestRunResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.ToolHelper;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.function.Supplier;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class GetDeclaredMethodsErrorRemovalTest extends TestBase {
+
+ private final TestParameters parameters;
+ private static final String TYPE =
+ "com.android.tools.r8.desugar.softverificationerrorremoval."
+ + "GetDeclaredMethodsErrorRemovalTest";
+ private static final String EXPECTED_RESULT =
+ "[void"
+ + " "
+ + TYPE
+ + "$ExampleClass.hello(),"
+ + " void"
+ + " "
+ + TYPE
+ + "$ExampleClass.hello(java.util.function.Supplier)]";
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDexRuntimes().withAllApiLevels().build();
+ }
+
+ public GetDeclaredMethodsErrorRemovalTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testWithoutJavaStub() throws Exception {
+ D8TestRunResult run =
+ testForD8()
+ .addInnerClasses(GetDeclaredMethodsErrorRemovalTest.class)
+ .setMinApi(parameters.getApiLevel())
+ .compile()
+ .run(parameters.getRuntime(), TestClass.class);
+ if (parameters.getDexRuntimeVersion().isOlderThanOrEqual(ToolHelper.DexVm.Version.V6_0_1)) {
+ run.assertFailureWithErrorThatMatches(containsString("java.lang.NoClassDefFoundError"));
+ } else {
+ run.assertSuccessWithOutputLines(EXPECTED_RESULT);
+ }
+ }
+
+ @Test
+ public void testWithJavaStub() throws Exception {
+ Path stubs =
+ javac(getCheckedInJdk8())
+ .addSourceFiles(Paths.get("src/test/javaStubs/Supplier.java"))
+ .compile();
+ testForD8()
+ .addInnerClasses(GetDeclaredMethodsErrorRemovalTest.class)
+ .addProgramFiles(stubs)
+ .setMinApi(parameters.getApiLevel())
+ .compile()
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutputLines(EXPECTED_RESULT);
+ }
+
+ static class TestClass {
+
+ public static void main(String[] args) {
+ System.out.println(Arrays.toString(ExampleClass.class.getDeclaredMethods()));
+ }
+ }
+
+ static class ExampleClass {
+ void hello() {
+ System.out.println("hello");
+ }
+
+ void hello(Supplier<String> stringSupplier) {
+ System.out.println(stringSupplier.get());
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/softverificationerrorremoval/SoftVerificationErrorRemovalTest.java b/src/test/java/com/android/tools/r8/desugar/softverificationerrorremoval/SoftVerificationErrorRemovalTest.java
new file mode 100644
index 0000000..1a1720e
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/softverificationerrorremoval/SoftVerificationErrorRemovalTest.java
@@ -0,0 +1,94 @@
+// Copyright (c) 2020, 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.softverificationerrorremoval;
+
+import static com.android.tools.r8.TestRuntime.CfRuntime.getCheckedInJdk8;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.D8TestRunResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.ToolHelper;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.function.Supplier;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class SoftVerificationErrorRemovalTest extends TestBase {
+
+ private final TestParameters parameters;
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withDexRuntimes().withAllApiLevels().build();
+ }
+
+ public SoftVerificationErrorRemovalTest(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testWithoutJavaStub() throws Exception {
+ D8TestRunResult run =
+ testForD8()
+ .addInnerClasses(SoftVerificationErrorRemovalTest.class)
+ .setMinApi(parameters.getApiLevel())
+ .compile()
+ .run(parameters.getRuntime(), TestClass.class);
+ assertVerificationErrorsPresent(
+ run.getStdErr(),
+ parameters.getDexRuntimeVersion().isOlderThanOrEqual(ToolHelper.DexVm.Version.V4_4_4));
+ }
+
+ private void assertVerificationErrorsPresent(String stdErr, boolean present) {
+ assertEquals(
+ present,
+ stdErr.contains(
+ "VFY: unable to find class referenced in signature (Ljava/util/function/Supplier;)"));
+ assertEquals(
+ present,
+ stdErr.contains(
+ "VFY: unable to resolve interface method 7: Ljava/util/function/Supplier;.get"
+ + " ()Ljava/lang/Object;"));
+ }
+
+ @Test
+ public void testWithJavaStub() throws Exception {
+ Path stubs =
+ javac(getCheckedInJdk8())
+ .addSourceFiles(Paths.get("src/test/javaStubs/Supplier.java"))
+ .compile();
+ D8TestRunResult run =
+ testForD8()
+ .addInnerClasses(SoftVerificationErrorRemovalTest.class)
+ .addProgramFiles(stubs)
+ .setMinApi(parameters.getApiLevel())
+ .compile()
+ .run(parameters.getRuntime(), TestClass.class);
+ assertVerificationErrorsPresent(run.getStdErr(), false);
+ }
+
+ static class TestClass {
+
+ public static void main(String[] args) {
+ ExampleClass exampleClass = new ExampleClass();
+ exampleClass.hello();
+ }
+ }
+
+ static class ExampleClass {
+ void hello() {
+ System.out.println("hello");
+ }
+
+ void hello(Supplier<String> stringSupplier) {
+ System.out.println(stringSupplier.get());
+ }
+ }
+}
diff --git a/src/test/javaStubs/Supplier.java b/src/test/javaStubs/Supplier.java
new file mode 100644
index 0000000..f9c4860
--- /dev/null
+++ b/src/test/javaStubs/Supplier.java
@@ -0,0 +1,9 @@
+// Copyright (c) 2020, 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 java.util.function;
+
+public interface Supplier {
+ Object get();
+}