Cleanup ServiceLoaderRewritingTest for finding service loader loads
Change-Id: Iafee583f3d1c793019b3d0bd1f56834e10a96d60
diff --git a/src/test/java/com/android/tools/r8/rewrite/ServiceLoaderRewritingTest.java b/src/test/java/com/android/tools/r8/rewrite/ServiceLoaderRewritingTest.java
index 91411e5..d0e77f1 100644
--- a/src/test/java/com/android/tools/r8/rewrite/ServiceLoaderRewritingTest.java
+++ b/src/test/java/com/android/tools/r8/rewrite/ServiceLoaderRewritingTest.java
@@ -22,7 +22,6 @@
import com.android.tools.r8.utils.codeinspector.ClassSubject;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import com.android.tools.r8.utils.codeinspector.InstructionSubject;
-import com.android.tools.r8.utils.codeinspector.MethodSubject;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ServiceLoader;
@@ -144,13 +143,7 @@
.inspect(
inspector -> {
// Check that we have actually rewritten the calls to ServiceLoader.load.
- ClassSubject clazz = inspector.clazz(MainRunner.class);
- assertTrue(clazz.isPresent());
- MethodSubject main = clazz.uniqueMethodWithName("main");
- assertTrue(main.isPresent());
- assertTrue(
- main.streamInstructions()
- .noneMatch(ServiceLoaderRewritingTest::isServiceLoaderLoad));
+ assertEquals(0, getServiceLoaderLoads(inspector, MainRunner.class));
});
// Check that we have removed the service configuration from META-INF/services.
@@ -178,13 +171,7 @@
.inspector();
// Check that we have not rewritten the calls to ServiceLoader.load.
- ClassSubject clazz = inspector.clazz(OtherRunner.class);
- assertTrue(clazz.isPresent());
- MethodSubject main = clazz.uniqueMethodWithName("main");
- assertTrue(main.isPresent());
- assertEquals(
- 3,
- main.streamInstructions().filter(ServiceLoaderRewritingTest::isServiceLoaderLoad).count());
+ assertEquals(3, getServiceLoaderLoads(inspector, OtherRunner.class));
// Check that we have not removed the service configuration from META-INF/services.
ZipFile zip = new ZipFile(path.toFile());
@@ -216,19 +203,8 @@
.inspector();
// Check that we have not rewritten the calls to ServiceLoader.load.
- ClassSubject clazz = inspector.clazz(EscapingRunner.class);
- assertTrue(clazz.isPresent());
- int allServiceLoaders =
- clazz.allMethods().stream()
- .mapToInt(
- method ->
- (int)
- method
- .streamInstructions()
- .filter(ServiceLoaderRewritingTest::isServiceLoaderLoad)
- .count())
- .sum();
- assertEquals(3, allServiceLoaders);
+ assertEquals(3, getServiceLoaderLoads(inspector, EscapingRunner.class));
+
// Check that we have not removed the service configuration from META-INF/services.
ZipFile zip = new ZipFile(path.toFile());
ClassSubject serviceImpl = inspector.clazz(ServiceImpl.class);
@@ -263,13 +239,7 @@
.inspector();
// Check that we have not rewritten the calls to ServiceLoader.load.
- ClassSubject clazz = inspector.clazz(MainRunner.class);
- assertTrue(clazz.isPresent());
- MethodSubject main = clazz.uniqueMethodWithName("main");
- assertTrue(main.isPresent());
- assertEquals(
- 3,
- main.streamInstructions().filter(ServiceLoaderRewritingTest::isServiceLoaderLoad).count());
+ assertEquals(3, getServiceLoaderLoads(inspector, MainRunner.class));
// Check that we have not removed the service configuration from META-INF/services.
ZipFile zip = new ZipFile(path.toFile());
@@ -278,6 +248,19 @@
assertNotNull(zip.getEntry("META-INF/services/" + service.getFinalName()));
}
+ private static long getServiceLoaderLoads(CodeInspector inspector, Class<?> clazz) {
+ ClassSubject classSubject = inspector.clazz(clazz);
+ assertTrue(classSubject.isPresent());
+ return classSubject.allMethods().stream()
+ .mapToLong(
+ method ->
+ method
+ .streamInstructions()
+ .filter(ServiceLoaderRewritingTest::isServiceLoaderLoad)
+ .count())
+ .sum();
+ }
+
private static boolean isServiceLoaderLoad(InstructionSubject instruction) {
return instruction.isInvokeStatic()
&& instruction.getMethod().qualifiedName().contains("ServiceLoader.load");