I have a possible false positive Sonar Warning:
Use static access with “AbstractEntity” for “builder”.“static” base class members should not be accessed via derived types java:S3252
For example It is triggered by AdminExportRequest.builder() call.Both this class and its parent abstract class are using SuperBuilder from lombok.
Lombok Version is 1.18.42 and managed by Spring Boot 3.5.11I have Sonar Version: Community Build v26.3.0.120487
Here is the class definitions:
@NoArgsConstructor
@Getter@Setter
@ToString
@SuperBuilder
@EntityListeners(MyEntityListener.class)
@MappedSuperclass
@Cacheable(cacheNames = {“cache”})
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class AbstractEntity {…}
@Getter
@Setter
@Table(name = “admin_export_requests”)
@RequiredArgsConstructor
@SuperBuilder
@Entity
@EntityListeners({MyEntityListener.class})
public class AdminExportRequest extends AbstractEntity {…}
AbstractEntity is the base Class of all Entities and it has more than 50 sub-classes.It have only 12 warnings in this type related with builder.
Here code snippets for 2 example calls that has triggered the warning.
returnValue = request
.orElseGet(() → AdminExportRequest.builder()
.exportRequestStatus(ExportRequestStatus.NOT_REQUESTED)
.target(outerTarget)
.build());
protected AdminDetailsDtoV2 createDetailsDto(OtherEntity p) {
return AdminDetailsDtoV2.builder()
.id(p.getId())
.createdDate(p.getCreatedDate())
.lastModifiedDate(p.getLastModifiedDate())
.labels(loadLabels(p))
.build();
}
In the same project I have a different hierarchies but they not trigger this warning:
Non Entity parent and sub-class:
Getter
NoArgsConstructor
Setter
SuperBuilder
public abstract class FunctionContext {
}
SuperBuilder
Getter
public class DeleteAttachment extends FunctionContext {
}
This code part does not trigger any warning:
deleteAttachment.execute(DeleteAttachmentContext.builder().attachmentId(aid).build());
If this is not a false positive, how can I fix this?
AbstractEntity does not define all fields, it is enhanced by the sub-classes and I need to call concrete sub-class builder to build a valid instance.