| // Copyright (c) 2019, 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.KotlinCompilerTool.KotlinCompilerVersion.MIN_SUPPORTED_VERSION; |
| import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent; |
| import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndNotRenamed; |
| import static com.android.tools.r8.utils.codeinspector.Matchers.isPresentAndRenamed; |
| import static org.hamcrest.CoreMatchers.not; |
| import static org.hamcrest.MatcherAssert.assertThat; |
| import static org.junit.Assert.assertTrue; |
| |
| import com.android.tools.r8.KotlinTestParameters; |
| import com.android.tools.r8.TestParameters; |
| 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.KmClassSubject; |
| import java.nio.file.Path; |
| import java.util.Collection; |
| import java.util.List; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.junit.runners.Parameterized; |
| |
| @RunWith(Parameterized.class) |
| public class MetadataRewriteInSuperTypeTest extends KotlinMetadataTestBase { |
| private static final String EXPECTED = StringUtils.lines("Impl::foo", "Program::foo"); |
| |
| private final TestParameters parameters; |
| |
| @Parameterized.Parameters(name = "{0}, {1}") |
| public static Collection<Object[]> data() { |
| return buildParameters( |
| getTestParameters().withCfRuntimes().build(), |
| getKotlinTestParameters() |
| .withCompilersStartingFromIncluding(MIN_SUPPORTED_VERSION) |
| .withAllTargetVersions() |
| .build()); |
| } |
| |
| public MetadataRewriteInSuperTypeTest( |
| TestParameters parameters, KotlinTestParameters kotlinParameters) { |
| super(kotlinParameters); |
| this.parameters = parameters; |
| } |
| |
| private static final KotlinCompileMemoizer superTypeLibJarMap = |
| getCompileMemoizer( |
| getKotlinFileInTest(PKG_PREFIX + "/supertype_lib", "impl"), |
| getKotlinFileInTest(PKG_PREFIX + "/supertype_lib" + "/internal", "itf")); |
| |
| @Test |
| public void smokeTest() throws Exception { |
| Path libJar = superTypeLibJarMap.getForConfiguration(kotlinc, targetVersion); |
| |
| Path output = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/supertype_app", "main")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| |
| testForJvm() |
| .addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar) |
| .addClasspath(output) |
| .run(parameters.getRuntime(), PKG + ".supertype_app.MainKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| |
| @Test |
| public void testMetadataInSupertype_merged() throws Exception { |
| Path libJar = |
| testForR8(parameters.getBackend()) |
| .addClasspathFiles(kotlinc.getKotlinStdlibJar()) |
| .addProgramFiles(superTypeLibJarMap.getForConfiguration(kotlinc, targetVersion)) |
| // Keep non-private members except for ones in `internal` definitions. |
| .addKeepRules("-keep public class !**.internal.**, * { !private *; }") |
| .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS) |
| .compile() |
| .inspect(this::inspectMerged) |
| .writeToZip(); |
| |
| Path output = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/supertype_app", "main")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| |
| testForJvm() |
| .addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar) |
| .addClasspath(output) |
| .run(parameters.getRuntime(), PKG + ".supertype_app.MainKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| |
| private void inspectMerged(CodeInspector inspector) { |
| String itfClassName = PKG + ".supertype_lib.internal.Itf"; |
| String implClassName = PKG + ".supertype_lib.Impl"; |
| |
| assertThat(inspector.clazz(itfClassName), not(isPresent())); |
| |
| ClassSubject impl = inspector.clazz(implClassName); |
| assertThat(impl, isPresentAndNotRenamed()); |
| // API entry is kept, hence the presence of Metadata. |
| KmClassSubject kmClass = impl.getKmClass(); |
| assertThat(kmClass, isPresent()); |
| List<ClassSubject> superTypes = kmClass.getSuperTypes(); |
| assertTrue(superTypes.stream().noneMatch( |
| supertype -> supertype.getFinalDescriptor().contains("internal"))); |
| assertTrue(superTypes.stream().noneMatch( |
| supertype -> supertype.getFinalDescriptor().contains("Itf"))); |
| } |
| |
| @Test |
| public void testMetadataInSupertype_renamed() throws Exception { |
| Path libJar = |
| testForR8(parameters.getBackend()) |
| .addClasspathFiles(kotlinc.getKotlinStdlibJar()) |
| .addProgramFiles(superTypeLibJarMap.getForConfiguration(kotlinc, targetVersion)) |
| // Keep non-private members except for ones in `internal` definitions. |
| .addKeepRules("-keep public class !**.internal.**, * { !private *; }") |
| // Keep `internal` definitions, but allow minification. |
| .addKeepRules("-keep,allowobfuscation class **.internal.** { *; }") |
| .addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS) |
| .compile() |
| .inspect(this::inspectRenamed) |
| .writeToZip(); |
| |
| Path output = |
| kotlinc(parameters.getRuntime().asCf(), kotlinc, targetVersion) |
| .addClasspathFiles(libJar) |
| .addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/supertype_app", "main")) |
| .setOutputPath(temp.newFolder().toPath()) |
| .compile(); |
| |
| testForJvm() |
| .addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar) |
| .addClasspath(output) |
| .run(parameters.getRuntime(), PKG + ".supertype_app.MainKt") |
| .assertSuccessWithOutput(EXPECTED); |
| } |
| |
| private void inspectRenamed(CodeInspector inspector) { |
| String itfClassName = PKG + ".supertype_lib.internal.Itf"; |
| String implClassName = PKG + ".supertype_lib.Impl"; |
| |
| ClassSubject itf = inspector.clazz(itfClassName); |
| assertThat(itf, isPresentAndRenamed()); |
| |
| ClassSubject impl = inspector.clazz(implClassName); |
| assertThat(impl, isPresent()); |
| assertThat(impl, isPresentAndNotRenamed()); |
| // API entry is kept, hence the presence of Metadata. |
| KmClassSubject kmClass = impl.getKmClass(); |
| assertThat(kmClass, isPresent()); |
| List<ClassSubject> superTypes = kmClass.getSuperTypes(); |
| assertTrue(superTypes.stream().noneMatch( |
| supertype -> supertype.getFinalDescriptor().contains("internal"))); |
| assertTrue(superTypes.stream().noneMatch( |
| supertype -> supertype.getFinalDescriptor().contains("Itf"))); |
| assertTrue(superTypes.stream().anyMatch( |
| supertype -> supertype.getFinalDescriptor().equals(itf.getFinalDescriptor()))); |
| } |
| } |