Kapt: Convert valid references to qualified names (KT-26304)

This commit is contained in:
Yan Zhulanow
2018-11-09 16:49:40 +09:00
parent 6a8a8b794d
commit 47657df8da
4 changed files with 107 additions and 0 deletions
@@ -973,6 +973,15 @@ class ClassFileToSourceStubConverter(val kaptContext: KaptContextForStubGenerati
}
fun tryParseTypeExpression(expression: KtExpression?): JCExpression? {
if (expression is KtReferenceExpression) {
val descriptor = kaptContext.bindingContext[BindingContext.REFERENCE_TARGET, expression]
if (descriptor is ClassDescriptor) {
return treeMaker.FqName(descriptor.fqNameSafe)
} else if (descriptor is TypeAliasDescriptor) {
descriptor.classDescriptor?.fqNameSafe?.let { return treeMaker.FqName(it) }
}
}
return when (expression) {
is KtSimpleNameExpression -> treeMaker.SimpleName(expression.getReferencedName())
is KtDotQualifiedExpression -> {
@@ -44,6 +44,11 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("plugins/kapt3/kapt3-compiler/testData/converter"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("annotationWithFqNames.kt")
public void testAnnotationWithFqNames() throws Exception {
runTest("plugins/kapt3/kapt3-compiler/testData/converter/annotationWithFqNames.kt");
}
@TestMetadata("annotations.kt")
public void testAnnotations() throws Exception {
runTest("plugins/kapt3/kapt3-compiler/testData/converter/annotations.kt");
@@ -0,0 +1,32 @@
// CORRECT_ERROR_TYPES
// NO_VALIDATION
//FILE: lib/Anno.java
package lib;
public @interface Anno {
Class<?>[] impls() default {};
}
//FILE: lib/impl/Impl.java
package lib.impl;
public class Impl {}
// FILE: test.kt
@file:Suppress("UNRESOLVED_REFERENCE", "ANNOTATION_ARGUMENT_MUST_BE_CONST", "NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION")
package test
import lib.Anno
import lib.impl.Impl
typealias Joo = Impl
@Anno(impls = [Impl::class])
class Foo
@Anno(impls = [Impl::class, ABC::class])
class Bar
@Anno(impls = [Joo::class])
class Boo
@@ -0,0 +1,61 @@
package lib;
public @interface Anno {
Class<?>[] impls() default {};
}
////////////////////
package lib.impl;
public class Impl {
}
////////////////////
package test;
import lib.Anno;
import lib.impl.Impl;
@kotlin.Metadata()
@lib.Anno(impls = {lib.impl.Impl.class, ABC.class})
public final class Bar {
public Bar() {
super();
}
}
////////////////////
package test;
import lib.Anno;
import lib.impl.Impl;
@kotlin.Metadata()
@lib.Anno(impls = {lib.impl.Impl.class})
public final class Boo {
public Boo() {
super();
}
}
////////////////////
package test;
import lib.Anno;
import lib.impl.Impl;
@kotlin.Metadata()
@lib.Anno(impls = {lib.impl.Impl.class})
public final class Foo {
public Foo() {
super();
}
}