blob: ee6dc18a75aa4959e534efe10915573358698339 [file] [log] [blame]
// 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.classmerging.horizontal;
import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
import static org.hamcrest.MatcherAssert.assertThat;
import com.android.tools.r8.DataEntryResource;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.origin.Origin;
import com.android.tools.r8.utils.StringUtils;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.ServiceLoader;
import org.junit.Test;
public class ServiceLoaderTest extends HorizontalClassMergingTestBase {
public ServiceLoaderTest(TestParameters parameters, boolean enableHorizontalClassMerging) {
super(parameters, enableHorizontalClassMerging);
}
@Test
public void testR8() throws Exception {
List<String> serviceImplementations = Lists.newArrayList();
serviceImplementations.add(B.class.getTypeName());
serviceImplementations.add(C.class.getTypeName());
testForR8(parameters.getBackend())
.addInnerClasses(getClass())
.addKeepMainRule(Main.class)
.addDataEntryResources(
DataEntryResource.fromBytes(
StringUtils.lines(serviceImplementations).getBytes(),
"META-INF/services/" + A.class.getTypeName(),
Origin.unknown()))
.addOptionsModification(
options -> options.enableHorizontalClassMerging = enableHorizontalClassMerging)
.setMinApi(parameters.getApiLevel())
.run(parameters.getRuntime(), Main.class)
.assertSuccess()
.inspect(
codeInspector -> {
assertThat(codeInspector.clazz(A.class), isPresent());
assertThat(codeInspector.clazz(B.class), isPresent());
assertThat(codeInspector.clazz(C.class), isPresent());
});
}
public interface A {
String foo();
}
public static class B implements A {
@Override
public String foo() {
return "B";
}
}
public static class C implements A {
@Override
public String foo() {
return "C";
}
}
public static class Main {
public static void main(String[] args) {
for (A a : ServiceLoader.load(A.class)) {
System.out.println(a.foo());
}
}
}
}