为 IntelliJ 中的所有参数添加 @NotNull 注解

Add @NotNull annotation to all the arguments in IntelliJ

对于具有多个参数的方法,例如这个构造函数:

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

…在 IntelliJ 2019.2 中编辑我的源代码时,有没有办法用 @NotNull 注释标记所有这些参数?重复粘贴似乎很愚蠢。

你试过了吗@ParametersAreNonnullByDefault as explained here.

package-info.java 文件中 package 级别的用法示例。

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

import javax.annotation.ParametersAreNonnullByDefault;

要使用此注释,请添加 jsr305 库,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>

您可以在包级别使用注释。为此,您必须创建 package-info.java 文件:

@NonnullByDefault
package package.name;

并将注释定义为:

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 { }