java inspections: implement preview for quick fix

GitOrigin-RevId: d2930f7ddab6bf3d761b20b1759ff83f0cc06605
This commit is contained in:
Bas Leijdekkers
2022-07-20 18:53:12 +00:00
committed by intellij-monorepo-bot
parent bb7e382697
commit a719a82ae4
@@ -1,10 +1,11 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.daemon.impl.analysis.GenericsHighlightUtil;
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil;
import com.intellij.codeInsight.intention.AddAnnotationPsiFix;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.java.analysis.JavaAnalysisBundle;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.diagnostic.Logger;
@@ -68,6 +69,10 @@ public class PossibleHeapPollutionVarargsInspection extends AbstractBaseJavaLoca
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
applyFix(project, descriptor, true);
}
private void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor, boolean writeCommand) {
final PsiElement psiElement = descriptor.getPsiElement();
if (!(psiElement instanceof PsiIdentifier)) return;
PsiModifierListOwner owner = (PsiModifierListOwner)psiElement.getParent();
@@ -77,13 +82,24 @@ public class PossibleHeapPollutionVarargsInspection extends AbstractBaseJavaLoca
String compactCtorText = "public " + rec.getName() + " {}";
PsiMethod ctor = JavaPsiFacade.getElementFactory(project).createMethodFromText(compactCtorText, owner);
PsiMethod firstMethod = ArrayUtil.getFirstElement(rec.getMethods());
owner = (PsiMethod)WriteCommandAction.writeCommandAction(owner.getContainingFile()).withName(getFamilyName())
.compute(() -> rec.addBefore(ctor, firstMethod));
if (writeCommand) {
owner = (PsiMethod)WriteCommandAction.writeCommandAction(owner.getContainingFile()).withName(getFamilyName())
.compute(() -> rec.addBefore(ctor, firstMethod));
}
else {
owner = (PsiMethod)rec.addBefore(ctor, firstMethod);
}
}
if (owner instanceof PsiMethod) {
new AddAnnotationPsiFix(CommonClassNames.JAVA_LANG_SAFE_VARARGS, owner).applyFix(project, descriptor);
}
}
@Override
public @NotNull IntentionPreviewInfo generatePreview(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
applyFix(project, descriptor, false);
return IntentionPreviewInfo.DIFF;
}
}
private static class MakeFinalAndAnnotateQuickFix extends AddAnnotationPsiFix {