8
8
Stephan Herrmann GK Software
Java 8 ready
8 JDT embraces Type Annotations JDT embraces Type Annotations Java - - PowerPoint PPT Presentation
8 8 JDT embraces Type Annotations JDT embraces Type Annotations Java 8 ready Stephan Herrmann GK Software 8 Eclipse and Java 8 Java 8 features supported: JSR308 - Type Annotations. JEP120 - Repeating Annotations. JEP118 -
Java 8 ready
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 2
– JSR308 - Type Annotations. – JEP120 - Repeating Annotations. – JEP118 - Method Parameter Refmection. – JSR269 - Pluggable Annotation Processor API &
– JSR 335 – Lambda Expressions
inference
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 3
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 4
Tomorrow 17:00 to 18:00: Grand Peninsula C
JDT embraces lambda expressions
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 5
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 6
– ElementType: packages, classes, fjelds, methods, locals …
– ElementType.TYPE_USE – ElementType.TYPE_PARAMETER
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 7
– anything goes – but may fail at runtime – e.g.: “method not understood”
– missing capability – incompatible assignment – undeclared capability dog1 = new Dog(); dog1.bark(); dog2 = new Object(); dog2.bark();
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 8
– anything goes – but may fail at runtime – e.g.: “method not understood”
– missing capability – incompatible assignment – undeclared capability dog1 = new Dog(); dog1.bark(); dog2 = new Object(); dog2.bark();
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 9
– No Such Method / Field
– ClassCastException
– ??Exception
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 10
– String
– String?
– Type system ensures: no dereferencing of null
– Heavy weight, incompatible change – Language change for each new constraint?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 11
– Only one new syntax for all kinds of constraints
– Checker Framework (Michael Ernst – U of Washington)
– @NonNull – @Interned
equals(== , equals)
– @Immutable
value cannot change (Java 5 ?)
– @ReadOnly
value cannot change via this reference
– @UI
code requires to run on the SWT UI thread
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 12
@Target(ElementType.TYPE_USE) @interface NonNull8 {} @NonNull8 String java8();
– but we can't lie about everything, e.g.:
@Target(ElementType.PARAMETER) @interface NonNull5 {} void java5(@NonNull5 String arg);
arg is qualifjed to be non-null
@Target(ElementType.TYPE_USE) @interface NonNull8 {} void java8(@NonNull8 String arg);
String is qualifjed to be non-null
@Target(ElementType.METHOD) @interface NonNull5 {} @NonNull5 String java5(); void letemBark(@NonNull List<Dog> dogs) { dogs.get(0).bark(); }
NPE?
String is qualifjed to be non-null java5 is qualifjed to be non-null
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 13
void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>();
l2 can contain null elements l1 cannot contain null elements
void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); }
Null type mismatch: required '@NonNull String' but the provided value is null Null type mismatch: required '@NonNull String' but the provided value is null
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 14
void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>();
l2 can contain null elements l1 cannot contain null elements
void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); }
Null comparison always yields false: The variable fjrst cannot be null at this location Null comparison always yields false: The variable fjrst cannot be null at this location
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 15
void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>();
l2 can contain null elements l1 cannot contain null elements
void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); }
Null type safety (type annotations): The expression of type 'List<String>' needs unchecked conversion to conform to '@NonNull List<@NonNull String>' Null type safety (type annotations): The expression of type 'List<String>' needs unchecked conversion to conform to '@NonNull List<@NonNull String>'
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 16
void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>();
l2 can contain null elements l1 cannot contain null elements
void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); }
Null type mismatch (type annotations): required '@NonNull List<@NonNull String>' but this expression has type 'List<@Nullable String>' Null type mismatch (type annotations): required '@NonNull List<@NonNull String>' but this expression has type 'List<@Nullable String>'
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 17
void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add(null); String first = l1.get(0); if (first == null) return; l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System.out.println(canNull.toUpperCase()); } @NonNull List<@Nullable String> l2 = new ArrayList<>();
l2 can contain null elements l1 cannot contain null elements
void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add("Hello"); for (String elem : l1) System.out.println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2.add(null); for (String unknown : l2) if (unknown != null) System.out.println(unknown.toUpperCase()); }
Potential null pointer access: The variable canNull may be null at this location Potential null pointer access: The variable canNull may be null at this location
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 18
– Type parameter, type variable, type argument, type bound – Covariance, contravariance, invariance
– Always @NonNull, always @Nullable?
– Should clients be able to choose?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 19
– Unconstrained type parameter:
– Free to choose the type argument:
– No knowledge about type variable T – Must assume the worst
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 20
– constrain the type
– constrain the nullness
– client can provide same or more specifjc type
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 21
– abstract O apply (@Nullable I arg);
– can pass null
– must accept null – cannot override @Nullable
– could override @NonNull
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 22
– will code become unreadable due to null annotations?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 23
void test (@NonNull String [] stringsOrNulls) { System.out.println(stringsOrNulls[0]); } void test (@NonNull String [] stringsOrNulls) { System.out.println(stringsOrNulls[0]); }
void test (String @NonNull [] stringsOrNulls) { System.out.println(stringsOrNulls[0]); }
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 24
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 25
– Statically check that
– Is the approach safe?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 26
– Can we statically check that
– Is the approach safe?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 27
– To statically check that
– Is the approach safe? – Is it practical?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 28
– To statically check that
– Is the approach safe? – Is it practical?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 29
– To statically check that
– Is the approach safe? – Is it practical?
– Does it need JSR 308?
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 30
– OK to use ==
– Find/replace == comparisons for T <: TypeBinding – Tweak our compiler report afgected locations – Plan: publish the tweak, controlled by @Uninterned
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 31
– org.eclipse.jdt.annotation_2.0.0
– Nullness is an integral part of the type system – Fine tuned defaults only in Luna – TODO: Strict checking against type variables
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 32
– Detect accidental comparison using == or !=
– by [Colin S. Gordon, Werner Dietl, Michael D. Ernst, and Dan
Grossman]
– SWT: bye, bye, “Invalid thread access”
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 33
– OK: completion, refactoring, etc. – TODO: show in hover – TODO: update / add more quickfjxes
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 34
Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 35
Java 8 ready