Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.jetbrains.annotations.NotNull instead of javax.annotation.Nonnull when implement method in Intellij IDEA

After recent JetBrains Intellij IDEA updates I found out that when I'm trying to implement method annotated with javax.annotation.Nonnull - IDE implements it with org.jetbrains.annotations.NotNull instead.

Example:

If you have an interface:

import javax.annotation.Nonnull;

interface User {

    @Nonnull
    String getName();
}

it will be implemented as:

import org.jetbrains.annotations.NotNull;

class Customer implements User {

    @NotNull
    @Override
    public String getName() {
        return null;
    } 
}

The question is how to configure IDE to implement methods with strict validation annotation?

like image 530
Mikita Bughunter Avatar asked Jan 24 '23 16:01

Mikita Bughunter


2 Answers

Looks like a defect (https://youtrack.jetbrains.com/issue/IDEA-253324) although there is a workaround exist: Inspections > Java > Probable bugs > Nullability problems > @NotNull/@Nullable problems > Configure Annotations. Set javax.annotation.Nullable/javax.annotation.Nonnull as defaults and restart the IDE.

like image 169
Aliaksei Kaniaveha Avatar answered Jan 30 '23 05:01

Aliaksei Kaniaveha


To add the library with annotations to a Gradle project, add the implementation org.jetbrains:annotations:23.0.0 dependency to the build.gradle file.

like image 23
Swetabh Suman Avatar answered Jan 30 '23 05:01

Swetabh Suman