blob: 25f0c3a573104a2bb5a559bb34b349b5acbf05f2 [file] [log] [blame]
// 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.apimodel;
import static com.android.tools.r8.apimodel.ApiModelingTestHelper.setMockApiLevelForClass;
import static com.android.tools.r8.apimodel.ApiModelingTestHelper.setMockApiLevelForMethod;
import static com.android.tools.r8.apimodel.ApiModelingTestHelper.verifyThat;
import com.android.tools.r8.NeverInline;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.testing.AndroidBuildVersion;
import com.android.tools.r8.utils.AndroidApiLevel;
import java.lang.reflect.Method;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ApiModelTypeReferenceInvokeTest extends TestBase {
@Parameter() public TestParameters parameters;
@Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withAllRuntimesAndApiLevels().build();
}
@Test
public void testR8() throws Exception {
Method apiMethod = LibraryClass.class.getDeclaredMethod("apiMethod");
Method apiCaller = ApiHelper.class.getDeclaredMethod("apiCaller", LibraryClass.class);
Method apiCallerCaller = Main.class.getDeclaredMethod("typeReference", Object.class);
boolean libraryClassOnBoot =
parameters.isDexRuntime()
&& parameters.getApiLevel().isGreaterThanOrEqualTo(AndroidApiLevel.M);
testForR8(parameters.getBackend())
.addProgramClasses(Main.class, ApiHelper.class)
.addLibraryClasses(LibraryClass.class)
.addDefaultRuntimeLibrary(parameters)
.setMinApi(parameters.getApiLevel())
.apply(setMockApiLevelForClass(LibraryClass.class, AndroidApiLevel.M))
.apply(setMockApiLevelForMethod(apiMethod, AndroidApiLevel.M))
.apply(ApiModelingTestHelper::enableApiCallerIdentification)
.addKeepMainRule(Main.class)
.enableInliningAnnotations()
.addAndroidBuildVersion()
.compile()
.inspect(
inspector ->
verifyThat(inspector, parameters, apiCaller)
.inlinedIntoFromApiLevel(apiCallerCaller, AndroidApiLevel.M))
.addRunClasspathClasses(LibraryClass.class)
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutputLinesIf(
libraryClassOnBoot, "LibraryClass::apiMethod", "Hello World")
.assertSuccessWithOutputLinesIf(!libraryClassOnBoot, "Hello World");
}
public static class LibraryClass {
public void apiMethod() {
System.out.println("LibraryClass::apiMethod");
}
}
public static class ApiHelper {
public static void apiCaller(LibraryClass libraryClass) {
if (AndroidBuildVersion.VERSION >= 23) {
libraryClass.apiMethod();
}
}
}
public static class Main {
@NeverInline
public static void typeReference(Object object) {
if (object instanceof LibraryClass) {
ApiHelper.apiCaller((LibraryClass) object);
}
}
public static void main(String[] args) {
typeReference(args.length == 0 ? new LibraryClass() : "Foo");
System.out.println("Hello World");
}
}
}