I recently started to use lombok
in my project. In lombok documentation, it is specified that @NonNull
annotation can be configured to throw either NullPointerException
or IllegalArgumentException
. It is specified that by default NullPointerException
will be thrown. It is also specified that to throw IllegalArgumentException
, I should set lombok.nonNull.exceptionType = IllegalArgumentException
. But, I am not understanding where I should specify lombok.nonNull.exceptionType = IllegalArgumentException
in my code.
`
import com.sandesha.lombak.domain.Employee;
import lombok.NonNull;
public class EmployeeOperation {
/**
* @NonNull performs null check
* @param e1
* @param e2
* @return
*/
public boolean isEqual(@NonNull Employee e1, @NonNull Employee e2)
{
return e1.equals(e2);
}
}
Please help me. Thank you.
The @NonNull annotation generates a null check for fields and arguments annotated with this annotation. This annotation can be used on fields, constructor arguments, and method arguments. 51. 1. import lombok.
The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.
The @NonNull Annotation The @NonNull annotation is the most important among all the annotations of the null-safety feature. We can use this annotation t0 declare non-null constraint anywhere an object reference is expected: a field, a method parameter or a method's return value.
The nonNull method is a static method of the Objects class in Java that checks whether the input object reference supplied to it is non-null or not. If the passed object is non-null, then the method returns true. If the passed object is null , then the method returns false.
You need to create a file named lombok.config
in the home directory of your project, which is the default lombok configuration file to your project. The file looks like:
lombok.nonNull.exceptionType = IllegalArgumentException
lombok.nonNull.flagUsage = [warning | error]
You may see the project
This is not parameterized in the @NonNull
annotation, it can be only specified in the Lombok configuration keys of @NonNull:
Supported configuration keys:
lombok.nonNull.exceptionType = [NullPointerException | IllegalArgumentException | Assertion] (default: NullPointerException).
[...]
Create a lombok.config
file in your project root directory, containing this line:
lombok.nonNull.exceptionType = IllegalArgumentException
More details about the Lombok configuration system are in the Lombok documentation page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With