1

For a method with several arguments, such as this constructor:

    public Chron ( UUID id , UUID trackId , Instant whenCreated , Instant whenLastModified , Instant start , Instant stop , String summary , String notes )

…is there a way to mark all of those parameters with the @NotNull annotation while editing my source code in IntelliJ 2019.2? Repeated pasting seems silly.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • is @NonNull from lombok working for you? – Nghia Do Sep 05 '19 at 07:55
  • @NghiaDo I am using the [org.jetbrains annotations](https://www.jetbrains.com/help/idea/nullable-and-notnull-annotations.html?keymap=primary_default_for_macos) – Basil Bourque Sep 05 '19 at 08:11
  • There is no tool to make all parameters of the current method annotated with NotNull, but in case you want parameters of all the methods to be annotated with NotNull, you can define it on package level with TypeQualifierDefault(ElementType.PARAMETER) – Olga Klisho Sep 05 '19 at 12:11
  • @OlgaKlisho If you make an Answer of your Comment, I will accept it. And please give an example of your package-level suggestion. – Basil Bourque Sep 05 '19 at 16:51

2 Answers2

2

Have you tried @ParametersAreNonnullByDefault as explained here.

enter image description here

Example usage at package level in package-info.java file.

@ParametersAreNonnullByDefault
package com.example.acme.backend.data;

import javax.annotation.ParametersAreNonnullByDefault;

To use this annotation, add the jsr305 library, part of the Google Code FindBugs project. See another Question, What is the status of JSR 305?.

    <!-- https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 -->
    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>3.0.2</version>
    </dependency>
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Where does that annotation come from? Your linked page says “ add the jsr305 library ”, but has no link. Is it [*FindBugs JSR305*](https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305)? – Basil Bourque Sep 06 '19 at 06:14
  • I am trying to take advantage of the IntelliJ feature to modify the code during compilation to add runtime null-checking code. IntelliJ > Preferences > Build, Execution, Deployment > Compiler > Add runtime assertions for notnull-annotated methods and parameters. Not sure in that `Configure annotations…` dialog which of those dozen kinds of NotNull annotations, if any, refer to this `@ParametersAreNonnullByDefault`. – Basil Bourque Sep 06 '19 at 06:19
  • afaik these are referred at compile time. Which library/tool processes it at runtime? – jmj Sep 06 '19 at 21:35
  • IntelliJ itself auto-generates the code (or modifies the byte code, not sure which). No other library involved. See screenshot in [my other Question](https://stackoverflow.com/q/57828113/642706) which asks if your suggestion here can be made to work this IntelliJ feature. – Basil Bourque Sep 06 '19 at 22:13
  • 1
    Update: Regarding my desire to use this package-wide annotation suggested in this Answer with IntelliJ's "Add runtime assertions for not-null methods and parameters" feature to generate runtime null-checks: Ticket # [IDEA-164347](https://youtrack.jetbrains.com/issue/IDEA-164347) is an open feature-request for such support. – Basil Bourque Sep 09 '19 at 18:22
  • Great thanks for updating it back. It is nice feature to have @Basil – jmj Sep 09 '19 at 18:23
1

You may use annotation at package level. For this you have to create package-info.java file with:

@NonnullByDefault
package package.name;

and define annotation as:

import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Documented
@Nonnull
TypeQualifierDefault(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface NonnullByDefault { }
Olga Klisho
  • 1,206
  • 5
  • 7
  • What library is that annotation from? – Basil Bourque Sep 09 '19 at 18:24
  • You can use "ParametersAreNonnullByDefault" from https://static.javadoc.io/com.google.code.findbugs/jsr305/3.0.1/javax/annotation/ParametersAreNonnullByDefault.html or create your own defining the "TypeQualifierDefault" that suits you. – Olga Klisho Sep 10 '19 at 12:12