blob: a177f44bdf864e5ff22faa7a9c6675d5d721b3df [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.kotlin.metadata;
import static com.android.tools.r8.KotlinCompilerTool.KotlinCompilerVersion.KOTLINC_1_4_20;
import static com.android.tools.r8.KotlinCompilerTool.KotlinCompilerVersion.MIN_SUPPORTED_VERSION;
import static com.android.tools.r8.utils.codeinspector.Matchers.isExtensionFunction;
import static com.android.tools.r8.utils.codeinspector.Matchers.isExtensionProperty;
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.assertNotNull;
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 com.android.tools.r8.utils.codeinspector.KmFunctionSubject;
import com.android.tools.r8.utils.codeinspector.KmPackageSubject;
import com.android.tools.r8.utils.codeinspector.KmPropertySubject;
import com.android.tools.r8.utils.codeinspector.Matchers;
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 MetadataRewriteInExtensionPropertyTest extends KotlinMetadataTestBase {
private static final String EXPECTED = StringUtils.lines("do stuff", "do stuff");
private final TestParameters parameters;
@Parameterized.Parameters(name = "{0}, {1}")
public static Collection<Object[]> data() {
return buildParameters(
getTestParameters().withCfRuntimes().build(),
getKotlinTestParameters()
.withCompilersStartingFromIncluding(MIN_SUPPORTED_VERSION)
.withAllLambdaGenerations()
.withAllTargetVersions()
.build());
}
public MetadataRewriteInExtensionPropertyTest(
TestParameters parameters, KotlinTestParameters kotlinParameters) {
super(kotlinParameters);
this.parameters = parameters;
}
private static final KotlinCompileMemoizer extLibJarMap =
getCompileMemoizer(getKotlinFileInTest(PKG_PREFIX + "/extension_property_lib", "B"));
@Test
public void smokeTest() throws Exception {
Path libJar = extLibJarMap.getForConfiguration(kotlinParameters);
Path output =
kotlinc(parameters.getRuntime().asCf(), kotlinParameters)
.addClasspathFiles(libJar)
.addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/extension_property_app", "main"))
.setOutputPath(temp.newFolder().toPath())
.compile();
testForJvm(parameters)
.addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar)
.addClasspath(output)
.run(parameters.getRuntime(), PKG + ".extension_property_app.MainKt")
.assertSuccessWithOutput(EXPECTED);
}
@Test
public void testMetadataInExtensionProperty_merged_compat() throws Exception {
testMetadataInExtensionProperty_merged(false);
}
@Test
public void testMetadataInExtensionProperty_merged_full() throws Exception {
testMetadataInExtensionProperty_merged(true);
}
public void testMetadataInExtensionProperty_merged(boolean full) throws Exception {
Path libJar =
(full ? testForR8(parameters.getBackend()) : testForR8Compat(parameters.getBackend()))
.addClasspathFiles(kotlinc.getKotlinStdlibJar(), kotlinc.getKotlinAnnotationJar())
.addProgramFiles(extLibJarMap.getForConfiguration(kotlinParameters))
// Keep the B class and its interface (which has the doStuff method).
.addKeepRules("-keep class **.B")
.addKeepRules("-keep class **.I { <methods>; }")
// Keep the BKt extension property which requires metadata
// to be called with Kotlin syntax from other kotlin code.
.addKeepRules("-keep class **.BKt { <methods>; }")
.addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
.compile()
.inspect(inspector -> inspectMerged(inspector, full))
.writeToZip();
Path output =
kotlinc(parameters.getRuntime().asCf(), kotlinParameters)
.addClasspathFiles(libJar)
.addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/extension_property_app", "main"))
.setOutputPath(temp.newFolder().toPath())
.compile(full || kotlinParameters.isOlderThan(KOTLINC_1_4_20));
if (full || kotlinParameters.isOlderThan(KOTLINC_1_4_20)) {
return;
}
testForJvm(parameters)
.addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar)
.addClasspath(output)
.run(parameters.getRuntime(), PKG + ".extension_property_app.MainKt")
.assertSuccessWithOutput(EXPECTED);
}
private void inspectMerged(CodeInspector inspector, boolean full) {
String superClassName = PKG + ".extension_property_lib.Super";
String bClassName = PKG + ".extension_property_lib.B";
String bKtClassName = PKG + ".extension_property_lib.BKt";
assertThat(inspector.clazz(superClassName), Matchers.notIf(isPresent(), full));
ClassSubject impl = inspector.clazz(bClassName);
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("Super")));
KmFunctionSubject kmFunction = kmClass.kmFunctionWithUniqueName("doStuff");
assertThat(kmFunction, not(isPresent()));
assertThat(kmFunction, not(isExtensionFunction()));
ClassSubject bKt = inspector.clazz(bKtClassName);
assertThat(bKt, isPresentAndNotRenamed());
// API entry is kept, hence the presence of Metadata.
KmPackageSubject kmPackage = bKt.getKmPackage();
assertThat(kmPackage, isPresent());
KmPropertySubject kmProperty = kmPackage.kmPropertyExtensionWithUniqueName("asI");
assertThat(kmProperty, isExtensionProperty());
assertNotNull(kmProperty.getterSignature());
}
@Test
public void testMetadataInExtensionProperty_renamed() throws Exception {
Path libJar =
testForR8(parameters.getBackend())
.addClasspathFiles(kotlinc.getKotlinStdlibJar(), kotlinc.getKotlinAnnotationJar())
.addProgramFiles(extLibJarMap.getForConfiguration(kotlinParameters))
// Keep the B class and its interface (which has the doStuff method).
.addKeepRules("-keep class **.B")
.addKeepRules("-keep class **.I { <methods>; }")
// Keep Super, but allow minification.
.addKeepRules("-keep,allowobfuscation class **.Super")
// Keep the BKt extension property which requires metadata
// to be called with Kotlin syntax from other kotlin code.
.addKeepRules("-keep class **.BKt { <methods>; }")
.addKeepAttributes(ProguardKeepAttributes.RUNTIME_VISIBLE_ANNOTATIONS)
.compile()
.inspect(this::inspectRenamed)
.writeToZip();
Path output =
kotlinc(parameters.getRuntime().asCf(), kotlinParameters)
.addClasspathFiles(libJar)
.addSourceFiles(getKotlinFileInTest(PKG_PREFIX + "/extension_property_app", "main"))
.setOutputPath(temp.newFolder().toPath())
.compile(kotlinParameters.isOlderThan(KOTLINC_1_4_20));
if (kotlinParameters.isOlderThan(KOTLINC_1_4_20)) {
return;
}
testForJvm(parameters)
.addRunClasspathFiles(kotlinc.getKotlinStdlibJar(), libJar)
.addClasspath(output)
.run(parameters.getRuntime(), PKG + ".extension_property_app.MainKt")
.assertSuccessWithOutput(EXPECTED);
}
private void inspectRenamed(CodeInspector inspector) {
String superClassName = PKG + ".extension_property_lib.Super";
String bClassName = PKG + ".extension_property_lib.B";
String bKtClassName = PKG + ".extension_property_lib.BKt";
ClassSubject sup = inspector.clazz(superClassName);
assertThat(sup, isPresentAndRenamed());
ClassSubject impl = inspector.clazz(bClassName);
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("Super")));
assertTrue(superTypes.stream().anyMatch(
supertype -> supertype.getFinalDescriptor().equals(sup.getFinalDescriptor())));
ClassSubject bKt = inspector.clazz(bKtClassName);
assertThat(bKt, isPresentAndNotRenamed());
// API entry is kept, hence the presence of Metadata.
KmPackageSubject kmPackage = bKt.getKmPackage();
assertThat(kmPackage, isPresent());
KmPropertySubject kmProperty = kmPackage.kmPropertyExtensionWithUniqueName("asI");
assertThat(kmProperty, isExtensionProperty());
assertNotNull(kmProperty.getterSignature());
}
}