When using Lombok's @Value
annotation in a Java class, Eclipse may show an error message saying "@NonNullByDefault
is applied to the module default package" or "@NonNullByDefault
in package-info.java does not affect source files" or similar.
This error occurs because @NonNullByDefault
is a Java 8 annotation that specifies that all parameters and return values in a package or module are non-null by default unless explicitly annotated otherwise. However, Lombok's @Value
annotation generates a constructor and getters that do not use nullability annotations, so Eclipse doesn't know whether the generated code is supposed to be non-null or nullable by default.
To resolve this error, you can add @NonNull
annotations to the fields of the Lombok class, or annotate the Lombok class with @lombok.NonNull
, or use @Nullable
annotations to denote nullable fields or arguments. Another solution would be to explicitly declare @NonNullByDefault
at the top of the file or in the package-info.java
file.
For example, if you want all fields in a package to be assumed as non-null by default, you can add @NonNullByDefault
to the package-info.java
file like this:
@NonNullByDefault
package com.example;
import org.eclipse.jdt.annotation.NonNullByDefault;
This will tell Eclipse that all fields in the com.example
package are non-null by default, and the @Value
-annotated Lombok classes in that package will not trigger the error message.
Asked: 2023-07-03 20:08:21 +0000
Seen: 7 times
Last updated: Jul 03 '23