Skip to content

Commit

Permalink
Filter default arguments by name, not by index
Browse files Browse the repository at this point in the history
KSAnnotation.defaultArguments doesn't have arguments without defaults.
Therefore it can be shorter than KSAnnotation.arguments. Their indexes
may not match.
  • Loading branch information
ting-yuan committed Oct 3, 2024
1 parent e19bc86 commit 10657b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o
)
}
val presentValueArgumentNames = presentArgs.map { it.name?.asString() ?: "" }
presentArgs + defaultArguments.filterIndexed { idx, ksValueArgument ->
ksValueArgument.name?.asString() !in presentValueArgumentNames &&
annotationConstructor?.valueParameters?.get(idx)?.hasDefaultValue == true
presentArgs + defaultArguments.filter { ksValueArgument ->
val name = ksValueArgument.name?.asString() ?: return@filter false
if (name in presentValueArgumentNames)
return@filter false
annotationConstructor?.valueParameters?.any { it.name.asString() == name && it.hasDefaultValue } == true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ class KSAnnotationResolvedImpl private constructor(
val annotationClass = annotationApplication.classId?.toKtClassSymbol()
val annotationConstructor = annotationClass?.memberScope?.constructors?.singleOrNull()
val params = annotationConstructor?.valueParameters
defaultArguments.filterIndexed { idx, arg ->
arg.name?.asString() !in presentNames && params?.get(idx)?.hasDefaultValue == true
defaultArguments.filter { arg ->
val name = arg.name?.asString() ?: return@filter false
if (name in presentNames)
return@filter false
params?.any { it.name.asString() == name && it.hasDefaultValue } == true
}
}
presentArgs + absentArgs
Expand Down

0 comments on commit 10657b6

Please sign in to comment.