Using null type annotations in practice
Till Brychcy, Mercateo
EclipseCon Europe, 2017
Using null type annotations in practice Till Brychcy, Mercateo - - PowerPoint PPT Presentation
Using null type annotations in practice Till Brychcy, Mercateo EclipseCon Europe, 2017 What they are, why and when to use them @Nullable vs. java.util.Optional Configuration choices Switching from declaration annotations to type
Till Brychcy, Mercateo
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
/** * * @param catalogID * @param groupID * @param searchSpec * (may be null) * @param minIndex * @param maxIndex * @param sortBy * @param sortAscending * @return String */
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
@Target({ FIELD, METHOD, PARAMETER, LOCAL_VARIABLE }) public @interface Nullable { }
@Target({ TYPE_USE }) public @interface Nullable { }
@Target({ TYPE_USE, FIELD, METHOD, PARAMETER, LOCAL_VARIABLE }) public @interface Nullable { }
EclipseCon Europe, 2017
@NonNullByDefault public class Example { @Nullable String field; @Nullable String add(@Nullable String arg1, List<String> list, @Nullable String[] array) { // just to show the syntax (local variables usually don't need annotations) @Nullable String local = arg1; list.add(null); // is this OK? if(array != null) { array[0] = null; // or this? } return local; } }
EclipseCon Europe, 2017
@NonNullByDefault({ FIELD, PARAMETER, RETURN_TYPE }) public class Example { @Nullable String field; @Nullable String add(@Nullable String arg1, List<String> list, String @Nullable [] array) { // just to show the syntax (local variables usually don't need annotations) @Nullable String local = arg1; list.add(null); // is this OK? if(array != null) { array[0] = null; // or this? } return local; } }
EclipseCon Europe, 2017
@NonNullByDefault({ FIELD, PARAMETER, RETURN_TYPE, ARRAY_CONTENTS, TYPE_ARGUMENT }) public class Example { @Nullable String field; @Nullable String add(@Nullable String arg1, List<@Nullable String> list, @Nullable String @Nullable [] array) { // just to show the syntax (local variables usually don't need annotations) @Nullable String local = arg1; list.add(null); // OK! if (array != null) { array[0] = null; // OK! } return local; } }
EclipseCon Europe, 2017
public enum DefaultLocation { PARAMETER, RETURN_TYPE, FIELD, TYPE_PARAMETER, TYPE_BOUND, TYPE_ARGUMENT, ARRAY_CONTENTS } @Target({ ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.LOCAL_VARIABLE }) public @interface NonNullByDefault { DefaultLocation[] value() default { DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE, DefaultLocation.FIELD, DefaultLocation.TYPE_ARGUMENT, DefaultLocation.TYPE_BOUND }; }
EclipseCon Europe, 2017
Object x = ""; // OK
String s = new Object(); // error @Nullable String s1 = ""; // OK @NonNull String s2 = null; // error
⇒"String extends Object" corresponds to "@NonNull String extends @Nullable String"
"@NonNull String extends @NonNull Object" "@NonNull String extends @Nullable Object" "@Nullable String extends @Nullable Object"
EclipseCon Europe, 2017
With standard @NonNullByDefault:
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
<T extends @Nullable String> @NonNull String concat(T[] strings)
EclipseCon Europe, 2017
EclipseCon Europe, 2017
boolean isValid(Some x) {return x != null && …}
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
EclipseCon Europe, 2017
return type)
variable
EclipseCon Europe, 2017
(TypeQualifierDefault)