Refine loading annotation parameters from java

- Parameter named `value` is always first
- Array parameter represented as vararg iff its name is `value` and all
  other parameters have default values

 #KT-2576 Fixed
 #KT-6641 Fixed
 #KT-6220 Fixed
 #KT-6652 Fixed
This commit is contained in:
Denis Zharkov
2015-04-06 15:41:57 +03:00
parent 84ed0e7255
commit 9b1443954f
15 changed files with 260 additions and 29 deletions
@@ -0,0 +1,9 @@
// FILE: A.java
public @interface A {
double b();
Class<?> x1();
int a();
String value();
Class<?> x();
Class<?> x2();
}
@@ -0,0 +1,14 @@
package
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ value: kotlin.String, /*1*/ b: kotlin.Double, /*2*/ x1: java.lang.Class<*>, /*3*/ a: kotlin.Int, /*4*/ x: java.lang.Class<*>, /*5*/ x2: java.lang.Class<*>)
public abstract fun a(): kotlin.Int
public abstract fun b(): kotlin.Double
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 abstract fun value(): kotlin.String
public abstract fun x(): java.lang.Class<*>
public abstract fun x1(): java.lang.Class<*>
public abstract fun x2(): java.lang.Class<*>
}
@@ -0,0 +1,8 @@
// FILE: A.java
public @interface A {
double b();
Class<?> x1();
int a();
Class<?> x();
Class<?> x2();
}
@@ -0,0 +1,13 @@
package
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ b: kotlin.Double, /*1*/ x1: java.lang.Class<*>, /*2*/ a: kotlin.Int, /*3*/ x: java.lang.Class<*>, /*4*/ x2: java.lang.Class<*>)
public abstract fun a(): kotlin.Int
public abstract fun b(): kotlin.Double
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 abstract fun x(): java.lang.Class<*>
public abstract fun x1(): java.lang.Class<*>
public abstract fun x2(): java.lang.Class<*>
}
@@ -0,0 +1,23 @@
// FILE: A.java
public @interface A {
String[] value();
Class<?> x() default Integer.class;
int y();
}
// FILE: b.kt
[A("1", "2", "3", y = 1)] fun test1() {}
[A("4", y = 2)] fun test2() {}
[A(*array("5", "6"), "7", y = 3)] fun test3() {}
[A("1", "2", "3", x = javaClass<String>(), y = 4)] fun test4() {}
[A("4", y = 5)] fun test5() {}
[A(*array("5", "6"), "7", x = javaClass<Any>(), y = 6)] fun test6() {}
[A(y = 7)] fun test7() {}
[A("8", "9", "10"<!NO_VALUE_FOR_PARAMETER!>)<!>] fun test8() {}
@@ -0,0 +1,20 @@
package
A(value = {"1", "2", "3"}: kotlin.Array<out kotlin.String>, y = IntegerValueType(1): IntegerValueType(1)) internal fun test1(): kotlin.Unit
A(value = {"4"}: kotlin.Array<out kotlin.String>, y = IntegerValueType(2): IntegerValueType(2)) internal fun test2(): kotlin.Unit
A(value = {{"5", "6"}, "7"}: kotlin.Array<out kotlin.String>, y = IntegerValueType(3): IntegerValueType(3)) internal fun test3(): kotlin.Unit
A(value = {"1", "2", "3"}: kotlin.Array<out kotlin.String>, x = kotlin.String.class: java.lang.Class<kotlin.String>, y = IntegerValueType(4): IntegerValueType(4)) internal fun test4(): kotlin.Unit
A(value = {"4"}: kotlin.Array<out kotlin.String>, y = IntegerValueType(5): IntegerValueType(5)) internal fun test5(): kotlin.Unit
A(value = {{"5", "6"}, "7"}: kotlin.Array<out kotlin.String>, x = kotlin.Any.class: java.lang.Class<kotlin.Any>, y = IntegerValueType(6): IntegerValueType(6)) internal fun test6(): kotlin.Unit
A(value = {}: kotlin.Array<out kotlin.String>, y = IntegerValueType(7): IntegerValueType(7)) internal fun test7(): kotlin.Unit
A(value = {"8", "9", "10"}: kotlin.Array<out kotlin.String>) internal fun test8(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ vararg value: kotlin.String /*kotlin.Array<out kotlin.String>*/, /*1*/ x: java.lang.Class<*> = ..., /*2*/ y: 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 abstract fun value(): kotlin.Array<kotlin.String>
public abstract fun x(): java.lang.Class<*>
public abstract fun y(): kotlin.Int
}
@@ -0,0 +1,27 @@
// FILE: A.java
public @interface A {
String[] value();
Class<?> x() default Integer.class;
int y() default 1;
}
// FILE: b.kt
[A("1", "2", "3")] fun test1() {}
[A("4")] fun test2() {}
[A(*array("5", "6"), "7")] fun test3() {}
[A("1", "2", "3", x = javaClass<String>())] fun test4() {}
[A("4", y = 2)] fun test5() {}
[A(*array("5", "6"), "7", x = javaClass<Any>(), y = 3)] fun test6() {}
[A()] fun test7() {}
[A] fun test8() {}
[A(x = javaClass<Any>(), <!MIXING_NAMED_AND_POSITIONED_ARGUMENTS!>*array("5", "6")<!>, <!MIXING_NAMED_AND_POSITIONED_ARGUMENTS!>"7"<!>, y = 3)] fun test9() {}
[A(x = javaClass<Any>(), value = *array("5", "6"), <!MIXING_NAMED_AND_POSITIONED_ARGUMENTS!>"7"<!>, y = 3)] fun test10() {}
[A(x = javaClass<Any>(), value = *array("5", "6", "7"), y = 3)] fun test11() {}
@@ -0,0 +1,23 @@
package
A(value = {"1", "2", "3"}: kotlin.Array<out kotlin.String>) internal fun test1(): kotlin.Unit
A(value = {"5", "6"}: kotlin.Array<kotlin.String>, x = kotlin.Any.class: java.lang.Class<kotlin.Any>, y = IntegerValueType(3): IntegerValueType(3)) internal fun test10(): kotlin.Unit
A(value = {"5", "6", "7"}: kotlin.Array<kotlin.String>, x = kotlin.Any.class: java.lang.Class<kotlin.Any>, y = IntegerValueType(3): IntegerValueType(3)) internal fun test11(): kotlin.Unit
A(value = {"4"}: kotlin.Array<out kotlin.String>) internal fun test2(): kotlin.Unit
A(value = {{"5", "6"}, "7"}: kotlin.Array<out kotlin.String>) internal fun test3(): kotlin.Unit
A(value = {"1", "2", "3"}: kotlin.Array<out kotlin.String>, x = kotlin.String.class: java.lang.Class<kotlin.String>) internal fun test4(): kotlin.Unit
A(value = {"4"}: kotlin.Array<out kotlin.String>, y = IntegerValueType(2): IntegerValueType(2)) internal fun test5(): kotlin.Unit
A(value = {{"5", "6"}, "7"}: kotlin.Array<out kotlin.String>, x = kotlin.Any.class: java.lang.Class<kotlin.Any>, y = IntegerValueType(3): IntegerValueType(3)) internal fun test6(): kotlin.Unit
A(value = {}: kotlin.Array<out kotlin.String>) internal fun test7(): kotlin.Unit
A(value = {}: kotlin.Array<out kotlin.String>) internal fun test8(): kotlin.Unit
A(value = {}: kotlin.Array<out kotlin.String>, x = kotlin.Any.class: java.lang.Class<kotlin.Any>, y = IntegerValueType(3): IntegerValueType(3)) internal fun test9(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ vararg value: kotlin.String /*kotlin.Array<out kotlin.String>*/, /*1*/ x: java.lang.Class<*> = ..., /*2*/ y: 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 abstract fun value(): kotlin.Array<kotlin.String>
public abstract fun x(): java.lang.Class<*>
public abstract fun y(): kotlin.Int
}
@@ -0,0 +1,15 @@
// FILE: A.java
public @interface A {
String[] value();
}
// FILE: b.kt
[A("1", "2", "3")] fun test1() {}
[A("4")] fun test2() {}
[A(*array("5", "6"), "7")] fun test3() {}
[A()] fun test4() {}
[A] fun test5() {}
@@ -0,0 +1,15 @@
package
A(value = {"1", "2", "3"}: kotlin.Array<out kotlin.String>) internal fun test1(): kotlin.Unit
A(value = {"4"}: kotlin.Array<out kotlin.String>) internal fun test2(): kotlin.Unit
A(value = {{"5", "6"}, "7"}: kotlin.Array<out kotlin.String>) internal fun test3(): kotlin.Unit
A(value = {}: kotlin.Array<out kotlin.String>) internal fun test4(): kotlin.Unit
A(value = {}: kotlin.Array<out kotlin.String>) internal fun test5(): kotlin.Unit
public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ vararg value: kotlin.String /*kotlin.Array<out kotlin.String>*/)
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 abstract fun value(): kotlin.Array<kotlin.String>
}
@@ -1,6 +1,6 @@
// FILE: A.java // FILE: A.java
public @interface A { public @interface A {
String[] arg(); String[] value();
} }
// FILE: b.kt // FILE: b.kt
@@ -1,11 +1,11 @@
package package
A(arg = {IntegerValueType(1), "b"}: kotlin.Array<???>) internal fun test(): kotlin.Unit A(value = {IntegerValueType(1), "b"}: kotlin.Array<???>) internal fun test(): kotlin.Unit
public final annotation class A : kotlin.Annotation { public final annotation class A : kotlin.Annotation {
public constructor A(/*0*/ vararg arg: kotlin.String /*kotlin.Array<out kotlin.String>*/) public constructor A(/*0*/ vararg value: kotlin.String /*kotlin.Array<out kotlin.String>*/)
public abstract fun arg(): kotlin.Array<kotlin.String>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean 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 hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public abstract fun value(): kotlin.Array<kotlin.String>
} }
@@ -3,10 +3,10 @@ package test
public /*synthesized*/ fun NullInAnnotation(/*0*/ function: () -> kotlin.Unit): test.NullInAnnotation public /*synthesized*/ fun NullInAnnotation(/*0*/ function: () -> kotlin.Unit): test.NullInAnnotation
public trait NullInAnnotation { public trait NullInAnnotation {
test.NullInAnnotation.Ann(a = null: kotlin.Nothing?, b = {null}: kotlin.Array<out kotlin.String>) public abstract fun foo(): kotlin.Unit test.NullInAnnotation.Ann(a = null: kotlin.Nothing?, b = {null}: kotlin.Array<kotlin.String>) public abstract fun foo(): kotlin.Unit
public final annotation class Ann : kotlin.Annotation { public final annotation class Ann : kotlin.Annotation {
public constructor Ann(/*0*/ a: kotlin.String, /*1*/ vararg b: kotlin.String /*kotlin.Array<out kotlin.String>*/) public constructor Ann(/*0*/ a: kotlin.String, /*1*/ b: kotlin.Array<kotlin.String>)
public abstract fun a(): kotlin.String public abstract fun a(): kotlin.String
public abstract fun b(): kotlin.Array<kotlin.String> public abstract fun b(): kotlin.Array<kotlin.String>
} }
@@ -54,6 +54,7 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
@InnerTestClasses({ @InnerTestClasses({
Annotations.AnnotationApplicability.class, Annotations.AnnotationApplicability.class,
Annotations.AnnotationParameterMustBeConstant.class, Annotations.AnnotationParameterMustBeConstant.class,
Annotations.AnnotationParameters.class,
Annotations.AnnotationWithVarargParameter.class, Annotations.AnnotationWithVarargParameter.class,
Annotations.JvmOverloads.class, Annotations.JvmOverloads.class,
Annotations.PlatformStatic.class, Annotations.PlatformStatic.class,
@@ -130,6 +131,45 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic
} }
} }
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AnnotationParameters extends AbstractJetDiagnosticsTestWithStdLib {
public void testAllFilesPresentInAnnotationParameters() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("orderWithValue.kt")
public void testOrderWithValue() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/orderWithValue.kt");
doTest(fileName);
}
@TestMetadata("orderWithoutValue.kt")
public void testOrderWithoutValue() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/orderWithoutValue.kt");
doTest(fileName);
}
@TestMetadata("valueArray.kt")
public void testValueArray() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArray.kt");
doTest(fileName);
}
@TestMetadata("valueArrayAndOtherDefault.kt")
public void testValueArrayAndOtherDefault() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArrayAndOtherDefault.kt");
doTest(fileName);
}
@TestMetadata("valueArrayOnly.kt")
public void testValueArrayOnly() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationParameters/valueArrayOnly.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter") @TestMetadata("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationWithVarargParameter")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.load.java.lazy.resolveAnnotations import org.jetbrains.kotlin.load.java.lazy.resolveAnnotations
import org.jetbrains.kotlin.load.java.JavaVisibilities import org.jetbrains.kotlin.load.java.JavaVisibilities
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor
import org.jetbrains.kotlin.load.java.components.DescriptorResolverUtils import org.jetbrains.kotlin.load.java.components.DescriptorResolverUtils
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
@@ -167,38 +168,61 @@ public class LazyJavaClassMemberScope(
val methods = jClass.getMethods() val methods = jClass.getMethods()
val result = ArrayList<ValueParameterDescriptor>(methods.size()) val result = ArrayList<ValueParameterDescriptor>(methods.size())
for ((index, method) in methods.withIndices()) { val attr = TypeUsage.MEMBER_SIGNATURE_INVARIANT.toAttributes(allowFlexible = false)
assert(method.getValueParameters().isEmpty(), "Annotation method can't have parameters: " + method)
val jReturnType = method.getReturnType() ?: throw AssertionError("Annotation method has no return type: " + method) val (methodsNamedValue, otherMethods) = methods.
partition { it.getName() == JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME }
val attr = TypeUsage.MEMBER_SIGNATURE_INVARIANT.toAttributes(allowFlexible = false) assert(methodsNamedValue.size() <= 1, "There can't be to methods named 'value' in annotation class: " + jClass)
val methodNamedValue = methodsNamedValue.firstOrNull()
val (returnType, varargElementType) = if (methodNamedValue != null) {
if (index == methods.size() - 1 && jReturnType is JavaArrayType) val parameterNamedValueJavaType = methodNamedValue.getAnnotationMethodReturnJavaType()
Pair(c.typeResolver.transformArrayType(jReturnType, attr, isVararg = true), val (parameterType, varargType) =
c.typeResolver.transformJavaType(jReturnType.getComponentType(), attr)) if (parameterNamedValueJavaType is JavaArrayType)
Pair(c.typeResolver.transformArrayType(parameterNamedValueJavaType, attr, isVararg = true),
c.typeResolver.transformJavaType(parameterNamedValueJavaType.getComponentType(), attr))
else else
Pair(c.typeResolver.transformJavaType(jReturnType, attr), null) Pair(c.typeResolver.transformJavaType(parameterNamedValueJavaType, attr), null)
result.add(ValueParameterDescriptorImpl( result.addAnnotationValueParameter(constructor, 0, methodNamedValue, parameterType, varargType)
constructor, }
null,
index, val startIndex = if (methodNamedValue != null) 1 else 0
Annotations.EMPTY, for ((index, method) in otherMethods.withIndex()) {
method.getName(), val parameterType = c.typeResolver.transformJavaType(method.getAnnotationMethodReturnJavaType(), attr)
// Parameters of annotation constructors in Java are never nullable result.addAnnotationValueParameter(constructor, index + startIndex, method, parameterType, null)
TypeUtils.makeNotNullable(returnType),
method.hasAnnotationParameterDefaultValue(),
// Nulls are not allowed in annotation arguments in Java
varargElementType?.let { TypeUtils.makeNotNullable(it) },
c.sourceElementFactory.source(method)
))
} }
return result return result
} }
private fun JavaMethod.getAnnotationMethodReturnJavaType(): JavaType {
assert(getValueParameters().isEmpty(), "Annotation method can't have parameters: " + this)
return getReturnType() ?: throw AssertionError("Annotation method has no return type: " + this)
}
private fun MutableList<ValueParameterDescriptor>.addAnnotationValueParameter(
constructor: ConstructorDescriptor,
index: Int,
method: JavaMethod,
returnType: JetType,
varargElementType: JetType?
) {
add(ValueParameterDescriptorImpl(
constructor,
null,
index,
Annotations.EMPTY,
method.getName(),
// Parameters of annotation constructors in Java are never nullable
TypeUtils.makeNotNullable(returnType),
method.hasAnnotationParameterDefaultValue(),
// Nulls are not allowed in annotation arguments in Java
varargElementType?.let { TypeUtils.makeNotNullable(it) },
c.sourceElementFactory.source(method)
))
}
private val nestedClassIndex = c.storageManager.createLazyValue { private val nestedClassIndex = c.storageManager.createLazyValue {
jClass.getInnerClasses().valuesToMap { c -> c.getName() } jClass.getInnerClasses().valuesToMap { c -> c.getName() }
} }