Annotation parameters now cannot be mutable #KT-12367 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-05-20 14:07:48 +03:00
parent 81bdd6b4bb
commit cffdce908e
7 changed files with 15 additions and 2 deletions
@@ -155,6 +155,7 @@ public interface Errors {
DiagnosticFactory0<KtSuperTypeList> SUPERTYPES_FOR_ANNOTATION_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtParameter> MISSING_VAL_ON_ANNOTATION_PARAMETER = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtParameter> VAR_ANNOTATION_PARAMETER = DiagnosticFactory0.create(ERROR, VAL_OR_VAR_NODE);
DiagnosticFactory0<KtCallExpression> ANNOTATION_CLASS_CONSTRUCTOR_CALL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtAnnotationEntry, DeclarationDescriptor> NOT_AN_ANNOTATION_CLASS = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> ANNOTATION_CLASS_WITH_BODY = DiagnosticFactory0.create(ERROR);
@@ -679,6 +679,7 @@ public class DefaultErrorMessages {
MAP.put(SUPERTYPES_FOR_ANNOTATION_CLASS, "Annotation class cannot have supertypes");
MAP.put(MISSING_VAL_ON_ANNOTATION_PARAMETER, "'val' keyword is missing on annotation parameter");
MAP.put(VAR_ANNOTATION_PARAMETER, "An annotation parameter cannot be 'var'");
MAP.put(ANNOTATION_CLASS_CONSTRUCTOR_CALL, "Annotation class cannot be instantiated");
MAP.put(NOT_AN_ANNOTATION_CLASS, "''{0}'' is not an annotation class", NAME);
MAP.put(ANNOTATION_CLASS_WITH_BODY, "Body is not allowed for annotation class");
@@ -368,6 +368,9 @@ class DeclarationsChecker(
if (!parameter.hasValOrVar()) {
trace.report(MISSING_VAL_ON_ANNOTATION_PARAMETER.on(parameter))
}
else if (parameter.isMutable) {
trace.report(VAR_ANNOTATION_PARAMETER.on(parameter))
}
}
}
@@ -22,7 +22,7 @@ annotation class Annotation1() <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation7(val name: String) <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation8(var name: String = "") <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation8(<!VAR_ANNOTATION_PARAMETER!>var<!> name: String = "") <!ANNOTATION_CLASS_WITH_BODY!>{}<!>
annotation class Annotation9(val name: String)
@@ -1,5 +1,5 @@
annotation class Ann(
val a: Int,
var b: Int,
<!VAR_ANNOTATION_PARAMETER!>var<!> b: Int,
<!MISSING_VAL_ON_ANNOTATION_PARAMETER!>c: String<!>
)
@@ -74,5 +74,12 @@ class ChangeVariableMutabilityFix(element: KtValVarKeywordOwner, private val mak
}
}
}
val VAR_ANNOTATION_PARAMETER_FACTORY: KotlinSingleIntentionActionFactory = object: KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = diagnostic.psiElement as KtParameter
return ChangeVariableMutabilityFix(element, false)
}
}
}
}
@@ -171,6 +171,7 @@ class QuickFixRegistrar : QuickFixContributor {
VAL_REASSIGNMENT.registerFactory(ChangeVariableMutabilityFix.VAL_REASSIGNMENT_FACTORY)
CAPTURED_VAL_INITIALIZATION.registerFactory(ChangeVariableMutabilityFix.CAPTURED_VAL_INITIALIZATION_FACTORY)
VAR_OVERRIDDEN_BY_VAL.registerFactory(ChangeVariableMutabilityFix.VAR_OVERRIDDEN_BY_VAL_FACTORY)
VAR_ANNOTATION_PARAMETER.registerFactory(ChangeVariableMutabilityFix.VAR_ANNOTATION_PARAMETER_FACTORY)
VAL_OR_VAR_ON_FUN_PARAMETER.registerFactory(RemoveValVarFromParameterFix)
VAL_OR_VAR_ON_LOOP_PARAMETER.registerFactory(RemoveValVarFromParameterFix)