Martin Mucha
Martin Mucha

Reputation: 3091

IntelliJ refactoring, disable/customize generating "@org.jetbrains.annotations.NotNull"

Every time I extract method, this line is generated "for me":

@org.jetbrains.annotations.NotNull

around the method. 1. I don't have this on my class path, so it does not compile 2. even if I have, I don't want to include JetBrains annotations in my project. Even if I have more suitable (maybe) javax.validation.constraints.NotNull annotation on class path, IntelliJ does not use that one.

Questions:

  1. how to turn it off entirely?
  2. how to customize, which annotation to use?

Upvotes: 20

Views: 4589

Answers (4)

ucMax
ucMax

Reputation: 5476

I ran into the same problem and was able to fix it by following these steps:

First, go to Settings > Editor > Inspections >

enter image description here

enter image description here

Find Java > Probable Bugs > Nullability Problems > @NotNull/@Nullable Problems

enter image description here

enter image description here

Click Configure Annotations button and check these items

enter image description here

Upvotes: 8

ursa
ursa

Reputation: 4611

IntelliJ IDEA 2024+

  1. Press Cmd+Alt+M twice to show full dialog

enter image description here

  1. Press Cmd+Alt+M and then gear icon near method name

enter image description here enter image description here

Upvotes: 0

Bas Leijdekkers
Bas Leijdekkers

Reputation: 26577

The extract method dialog has a Generate annotations check box. When disabled, it does not generate the nullability annotations. The state of this check box is remembered between invocations, so disabling it once is enough. extract method dialog

If this check box does not appear, annotations for the extracted method should not be generated.

It is possible to customize the annotation used in the settings of the Java | Probable bugs | Constant conditions & exceptions inspection. The configuration dialog looks like: enter image description here Use the check mark button to mark the selected annotation as the one that should be used for code generation.

Upvotes: 18

lolo101
lolo101

Reputation: 439

With IntelliJ 2024

I could not find the option in the settings. However, I could find it on the method extraction popup

Extract method popup

Notice the subtle hint "Press Alt+Maj+O to show options popup"

The keys combination shows this short popup:

short options popup

In this popup it's possible to toggle annotation generation.

Ctrl+Alt+M shows the popup mentioned earlier:

full options popup

I was unable to find this option pane in the settings. I'm a little disappointed as JetBrains used to be quite good in UX

Upvotes: 1

Related Questions