mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: make quick fix respect "Make generated local variables final" setting
for "Pattern variable can be used" inspection GitOrigin-RevId: 7ca41a5435635f951b10f513def9fa605e8274a3
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8a07022c21
commit
464f0a4cfe
+18
-8
@@ -6,6 +6,7 @@ import com.intellij.codeInsight.daemon.impl.analysis.HighlightingFeature;
|
||||
import com.intellij.codeInsight.intention.FileModifier;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.impl.light.LightRecordMethod;
|
||||
import com.intellij.psi.util.JavaPsiPatternUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
@@ -228,21 +229,30 @@ public class PatternVariableCanBeUsedInspection extends AbstractBaseJavaLocalIns
|
||||
PsiInstanceOfExpression instanceOf = myInstanceOfPointer.getElement();
|
||||
if (instanceOf == null) return;
|
||||
CommentTracker ct = new CommentTracker();
|
||||
StringBuilder text = new StringBuilder(ct.text(instanceOf.getOperand()));
|
||||
text.append(" instanceof ");
|
||||
PsiModifierList modifierList = variable.getModifierList();
|
||||
String modifiers = modifierList == null || modifierList.getTextLength() == 0 || !PsiUtil.isLanguageLevel16OrHigher(variable) ?
|
||||
"" : modifierList.getText() + " ";
|
||||
String deconstructionList =
|
||||
instanceOf.getPattern() instanceof PsiDeconstructionPattern deconstruction ? deconstruction.getDeconstructionList().getText() : "";
|
||||
ct.replace(instanceOf, ct.text(instanceOf.getOperand()) +
|
||||
" instanceof " + modifiers + typeElement.getText() + deconstructionList + " " + variable.getName());
|
||||
JavaCodeStyleSettings codeStyleSettings = JavaCodeStyleSettings.getInstance(variable.getContainingFile());
|
||||
if (modifierList != null && modifierList.getTextLength() > 0) {
|
||||
modifierList.setModifierProperty(PsiModifier.FINAL, codeStyleSettings.GENERATE_FINAL_LOCALS);
|
||||
text.append(ct.text(modifierList)).append(' ');
|
||||
}
|
||||
else if (codeStyleSettings.GENERATE_FINAL_LOCALS) {
|
||||
text.append("final ");
|
||||
}
|
||||
text.append(typeElement.getText()).append(' ');
|
||||
if (instanceOf.getPattern() instanceof PsiDeconstructionPattern deconstructionPattern) {
|
||||
text.append(ct.text(deconstructionPattern.getDeconstructionList())).append(' ');
|
||||
}
|
||||
text.append(variable.getName());
|
||||
ct.replace(instanceOf, text.toString());
|
||||
ct.deleteAndRestoreComments(variable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable FileModifier getFileModifierForPreview(@NotNull PsiFile target) {
|
||||
PsiInstanceOfExpression instanceOf = myInstanceOfPointer.getElement();
|
||||
return instanceOf == null ? null : new PatternVariableCanBeUsedFix(myName, PsiTreeUtil
|
||||
.findSameElementInCopy(instanceOf, target));
|
||||
return instanceOf == null ? null : new PatternVariableCanBeUsedFix(myName, PsiTreeUtil.findSameElementInCopy(instanceOf, target));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// "Replace 's' with pattern variable" "true"
|
||||
class X {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof final String s) {
|
||||
if (obj instanceof @Ann String s) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// "Replace 'point' with pattern variable" "true"
|
||||
class X {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof Point(double x, double y) point && y == x) {
|
||||
if (obj instanceof Point(double x/*c*/, double y) point && y == x) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
class X {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String) {
|
||||
final String <caret>s = (String)obj;
|
||||
@Ann final String <caret>s = (String)obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
// "Replace 'point' with pattern variable" "true"
|
||||
class X {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof Point(double x, double y) && y == x) {
|
||||
if (obj instanceof Point(double x/*c*/, double y) && y == x) {
|
||||
Point point<caret> = (Point) obj;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user