Introduce KotlinSyntheticClass.Kind.LOCAL_TRAIT_IMPL

This commit is contained in:
Alexander Udalov
2015-02-13 21:44:48 +03:00
parent ce26ce0458
commit e812c4e67f
7 changed files with 31 additions and 29 deletions
@@ -54,10 +54,8 @@ public class TraitImplBodyCodegen extends ClassBodyCodegen {
@Override
protected void generateKotlinAnnotation() {
// We write LOCAL_CLASS to local trait-impl, because we don't want PSI classes to be constructed for such files
// (currently PSI for synthetic class is built only if this class is a trait-impl, see DecompiledUtils.kt)
writeKotlinSyntheticClassAnnotation(v, DescriptorUtils.isTopLevelOrInnerClass(descriptor)
? KotlinSyntheticClass.Kind.TRAIT_IMPL
: KotlinSyntheticClass.Kind.LOCAL_CLASS);
: KotlinSyntheticClass.Kind.LOCAL_TRAIT_IMPL);
}
}
@@ -45,92 +45,90 @@ public class KotlinSyntheticClassAnnotationTest extends CodegenTestCase {
public void testPackagePart() {
doTest("fun foo() = 42",
"$",
PACKAGE_PART, false);
PACKAGE_PART);
}
public void testTraitImpl() {
doTest("trait A { fun foo() = 42 }",
JvmAbi.TRAIT_IMPL_SUFFIX,
TRAIT_IMPL, true);
TRAIT_IMPL);
}
public void testSamWrapper() {
doTest("val f = {}\nval foo = Thread(f)",
"$sam",
SAM_WRAPPER, false);
SAM_WRAPPER);
}
public void testSamLambda() {
doTest("val foo = Thread { }",
"$1",
SAM_LAMBDA, true);
SAM_LAMBDA);
}
public void testCallableReferenceWrapper() {
doTest("val f = String::get",
"$1",
CALLABLE_REFERENCE_WRAPPER, true);
CALLABLE_REFERENCE_WRAPPER);
}
public void testLocalFunction() {
doTest("fun foo() { fun bar() {} }",
"$1",
LOCAL_FUNCTION, true);
LOCAL_FUNCTION);
}
public void testAnonymousFunction() {
doTest("val f = {}",
"$1",
ANONYMOUS_FUNCTION, true);
ANONYMOUS_FUNCTION);
}
public void testLocalClass() {
doTest("fun foo() { class Local }",
"Local",
LOCAL_CLASS, true);
LOCAL_CLASS);
}
public void testLocalTraitImpl() {
doTest("fun foo() { trait Local { fun bar() = 42 } }",
"Local$$TImpl",
LOCAL_CLASS, true);
"Local$$TImpl.class",
LOCAL_TRAIT_IMPL);
}
public void testLocalTraitInterface() {
doTest("fun foo() { trait Local { fun bar() = 42 } }",
"Local",
LOCAL_CLASS, true);
"Local.class",
LOCAL_CLASS);
}
public void testInnerClassOfLocalClass() {
doTest("fun foo() { class Local { inner class Inner } }",
"Inner",
LOCAL_CLASS, true);
LOCAL_CLASS);
}
public void testAnonymousObject() {
doTest("val o = object {}",
"$1",
ANONYMOUS_OBJECT, true);
ANONYMOUS_OBJECT);
}
private void doTest(
@NotNull String code,
@NotNull final String classNamePart,
@NotNull KotlinSyntheticClass.Kind expectedKind,
final boolean endWithNotContains
@NotNull final String classFilePart,
@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) {
String path = file.getRelativePath();
return endWithNotContains ? path.endsWith(classNamePart + ".class") : path.contains(classNamePart);
return file.getRelativePath().contains(classFilePart);
}
});
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);
assertFalse("No files with \"" + classFilePart + "\" in the name are found: " + output, files.isEmpty());
assertTrue("Exactly one file with \"" + classFilePart + "\" 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('/', '.');
@@ -59,6 +59,7 @@ public final class JvmAnnotationNames {
public enum Kind {
PACKAGE_PART,
TRAIT_IMPL,
LOCAL_TRAIT_IMPL,
SAM_WRAPPER,
SAM_LAMBDA,
CALLABLE_REFERENCE_WRAPPER,
@@ -26,10 +26,10 @@ public @interface KotlinSyntheticClass {
Kind kind();
// Inner classes of local classes have kind LOCAL_CLASS
// Local trait-impl also has kind LOCAL_CLASS
public static enum Kind {
PACKAGE_PART,
TRAIT_IMPL,
LOCAL_TRAIT_IMPL,
SAM_WRAPPER,
SAM_LAMBDA,
CALLABLE_REFERENCE_WRAPPER,
@@ -29,7 +29,9 @@ public fun isKotlinCompiledFile(file: VirtualFile): Boolean {
}
val header = KotlinBinaryClassCache.getKotlinBinaryClass(file)?.getClassHeader()
return header != null && header.syntheticClassKind != KotlinSyntheticClass.Kind.TRAIT_IMPL
return header != null &&
header.syntheticClassKind != KotlinSyntheticClass.Kind.TRAIT_IMPL &&
header.syntheticClassKind != KotlinSyntheticClass.Kind.LOCAL_TRAIT_IMPL
}
public fun isKotlinWithCompatibleAbiVersion(file: VirtualFile): Boolean {
@@ -23,6 +23,9 @@ fun f() {
class MyLocalClass {
}
trait MyLocalTrait {
}
val myAnonymousObject = object {
}
@@ -37,7 +37,7 @@ public abstract class AbstractInternalCompiledClassesTest : JetLightCodeInsightF
}
protected fun doTestTraitImplClassIsVisibleAsJavaClass() {
val project = getProject()!!
val project = getProject()
doTest("trait impl", isSyntheticClassOfKind(TRAIT_IMPL)) {
val psiFile = PsiManager.getInstance(project).findFile(this)!!
Assert.assertTrue("Should not be kotlin file",
@@ -50,7 +50,7 @@ public abstract class AbstractInternalCompiledClassesTest : JetLightCodeInsightF
decompiledPsiFile is PsiJavaFile)
val classes = (decompiledPsiFile as PsiJavaFile).getClasses()
Assert.assertTrue("Should have some decompiled text",
classes.size == 1 && classes[0].getName()!!.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX))
classes.size() == 1 && classes[0].getName()!!.endsWith(JvmAbi.TRAIT_IMPL_SUFFIX))
}
}
@@ -58,7 +58,7 @@ public abstract class AbstractInternalCompiledClassesTest : JetLightCodeInsightF
doTestNoPsiFilesAreBuiltFor(kind.toString(), isSyntheticClassOfKind(kind))
protected fun doTestNoPsiFilesAreBuiltFor(fileKind: String, acceptFile: VirtualFile.() -> Boolean) {
val project = getProject()!!
val project = getProject()
doTest(fileKind, acceptFile) {
val psiFile = PsiManager.getInstance(project).findFile(this)
Assert.assertNull("PSI files for $fileKind classes should not be build, is was build for: ${this.getPresentableName()}",