Change map key type in AnnotationDescriptor.getAllValueArguments
Turns out, only the parameter's name is needed at all usages of this method. Such a map is both easier to use (no need to call ValueParameterDescriptor.getName) and easier to construct (no need to resolve annotation class, its constructor, its parameters). In this commit, only usages have changed but the implementations are still using the old logic, this is going to be refactored in subsequent commits
This commit is contained in:
+1
-1
@@ -92,7 +92,7 @@ class AnnotationTypeQualifierResolverImpl(storageManager: StorageManager) : Anno
|
||||
.annotations.findAnnotation(TYPE_QUALIFIER_DEFAULT_FQNAME)!!
|
||||
.allValueArguments
|
||||
.flatMap { (parameter, argument) ->
|
||||
if (parameter.name == JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME)
|
||||
if (parameter == JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME)
|
||||
argument.mapConstantToQualifierApplicabilityTypes()
|
||||
else
|
||||
emptyList()
|
||||
|
||||
+4
-4
@@ -109,14 +109,14 @@ open class JavaAnnotationDescriptor(
|
||||
|
||||
protected val firstArgument: JavaAnnotationArgument? = annotation?.arguments?.firstOrNull()
|
||||
|
||||
override val allValueArguments: Map<ValueParameterDescriptor, ConstantValue<*>> get() = emptyMap()
|
||||
override val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>> get() = emptyMap()
|
||||
}
|
||||
|
||||
class JavaDeprecatedAnnotationDescriptor(
|
||||
annotation: JavaAnnotation?,
|
||||
c: LazyJavaResolverContext
|
||||
): JavaAnnotationDescriptor(c, annotation, c.module.builtIns.deprecatedAnnotation) {
|
||||
override val allValueArguments: Map<ValueParameterDescriptor, ConstantValue<*>> by c.storageManager.createLazyValue {
|
||||
override val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>> by c.storageManager.createLazyValue {
|
||||
val parameterDescriptor = valueParameters.firstOrNull {
|
||||
it.name == JavaAnnotationMapper.DEPRECATED_ANNOTATION_MESSAGE
|
||||
}
|
||||
@@ -130,7 +130,7 @@ class JavaTargetAnnotationDescriptor(
|
||||
annotation: JavaAnnotation,
|
||||
c: LazyJavaResolverContext
|
||||
): JavaAnnotationDescriptor(c, annotation, c.module.builtIns.targetAnnotation) {
|
||||
override val allValueArguments by c.storageManager.createLazyValue {
|
||||
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue {
|
||||
val targetArgument = when (firstArgument) {
|
||||
is JavaArrayAnnotationArgument -> JavaAnnotationTargetMapper.mapJavaTargetArguments(firstArgument.getElements(), c.module.builtIns)
|
||||
is JavaEnumValueAnnotationArgument -> JavaAnnotationTargetMapper.mapJavaTargetArguments(listOf(firstArgument), c.module.builtIns)
|
||||
@@ -144,7 +144,7 @@ class JavaRetentionAnnotationDescriptor(
|
||||
annotation: JavaAnnotation,
|
||||
c: LazyJavaResolverContext
|
||||
): JavaAnnotationDescriptor(c, annotation, c.module.builtIns.retentionAnnotation) {
|
||||
override val allValueArguments by c.storageManager.createLazyValue {
|
||||
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue {
|
||||
val retentionArgument = JavaAnnotationTargetMapper.mapJavaRetentionArgument(firstArgument, c.module.builtIns)
|
||||
retentionArgument?.let { mapOf(valueParameters.single() to it) }.orEmpty()
|
||||
}
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ class LazyJavaAnnotationDescriptor(
|
||||
|
||||
private val factory = ConstantValueFactory(c.module.builtIns)
|
||||
|
||||
override val allValueArguments by c.storageManager.createLazyValue {
|
||||
override val valueArgumentsByParameterDescriptor by c.storageManager.createLazyValue {
|
||||
val constructors = annotationClass!!.constructors
|
||||
if (constructors.isEmpty()) return@createLazyValue emptyMap<ValueParameterDescriptor, ConstantValue<*>>()
|
||||
|
||||
|
||||
+1
-1
@@ -209,7 +209,7 @@ private class EnhancedTypeAnnotations(private val fqNameToMatch: FqName) : Annot
|
||||
private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor {
|
||||
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 allValueArguments: Map<ValueParameterDescriptor, ConstantValue<*>> get() = throwError()
|
||||
override val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>> get() = throwError()
|
||||
override val source: SourceElement get() = throwError()
|
||||
override fun toString() = "[EnhancedType]"
|
||||
}
|
||||
|
||||
+6
-1
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.SourceElement
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
@@ -31,7 +32,11 @@ interface AnnotationDescriptor {
|
||||
val fqName: FqName? get() =
|
||||
(type.constructor.declarationDescriptor as? ClassDescriptor)?.fqNameUnsafe?.takeIf(FqNameUnsafe::isSafe)?.toSafe()
|
||||
|
||||
val allValueArguments: Map<ValueParameterDescriptor, ConstantValue<*>>
|
||||
@Deprecated("Use allValueArguments instead")
|
||||
val valueArgumentsByParameterDescriptor: Map<ValueParameterDescriptor, ConstantValue<*>>
|
||||
|
||||
val allValueArguments: Map<Name, ConstantValue<*>>
|
||||
get() = valueArgumentsByParameterDescriptor.mapKeys { (parameter) -> parameter.name }
|
||||
|
||||
val source: SourceElement
|
||||
}
|
||||
|
||||
+8
-1
@@ -21,6 +21,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.descriptors.SourceElement;
|
||||
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
|
||||
import org.jetbrains.kotlin.name.FqName;
|
||||
import org.jetbrains.kotlin.name.Name;
|
||||
import org.jetbrains.kotlin.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue;
|
||||
import org.jetbrains.kotlin.types.KotlinType;
|
||||
@@ -57,10 +58,16 @@ public class AnnotationDescriptorImpl implements AnnotationDescriptor {
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public Map<ValueParameterDescriptor, ConstantValue<?>> getAllValueArguments() {
|
||||
public Map<ValueParameterDescriptor, ConstantValue<?>> getValueArgumentsByParameterDescriptor() {
|
||||
return valueArguments;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Map<Name, ConstantValue<?>> getAllValueArguments() {
|
||||
return AnnotationDescriptor.DefaultImpls.getAllValueArguments(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public SourceElement getSource() {
|
||||
|
||||
@@ -429,17 +429,14 @@ internal class DescriptorRendererImpl(
|
||||
private fun renderAndSortAnnotationArguments(descriptor: AnnotationDescriptor): List<String> {
|
||||
val allValueArguments = descriptor.allValueArguments
|
||||
val classDescriptor = if (renderDefaultAnnotationArguments) TypeUtils.getClassDescriptor(descriptor.type) else null
|
||||
val parameterDescriptorsWithDefaultValue = classDescriptor?.unsubstitutedPrimaryConstructor?.valueParameters?.filter {
|
||||
it.declaresDefaultValue()
|
||||
} ?: emptyList()
|
||||
val defaultList = parameterDescriptorsWithDefaultValue.filter { !allValueArguments.containsKey(it) }.map {
|
||||
"${it.name.asString()} = ..."
|
||||
}
|
||||
val parameterDescriptorsWithDefaultValue = classDescriptor?.unsubstitutedPrimaryConstructor?.valueParameters
|
||||
?.filter { it.declaresDefaultValue() }
|
||||
?.map { it.name }
|
||||
.orEmpty()
|
||||
val defaultList = parameterDescriptorsWithDefaultValue.filter { it !in allValueArguments }.map { "${it.asString()} = ..." }
|
||||
val argumentList = allValueArguments.entries
|
||||
.map { entry ->
|
||||
val name = entry.key.name.asString()
|
||||
val value = if (!parameterDescriptorsWithDefaultValue.contains(entry.key)) renderConstant(entry.value) else "..."
|
||||
"$name = $value"
|
||||
.map { (name, value) ->
|
||||
"${name.asString()} = ${if (name !in parameterDescriptorsWithDefaultValue) renderConstant(value) else "..."}"
|
||||
}
|
||||
return (defaultList + argumentList).sorted()
|
||||
}
|
||||
|
||||
@@ -522,10 +522,10 @@ public class DescriptorUtils {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getJvmName(@Nullable AnnotationDescriptor jvmNameAnnotation) {
|
||||
private static String getJvmName(@Nullable AnnotationDescriptor jvmNameAnnotation) {
|
||||
if (jvmNameAnnotation == null) return null;
|
||||
|
||||
Map<ValueParameterDescriptor, ConstantValue<?>> arguments = jvmNameAnnotation.getAllValueArguments();
|
||||
Map<Name, ConstantValue<?>> arguments = jvmNameAnnotation.getAllValueArguments();
|
||||
if (arguments.isEmpty()) return null;
|
||||
|
||||
ConstantValue<?> name = arguments.values().iterator().next();
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.incremental.components.LookupLocation
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.FqNameUnsafe
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils.getContainingClass
|
||||
import org.jetbrains.kotlin.resolve.constants.EnumValue
|
||||
@@ -213,10 +214,10 @@ fun Annotated.isDocumentedAnnotation(): Boolean =
|
||||
annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.mustBeDocumented) != null
|
||||
|
||||
fun Annotated.getAnnotationRetention(): KotlinRetention? {
|
||||
val annotationEntryDescriptor = annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.retention) ?: return null
|
||||
val retentionArgumentValue = annotationEntryDescriptor.allValueArguments.entries.firstOrNull {
|
||||
it.key.name.asString() == "value"
|
||||
}?.value as? EnumValue ?: return null
|
||||
val retentionArgumentValue = annotations.findAnnotation(KotlinBuiltIns.FQ_NAMES.retention)
|
||||
?.allValueArguments
|
||||
?.get(Name.identifier("value"))
|
||||
as? EnumValue ?: return null
|
||||
return KotlinRetention.valueOf(retentionArgumentValue.value.name.asString())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user