[lombok] IDEA-354173 java: Improve @Getter/@Setter 'supported on' validation (is invalid on records)

GitOrigin-RevId: fa6a65ad6c3c5ea0befa1a55cdf9987fd009fa68
This commit is contained in:
Michail Plushnikov
2024-09-10 18:32:59 +02:00
committed by intellij-monorepo-bot
parent 9dcd3e8ec5
commit eaf8c3f891
5 changed files with 10 additions and 6 deletions

View File

@@ -106,7 +106,7 @@ public abstract class AbstractClassProcessor extends AbstractProcessor implement
public Collection<LombokProblem> verifyAnnotation(@NotNull PsiAnnotation psiAnnotation) {
Collection<LombokProblem> result = Collections.emptyList();
// check first for fields, methods and filter it out, because PsiClass is parent of all annotations and will match other parents too
PsiElement psiElement = PsiTreeUtil.getParentOfType(psiAnnotation, PsiField.class, PsiMethod.class, PsiClass.class);
PsiElement psiElement = PsiTreeUtil.getParentOfType(psiAnnotation, PsiField.class, PsiRecordComponent.class, PsiMethod.class, PsiClass.class);
if (psiElement instanceof PsiClass) {
ProblemValidationSink problemNewBuilder = new ProblemValidationSink();
validate(psiAnnotation, (PsiClass) psiElement, problemNewBuilder);

View File

@@ -71,7 +71,7 @@ public final class GetterProcessor extends AbstractClassProcessor {
}
private static void validateAnnotationOnRightType(@NotNull PsiClass psiClass, @NotNull ProblemSink builder) {
if (psiClass.isAnnotationType() || psiClass.isInterface()) {
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isRecord()) {
builder.addErrorMessage("inspection.message.getter.only.supported.on.class.enum.or.field.type");
builder.markFailed();
}

View File

@@ -70,8 +70,8 @@ public final class SetterProcessor extends AbstractClassProcessor {
private static void validateAnnotationOnRightType(@NotNull PsiAnnotation psiAnnotation,
@NotNull PsiClass psiClass,
@NotNull ProblemSink builder) {
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum()) {
builder.addErrorMessage("inspection.message.s.only.supported.on.class.or.field.type", psiAnnotation.getQualifiedName());
if (psiClass.isAnnotationType() || psiClass.isInterface() || psiClass.isEnum() || psiClass.isRecord()) {
builder.addErrorMessage("inspection.message.setter.only.supported.on.class.or.field.type");
builder.markFailed();
}
}

View File

@@ -62,7 +62,7 @@ inspection.message.generating.equals.hashcode.with.super.call=Generating equals/
inspection.message.generating.equals.hashcode.implementation=Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '(callSuper=false)' to your type.
inspection.message.lazy.not.supported.for.getter.on.type='lazy' is not supported for @Getter on a type
inspection.message.getter.only.supported.on.class.enum.or.field.type=@Getter is only supported on a class, enum or field type
inspection.message.s.only.supported.on.class.or.field.type=''@{0}'' is only supported on a class or field type
inspection.message.setter.only.supported.on.class.or.field.type=@Setter is only supported on a class or field type
inspection.message.to.string.only.supported.on.class.or.enum.type=@ToString is only supported on a class or enum type
inspection.message.not.generated.s.method.with.same.name.already.exists=Not generated ''{0}()'': A method with same name already exists
inspection.message.utility.classes.cannot.have.declared.constructors=@UtilityClasses cannot have declared constructors.

View File

@@ -1,4 +1,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@@ -11,6 +13,8 @@ import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder;
import lombok.experimental.UtilityClass;
<error descr="@Getter is only supported on a class, enum or field type">@Getter</error>
<error descr="@Setter is only supported on a class or field type">@Setter</error>
<error descr="@Data is only supported on a class type">@Data</error>
<error descr="@Value is only supported on a class type">@Value</error>
<error descr="@ToString is only supported on a class or enum type">@ToString</error>
@@ -23,5 +27,5 @@ import lombok.experimental.UtilityClass;
@With//OK on records
@Builder//OK on records
@FieldNameConstants//OK on records
public record InvalidLombokAnnotationsOnRecord(int a, int b) {
public record InvalidLombokAnnotationsOnRecord(@Getter /*OK on record param*/int a, int b) {
}