[lombok] EA-977599 EA-932894 EA-927010 EA-921512 validation for Builder/SuperBuilder should skip anonymous classes

GitOrigin-RevId: f949f05a6e2211c277b285b2d64c4e58b2cb72ca
This commit is contained in:
Michail Plushnikov
2023-12-17 18:35:43 +01:00
committed by intellij-monorepo-bot
parent 953dbacb92
commit 7aeec8cea6
4 changed files with 42 additions and 4 deletions

View File

@@ -287,13 +287,17 @@ public class BuilderHandler {
}
boolean validateAnnotationOnRightType(@NotNull PsiClass psiClass, @NotNull ProblemSink builder) {
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum()) {
if (isNotSupported(psiClass)) {
builder.addErrorMessage("inspection.message.builder.can.be.used.only");
return false;
}
return true;
}
protected static boolean isNotSupported(@NotNull PsiClass psiClass) {
return psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum() || psiClass instanceof PsiAnonymousClass;
}
private static boolean validateObtainViaAnnotations(Stream<BuilderInfo> builderInfos, @NotNull ProblemSink problemSink) {
builderInfos.map(BuilderInfo::withObtainVia)
.filter(BuilderInfo::hasObtainViaAnnotation)
@@ -379,11 +383,16 @@ public class BuilderHandler {
}
String relevantReturnType = psiClass.getName();
if(psiClass instanceof PsiAnonymousClass psiAnonymousClass) {
relevantReturnType = psiAnonymousClass.getBaseClassType().getClassName();
}
if (null != psiMethod && !psiMethod.isConstructor()) {
final PsiType psiMethodReturnType = psiMethod.getReturnType();
if (null != psiMethodReturnType) {
relevantReturnType = PsiNameHelper.getQualifiedClassName(psiMethodReturnType.getPresentableText(), false);
if (psiMethodReturnType instanceof PsiClassType psiClassType) {
relevantReturnType = psiClassType.getClassName();
}else if (null != psiMethodReturnType) {
relevantReturnType = PsiNameHelper.getShortClassName(psiMethodReturnType.getPresentableText());
}
}

View File

@@ -40,7 +40,7 @@ public class SuperBuilderHandler extends BuilderHandler {
@Override
boolean validateAnnotationOnRightType(@NotNull PsiClass psiClass, @NotNull ProblemSink builder) {
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum() || psiClass.isRecord()) {
if (isNotSupported(psiClass) || psiClass.isRecord()) {
builder.addErrorMessage("inspection.message.superbuilder.can.be.used.on.classes.only");
return false;
}

View File

@@ -49,3 +49,19 @@ class BuilderWithPredefinedClassAnnotation {
}
}
public class BuilderOnAnonymousClass {
interface HelloWorld {
void greet();
}
HelloWorld myWorld = new <error descr="'@lombok.Builder' not applicable to type use"><error descr="@Builder is only supported on classes, records, constructors, and methods.">@lombok.Builder</error></error> HelloWorld() {
public void greet() {
System.out.println("Hello World");
}
};
public static void main(String[] args) {
}
}

View File

@@ -10,4 +10,17 @@ interface BuilderInterfaceError {
@interface BuilderAnnotationError {
}
public class SuperBuilderOnAnonymousClass {
interface HelloWorld {
void greet();
}
HelloWorld myWorld = new <error descr="'@lombok.experimental.SuperBuilder' not applicable to type use"><error descr="@SuperBuilder is only supported on classes.">@lombok.experimental.SuperBuilder</error></error> HelloWorld() {
public void greet() {
System.out.println("Hello World");
}
};
public static void main(String[] args) {
}
}