Can you comment that the return value is NotNull?

Asked 2 years ago, Updated 2 years ago, 136 views

We would like to ensure that the method returning List does not return null. For implementation, I only return Collections.emptyList() instead of null, but is it possible to express in an annotation that I do not return null?

It would be better if there was some connection with IDE, but I didn't know it from my perspective.

java annotations

2022-09-29 21:50

2 Answers

JSR-305 is best deployed when you want to do an annotation that ensures that null is not returned. (Let me call this comment "@NonNull series" here.)

JSR-305:deployment and description

The deployment should be add jsr305-X.X.jar as an external jar to the class path, or Maven

.

<dependency>
    <groupId><code class="findbugs">/code></groupId>
    <artifactId>jsr305</artifactId>
    <version>3.0.0</version>
</dependency>

In addition, to indicate that the return value of the method is not null, add the annotation on the source code as follows:

public@Nonnull static String fold (Iterator<?>iterator) {
   ...
   return nonNullString;    
}

@Nonnull 
public static String join (Iterator<?>iterator) {
   ...
   return nonNullString;    
}

@Nonnull Annotation Ecology

Annotations of the @Nonnull series are not included with the JDK and are more like part of JavaEE.It is not clear which one is standard, and besides JSR-305, @Nonnull series announcements exist in various packages.

  • JSR-305:javax.annotation.Nonnull
  • JSR-303:javax.validation.constraints.NotNull
  • IDEA:org.jetbrains.annotations.NotNull
  • Eclipse:org.eclipse.jdt.announcement.NonNull
  • Android:android.support.annotation.NonNull
  • CheckerFramework: org.checkerframework.checker.nullness.qual.NonNull

All of these things have the same annotation purpose and have different names.Depending on your project configuration, you will be using one of the above classes.

IDE:IntelliJ IDEA Support

InteliJ IDEA's null check support is probably the oldest and most advanced of all IDEs, so if you want advanced null checks in your development environment, you can't miss the IDEA.IDEA also has its own @NotNull annotation for historical reasons. The IDE supports a little more features than javax.announcement.Nonnull, so if you want to use IDEA primarily, it is the ant that selects org.jetbrains.announctions.NotNull for the annotation.

import org.jetbrains.notations.NotNull;
...
public@NotNull String fold (Iterator<?>iterator) {
   ...
   return it;   
}

In addition, IDEA provides a convenient feature that automatically adds @Nullable, @NotNull annotations by selecting Analyze>Infer Nullity from the menu.

IDE:support for Eclipse

Eclipse recognizes basic, javax and Eclipse-specific annotations.IDE will react by changing the compiler alert/error setting.Functionally, it is easy to deploy other @Nonnull-related annotations with minimal support.You should deploy FindBugs if you want more in-depth warnings.

What's interesting about Eclipse is that it supports an annotation called @NonNullByDefault, which allows packages to have @NonNull as the default.It's so powerful that I think it's hard to get the number of warnings at first, but if you want thorough management, you can use it.

@NonNullByDefault is attached to the package declaration in the package comment file package-info.java as follows:(*This is not the source code with the class written on it!)

/**
* @authorXXXXX
 */
@NonNullByDefault
package jp.domain;
import org.eclipse.jdt.annotation.NonNullByDefault;

IDE:supported by Netbeans

Netbeans can recognize @Nonnull-related annotations and generate alerts through source code parsing.In the Configuration Dialog Editor>Hint, Null Pointer Reference is the warning.

If there is a shortage, set up Firebugs according to the documentation to create a sufficient static analysis environment.

For older projects

This is not an IDE, but you can also write like this using the Checker Framework framework. (There is also Eclipse Plugin)

public/*@NonNull*/String fold (Iterator<?>iterator) {
   ...
   return it;   
}

Some people may be happy to be able to quietly introduce them into old projects that seem to stop time in JDK 1.5.I like this way of writing.


2022-09-29 21:50

You can add a library of JSR-305 to your project and use an annotation with @javax.announcement.Nonnull as follows:

@Nonnull
publicList<?>getList(){
    // ...
}

The JSR-305 library can be downloaded from the Maven central repository.

Recent versions of Netbeans, Eclipse, and IntelliJ support JSR-305 annotations and issue violation warnings.


2022-09-29 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.