Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a default, class-level annotation that is NOT deprecated that specifies non-null return values by default

Google is failing me. There used to be this annotation: ReturnValuesAreNonnullByDefault.

But this is now deprecated and the javadoc does not indicate which new annotation to use. @Nonnull on the whole class does not apply to return values because I just tested that out and I get no warning for a method returning null. I don't want to have to specifically annotate every single return value, so is there a good option out there?

like image 304
Selena Avatar asked Feb 03 '15 17:02

Selena


People also ask

What is the use of @nullable annotation?

On the other hand, if the parameter is mark as @Nullable and we don't add null check inside the function, Android Studio will warn you with lint error and some visual hint. The @NonNull/@Nullable annotation can put in front of functions as well to indicate the return value nullability.

What is @NonNull annotation in Java?

@NotNull The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.

What are Annotations used for?

Annotations have a number of uses, among them: Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings. Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.

What does Java Annotations do?

Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program.


1 Answers

You can use this answer to build your own simple @EverythingIsNonnullByDefault annotation to apply at the package/class level to cover all cases, or this one which shows you how to create separate annotations to govern fields and method return values. We opted to use them all but tend to apply the "everything" version at the package level.

If you're in a real hurry, copy-n-paste the deprecated annotation and remove the deprecation.

package com.sample;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.annotation.meta.TypeQualifierDefault;

/**
 * This annotation can be applied to a package or class to indicate that the 
 * classes' methods in that element all return nonnull values by default
 * unless there is
 * <ul>
 *   <li>an explicit nullness annotation
 *   <li>a default method annotation applied to a more tightly nested element.
 * </ul>
 */
@Documented
@Nonnull
@TypeQualifierDefault(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReturnValuesAreNonnullByDefault {
    // feel free to name it MethodsAreNonnullByDefault; I find that confusing
}
like image 158
David Harkness Avatar answered Sep 29 '22 10:09

David Harkness