[java-highlighting] IDEA-352727 Incomplete model with lombok

GitOrigin-RevId: ea0f6f9cf44d704d4ae7e45fa3ba262c1496ffa4
This commit is contained in:
Mikhail Pyltsin
2024-05-13 16:13:26 +02:00
committed by intellij-monorepo-bot
parent eb7c026662
commit de02a19083
8 changed files with 192 additions and 5 deletions

View File

@@ -5,7 +5,7 @@ import com.intellij.psi.*;
import com.intellij.psi.augment.PsiAugmentProvider;
import com.intellij.psi.augment.PsiExtensionMethod;
import com.intellij.psi.impl.source.PsiExtensibleClass;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.InitializationUtils;
import de.plushnikov.intellij.plugin.LombokClassNames;
import de.plushnikov.intellij.plugin.processor.LombokProcessorManager;
@@ -14,9 +14,6 @@ import de.plushnikov.intellij.plugin.processor.ValProcessor;
import de.plushnikov.intellij.plugin.processor.lombok.LombokAnnotationProcessor;
import de.plushnikov.intellij.plugin.processor.method.ExtensionMethodsHelper;
import de.plushnikov.intellij.plugin.processor.modifier.ModifierProcessor;
import de.plushnikov.intellij.plugin.psi.LombokLightAnnotationMethodBuilder;
import de.plushnikov.intellij.plugin.psi.LombokLightClassBuilder;
import de.plushnikov.intellij.plugin.psi.LombokLightMethodBuilder;
import de.plushnikov.intellij.plugin.util.PsiAnnotationSearchUtil;
import de.plushnikov.intellij.plugin.util.PsiAnnotationUtil;
import org.jetbrains.annotations.NotNull;
@@ -173,4 +170,43 @@ public final class LombokAugmentProvider extends PsiAugmentProvider {
}
return ExtensionMethodsHelper.getExtensionMethods(aClass, nameHint, context);
}
@Override
protected boolean mightBeAugmentedForIncompleteMode(@NotNull PsiClass psiClass) {
if(!(psiClass instanceof PsiExtensibleClass extensibleClass)) {
return false;
}
if(!(extensibleClass.getContainingFile() instanceof PsiJavaFile file)) {
return false;
}
return hasAnyLombokAnnotation(extensibleClass.getAnnotations()) ||
ContainerUtil.exists(extensibleClass.getOwnFields(), field -> hasAnyLombokAnnotation(field.getAnnotations())) ||
(file.getImportList() != null &&
ContainerUtil.exists(file.getImportList().getAllImportStatements(), statement -> {
PsiJavaCodeReferenceElement reference = statement.getImportReference();
return reference != null && reference.getText().startsWith("lombok");
}));
}
/**
* Checks if the given PsiModifierListOwner has any Lombok annotation.
* It is used only for incomplete mode.
*
* @param annotations The annotations to check for Lombok annotations.
* @return true if the modifierListOwner has any Lombok annotation, otherwise false.
*/
private static boolean hasAnyLombokAnnotation(PsiAnnotation @NotNull [] annotations) {
return ContainerUtil.exists(annotations, annotation -> {
if (annotation == null) {
return false;
}
String qualifiedName = annotation.getQualifiedName();
if (qualifiedName == null) {
return false;
}
return qualifiedName.startsWith("lombok.");
});
}
}

View File

@@ -0,0 +1,34 @@
package com.intellij.java.lomboktest;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.project.IncompleteDependenciesService;
import com.intellij.testFramework.PlatformTestUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
public class LombokIncompleteModeHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testLombokBasics() { doTest(); }
public void testLombokBasicsWithExplicitImport() { doTest(); }
private void doTest() {
doTest(getTestName(false) + ".java");
}
private void doTest(String fileName) {
var ignored = WriteAction.compute(() -> getProject().getService(IncompleteDependenciesService.class).enterIncompleteState());
try {
doTest("/plugins/lombok/testData/highlightingIncompleteMode/" + fileName, true, true);
}
finally {
WriteAction.run(ignored::close);
}
}
@Override
protected @NonNls @NotNull String getTestDataPath() {
return PlatformTestUtil.getCommunityPath();
}
}

View File

@@ -0,0 +1,32 @@
import <info descr="Not resolved until the project is fully loaded">lombok</info>.*<EOLError descr="';' expected"></EOLError>
public final class LombokBasics {
public static void main(String[] args) {
UserDao userDao = UserDao.<info descr="Not resolved until the project is fully loaded">builder</info>()
.<info descr="Not resolved until the project is fully loaded">info</info>("1")
.<info descr="Not resolved until the project is fully loaded">build</info>();
String name = userDao.<info descr="Not resolved until the project is fully loaded">name</info>();
UserChain userChain = new UserChain();
String name1 = userChain.<info descr="Not resolved until the project is fully loaded">getName</info>();
}
}
@<info descr="Not resolved until the project is fully loaded">Getter</info>
@<info descr="Not resolved until the project is fully loaded">SuperBuilder</info>
class UserDao extends UserId {
<info descr="Not resolved until the project is fully loaded">private final String name;</info>
<info descr="Not resolved until the project is fully loaded">private final String surname;</info>
<info descr="Not resolved until the project is fully loaded">private final String email;</info>
}
@<info descr="Not resolved until the project is fully loaded">SuperBuilder</info>
abstract class UserId {
<info descr="Not resolved until the project is fully loaded">private final long id;</info>
<info descr="Not resolved until the project is fully loaded">private final String info;</info>
}
class UserChain {
@<info descr="Not resolved until the project is fully loaded">Getter</info>
@<info descr="Not resolved until the project is fully loaded">Setter</info>
private String name;
}

View File

@@ -0,0 +1,34 @@
import <info descr="Not resolved until the project is fully loaded">lombok</info>.<info descr="Not resolved until the project is fully loaded">Getter</info>;
import <info descr="Not resolved until the project is fully loaded">lombok</info>.<info descr="Not resolved until the project is fully loaded">Setter</info>;
import <info descr="Not resolved until the project is fully loaded">lombok</info>.<info descr="Not resolved until the project is fully loaded">SuperBuilder</info>;
public final class LombokBasicsWithExplicitImport {
public static void main(String[] args) {
UserDao userDao = UserDao.<info descr="Not resolved until the project is fully loaded">builder</info>()
.<info descr="Not resolved until the project is fully loaded">info</info>("1")
.<info descr="Not resolved until the project is fully loaded">build</info>();
String name = userDao.<info descr="Not resolved until the project is fully loaded">name</info>();
UserChain userChain = new UserChain();
String name1 = userChain.<info descr="Not resolved until the project is fully loaded">getName</info>();
}
}
@<info descr="Not resolved until the project is fully loaded">Getter</info>
@<info descr="Not resolved until the project is fully loaded">SuperBuilder</info>
class UserDao extends UserId {
<info descr="Not resolved until the project is fully loaded">private final String name;</info>
<info descr="Not resolved until the project is fully loaded">private final String surname;</info>
<info descr="Not resolved until the project is fully loaded">private final String email;</info>
}
@<info descr="Not resolved until the project is fully loaded">SuperBuilder</info>
abstract class UserId {
<info descr="Not resolved until the project is fully loaded">private final long id;</info>
<info descr="Not resolved until the project is fully loaded">private final String info;</info>
}
class UserChain {
@<info descr="Not resolved until the project is fully loaded">Getter</info>
@<info descr="Not resolved until the project is fully loaded">Setter</info>
private String name;
}