Write KotlinSyntheticClass to generated classes for functions
That is SAM wrappers, callable reference wrappers, local functions and anonymous functions
This commit is contained in:
@@ -16,20 +16,22 @@
|
||||
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Collections2;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.ConfigurationKind;
|
||||
import org.jetbrains.jet.OutputFile;
|
||||
import org.jetbrains.jet.OutputFileCollection;
|
||||
import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmAbi;
|
||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.ABI_VERSION_FIELD_NAME;
|
||||
import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass;
|
||||
import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass.Kind.*;
|
||||
|
||||
public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
|
||||
public static final FqName PACKAGE_NAME = new FqName("test");
|
||||
@@ -40,36 +42,69 @@ public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
public void testAnnotationIsWrittenOnPackagePart() throws Exception {
|
||||
loadText("package " + PACKAGE_NAME + "\n\nfun foo() = 42\n");
|
||||
String facadeFileName = JvmClassName.byFqNameWithoutInnerClasses(PackageClassUtils.getPackageClassFqName(PACKAGE_NAME)).getInternalName() + ".class";
|
||||
|
||||
OutputFileCollection outputFiles = generateClassesInFile();
|
||||
for (OutputFile outputFile : outputFiles.asList()) {
|
||||
// The file which is not a facade is a package part
|
||||
String filePath = outputFile.getRelativePath();
|
||||
if (filePath.equals(facadeFileName)) continue;
|
||||
|
||||
String fqName = filePath.substring(0, filePath.length() - ".class".length()).replace('/', '.');
|
||||
Class<?> aClass = generateClass(fqName);
|
||||
|
||||
assertAnnotatedWithKind(aClass, "PACKAGE_PART");
|
||||
return;
|
||||
}
|
||||
|
||||
fail("No package part was found: " + outputFiles.asList());
|
||||
public void testPackagePart() {
|
||||
doTest("fun foo() = 42",
|
||||
"-",
|
||||
PACKAGE_PART);
|
||||
}
|
||||
|
||||
public void testAnnotationIsWrittenOnTraitImpl() throws Exception {
|
||||
loadText("package " + PACKAGE_NAME + "\n\ntrait A { fun foo() = 42 }\n");
|
||||
|
||||
Class<?> aClass = generateClass(PACKAGE_NAME + ".A" + JvmAbi.TRAIT_IMPL_SUFFIX);
|
||||
assertNotNull("TImpl is not generated", aClass);
|
||||
assertAnnotatedWithKind(aClass, "TRAIT_IMPL");
|
||||
public void testTraitImpl() {
|
||||
doTest("trait A { fun foo() = 42 }",
|
||||
JvmAbi.TRAIT_IMPL_SUFFIX,
|
||||
TRAIT_IMPL);
|
||||
}
|
||||
|
||||
private void assertAnnotatedWithKind(@NotNull Class<?> aClass, @NotNull String expectedKind) {
|
||||
Class<? extends Annotation> annotationClass = loadAnnotationClassQuietly(KotlinSyntheticClass.FQ_NAME.asString());
|
||||
public void testSamWrapper() {
|
||||
doTest("val f = {}\nval foo = Thread(f)",
|
||||
"$sam",
|
||||
SAM_WRAPPER);
|
||||
}
|
||||
|
||||
public void testSamLambda() {
|
||||
doTest("val foo = Thread { }",
|
||||
"$",
|
||||
SAM_LAMBDA);
|
||||
}
|
||||
|
||||
public void testCallableReferenceWrapper() {
|
||||
doTest("val f = String::get",
|
||||
"$",
|
||||
CALLABLE_REFERENCE_WRAPPER);
|
||||
}
|
||||
|
||||
public void testLocalFunction() {
|
||||
doTest("fun foo() { fun bar() {} }",
|
||||
"$",
|
||||
LOCAL_FUNCTION);
|
||||
}
|
||||
|
||||
public void testAnonymousFunction() {
|
||||
doTest("val f = {}",
|
||||
"$",
|
||||
ANONYMOUS_FUNCTION);
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String code, @NotNull final String classNamePart, @NotNull KotlinSyntheticClass.Kind expectedKind) {
|
||||
loadText("package " + PACKAGE_NAME + "\n\n" + code);
|
||||
List<OutputFile> output = generateClassesInFile().asList();
|
||||
Collection<OutputFile> files = Collections2.filter(output, new Predicate<OutputFile>() {
|
||||
@Override
|
||||
public boolean apply(OutputFile file) {
|
||||
return file.getRelativePath().contains(classNamePart);
|
||||
}
|
||||
});
|
||||
assertFalse("No files with \"" + classNamePart + "\" in the name are found: " + output, files.isEmpty());
|
||||
assertTrue("Exactly one file with \"" + classNamePart + "\" in the name should be found: " + files, files.size() == 1);
|
||||
|
||||
String path = files.iterator().next().getRelativePath();
|
||||
String fqName = path.substring(0, path.length() - ".class".length()).replace('/', '.');
|
||||
Class<?> aClass = generateClass(fqName);
|
||||
assertAnnotatedWithKind(aClass, expectedKind);
|
||||
}
|
||||
|
||||
private void assertAnnotatedWithKind(@NotNull Class<?> aClass, @NotNull KotlinSyntheticClass.Kind expectedKind) {
|
||||
Class<? extends Annotation> annotationClass = loadAnnotationClassQuietly(
|
||||
KotlinSyntheticClass.CLASS_NAME.getFqNameForClassNameWithoutDollars().asString());
|
||||
assertTrue("No KotlinSyntheticClass annotation found", aClass.isAnnotationPresent(annotationClass));
|
||||
|
||||
Annotation annotation = aClass.getAnnotation(annotationClass);
|
||||
@@ -80,6 +115,6 @@ public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
|
||||
|
||||
Object actualKind = CodegenTestUtil.getAnnotationAttribute(annotation, KotlinSyntheticClass.KIND_FIELD_NAME.asString());
|
||||
assertNotNull(actualKind);
|
||||
assertEquals("KotlinSyntheticClass annotation has the wrong kind", expectedKind, actualKind.toString());
|
||||
assertEquals("KotlinSyntheticClass annotation has the wrong kind", expectedKind.toString(), actualKind.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user