Do not use parameter descriptors in most annotation implementations

Except AnnotationDescriptorImpl, which is refactored in the subsequent
commit.

Note that we no longer check the presence of parameters with the
corresponding names in the annotation class in
LazyJavaAnnotationDescriptor, this is why test data changed
This commit is contained in:
Alexander Udalov
2017-07-04 16:27:06 +03:00
parent cc7ed2ba54
commit eb205f620c
9 changed files with 53 additions and 91 deletions
@@ -126,19 +126,18 @@ class LazyAnnotationDescriptor(
c.scope c.scope
} }
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue { override val allValueArguments by c.storageManager.createLazyValue {
val resolutionResults = c.annotationResolver.resolveAnnotationCall(annotationEntry, scope, c.trace) val resolutionResults = c.annotationResolver.resolveAnnotationCall(annotationEntry, scope, c.trace)
AnnotationResolverImpl.checkAnnotationType(annotationEntry, c.trace, resolutionResults) AnnotationResolverImpl.checkAnnotationType(annotationEntry, c.trace, resolutionResults)
if (!resolutionResults.isSingleResult) return@createLazyValue emptyMap<ValueParameterDescriptor, ConstantValue<*>>() if (!resolutionResults.isSingleResult) return@createLazyValue emptyMap<Name, ConstantValue<*>>()
@Suppress("UNCHECKED_CAST") resolutionResults.resultingCall.valueArguments.mapNotNull { (valueParameter, resolvedArgument) ->
resolutionResults.resultingCall.valueArguments if (resolvedArgument == null) null
.mapValues { val (valueParameter, resolvedArgument) = it else c.annotationResolver.getAnnotationArgumentValue(c.trace, valueParameter, resolvedArgument)?.let { value ->
if (resolvedArgument == null) null valueParameter.name to value
else c.annotationResolver.getAnnotationArgumentValue(c.trace, valueParameter, resolvedArgument) }
} }.toMap()
.filterValues { it != null } as Map<ValueParameterDescriptor, ConstantValue<*>>
} }
override fun forceResolveAllContents() { override fun forceResolveAllContents() {
@@ -5,7 +5,7 @@ public fun bar(): @test.Ann kotlin.String
public interface Ann { public interface Ann {
} }
@test.Ann public final class Test { @test.Ann(s = "class") public final class Test {
public constructor Test() public constructor Test()
@test.Ann public final fun foo(/*0*/ @test.Ann s: @test.Ann kotlin.String): @test.Ann kotlin.String @test.Ann(s = "function") public final fun foo(/*0*/ @test.Ann(s = "parameter") s: @test.Ann kotlin.String): @test.Ann kotlin.String
} }
@@ -2,10 +2,10 @@ package
public fun main(): kotlin.Unit public fun main(): kotlin.Unit
@missing.Ann /* annotation class not found */ public open class A { @missing.Ann(x = "") /* annotation class not found */ public open class A {
public constructor A() public constructor A()
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
@missing.Ann /* annotation class not found */ public open fun foo(): kotlin.String! @missing.Ann(value = 1) /* annotation class not found */ public open fun foo(): kotlin.String!
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
} }
@@ -17,9 +17,7 @@
package org.jetbrains.kotlin.load.java.components package org.jetbrains.kotlin.load.java.components
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.SourceElement import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
@@ -52,20 +50,22 @@ object JavaAnnotationMapper {
internal val DEPRECATED_ANNOTATION_MESSAGE = Name.identifier("message") internal val DEPRECATED_ANNOTATION_MESSAGE = Name.identifier("message")
internal val TARGET_ANNOTATION_ALLOWED_TARGETS = Name.identifier("allowedTargets") internal val TARGET_ANNOTATION_ALLOWED_TARGETS = Name.identifier("allowedTargets")
internal val RETENTION_ANNOTATION_VALUE = Name.identifier("value")
fun mapOrResolveJavaAnnotation(annotation: JavaAnnotation, c: LazyJavaResolverContext): AnnotationDescriptor? = fun mapOrResolveJavaAnnotation(annotation: JavaAnnotation, c: LazyJavaResolverContext): AnnotationDescriptor? =
when (annotation.classId) { when (annotation.classId) {
ClassId.topLevel(JAVA_TARGET_FQ_NAME) -> JavaTargetAnnotationDescriptor(annotation, c) ClassId.topLevel(JAVA_TARGET_FQ_NAME) -> JavaTargetAnnotationDescriptor(annotation, c)
ClassId.topLevel(JAVA_RETENTION_FQ_NAME) -> JavaRetentionAnnotationDescriptor(annotation, c) ClassId.topLevel(JAVA_RETENTION_FQ_NAME) -> JavaRetentionAnnotationDescriptor(annotation, c)
ClassId.topLevel(JAVA_REPEATABLE_FQ_NAME) -> JavaAnnotationDescriptor(c, annotation, c.module.builtIns.repeatableAnnotation) ClassId.topLevel(JAVA_REPEATABLE_FQ_NAME) -> JavaAnnotationDescriptor(c, annotation, KotlinBuiltIns.FQ_NAMES.repeatable)
ClassId.topLevel(JAVA_DOCUMENTED_FQ_NAME) -> JavaAnnotationDescriptor(c, annotation, c.module.builtIns.mustBeDocumentedAnnotation) ClassId.topLevel(JAVA_DOCUMENTED_FQ_NAME) -> JavaAnnotationDescriptor(c, annotation, KotlinBuiltIns.FQ_NAMES.mustBeDocumented)
ClassId.topLevel(JAVA_DEPRECATED_FQ_NAME) -> null ClassId.topLevel(JAVA_DEPRECATED_FQ_NAME) -> null
else -> LazyJavaAnnotationDescriptor(c, annotation) else -> LazyJavaAnnotationDescriptor(c, annotation)
} }
fun findMappedJavaAnnotation(kotlinName: FqName, fun findMappedJavaAnnotation(
annotationOwner: JavaAnnotationOwner, kotlinName: FqName,
c: LazyJavaResolverContext annotationOwner: JavaAnnotationOwner,
c: LazyJavaResolverContext
): AnnotationDescriptor? { ): AnnotationDescriptor? {
if (kotlinName == KotlinBuiltIns.FQ_NAMES.deprecated) { if (kotlinName == KotlinBuiltIns.FQ_NAMES.deprecated) {
val javaAnnotation = annotationOwner.findAnnotation(JAVA_DEPRECATED_FQ_NAME) val javaAnnotation = annotationOwner.findAnnotation(JAVA_DEPRECATED_FQ_NAME)
@@ -98,55 +98,48 @@ object JavaAnnotationMapper {
open class JavaAnnotationDescriptor( open class JavaAnnotationDescriptor(
c: LazyJavaResolverContext, c: LazyJavaResolverContext,
annotation: JavaAnnotation?, annotation: JavaAnnotation?,
private val kotlinAnnotationClassDescriptor: ClassDescriptor override val fqName: FqName
): AnnotationDescriptor { ): AnnotationDescriptor {
override val source: SourceElement = annotation?.let { c.components.sourceElementFactory.source(it) } ?: SourceElement.NO_SOURCE override val source: SourceElement = annotation?.let { c.components.sourceElementFactory.source(it) } ?: SourceElement.NO_SOURCE
override val type: SimpleType get() = kotlinAnnotationClassDescriptor.defaultType override val type: SimpleType by c.storageManager.createLazyValue { c.module.builtIns.getBuiltInClassByFqName(fqName).defaultType }
protected val valueParameters: List<ValueParameterDescriptor>
get() = kotlinAnnotationClassDescriptor.constructors.single().valueParameters
protected val firstArgument: JavaAnnotationArgument? = annotation?.arguments?.firstOrNull() protected val firstArgument: JavaAnnotationArgument? = annotation?.arguments?.firstOrNull()
override val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>> get() = emptyMap() override val allValueArguments: Map<Name, ConstantValue<*>> get() = emptyMap()
} }
class JavaDeprecatedAnnotationDescriptor( class JavaDeprecatedAnnotationDescriptor(
annotation: JavaAnnotation?, annotation: JavaAnnotation?,
c: LazyJavaResolverContext c: LazyJavaResolverContext
): JavaAnnotationDescriptor(c, annotation, c.module.builtIns.deprecatedAnnotation) { ): JavaAnnotationDescriptor(c, annotation, KotlinBuiltIns.FQ_NAMES.deprecated) {
override val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>> by c.storageManager.createLazyValue { override val allValueArguments: Map<Name, ConstantValue<*>> by c.storageManager.createLazyValue {
val parameterDescriptor = valueParameters.firstOrNull { mapOf(JavaAnnotationMapper.DEPRECATED_ANNOTATION_MESSAGE to
it.name == JavaAnnotationMapper.DEPRECATED_ANNOTATION_MESSAGE ConstantValueFactory(c.module.builtIns).createStringValue("Deprecated in Java"))
}
parameterDescriptor?.let {
mapOf(it to ConstantValueFactory(c.module.builtIns).createStringValue("Deprecated in Java"))
}.orEmpty()
} }
} }
class JavaTargetAnnotationDescriptor( class JavaTargetAnnotationDescriptor(
annotation: JavaAnnotation, annotation: JavaAnnotation,
c: LazyJavaResolverContext c: LazyJavaResolverContext
): JavaAnnotationDescriptor(c, annotation, c.module.builtIns.targetAnnotation) { ): JavaAnnotationDescriptor(c, annotation, KotlinBuiltIns.FQ_NAMES.target) {
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue { override val allValueArguments by c.storageManager.createLazyValue {
val targetArgument = when (firstArgument) { val targetArgument = when (firstArgument) {
is JavaArrayAnnotationArgument -> JavaAnnotationTargetMapper.mapJavaTargetArguments(firstArgument.getElements(), c.module.builtIns) is JavaArrayAnnotationArgument -> JavaAnnotationTargetMapper.mapJavaTargetArguments(firstArgument.getElements(), c.module.builtIns)
is JavaEnumValueAnnotationArgument -> JavaAnnotationTargetMapper.mapJavaTargetArguments(listOf(firstArgument), c.module.builtIns) is JavaEnumValueAnnotationArgument -> JavaAnnotationTargetMapper.mapJavaTargetArguments(listOf(firstArgument), c.module.builtIns)
else -> return@createLazyValue emptyMap<ValueParameterDescriptor, ConstantValue<*>>() else -> null
} }
mapOf(valueParameters.single() to targetArgument) targetArgument?.let { mapOf(JavaAnnotationMapper.TARGET_ANNOTATION_ALLOWED_TARGETS to it) }.orEmpty()
} }
} }
class JavaRetentionAnnotationDescriptor( class JavaRetentionAnnotationDescriptor(
annotation: JavaAnnotation, annotation: JavaAnnotation,
c: LazyJavaResolverContext c: LazyJavaResolverContext
): JavaAnnotationDescriptor(c, annotation, c.module.builtIns.retentionAnnotation) { ): JavaAnnotationDescriptor(c, annotation, KotlinBuiltIns.FQ_NAMES.retention) {
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue { override val allValueArguments by c.storageManager.createLazyValue {
val retentionArgument = JavaAnnotationTargetMapper.mapJavaRetentionArgument(firstArgument, c.module.builtIns) val retentionArgument = JavaAnnotationTargetMapper.mapJavaRetentionArgument(firstArgument, c.module.builtIns)
retentionArgument?.let { mapOf(valueParameters.single() to it) }.orEmpty() retentionArgument?.let { mapOf(JavaAnnotationMapper.RETENTION_ANNOTATION_VALUE to it) }.orEmpty()
} }
} }
@@ -174,7 +167,8 @@ object JavaAnnotationTargetMapper {
.mapNotNull { builtIns.getAnnotationTargetEnumEntry(it) } .mapNotNull { builtIns.getAnnotationTargetEnumEntry(it) }
.map(::EnumValue) .map(::EnumValue)
val parameterDescriptor = DescriptorResolverUtils.getAnnotationParameterByName( val parameterDescriptor = DescriptorResolverUtils.getAnnotationParameterByName(
JavaAnnotationMapper.TARGET_ANNOTATION_ALLOWED_TARGETS, builtIns.targetAnnotation JavaAnnotationMapper.TARGET_ANNOTATION_ALLOWED_TARGETS,
builtIns.getBuiltInClassByFqName(KotlinBuiltIns.FQ_NAMES.target)
) )
return ArrayValue(kotlinTargets, parameterDescriptor?.type ?: ErrorUtils.createErrorType("Error: AnnotationTarget[]"), builtIns) return ArrayValue(kotlinTargets, parameterDescriptor?.type ?: ErrorUtils.createErrorType("Error: AnnotationTarget[]"), builtIns)
} }
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.load.java.lazy.descriptors package org.jetbrains.kotlin.load.java.lazy.descriptors
import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.findNonGenericClassAcrossDependencies import org.jetbrains.kotlin.descriptors.findNonGenericClassAcrossDependencies
@@ -39,7 +38,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass import org.jetbrains.kotlin.resolve.descriptorUtil.resolveTopLevelClass
import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.utils.keysToMapExceptNulls
class LazyJavaAnnotationDescriptor( class LazyJavaAnnotationDescriptor(
private val c: LazyJavaResolverContext, private val c: LazyJavaResolverContext,
@@ -61,20 +59,11 @@ class LazyJavaAnnotationDescriptor(
private val factory = ConstantValueFactory(c.module.builtIns) private val factory = ConstantValueFactory(c.module.builtIns)
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue { override val allValueArguments by c.storageManager.createLazyValue {
val constructors = annotationClass!!.constructors javaAnnotation.arguments.mapNotNull { arg ->
if (constructors.isEmpty()) return@createLazyValue emptyMap<ValueParameterDescriptor, ConstantValue<*>>() val name = arg.name ?: DEFAULT_ANNOTATION_MEMBER_NAME
resolveAnnotationArgument(arg)?.let { value -> name to value }
val nameToArg = javaAnnotation.arguments.associateBy { it.name } }.toMap()
constructors.first().valueParameters.keysToMapExceptNulls { valueParameter ->
var javaAnnotationArgument = nameToArg[valueParameter.name]
if (javaAnnotationArgument == null && valueParameter.name == DEFAULT_ANNOTATION_MEMBER_NAME) {
javaAnnotationArgument = nameToArg[null]
}
resolveAnnotationArgument(javaAnnotationArgument)
}
} }
private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): ConstantValue<*>? { private fun resolveAnnotationArgument(argument: JavaAnnotationArgument?): ConstantValue<*>? {
@@ -19,7 +19,6 @@ package org.jetbrains.kotlin.load.java.typeEnhancement
import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.SourceElement import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
@@ -31,6 +30,7 @@ import org.jetbrains.kotlin.load.java.typeEnhancement.MutabilityQualifier.READ_O
import org.jetbrains.kotlin.load.java.typeEnhancement.NullabilityQualifier.NOT_NULL import org.jetbrains.kotlin.load.java.typeEnhancement.NullabilityQualifier.NOT_NULL
import org.jetbrains.kotlin.load.java.typeEnhancement.NullabilityQualifier.NULLABLE import org.jetbrains.kotlin.load.java.typeEnhancement.NullabilityQualifier.NULLABLE
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.JavaToKotlinClassMap import org.jetbrains.kotlin.platform.JavaToKotlinClassMap
import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
@@ -209,7 +209,7 @@ private class EnhancedTypeAnnotations(private val fqNameToMatch: FqName) : Annot
private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor { private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor {
private fun throwError(): Nothing = error("No methods should be called on this descriptor. Only its presence matters") private fun throwError(): Nothing = error("No methods should be called on this descriptor. Only its presence matters")
override val type: KotlinType get() = throwError() override val type: KotlinType get() = throwError()
override val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>> get() = throwError() override val allValueArguments: Map<Name, ConstantValue<*>> get() = throwError()
override val source: SourceElement get() = throwError() override val source: SourceElement get() = throwError()
override fun toString() = "[EnhancedType]" override fun toString() = "[EnhancedType]"
} }
@@ -580,26 +580,6 @@ public abstract class KotlinBuiltIns {
return getEnumEntry(getBuiltInClassByName(FQ_NAMES.deprecationLevel.shortName()), level); return getEnumEntry(getBuiltInClassByName(FQ_NAMES.deprecationLevel.shortName()), level);
} }
@NotNull
public ClassDescriptor getTargetAnnotation() {
return getAnnotationClassByName(FQ_NAMES.target.shortName());
}
@NotNull
public ClassDescriptor getRetentionAnnotation() {
return getAnnotationClassByName(FQ_NAMES.retention.shortName());
}
@NotNull
public ClassDescriptor getRepeatableAnnotation() {
return getAnnotationClassByName(FQ_NAMES.repeatable.shortName());
}
@NotNull
public ClassDescriptor getMustBeDocumentedAnnotation() {
return getAnnotationClassByName(FQ_NAMES.mustBeDocumented.shortName());
}
@Nullable @Nullable
public ClassDescriptor getAnnotationTargetEnumEntry(@NotNull KotlinTarget target) { public ClassDescriptor getAnnotationTargetEnumEntry(@NotNull KotlinTarget target) {
return getEnumEntry(getAnnotationClassByName(FQ_NAMES.annotationTarget.shortName()), target.name()); return getEnumEntry(getAnnotationClassByName(FQ_NAMES.annotationTarget.shortName()), target.name());
@@ -32,11 +32,7 @@ interface AnnotationDescriptor {
val fqName: FqName? get() = val fqName: FqName? get() =
(type.constructor.declarationDescriptor as? ClassDescriptor)?.fqNameUnsafe?.takeIf(FqNameUnsafe::isSafe)?.toSafe() (type.constructor.declarationDescriptor as? ClassDescriptor)?.fqNameUnsafe?.takeIf(FqNameUnsafe::isSafe)?.toSafe()
@Deprecated("Use allValueArguments instead")
val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>>
val allValueArguments: Map<Name, ConstantValue<*>> val allValueArguments: Map<Name, ConstantValue<*>>
get() = valueArgumentsByParameterDescriptor.mapKeys { (parameter) -> parameter.name }
val source: SourceElement val source: SourceElement
} }
@@ -16,6 +16,8 @@
package org.jetbrains.kotlin.descriptors.annotations; package org.jetbrains.kotlin.descriptors.annotations;
import kotlin.collections.MapsKt;
import kotlin.jvm.functions.Function1;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.SourceElement; import org.jetbrains.kotlin.descriptors.SourceElement;
@@ -56,16 +58,18 @@ public class AnnotationDescriptorImpl implements AnnotationDescriptor {
return AnnotationDescriptor.DefaultImpls.getFqName(this); return AnnotationDescriptor.DefaultImpls.getFqName(this);
} }
@Override
@NotNull
public Map<ValueParameterDescriptor, ConstantValue<?>> getValueArgumentsByParameterDescriptor() {
return valueArguments;
}
@NotNull @NotNull
@Override @Override
public Map<Name, ConstantValue<?>> getAllValueArguments() { public Map<Name, ConstantValue<?>> getAllValueArguments() {
return AnnotationDescriptor.DefaultImpls.getAllValueArguments(this); return MapsKt.mapKeys(
valueArguments,
new Function1<Map.Entry<? extends ValueParameterDescriptor, ? extends ConstantValue<?>>, Name>() {
@Override
public Name invoke(Map.Entry<? extends ValueParameterDescriptor, ? extends ConstantValue<?>> entry) {
return entry.getKey().getName();
}
}
);
} }
@Override @Override