disable assign parameter to final field (IDEA-173815)

for constructors with chaining calls, for non-constructors
This commit is contained in:
Anna Kozlova
2017-06-06 12:42:17 +03:00
parent 781137ab58
commit 6487bb6913
2 changed files with 31 additions and 0 deletions

View File

@@ -48,6 +48,24 @@ public class AssignFieldFromParameterAction extends BaseIntentionAction {
final PsiField field = findFieldToAssign(project, myParameter);
if (field == null || type == null || !field.getType().isAssignableFrom(type)) return false;
if (!field.getLanguage().isKindOf(JavaLanguage.INSTANCE)) return false;
PsiElement scope = myParameter.getDeclarationScope();
if (scope instanceof PsiMethod && field.hasModifierProperty(PsiModifier.FINAL)) {
if (((PsiMethod)scope).isConstructor()) {
PsiCodeBlock body = ((PsiMethod)scope).getBody();
LOG.assertTrue(body != null);
PsiStatement[] statements = body.getStatements();
if (statements.length > 0 && statements[0] instanceof PsiExpressionStatement) {
PsiExpression expression = ((PsiExpressionStatement)statements[0]).getExpression();
if (expression instanceof PsiMethodCallExpression &&
PsiKeyword.THIS.equals(((PsiMethodCallExpression)expression).getMethodExpression().getReferenceName())) {
return false;
}
}
}
else {
return false;
}
}
setText(CodeInsightBundle.message("intention.assign.field.from.parameter.text", field.getName()));
return true;

View File

@@ -0,0 +1,13 @@
// "Assign Parameter to Field 'myStr'" "false"
class Foo1 {
final String myStr;
Foo1(String str, int i) {
myStr = (str);
}
Foo1(String st<caret>r) {
this(str, 2);
}
}