Search code examples
javaintellij-ideaannotationscopy-paste

Add @NotNull annotation to all the arguments in IntelliJ


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.


Solution

  • 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>