modifying_psi.md: Remove hardcoded code snippet, cleanup applyFix and reference it from the page

This commit is contained in:
Karol Lewandowski 2023-10-18 16:21:17 +02:00
parent fa7f0d7e7b
commit 420cc1a328

View File

@ -98,13 +98,9 @@ public class ComparingStringReferencesInspection extends AbstractBaseJavaLocalIn
return InspectionBundle.message("inspection.comparing.string.references.use.quickfix"); return InspectionBundle.message("inspection.comparing.string.references.use.quickfix");
} }
/**
* This method manipulates the PSI tree to replace 'a==b' with 'a.equals(b)' or 'a!=b' with '!a.equals(b)'.
*
* @param project The project that contains the file being edited.
* @param descriptor A problem found by this inspection.
*/
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
// binaryExpression holds a PSI expression of the form "x == y",
// which needs to be replaced with "x.equals(y)"
PsiBinaryExpression binaryExpression = (PsiBinaryExpression) descriptor.getPsiElement(); PsiBinaryExpression binaryExpression = (PsiBinaryExpression) descriptor.getPsiElement();
IElementType opSign = binaryExpression.getOperationTokenType(); IElementType opSign = binaryExpression.getOperationTokenType();
PsiExpression lExpr = binaryExpression.getLOperand(); PsiExpression lExpr = binaryExpression.getLOperand();
@ -112,23 +108,29 @@ public class ComparingStringReferencesInspection extends AbstractBaseJavaLocalIn
if (rExpr == null) { if (rExpr == null) {
return; return;
} }
// Step 1: Create a replacement fragment from text, with "a" and "b" as placeholders
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiMethodCallExpression equalsCall = PsiMethodCallExpression equalsCall =
(PsiMethodCallExpression) factory.createExpressionFromText("a.equals(b)", null); (PsiMethodCallExpression) factory.createExpressionFromText("a.equals(b)", null);
// Step 2: Replace "a" and "b" with elements from the original file
PsiExpression qualifierExpression = equalsCall.getMethodExpression().getQualifierExpression(); PsiExpression qualifierExpression =
equalsCall.getMethodExpression().getQualifierExpression();
assert qualifierExpression != null; assert qualifierExpression != null;
qualifierExpression.replace(lExpr); qualifierExpression.replace(lExpr);
equalsCall.getArgumentList().getExpressions()[0].replace(rExpr); equalsCall.getArgumentList().getExpressions()[0].replace(rExpr);
// Step 3: Replace a larger element in the original file with the replacement tree
PsiExpression result = (PsiExpression) binaryExpression.replace(equalsCall); PsiExpression result = (PsiExpression) binaryExpression.replace(equalsCall);
// Steps 4-6 needed only for negation
if (opSign == JavaTokenType.NE) { if (opSign == JavaTokenType.NE) {
PsiPrefixExpression negation = (PsiPrefixExpression) factory.createExpressionFromText("!a", null); // Step 4: Create a replacement fragment with negation and negated operand placeholder
PsiPrefixExpression negation =
(PsiPrefixExpression) factory.createExpressionFromText("!a", null);
PsiExpression operand = negation.getOperand(); PsiExpression operand = negation.getOperand();
assert operand != null; assert operand != null;
// Step 5: Replace operand placeholder with the actual expression
operand.replace(result); operand.replace(result);
// Step 6: Replace the result with the negated expression
result.replace(negation); result.replace(negation);
} }
} }
@ -137,7 +139,5 @@ public class ComparingStringReferencesInspection extends AbstractBaseJavaLocalIn
public String getFamilyName() { public String getFamilyName() {
return getName(); return getName();
} }
} }
} }