mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-86852 Add final modifier to Create Field implemented
This commit is contained in:
+21
-1
@@ -66,7 +66,7 @@ public class CreateFieldFromUsageFix extends CreateVarFromUsageFix {
|
||||
PsiClass parentClass;
|
||||
do {
|
||||
enclosingContext = PsiTreeUtil.getParentOfType(enclosingContext == null ? myReferenceExpression : enclosingContext, PsiMethod.class,
|
||||
PsiField.class, PsiClassInitializer.class);
|
||||
PsiField.class, PsiClassInitializer.class);
|
||||
parentClass = enclosingContext == null ? null : enclosingContext.getContainingClass();
|
||||
}
|
||||
while (parentClass instanceof PsiAnonymousClass);
|
||||
@@ -104,6 +104,10 @@ public class CreateFieldFromUsageFix extends CreateVarFromUsageFix {
|
||||
PsiUtil.setModifierProperty(field, PsiModifier.STATIC, true);
|
||||
}
|
||||
|
||||
if (shouldCreateFinalMember(myReferenceExpression, targetClass)) {
|
||||
PsiUtil.setModifierProperty(field, PsiModifier.FINAL, true);
|
||||
}
|
||||
|
||||
if (createConstantField()) {
|
||||
PsiUtil.setModifierProperty(field, PsiModifier.STATIC, true);
|
||||
PsiUtil.setModifierProperty(field, PsiModifier.FINAL, true);
|
||||
@@ -146,6 +150,22 @@ public class CreateFieldFromUsageFix extends CreateVarFromUsageFix {
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean shouldCreateFinalMember(@NotNull PsiReferenceExpression ref, @NotNull PsiClass targetClass) {
|
||||
if (!PsiTreeUtil.isAncestor(targetClass, ref, true)) {
|
||||
return false;
|
||||
}
|
||||
final PsiElement element = PsiTreeUtil.getParentOfType(ref, PsiClassInitializer.class, PsiMethod.class);
|
||||
if (element instanceof PsiClassInitializer){
|
||||
return true;
|
||||
}
|
||||
|
||||
if (element instanceof PsiMethod && ((PsiMethod)element).isConstructor()){
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
|
||||
Reference in New Issue
Block a user