Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Annotate an Array NonNull?

I'm using org.eclipse.jdt.annotation.NonNull to add extra information for static null analysis. I donn't know how to annotate arrays correctly:

  1. How can I say that an array reference is non-null?
  2. How can I say that an array consists of non-null elements?

I've tested:

    public static void test(@NonNull String[] a) {
        assert a != null;
    }

    public static void main(String[] args) {
        test(null);
    }

However, Eclipse doesn't mark test(null); as wrong.

like image 207
Vertex Avatar asked Mar 12 '15 14:03

Vertex


1 Answers

If you 're using Java 8, it looks as follows:

@NonNull Object [] o1;

o1    = null;           // OK
o1    = new Object[1];
o1[0] = null;           // NOT OK

Object @NonNull[] o2;

o2    = null;           // NOT OK
o2    = new Object[1];
o2[0] = null;           // OK
like image 197
René Winkler Avatar answered Sep 27 '22 15:09

René Winkler