mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
MoveFieldAssignmentToInitializer: misc fixes; support Java 12 (IDEA-203693)
This commit is contained in:
@@ -36,8 +36,8 @@ public class MoveFieldAssignmentToInitializerInspection extends AbstractBaseJava
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitAssignmentExpression(PsiAssignmentExpression assignment) {
|
||||
PsiElement parent = assignment.getParent();
|
||||
if (!(parent instanceof PsiExpressionStatement)) return;
|
||||
if (!assignment.getOperationTokenType().equals(JavaTokenType.EQ)) return;
|
||||
if (assignment.getParent() instanceof PsiExpressionList || !ExpressionUtils.isVoidContext(assignment)) return;
|
||||
PsiField field = getAssignedField(assignment);
|
||||
if (field == null || field.hasInitializer()) return;
|
||||
PsiClass psiClass = field.getContainingClass();
|
||||
@@ -169,7 +169,8 @@ public class MoveFieldAssignmentToInitializerInspection extends AbstractBaseJava
|
||||
if (assignmentExpression == null) return;
|
||||
PsiExpression rValue = assignmentExpression.getRExpression();
|
||||
PsiMember member = PsiTreeUtil.getParentOfType(assignmentExpression, PsiMember.class);
|
||||
if (member instanceof PsiClassInitializer || member instanceof PsiMethod && ((PsiMethod)member).isConstructor()) {
|
||||
if ((member instanceof PsiClassInitializer || member instanceof PsiMethod && ((PsiMethod)member).isConstructor()) &&
|
||||
ExpressionUtils.isVoidContext(assignmentExpression) && assignmentExpression.getOperationTokenType().equals(JavaTokenType.EQ)) {
|
||||
// ignore usages other than initializing
|
||||
if (rValue == null || !EquivalenceChecker.getCanonicalPsiEquivalence().expressionsAreEquivalent(rValue, expression)) {
|
||||
result.set(Boolean.FALSE);
|
||||
@@ -234,16 +235,20 @@ public class MoveFieldAssignmentToInitializerInspection extends AbstractBaseJava
|
||||
PsiModifierListOwner owner = enclosingMethodOrClassInitializer(assignment, field);
|
||||
|
||||
for (PsiAssignmentExpression assignmentExpression : assignments) {
|
||||
PsiElement statement = assignmentExpression.getParent();
|
||||
PsiElement parent = statement.getParent();
|
||||
if (parent instanceof PsiIfStatement ||
|
||||
parent instanceof PsiWhileStatement ||
|
||||
parent instanceof PsiForStatement ||
|
||||
parent instanceof PsiForeachStatement) {
|
||||
ct.replace(statement, ";");
|
||||
}
|
||||
else {
|
||||
ct.delete(statement);
|
||||
PsiElement parent = assignmentExpression.getParent();
|
||||
if (parent instanceof PsiExpressionStatement) {
|
||||
PsiElement grandParent = parent.getParent();
|
||||
if (grandParent instanceof PsiIfStatement || grandParent instanceof PsiLoopStatement) {
|
||||
ct.replace(parent, ";");
|
||||
}
|
||||
else if (grandParent instanceof PsiSwitchLabeledRuleStatement) {
|
||||
ct.replace(parent, "{}");
|
||||
}
|
||||
else {
|
||||
ct.delete(parent);
|
||||
}
|
||||
} else if (parent instanceof PsiLambdaExpression) {
|
||||
ct.replace(assignmentExpression, factory.createCodeBlock());
|
||||
}
|
||||
ct.insertCommentsBefore(field);
|
||||
// if we replace/delete several assignments we want to restore comments at each place separately
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Move assignment to field declaration" "true"
|
||||
public class Main {
|
||||
int i = 1;
|
||||
|
||||
Main() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// "Move assignment to field declaration" "true"
|
||||
public class Main {
|
||||
int i = 1;
|
||||
|
||||
public void test(int x) {
|
||||
switch (x) {
|
||||
case 1 -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Move assignment to field declaration" "true"
|
||||
public class Main {
|
||||
int i = 1;
|
||||
|
||||
public void test() {
|
||||
Runnable r = () -> {
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Move assignment to field declaration" "false"
|
||||
public class Main {
|
||||
int i;
|
||||
|
||||
Main() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
i +<caret>= 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Move assignment to field declaration" "true"
|
||||
public class Main {
|
||||
int i;
|
||||
|
||||
Main() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
i <caret>= 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Move assignment to field declaration" "false"
|
||||
public class Main {
|
||||
int i;
|
||||
|
||||
public int test(int x) {
|
||||
return switch (x) {
|
||||
case 1 -> i <caret>= 1;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Move assignment to field declaration" "true"
|
||||
public class Main {
|
||||
int i;
|
||||
|
||||
public void test(int x) {
|
||||
switch (x) {
|
||||
case 1 -> i <caret>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Move assignment to field declaration" "false"
|
||||
import java.util.function.IntSupplier;
|
||||
|
||||
public class Main {
|
||||
int i;
|
||||
|
||||
public void test() {
|
||||
IntSupplier r = () -> i <caret>= 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Move assignment to field declaration" "true"
|
||||
public class Main {
|
||||
int i;
|
||||
|
||||
public void test() {
|
||||
Runnable r = () -> i <caret>= 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user