• IDEA对jsr305的Nonnull注解和Guava的Beta注解的支持


    前言

    jsr305是一个规范,具体来说是一套用来给代码分析工具(如IDEA)检查代码缺陷用的注解,类似jsr303(Bean Validation规范)。今天在学习Spring源码时,发现其中使用到了jsr305中的注解。


    Spring中的NonNull注解中使用到了jsr305中的Nonnull注解,可以看做是对Nonnull注解的一个封装。

    IDEA对jsr305注解的支持

    引入jsr305和spring核心的maven依赖

    <dependency>
       <groupId>com.google.code.findbugs</groupId>
       <artifactId>jsr305</artifactId>
       <version>3.0.2</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>5.2.1.RELEASE</version>
    </dependency>
    

    测试代码

    import org.springframework.lang.NonNull;
    
    public class Client2 {
    
      @NonNull
      private static String test() {
        return null;
      }
    
    }
    

    这种情况IDEA就会有警告,提示方法返回结果可能为null

    原理

    那么IDEA是如何认识Spring的注解的呢,查找得知,IDEA应该不认识Spring的注解,但它认识jsr305的注解,而Spring注解NonNull中使用到了jsr305的注解javax.annotation.Nonnull类。

    /**
     * A common Spring annotation to declare that annotated elements cannot be {@code null}.
     *
     * <p>Leverages JSR-305 meta-annotations to indicate nullability in Java to common
     * tools with JSR-305 support and used by Kotlin to infer nullability of Spring API.
     *
     * <p>Should be used at parameter, return value, and field level. Method overrides should
     * repeat parent {@code @NonNull} annotations unless they behave differently.
     */
    @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Nonnull
    @TypeQualifierNickname
    public @interface NonNull {
    }
    

    可以看到,IDEA本身配置了一些关于代码分析的注解,其中就包括了jsr305的javax.annotation.Nonnull注解。

    自定义检查注解

    我们也可以配置我们自定义的注解。

    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface MyNonnull {
    }
    

    将自定义注解添加到配置中

    可以看到,自定义注解在代码中也是生效的。

    IDEA对Guava的Beta注解的支持

    引入Guava的maven依赖

    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>27.1-jre</version>
    </dependency>
    

    代码测试

    import com.google.common.io.ByteStreams;
    
    public class Client3 {
    
      public static void main(String[] args) {
        System.out.println(ByteStreams.nullOutputStream());
      }
    
    }
    

    IDEA会有代码警告,因为ByteStreams的nullOutputStream()方法被@Beta注解修饰。

    原理

    IDEA本身配置了一些关于不稳定API的注解,只要被这些注解修饰,IDEA就会有代码提示。

    总结

    上述两个功能只是IDEA的代码检查提供功能的很小一部分,更多关于IDEA的代码检查的信息,可以查看 Code inspections

    参考

    JSR-305:供检查软件缺陷用的注解
    IDEA Inspections详解
    Code inspections

  • 相关阅读:
    git push 报错:missing Change-Id in commit message footer
    script命令录屏
    dubbo.xsd
    常规项目用到的jar包之maven的pom.xml
    WebSocket Demo
    对程序员有帮助的站点集锦
    java之finally的用法
    Java 中的四种引用
    字符串类型的对象与引用及字符串常量池详解
    如何掌握一项新的技能?
  • 原文地址:https://www.cnblogs.com/strongmore/p/15302475.html
Copyright © 2020-2023  润新知