mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-inspections] SimplifyBooleanExpressionFix: retarget to existing pattern variable (IDEA-336123)
GitOrigin-RevId: c3e7f12fb11926084fa8a71467394635d1cfc5d9
This commit is contained in:
committed by
intellij-monorepo-bot
parent
66d4dad965
commit
51849dbdf2
+36
-23
@@ -209,30 +209,43 @@ public class SimplifyBooleanExpressionFix extends PsiUpdateModCommandAction<PsiE
|
||||
private static void processPatternVariables(@NotNull PsiExpression subExpression) {
|
||||
List<PsiPatternVariable> variables = JavaPsiPatternUtil.getExposedPatternVariables(subExpression);
|
||||
for (PsiPatternVariable variable : variables) {
|
||||
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(variable, variable.getDeclarationScope());
|
||||
if (!refs.isEmpty()) {
|
||||
PsiInstanceOfExpression target = newTargetForPatternVariable(subExpression, variable);
|
||||
if (target != null) {
|
||||
PsiInstanceOfExpression updated = (PsiInstanceOfExpression)JavaPsiFacade.getElementFactory(subExpression.getProject())
|
||||
.createExpressionFromText("x instanceof T t", target);
|
||||
updated.getOperand().replace(target.getOperand());
|
||||
PsiTypeTestPattern newPattern = (PsiTypeTestPattern)Objects.requireNonNull(updated.getPattern());
|
||||
PsiTypeElement checkType = target.getCheckType();
|
||||
if (checkType == null) continue;
|
||||
Objects.requireNonNull(newPattern.getCheckType()).replace(checkType);
|
||||
PsiPatternVariable newVariable = (PsiPatternVariable)Objects.requireNonNull(newPattern.getPatternVariable()).replace(variable);
|
||||
variable.delete();
|
||||
String name = new VariableNameGenerator(target, VariableKind.LOCAL_VARIABLE).byName(variable.getName())
|
||||
.generate(true);
|
||||
if (!name.equals(newVariable.getName())) {
|
||||
newVariable.setName(name);
|
||||
for (PsiReferenceExpression ref : refs) {
|
||||
if (ref.isValid()) {
|
||||
ref.handleElementRename(name);
|
||||
}
|
||||
}
|
||||
retargetPatternVariable(subExpression, variable);
|
||||
}
|
||||
}
|
||||
|
||||
private static void retargetPatternVariable(@NotNull PsiExpression subExpression, @NotNull PsiPatternVariable variable) {
|
||||
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(variable, variable.getDeclarationScope());
|
||||
if (refs.isEmpty()) return;
|
||||
PsiInstanceOfExpression target = newTargetForPatternVariable(subExpression, variable);
|
||||
if (target == null) return;
|
||||
if (target.getPattern() instanceof PsiTypeTestPattern existingPattern) {
|
||||
PsiPatternVariable existingVar = existingPattern.getPatternVariable();
|
||||
if (existingVar != null) {
|
||||
for (PsiReferenceExpression ref : refs) {
|
||||
if (ref.isValid()) {
|
||||
ref.handleElementRename(existingVar.getName());
|
||||
}
|
||||
target.replace(updated);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
PsiInstanceOfExpression updated = (PsiInstanceOfExpression)JavaPsiFacade.getElementFactory(subExpression.getProject())
|
||||
.createExpressionFromText("x instanceof T t", target);
|
||||
updated.getOperand().replace(target.getOperand());
|
||||
PsiTypeTestPattern newPattern = (PsiTypeTestPattern)Objects.requireNonNull(updated.getPattern());
|
||||
PsiTypeElement checkType = target.getCheckType();
|
||||
if (checkType == null) return;
|
||||
Objects.requireNonNull(newPattern.getCheckType()).replace(checkType);
|
||||
PsiPatternVariable newVariable = (PsiPatternVariable)Objects.requireNonNull(newPattern.getPatternVariable()).replace(variable);
|
||||
variable.delete();
|
||||
target.replace(updated);
|
||||
String name = new VariableNameGenerator(target, VariableKind.LOCAL_VARIABLE).byName(variable.getName())
|
||||
.generate(true);
|
||||
if (!name.equals(newVariable.getName())) {
|
||||
newVariable.setName(name);
|
||||
for (PsiReferenceExpression ref : refs) {
|
||||
if (ref.isValid()) {
|
||||
ref.handleElementRename(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Simplify 'obj instanceof String str' to true" "true"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String s) {
|
||||
if (s.isEmpty()) {
|
||||
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Simplify 'obj instanceof String str' to true" "true"
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
if (obj instanceof String s) {
|
||||
if (obj instanceof<caret> String str && str.isEmpty()) {
|
||||
|
||||
}
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user