| // 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.kotlin.metadata; |
| |
| import static com.android.tools.r8.ToolHelper.getKotlinAnnotationJar; |
| import static com.android.tools.r8.ToolHelper.getKotlinStdlibJar; |
| import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent; |
| import static junit.framework.TestCase.assertEquals; |
| import static junit.framework.TestCase.assertNotNull; |
| import static org.hamcrest.MatcherAssert.assertThat; |
| import static org.junit.Assert.assertNull; |
| |
| import com.android.tools.r8.KotlinTestParameters; |
| import com.android.tools.r8.TestParameters; |
| import com.android.tools.r8.ToolHelper; |
| import com.android.tools.r8.kotlin.KotlinMetadataWriter; |
| import com.android.tools.r8.shaking.ProguardKeepAttributes; |
| import com.android.tools.r8.utils.StringUtils; |
| import com.android.tools.r8.utils.codeinspector.ClassSubject; |
| import com.android.tools.r8.utils.codeinspector.CodeInspector; |
| import com.android.tools.r8.utils.codeinspector.FoundClassSubject; |
| import java.io.IOException; |
| import java.nio.file.Path; |
| import java.util.Collection; |
| import java.util.concurrent.ExecutionException; |
| import kotlinx.metadata.jvm.KotlinClassHeader; |
| import kotlinx.metadata.jvm.KotlinClassMetadata; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.junit.runners.Parameterized; |
| |
| @RunWith(Parameterized.class) |
| public class MetadataRewriteBoxedTypesTest extends KotlinMetadataTestBase { |
| |
| private final String EXPECTED = |
| StringUtils.lines("false", "0", "a", "0.042", "0.42", "42", "442", "1", "2", "42", "42"); |
| |
| @Parameterized.Parameters(name = "{0}, {1}") |
| public static Collection<Object[]> data() { |
| return buildParameters( |
| getTestParameters().withCfRuntimes().build(), |
| getKotlinTestParameters().withAllCompilersAndTargetVersions().build()); |
| } |
| |
| public MetadataRewriteBoxedTypesTest( |
| TestParameters parameters, KotlinTestParameters kotlinParameters) { |
| super(kotlinParameters); |
| this.parameters = parameters; |
| } |
| |
| private static final KotlinCompileMemoizer libJars = |
| getCompileMemoizer(getKotlinFileInTest(PKG_PREFIX + "/box_primitives_lib", "lib")); |
| private final TestParameters parameters; |
| |
| @Test |
| public void smokeTest() throws Exception { |
| Path libJar = libJars.getForConfiguration(kotlinc, targetVersion); |
| Path output = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/box_primitives_app", "main")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| testForJvm() |
| .addRunClasspathFiles(getKotlinStdlibJar(kotlinc), libJar) |
| .addClasspath(output) |
| .run(parameters.getRuntime(), PKG + ".box_primitives_app.MainKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| |
| @Test |
| public void smokeTestReflection() throws Exception { |
| Path libJar = libJars.getForConfiguration(kotlinc, targetVersion); |
| Path output = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/box_primitives_app", "main_reflect")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| testForJvm() |
| .addVmArguments("-ea") |
| .addRunClasspathFiles( |
| getKotlinStdlibJar(kotlinc), ToolHelper.getKotlinReflectJar(kotlinc), libJar) |
| .addClasspath(output) |
| .run(parameters.getRuntime(), PKG + ".box_primitives_app.Main_reflectKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| |
| @Test |
| public void testMetadataForLib() throws Exception { |
| Path libJar = |
| testForR8(parameters.getBackend()) |
| .addClasspathFiles(getKotlinStdlibJar(kotlinc), getKotlinAnnotationJar(kotlinc)) |
| .addProgramFiles(libJars.getForConfiguration(kotlinc, targetVersion)) |
| .addKeepAllClassesRule() |
| .addKeepAttributes( |
| ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS, |
| ProguardKeepAttributes.SIGNATURE, |
| ProguardKeepAttributes.INNER_CLASSES, |
| ProguardKeepAttributes.ENCLOSING_METHOD) |
| .compile() |
| .inspect(this::inspect) |
| .writeToZip(); |
| Path main = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/box_primitives_app", "main")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| testForJvm() |
| .addRunClasspathFiles( |
| getKotlinStdlibJar(kotlinc), ToolHelper.getKotlinReflectJar(kotlinc), libJar) |
| .addClasspath(main) |
| .run(parameters.getRuntime(), PKG + ".box_primitives_app.MainKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| |
| private void inspect(CodeInspector inspector) throws IOException, ExecutionException { |
| // Since this has a keep-all classes rule, we should just assert that the meta-data is equal to |
| // the original one. |
| CodeInspector stdLibInspector = |
| new CodeInspector(libJars.getForConfiguration(kotlinc, targetVersion)); |
| for (FoundClassSubject clazzSubject : stdLibInspector.allClasses()) { |
| ClassSubject r8Clazz = inspector.clazz(clazzSubject.getOriginalName()); |
| assertThat(r8Clazz, isPresent()); |
| KotlinClassMetadata originalMetadata = clazzSubject.getKotlinClassMetadata(); |
| KotlinClassMetadata rewrittenMetadata = r8Clazz.getKotlinClassMetadata(); |
| if (originalMetadata == null) { |
| assertNull(rewrittenMetadata); |
| continue; |
| } |
| assertNotNull(rewrittenMetadata); |
| KotlinClassHeader originalHeader = originalMetadata.getHeader(); |
| KotlinClassHeader rewrittenHeader = rewrittenMetadata.getHeader(); |
| assertEquals(originalHeader.getKind(), rewrittenHeader.getKind()); |
| assertEquals(originalHeader.getPackageName(), rewrittenHeader.getPackageName()); |
| // We cannot assert equality of the data since it may be ordered differently. Instead we use |
| // the KotlinMetadataWriter. |
| String expected = KotlinMetadataWriter.kotlinMetadataToString("", originalMetadata); |
| String actual = KotlinMetadataWriter.kotlinMetadataToString("", rewrittenMetadata); |
| assertEquals(expected, actual); |
| } |
| } |
| |
| @Test |
| public void testMetadataForReflect() throws Exception { |
| Path libJar = |
| testForR8(parameters.getBackend()) |
| .addClasspathFiles(getKotlinStdlibJar(kotlinc), getKotlinAnnotationJar(kotlinc)) |
| .addProgramFiles(libJars.getForConfiguration(kotlinc, targetVersion)) |
| .addKeepAllClassesRule() |
| .addKeepAttributes( |
| ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS, |
| ProguardKeepAttributes.SIGNATURE, |
| ProguardKeepAttributes.INNER_CLASSES, |
| ProguardKeepAttributes.ENCLOSING_METHOD) |
| .compile() |
| .writeToZip(); |
| Path main = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/box_primitives_app", "main_reflect")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| testForJvm() |
| .addVmArguments("-ea") |
| .addRunClasspathFiles( |
| getKotlinStdlibJar(kotlinc), ToolHelper.getKotlinReflectJar(kotlinc), libJar) |
| .addClasspath(main) |
| .run(parameters.getRuntime(), PKG + ".box_primitives_app.Main_reflectKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| } |