Don't make public methods for lambda calls.
In cases where the classes are mocked by mockito we can see issues with return types of private classes.
Bug: 195402351
Bug: 187502966
Change-Id: I638ed6d71effb8ca0fd3fae9c503ace0ae2ccdd5
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/LambdaClass.java b/src/main/java/com/android/tools/r8/ir/desugar/LambdaClass.java
index dd815a3..e8bdbb9 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/LambdaClass.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/LambdaClass.java
@@ -661,7 +661,6 @@
// its accessibility and make it virtual.
MethodAccessFlags newAccessFlags = encodedMethod.accessFlags.copy();
newAccessFlags.unsetPrivate();
- newAccessFlags.setPublic();
DexEncodedMethod newMethod =
new DexEncodedMethod(
callTarget,
diff --git a/src/test/java/com/android/tools/r8/desugar/DesugarLambdaDontPublicize.java b/src/test/java/com/android/tools/r8/desugar/DesugarLambdaDontPublicize.java
new file mode 100644
index 0000000..cc9d06b
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/DesugarLambdaDontPublicize.java
@@ -0,0 +1,85 @@
+// Copyright (c) 2021, 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;
+
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.DesugarTestConfiguration;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.utils.codeinspector.ClassOrMemberSubject;
+import java.util.ArrayList;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class DesugarLambdaDontPublicize extends TestBase {
+
+ @Parameterized.Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
+ }
+
+ private final TestParameters parameters;
+
+ public DesugarLambdaDontPublicize(TestParameters parameters) {
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void testDesugar() throws Exception {
+ testForDesugaring(parameters)
+ .addInnerClasses(DesugarLambdaDontPublicize.class)
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccessWithOutputLines("foobar")
+ .inspectIf(
+ DesugarTestConfiguration::isDesugared,
+ inspector -> {
+ // Ensure that we don't add an extra public method for the lambda call.
+ // Mockito will potentially mock this, see: b/195402351
+ long count =
+ inspector.clazz(TestClass.class).allMethods().stream()
+ .filter(ClassOrMemberSubject::isPublic)
+ .count();
+ assertEquals(count, 3);
+ });
+ }
+
+ public static class StringList extends ArrayList<String> {
+ public void forEachString(MyConsumer<String> consumer) {
+ for (String s : this) {
+ consumer.accept(s);
+ }
+ }
+ }
+
+ public interface MyConsumer<T> {
+ void accept(T s);
+ }
+
+ public static class TestClass {
+
+ public void test() {
+ StringList list = new StringList();
+
+ list.add("foobar");
+ list.forEachString(
+ s -> {
+ class MyConsumerImpl implements MyConsumer<String> {
+ public void accept(String s) {
+ System.out.println(s);
+ }
+ }
+ new MyConsumerImpl().accept(s);
+ });
+ }
+
+ public static void main(String[] args) {
+ new TestClass().test();
+ }
+ }
+}