mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: don't report Lombok @Getter can be used on implicit classes (IDEA-380028)
GitOrigin-RevId: a3129385bd5fb4b86b6137c5de3987e1c36870ff
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8515e3c2b4
commit
8b4e3a7084
+13
-15
@@ -8,7 +8,6 @@ import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.modcommand.PsiUpdateModCommandQuickFix;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
@@ -29,7 +28,7 @@ import java.util.List;
|
||||
public abstract class LombokGetterOrSetterMayBeUsedInspection extends LombokJavaInspectionBase {
|
||||
|
||||
@Override
|
||||
protected @NotNull PsiElementVisitor createVisitor(final @NotNull ProblemsHolder holder, final boolean isOnTheFly) {
|
||||
protected @NotNull PsiElementVisitor createVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
return new LombokGetterOrSetterMayBeUsedVisitor(holder, null);
|
||||
}
|
||||
|
||||
@@ -50,6 +49,7 @@ public abstract class LombokGetterOrSetterMayBeUsedInspection extends LombokJava
|
||||
|
||||
@Override
|
||||
public void visitClass(@NotNull PsiClass psiClass) {
|
||||
if (psiClass instanceof PsiAnonymousClass || psiClass instanceof PsiImplicitClass) return;
|
||||
if (psiClass.isRecord()) return;
|
||||
|
||||
List<Pair<PsiField, PsiMethod>> instanceCandidates = new ArrayList<>();
|
||||
@@ -91,21 +91,19 @@ public abstract class LombokGetterOrSetterMayBeUsedInspection extends LombokJava
|
||||
List<Pair<PsiField, PsiMethod>> fieldsAndMethods = new ArrayList<>();
|
||||
if (!processMethod(psiMethod, fieldsAndMethods, fieldsAndMethods)) return;
|
||||
if (!fieldsAndMethods.isEmpty()) {
|
||||
final Pair<PsiField, PsiMethod> psiFieldPsiMethodPair = fieldsAndMethods.get(0);
|
||||
final Pair<PsiField, PsiMethod> psiFieldPsiMethodPair = fieldsAndMethods.getFirst();
|
||||
warnOrFix(psiFieldPsiMethodPair.getFirst(), psiFieldPsiMethodPair.getSecond());
|
||||
}
|
||||
}
|
||||
|
||||
private void warnOrFix(@NotNull PsiClass psiClass, @NotNull List<Pair<PsiField, PsiMethod>> fieldsAndMethods) {
|
||||
if (myHolder != null) {
|
||||
String className = psiClass.getName();
|
||||
if (StringUtil.isNotEmpty(className)) {
|
||||
final PsiIdentifier psiClassNameIdentifier = psiClass.getNameIdentifier();
|
||||
final LocalQuickFix fix = new LombokGetterOrSetterMayBeUsedFix(className);
|
||||
myHolder.registerProblem(psiClass, getClassErrorMessage(className), ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
psiClassNameIdentifier != null ? psiClassNameIdentifier.getTextRangeInParent() : psiClass.getTextRange(),
|
||||
fix);
|
||||
}
|
||||
final PsiIdentifier identifier = psiClass.getNameIdentifier();
|
||||
if (identifier == null) return;
|
||||
String className = identifier.getText();
|
||||
final LocalQuickFix fix = new LombokGetterOrSetterMayBeUsedFix(className);
|
||||
myHolder.registerProblem(psiClass, getClassErrorMessage(className), ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||
identifier.getTextRangeInParent(), fix);
|
||||
}
|
||||
else if (myLombokGetterOrSetterMayBeUsedFix != null) {
|
||||
myLombokGetterOrSetterMayBeUsedFix.effectivelyDoFix(psiClass, fieldsAndMethods);
|
||||
@@ -143,11 +141,11 @@ public abstract class LombokGetterOrSetterMayBeUsedInspection extends LombokJava
|
||||
|
||||
@Override
|
||||
protected void applyFix(@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
|
||||
if (element instanceof PsiMethod) {
|
||||
new LombokGetterOrSetterMayBeUsedVisitor(null, this).visitMethodForFix((PsiMethod)element);
|
||||
if (element instanceof PsiMethod method) {
|
||||
new LombokGetterOrSetterMayBeUsedVisitor(null, this).visitMethodForFix(method);
|
||||
}
|
||||
else if (element instanceof PsiClass) {
|
||||
new LombokGetterOrSetterMayBeUsedVisitor(null, this).visitClass((PsiClass)element);
|
||||
else if (element instanceof PsiClass aClass) {
|
||||
new LombokGetterOrSetterMayBeUsedVisitor(null, this).visitClass(aClass);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-7
@@ -21,16 +21,11 @@ public class LombokGetterMayBeUsedInspectionTest extends LightDaemonAnalyzerTest
|
||||
|
||||
@Override
|
||||
protected @NotNull String getTestDataPath() {
|
||||
return PathManager.getCommunityHomePath() + "/plugins/lombok/testData";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getFilePath() {
|
||||
return "/inspection/lombokGetterMayBeUsed/" + getTestName(false) + ".java";
|
||||
return PathManager.getCommunityHomePath() + "/plugins/lombok/testData/inspection/lombokGetterMayBeUsed/";
|
||||
}
|
||||
|
||||
private void doTest() {
|
||||
doTest(getFilePath(), true, false);
|
||||
doTest(getTestName(false) + ".java", true, false);
|
||||
}
|
||||
|
||||
public void testFieldsWithGetter() {
|
||||
@@ -48,4 +43,8 @@ public class LombokGetterMayBeUsedInspectionTest extends LightDaemonAnalyzerTest
|
||||
public void testRecord() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testCompactSourceFile() {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
int i;
|
||||
|
||||
public void setI(int i) {
|
||||
this.i = i;
|
||||
}
|
||||
|
||||
public int getI() {
|
||||
return i;
|
||||
}
|
||||
|
||||
void main() {}
|
||||
Reference in New Issue
Block a user