instructions

Jsr-305 annotations have been added to the Spring-core JAR package for Spring 5. Has been used extensively in the Spring source code. The diagram below:

JSR – 305 is introduced

Static analysis tools such as FindBugs, IntelliJ, Checkstyle, and PMD are widely used in Java development. These tools are powerful, but they share some common problems that are hard to solve. In the design of the API, some decisions are unspoken, such as when a value can be null or when a numeric value cannot be negative. These design details are documented in the JavaDoc by a well-developed API, but analysis tools are unable to detect such details, which can be ignored or lead to incorrect detection results.

To address these issues, some developers of static analysis tools try to define the details using annotations. For example, FindBugs and IntelliJ both define their own annotations to indicate when a method returns null. However, the two tools use slightly different annotations, leading to the need for standardization. The JSR-305 standard, led by FindBugs creator Bill Pugh, sought to create a standard set of annotations for use by analysis tools, and wanted to allow developers to add additional annotations as needed. The current proposal includes a number of annotations to determine nulls, signs, development languages, threads, and so on.

For more information on JSR-305, see JSR-305: Comments for Checking Software Defects

Usage scenarios

From the above, we know the goal of JSR-305: to check for software defects. It is convenient for static code review tools to find potential bugs in time. So these annotations are particularly suitable for base components and toolkits, enhancing IDE tips and reducing potential bugs.

Lutool 1.x copied jSR-305 related annotations from Spring 5 into the source code, and mica-Spring Boot microservices development core package directly used annotations from Spring Core due to its dependency on Spring Boot 2.1.x.

use

Add package-level rules

  1. NonNullFields indicates that the Field is not null.

  2. NonNullApi indicates that method parameters and return values are not null.

  3. If you don’t want the package level to be null, you can use @nonNULL in the same way as @Nullable below.

Add package-info.java to the package as follows.

@NonNullApi
@NonNullFields
package net.dreamlu.mica.core.utils;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;
Copy the code

The editor will prompt you when writing code after adding this annotation, as shown below:

@Nullable

Partially Nullable fields, method parameters, and return values need to be labeled with @nullable.

The parameter can be null

public static boolean isBlank(@Nullable final CharSequence cs) {
    return! StringUtils.hasText(cs); }Copy the code

The return value can be NULL

@Nullable
public static String getCookieVal(HttpServletRequest request, String name) {
    Cookie cookie = getCookie(request, name);
    returncookie ! =null ? cookie.getValue() : null;
}
Copy the code

The property can be null

@Nullable
private String msg;
Copy the code

Open source is recommended

  • micaSpring Boot microservices core component set:Gitee.com/596392912/m…
  • AvueA magical framework based on vUE configurable:Gitee.com/smallweigit…
  • pigThe most powerful Microservice in the Universe (essential for architects) :gitee.com/log4j/pig
  • SpringBladeComplete online solution (necessary for enterprise development) :Gitee.com/smallc/Spri…
  • IJPayPayment SDK makes payment within reach:Gitee.com/javen205/IJ…

Pay attention to our

Scan the qr code above, more exciting content recommended every day!