Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@NonNull annotation in project Lombok

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.

like image 537
Naanavanalla Avatar asked Aug 06 '17 07:08

Naanavanalla


People also ask

What is @NonNull Lombok?

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.

What does the @NonNull annotation do?

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.

How does @NonNull work?

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.

What is @NonNull in Java?

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.


2 Answers

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

like image 169
sunkuet02 Avatar answered Oct 11 '22 07:10

sunkuet02


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.

like image 35
tom Avatar answered Oct 11 '22 07:10

tom