Allow annotation constructor calls

in default parameter value expressions of anonymous classes
(if it is not a compile-time constant, it will be reported separately).

 #KT-10136 Fixed
This commit is contained in:
Dmitry Petrov
2015-11-25 13:43:01 +03:00
parent 73e3a252d3
commit db42941586
6 changed files with 82 additions and 6 deletions
@@ -284,14 +284,25 @@ public class CallExpressionResolver {
//noinspection unchecked
PsiElement parent = PsiTreeUtil.getParentOfType(expression, KtValueArgument.class, KtParameter.class);
if (parent instanceof KtValueArgument) {
return PsiTreeUtil.getParentOfType(parent, KtAnnotationEntry.class) != null;
if (PsiTreeUtil.getParentOfType(parent, KtAnnotationEntry.class) != null) {
return true;
}
parent = PsiTreeUtil.getParentOfType(parent, KtParameter.class);
if (parent != null) {
return isUnderAnnotationClassDeclaration(trace, parent);
}
}
else if (parent instanceof KtParameter) {
KtClass ktClass = PsiTreeUtil.getParentOfType(parent, KtClass.class);
if (ktClass != null) {
DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, ktClass);
return DescriptorUtils.isAnnotationClass(descriptor);
}
return isUnderAnnotationClassDeclaration(trace, parent);
}
return false;
}
private static boolean isUnderAnnotationClassDeclaration(@NotNull BindingTrace trace, PsiElement parent) {
KtClass ktClass = PsiTreeUtil.getParentOfType(parent, KtClass.class);
if (ktClass != null) {
DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, ktClass);
return DescriptorUtils.isAnnotationClass(descriptor);
}
return false;
}
@@ -0,0 +1,19 @@
annotation class A
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class B(val items: Array<A> = arrayOf(A()))
@B
class C
fun box(): String {
val bClass = B::class.java
val cClass = C::class.java
val items = cClass.getAnnotation(bClass).items
assert(items.size == 1) { "Expected: [A()], got ${items.asList()}" }
assert(items[0] is A) { "Expected: [A()], got ${items.asList()}" }
return "OK"
}
@@ -0,0 +1,8 @@
annotation class A
annotation class A1(val x: Int)
annotation class B(
val a: A = A(),
val x: Int = <!ANNOTATION_PARAMETER_DEFAULT_VALUE_MUST_BE_CONSTANT!>A1(42).x<!>,
val aa: Array<A> = arrayOf(A())
)
@@ -0,0 +1,26 @@
package
public final annotation class A : kotlin.Annotation {
public constructor A()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class A1 : kotlin.Annotation {
public constructor A1(/*0*/ x: kotlin.Int)
public final val x: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final annotation class B : kotlin.Annotation {
public constructor B(/*0*/ a: A = ..., /*1*/ x: kotlin.Int = ..., /*2*/ aa: kotlin.Array<A> = ...)
public final val a: A
public final val aa: kotlin.Array<A>
public final val x: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -207,6 +207,12 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("kt10136.kt")
public void testKt10136() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/kt10136.kt");
doTest(fileName);
}
@TestMetadata("orderWithValue.kt")
public void testOrderWithValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/orderWithValue.kt");
@@ -100,6 +100,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
doTestWithStdlib(fileName);
}
@TestMetadata("kt10136.kt")
public void testKt10136() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/annotations/kt10136.kt");
doTestWithStdlib(fileName);
}
@TestMetadata("nestedClassPropertyAsParameter.kt")
public void testNestedClassPropertyAsParameter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/annotations/nestedClassPropertyAsParameter.kt");