1

How to use @NonNull annotations on List items.

let consider, if i want to force A non-null list of Strings

This is how we can declare that : @NonNull List<String>

what if we want to force, A list of non-null Strings.

How we can do that ?

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104

1 Answers1

6

@NonNull Annotation are from the The Checker Framework.

The Framework provides a number of Annotations that could benefit developers to write clean code.

@NonNull Annotation – The compiler can determine cases where a code path might receive a null value.

Java 8 Annotation Improvements :

Prior to java 8 , annotations were only allowed on definitions.

Java SE 8 allows type annotations anywhere that a type is used.

A list of non-null Strings can be difine as List<@NonNull String>

Reference : Section 2.1

Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
  • +1, you can find more detailed answer from [here](https://stackoverflow.com/questions/45312946/annotate-type-parameter-in-java-for-kotlin-compiler/45313284#45313284). – holi-java Jan 31 '18 at 12:43
  • @holi-java - Yes, we can use that also.+1 to that answer – Niraj Sonawane Jan 31 '18 at 12:46
  • "`@NonNull` Annotation are from the Checker Framework" - maybe, but how do you know, there's [plenty of them](https://stackoverflow.com/questions/35892063/which-nonnull-java-annotation-to-use), relevant for this question is only that you choose a variety that is designed for `TYPE_USE`. – Stephan Herrmann Feb 02 '18 at 23:04