798
223















|
  • 171
  • 3
  • 5
  • 17
  • 3

20

90
+50

public @interface NonNull {}

public @interface Nullable {}

package android.support.annotation;
@Retention(CLASS)
@Target({FIELD, METHOD, PARAMETER})
public @interface NonNull {}

package edu.umd.cs.findbugs.annotations;
@Retention(CLASS)
@Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE})
public @interface NonNull {}

package org.eclipse.jdt.annotation;
@Retention(CLASS)
@Target({ TYPE_USE })
public @interface NonNull {}

package org.jetbrains.annotations;
@Retention(CLASS)
@Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE})
public @interface NotNull {String value() default "";}

package javax.annotation;
@TypeQualifier
@Retention(RUNTIME)
public @interface Nonnull {
    When when() default When.ALWAYS;
    static class Checker implements TypeQualifierValidator<Nonnull> {
        public When forConstantValue(Nonnull qualifierqualifierArgument,
                Object value) {
            if (value == null)
                return When.NEVER;
            return When.ALWAYS;
        }
    }
}

package org.checkerframework.checker.nullness.qual;
@Retention(RUNTIME)
@Target({TYPE_USE, TYPE_PARAMETER})
@SubtypeOf(MonotonicNonNull.class)
@ImplicitFor(
    types = {
        TypeKind.PACKAGE,
        TypeKind.INT,
        TypeKind.BOOLEAN,
        TypeKind.CHAR,
        TypeKind.DOUBLE,
        TypeKind.FLOAT,
        TypeKind.LONG,
        TypeKind.SHORT,
        TypeKind.BYTE
    },
    literals = {LiteralKind.STRING}
)
@DefaultQualifierInHierarchy
@DefaultFor({TypeUseLocation.EXCEPTION_PARAMETER})
@DefaultInUncheckedCodeFor({TypeUseLocation.PARAMETER, TypeUseLocation.LOWER_BOUND})
public @interface NonNull {}

package android.support.annotation;
@Retention(CLASS)
@Target({METHOD, PARAMETER, FIELD})
public @interface Nullable {}

package edu.umd.cs.findbugs.annotations;
@Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE})
@Retention(CLASS)
public @interface Nullable {}

package org.eclipse.jdt.annotation;
@Retention(CLASS)
@Target({ TYPE_USE })
public @interface Nullable {}

package org.jetbrains.annotations;
@Retention(CLASS)
@Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE})
public @interface Nullable {String value() default "";}

package javax.annotation;
@TypeQualifierNickname
@Nonnull(when = When.UNKNOWN)
@Retention(RUNTIME)
public @interface Nullable {}

package org.checkerframework.checker.nullness.qual;
@Retention(RUNTIME)
@Target({TYPE_USE, TYPE_PARAMETER})
@SubtypeOf({})
@ImplicitFor(
    literals = {LiteralKind.NULL},
    typeNames = {java.lang.Void.class}
)
@DefaultInUncheckedCodeFor({TypeUseLocation.RETURN, TypeUseLocation.UPPER_BOUND})
public @interface Nullable {}

package lombok;
@Retention(CLASS)
@Target({FIELD, METHOD, PARAMETER, LOCAL_VARIABLE})
public @interface NonNull {}

package javax.validation.constraints;
@Retention(RUNTIME)
@Target({ FIELD, METHOD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Constraint(validatedBy = {})
public @interface NotNull {
    String message() default "{javax.validation.constraints.NotNull.message}";
    Class<?>[] groups() default { };
    Class<? extends Payload>[] payload() default {};
    @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
    @Retention(RUNTIME)
    @Documented
    @interface List {
        NotNull[] value();
    }
}


|
  • 3
  • 5
  • 4
  • 1
83

|
  • 3
  • 13
  • 1
  • 1
  • 1
51

|
  • 58
  • 28
  • 5
  • 1
  • 3
32


|
  • 4
  • 2
25

|
  • 1
  • 2
17

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;


    /**
     * This annotation can be applied to a package, class or method to indicate that the class fields,
     * method return types and parameters in that element are not null by default unless there is: <ul>
     * <li>An explicit nullness annotation <li>The method overrides a method in a superclass (in which
     * case the annotation of the corresponding parameter in the superclass applies) <li> there is a
     * default parameter annotation applied to a more tightly nested element. </ul>
     * <p/>
     * @see /a/9256595/14731
     */
    @Documented
    @Nonnull
    @TypeQualifierDefault(
    {
        ElementType.ANNOTATION_TYPE,
        ElementType.CONSTRUCTOR,
        ElementType.FIELD,
        ElementType.LOCAL_VARIABLE,
        ElementType.METHOD,
        ElementType.PACKAGE,
        ElementType.PARAMETER,
        ElementType.TYPE
    })
    @Retention(RetentionPolicy.RUNTIME)
    public @interface NotNullByDefault
    {
    }

|
  • 2
17

|
12

|
11

|
    7

    310

    10

    |
      7

      import android.support.annotation.NonNull;
      
      void foobar(@NonNull Foo bar) {}
      
      |
        6

        // file: package-info.java
        @javax.annotation.ParametersAreNonnullByDefault
        package example;
        
        
        // file: PublicApi
        package example;
        
        public class PublicApi {
        
            /**
             * @param firstname MUST NOT be null
             * @param lastname MUST NOT be null
             */
            public Person createPerson(
                    // Spotbugs ignores the param annotations, but IDEs will show problems
                    @Nullable String firstname, // Users  might send null
                    @Nullable String lastname // Users might send null
                    ) {
                if (firstname == null) throw new IllagalArgumentException(...);
                if (lastname == null) throw new IllagalArgumentException(...);
                return doCreatePerson(fistname, lastname, nickname);
            }
        
            @NonNull // Spotbugs checks that method cannot return null
            private Person doCreatePerson(
                     String firstname, // Spotbugs checks null cannot be passed, because package has ParametersAreNonnullByDefault
                     String lastname,
                     @Nullable String nickname // tell Spotbugs null is ok
                     ) {
                 return new Person(firstname, lastname, nickname);
            }
        
            @CheckForNull // Do not use @Nullable here, Spotbugs will ignore it, though IDEs respect it
            private Person getNickname(
                 String firstname,
                 String lastname) {
                 return NICKNAMES.get(firstname + ':' + lastname);
            }
        }
        

        |
          5

          import java.lang.annotation.*;
          
          /**
           * Designates that a field, return value, argument, or variable is
           * guaranteed to be non-null.
           */
          @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
          @Documented
          @Retention(RetentionPolicy.CLASS)
          public @interface NotNull {}
          
          /**
           * Designates that a field, return value, argument, or variable may be null.
           */
          @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
          @Documented
          @Retention(RetentionPolicy.CLASS)
          public @interface Nullable {}
          

          |
            4

            |
            • 3
            • 1
            3

            import static java.util.Objects.requireNonNull;
            
            public class Role {
            
              private final UUID guid;
              private final String domain;
              private final String name;
              private final Optional<String> description;
            
              public Role(UUID guid, String domain, String name, Optional<String> description) {
                this.guid = requireNonNull(guid);
                this.domain = requireNonNull(domain);
                this.name = requireNonNull(name);
                this.description = requireNonNull(description);
              }
            

            |
            • 1
            • 1
            • 1
            3

            @java.lang.annotation.Documented
            @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS)
            @java.lang.annotation.Target({java.lang.annotation.ElementType.FIELD,
                                          java.lang.annotation.ElementType.METHOD,    
                                          java.lang.annotation.ElementType.PARAMETER,
                                          java.lang.annotation.ElementType.LOCAL_VARIABLE})
            public @interface Nullable 
            {
            }
            

            |
            • 1
              void test(@NonNull String s) {}
            • 3
            2


            |
            • 9
            • 3
            • 1
            2

            1.   @Configuration
                @ValidationConfig
                public class ValidationConfig implements MyService {
              
                      @Bean
                      public MethodValidationPostProcessor providePostProcessor() {
                            return new MethodValidationPostProcessor()
                      }
                }
              

            @Service
            @Validated
            public class MyServiceImpl implements MyService {
            
              @Override
              public Something doSomething(@NotNull String myParameter) {
                    // No need to do something like assert myParameter != null  
              }
            }
            

            |
              1

              |
                1

                |