gradlelombokmapstructjava-17annotation-processing

Java 17 Upgrade: Cannot find symbol error with Lombok `@FieldNameConstants` and MapStruct


After upgrading from Java 11 to Java 17, I'm encountering compilation errors related to Lombok's @FieldNameConstants annotation. I'm also using MapStruct, so I'm mentioning that in case that could be related to their annotation processors somehow.

Error

error: cannot find symbol
import static com.example.dto.ContactRegistration.Fields.contactGuid;
                                                 ^
  symbol:   class Fields
  location: class ContactRegistration

Environment

Gradle Configuration

lombokVersion = '1.18.34'
mapstructVersion = '1.6.3'
mapstructBindingVersion = '0.2.0'

dependencies {
    implementation("org.mapstruct:mapstruct:${mapstructVersion}")
    implementation("org.projectlombok:lombok:${lombokVersion}")
    
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
    annotationProcessor "org.projectlombok:lombok-mapstruct-binding:${mapstructBindingVersion}"
}

Auto-generated DTO (from OpenAPI/YAML)

The DTOs are generated from a YAML specification with Lombok imports:

x-imports:
  - lombok.experimental.FieldNameConstants

Resulting generated class:

import lombok.experimental.FieldNameConstants;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.Valid;

@FieldNameConstants
@JsonInclude(JsonInclude.Include.NON_NULL)
public final class ContactRegistration extends org.springframework.hateoas.RepresentationModel<ContactRegistration> {
    
    @Pattern(regexp = ".*\\S.*", message = "PATTERN_INVALID")
    @Size(min = 1, max = 100)
    private String firstName;
    
    @Valid
    private Address address;
    
    @Size(min = 1, max = 100)
    private String contactGuid;
    
    // Getters and setters omitted for brevity
}

Question

Since the DTOs are auto-generated from OpenAPI/YAML files, I cannot manually add the Fields class. The error only appeared after upgrading to Java 17.

What configuration changes are needed to make Lombok's @FieldNameConstants work properly with Java 17 and MapStruct?

Is this a known issue with the annotation processor ordering or Java 17 compatibility?

Thank you.


Solution

  • It seems we've found the cause of this issue: One of our internal dependencies was still pulling Java 11 related code, using javax instead of jakarta and that was breaking the annotation processors and causing the Fields errors reported here.