Make "Add annotation target" available on use-site annotation
So #KT-22861 Fixed So #KT-22862 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
442a89bc6c
commit
963e430b42
@@ -229,7 +229,7 @@ class AnnotationChecker(
|
|||||||
private fun DeclarationDescriptor?.hasBackingField(bindingTrace: BindingTrace) =
|
private fun DeclarationDescriptor?.hasBackingField(bindingTrace: BindingTrace) =
|
||||||
(this as? PropertyDescriptor)?.let { bindingTrace.get(BindingContext.BACKING_FIELD_REQUIRED, it) } ?: false
|
(this as? PropertyDescriptor)?.let { bindingTrace.get(BindingContext.BACKING_FIELD_REQUIRED, it) } ?: false
|
||||||
|
|
||||||
private fun getActualTargetList(annotated: KtElement, descriptor: DeclarationDescriptor?, trace: BindingTrace): TargetList {
|
fun getActualTargetList(annotated: KtElement, descriptor: DeclarationDescriptor?, trace: BindingTrace): TargetList {
|
||||||
return when (annotated) {
|
return when (annotated) {
|
||||||
is KtClassOrObject ->
|
is KtClassOrObject ->
|
||||||
(descriptor as? ClassDescriptor)?.let { TargetList(KotlinTarget.classActualTargets(it)) } ?: TargetLists.T_CLASSIFIER
|
(descriptor as? ClassDescriptor)?.let { TargetList(KotlinTarget.classActualTargets(it)) } ?: TargetLists.T_CLASSIFIER
|
||||||
@@ -385,7 +385,7 @@ class AnnotationChecker(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class TargetList(
|
class TargetList(
|
||||||
val defaultTargets: List<KotlinTarget>,
|
val defaultTargets: List<KotlinTarget>,
|
||||||
val canBeSubstituted: List<KotlinTarget> = emptyList(),
|
val canBeSubstituted: List<KotlinTarget> = emptyList(),
|
||||||
val onlyWithUseSiteTarget: List<KotlinTarget> = emptyList()
|
val onlyWithUseSiteTarget: List<KotlinTarget> = emptyList()
|
||||||
|
|||||||
@@ -21,9 +21,11 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.psi.search.GlobalSearchScope
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import com.intellij.psi.search.searches.ReferencesSearch
|
import com.intellij.psi.search.searches.ReferencesSearch
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
|
import org.jetbrains.kotlin.config.LanguageFeature
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||||
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
|
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
@@ -64,7 +66,8 @@ class AddAnnotationTargetFix(annotationEntry: KtAnnotationEntry) : KotlinQuickFi
|
|||||||
|
|
||||||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtAnnotationEntry>? {
|
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtAnnotationEntry>? {
|
||||||
val entry = diagnostic.psiElement as? KtAnnotationEntry ?: return null
|
val entry = diagnostic.psiElement as? KtAnnotationEntry ?: return null
|
||||||
if (entry.toAnnotationClass() == null) return null
|
val annotationClass = entry.toAnnotationClass() ?: return null
|
||||||
|
if (entry.useSiteTarget != null && entry.getRequiredAnnotationTargets(annotationClass, entry.project).isEmpty()) return null
|
||||||
|
|
||||||
return AddAnnotationTargetFix(entry)
|
return AddAnnotationTargetFix(entry)
|
||||||
}
|
}
|
||||||
@@ -81,14 +84,29 @@ private fun KtAnnotationEntry.getRequiredAnnotationTargets(annotationClass: KtCl
|
|||||||
}.flatten().toSet()
|
}.flatten().toSet()
|
||||||
|
|
||||||
val annotationTargetValueNames = AnnotationTarget.values().map { it.name }
|
val annotationTargetValueNames = AnnotationTarget.values().map { it.name }
|
||||||
return (requiredTargets + otherReferenceRequiredTargets).filter { it.name in annotationTargetValueNames }
|
return (requiredTargets + otherReferenceRequiredTargets).distinct().filter { it.name in annotationTargetValueNames }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtAnnotationEntry.getActualTargetList(): List<KotlinTarget> {
|
private fun KtAnnotationEntry.getActualTargetList(): List<KotlinTarget> {
|
||||||
val annotatedElement = getStrictParentOfType<KtModifierList>()?.owner as? KtElement
|
val annotatedElement = getStrictParentOfType<KtModifierList>()?.owner as? KtElement
|
||||||
?: getStrictParentOfType<KtAnnotatedExpression>()?.baseExpression
|
?: getStrictParentOfType<KtAnnotatedExpression>()?.baseExpression
|
||||||
|
?: getStrictParentOfType<KtFile>()
|
||||||
?: return emptyList()
|
?: return emptyList()
|
||||||
return AnnotationChecker.getDeclarationSiteActualTargetList(annotatedElement, null, BindingTraceContext())
|
|
||||||
|
val targetList = AnnotationChecker.getActualTargetList(annotatedElement, null, BindingTraceContext())
|
||||||
|
|
||||||
|
if (useSiteTarget == null) {
|
||||||
|
return targetList.defaultTargets
|
||||||
|
}
|
||||||
|
val target = KotlinTarget.USE_SITE_MAPPING[useSiteTarget?.getAnnotationUseSiteTarget()] ?: return emptyList()
|
||||||
|
if (target !in with(targetList) { defaultTargets + canBeSubstituted + onlyWithUseSiteTarget }) return emptyList()
|
||||||
|
return if (target == KotlinTarget.RECEIVER &&
|
||||||
|
!languageVersionSettings.supportsFeature(LanguageFeature.RestrictionOfWrongAnnotationsWithUseSiteTargetsOnTypes)
|
||||||
|
) {
|
||||||
|
listOf(KotlinTarget.VALUE_PARAMETER)
|
||||||
|
} else {
|
||||||
|
listOf(target)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtClass.addAnnotationTargets(annotationTargets: List<KotlinTarget>, psiFactory: KtPsiFactory) {
|
private fun KtClass.addAnnotationTargets(annotationTargets: List<KotlinTarget>, psiFactory: KtPsiFactory) {
|
||||||
|
|||||||
@@ -507,7 +507,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
|||||||
RETURN_NOT_ALLOWED.registerFactory(ChangeToLabeledReturnFix)
|
RETURN_NOT_ALLOWED.registerFactory(ChangeToLabeledReturnFix)
|
||||||
|
|
||||||
WRONG_ANNOTATION_TARGET.registerFactory(AddAnnotationTargetFix)
|
WRONG_ANNOTATION_TARGET.registerFactory(AddAnnotationTargetFix)
|
||||||
WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET.registerFactory(MoveReceiverAnnotationFix)
|
WRONG_ANNOTATION_TARGET_WITH_USE_SITE_TARGET.registerFactory(MoveReceiverAnnotationFix, AddAnnotationTargetFix)
|
||||||
|
|
||||||
NO_CONSTRUCTOR.registerFactory(RemoveNoConstructorFix)
|
NO_CONSTRUCTOR.registerFactory(RemoveNoConstructorFix)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
<caret>@Ann
|
||||||
|
package test
|
||||||
|
|
||||||
|
@Target
|
||||||
|
annotation class Ann
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Ann
|
||||||
|
package test
|
||||||
|
|
||||||
|
@Target(AnnotationTarget.FILE)
|
||||||
|
annotation class Ann
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
@Target
|
||||||
|
annotation class DelegateAnn
|
||||||
|
|
||||||
|
<caret>@delegate:DelegateAnn
|
||||||
|
val foo by lazy { "" }
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
// WITH_RUNTIME
|
||||||
|
@Target(AnnotationTarget.FIELD)
|
||||||
|
annotation class DelegateAnn
|
||||||
|
|
||||||
|
@delegate:DelegateAnn
|
||||||
|
val foo by lazy { "" }
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target
|
||||||
|
annotation class FieldAnn
|
||||||
|
|
||||||
|
class Field(<caret>@field:FieldAnn val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target(AnnotationTarget.FIELD)
|
||||||
|
annotation class FieldAnn
|
||||||
|
|
||||||
|
class Field(@field:FieldAnn val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
<caret>@file:FileAnn
|
||||||
|
|
||||||
|
@Target
|
||||||
|
annotation class FileAnn
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@file:FileAnn
|
||||||
|
|
||||||
|
@Target(AnnotationTarget.FILE)
|
||||||
|
annotation class FileAnn
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target
|
||||||
|
annotation class GetAnn
|
||||||
|
|
||||||
|
class Get(<caret>@get:GetAnn val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target(AnnotationTarget.PROPERTY_GETTER)
|
||||||
|
annotation class GetAnn
|
||||||
|
|
||||||
|
class Get(@get:GetAnn val foo: String)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Add annotation target" "false"
|
||||||
|
// ACTION: Create test
|
||||||
|
// ACTION: Make internal
|
||||||
|
// ACTION: Make private
|
||||||
|
// ERROR: This annotation is not applicable to target 'class' and use site target '@get'
|
||||||
|
annotation class Ann
|
||||||
|
|
||||||
|
<caret>@get:Ann
|
||||||
|
class Test(val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target
|
||||||
|
annotation class ParamAnn
|
||||||
|
|
||||||
|
class Param(<caret>@param:ParamAnn val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||||
|
annotation class ParamAnn
|
||||||
|
|
||||||
|
class Param(@param:ParamAnn val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target
|
||||||
|
annotation class PropertyAnn
|
||||||
|
|
||||||
|
class Property(<caret>@property:PropertyAnn val foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target(AnnotationTarget.PROPERTY)
|
||||||
|
annotation class PropertyAnn
|
||||||
|
|
||||||
|
class Property(@property:PropertyAnn val foo: String)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
class Foo
|
||||||
|
|
||||||
|
@Target
|
||||||
|
annotation class ReceiverAnn
|
||||||
|
|
||||||
|
fun <caret>@receiver:ReceiverAnn Foo.test() {}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
class Foo
|
||||||
|
|
||||||
|
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||||
|
annotation class ReceiverAnn
|
||||||
|
|
||||||
|
fun @receiver:ReceiverAnn Foo.test() {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target
|
||||||
|
annotation class SetAnn
|
||||||
|
|
||||||
|
class Set(<caret>@set:SetAnn var foo: String)
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target(AnnotationTarget.PROPERTY_SETTER)
|
||||||
|
annotation class SetAnn
|
||||||
|
|
||||||
|
class Set(@set:SetAnn var foo: String)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target
|
||||||
|
annotation class SetParamAnn
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
<caret>@setparam:SetParamAnn
|
||||||
|
var foo = 1
|
||||||
|
set(value) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// "Add annotation target" "true"
|
||||||
|
@Target(AnnotationTarget.VALUE_PARAMETER)
|
||||||
|
annotation class SetParamAnn
|
||||||
|
|
||||||
|
class Bar {
|
||||||
|
@setparam:SetParamAnn
|
||||||
|
var foo = 1
|
||||||
|
set(value) {}
|
||||||
|
}
|
||||||
@@ -264,6 +264,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("file.kt")
|
||||||
|
public void testFile() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/file.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("fromLib.kt")
|
@TestMetadata("fromLib.kt")
|
||||||
public void testFromLib() throws Exception {
|
public void testFromLib() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/fromLib.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/fromLib.kt");
|
||||||
@@ -287,6 +293,66 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/noBackingField.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/noBackingField.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_delegate.kt")
|
||||||
|
public void testUse_site_delegate() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_delegate.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_field.kt")
|
||||||
|
public void testUse_site_field() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_field.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_file.kt")
|
||||||
|
public void testUse_site_file() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_file.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_get.kt")
|
||||||
|
public void testUse_site_get() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_get.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_invalid.kt")
|
||||||
|
public void testUse_site_invalid() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_invalid.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_param.kt")
|
||||||
|
public void testUse_site_param() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_param.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_property.kt")
|
||||||
|
public void testUse_site_property() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_property.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_receiver.kt")
|
||||||
|
public void testUse_site_receiver() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_receiver.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_set.kt")
|
||||||
|
public void testUse_site_set() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_set.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("use-site_setparam.kt")
|
||||||
|
public void testUse_site_setparam() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/addAnnotationTarget/use-site_setparam.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/addCrossinline")
|
@TestMetadata("idea/testData/quickfix/addCrossinline")
|
||||||
|
|||||||
Reference in New Issue
Block a user