mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
MoveFieldAssignmentToInitializerInspection: allow unknown methods
As this is intention-like part of inspection (no warning), we can allow it even if we are not sure that semantics will be preserved. See comments in IDEA-177602
This commit is contained in:
@@ -11,6 +11,7 @@ import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PropertyUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
@@ -113,13 +114,16 @@ public class MoveFieldAssignmentToInitializerInspection extends AbstractBaseJava
|
||||
result.set(Boolean.FALSE);
|
||||
return;
|
||||
}
|
||||
if (resolved instanceof PsiMember && ((PsiMember)resolved).getContainingClass() == aClass) {
|
||||
if (resolved instanceof PsiMethod) {
|
||||
resolved = PropertyUtil.getFieldOfGetter((PsiMethod)resolved);
|
||||
}
|
||||
if (resolved instanceof PsiField && ((PsiField)resolved).getContainingClass() == aClass) {
|
||||
// refers to another field/method of the same class (except referring from non-static member to static and
|
||||
// referring to initialized field which is probably ok)
|
||||
boolean nonStaticRefersToStatic =
|
||||
!field.hasModifierProperty(PsiModifier.STATIC) && ((PsiMember)resolved).hasModifierProperty(PsiModifier.STATIC);
|
||||
!field.hasModifierProperty(PsiModifier.STATIC) && ((PsiField)resolved).hasModifierProperty(PsiModifier.STATIC);
|
||||
if (nonStaticRefersToStatic) return;
|
||||
boolean initializedField = resolved instanceof PsiField && ((PsiField)resolved).getInitializer() != null;
|
||||
boolean initializedField = ((PsiField)resolved).getInitializer() != null;
|
||||
if (initializedField) {
|
||||
PsiField[] fields = aClass.getFields();
|
||||
int indexSource = ArrayUtil.indexOf(fields, field);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Move assignment to field declaration" "INFORMATION"
|
||||
public class Test {
|
||||
String myField = getValue();
|
||||
private String value;
|
||||
|
||||
void f() {
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value+value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Move assignment to field declaration" "INFORMATION"
|
||||
public class Test {
|
||||
String myField;
|
||||
private String value;
|
||||
|
||||
void f() {
|
||||
<caret>myField = getValue();
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value+value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user