8
play

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 -


  1. 8 8 JDT embraces Type Annotations JDT embraces Type Annotations Java 8 ready Stephan Herrmann GK Software

  2. 8 Eclipse and Java ™ 8 ● Java 8 features supported: – JSR308 - Type Annotations. – JEP120 - Repeating Annotations. – JEP118 - Method Parameter Refmection. – JSR269 - Pluggable Annotation Processor API & javax.lang.model API enhancements for Java 8. – JSR 335 – Lambda Expressions ● Lambda expressions & method/constructor references ● Support for “code carrying” interface methods ● Enhanced target typing / new overload resolution & type inference Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 2

  3. 8 λ (JSR 335) Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 3

  4. 8 λ Tomorrow 17:00 to 18:00: (JSR 335) Grand Peninsula C JDT embraces lambda expressions ● Srikanth [IBM India] ● Noopur Gupta [IBM India] ● Stephan Herrmann Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 4

  5. 8 @ (JSR 308) Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 5

  6. 8 Annotations in more Places ● Java 5: annotate declarations – ElementType: packages, classes, fjelds, methods, locals … ● Java 8: annotate types – ElementType.TYPE_USE – ElementType.TYPE_PARAMETER So what? So what? Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 6

  7. 8 Why Care About Types? ● Dynamically typed languages dog1 = new Dog(); – anything goes dog1.bark(); dog2 = new Object(); – but may fail at runtime dog2.bark(); – e.g.: “method not understood” ● Type = Constraints on values To statically detect anomalies – missing capability – incompatible assignment – undeclared capability Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 7

  8. 8 Why Care About Types? ● Dynamically typed languages dog1 = new Dog(); – anything goes Annotate source code: dog1.bark(); Annotate source code: ● make assumptions explicit ● make assumptions explicit dog2 = new Object(); – but may fail at runtime dog2.bark(); ● convince the compiler that code is safe ● convince the compiler that code is safe – e.g.: “method not understood” ● Type = Constraints on values To statically detect anomalies – missing capability – incompatible assignment – undeclared capability Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 8

  9. 8 Why Care About Types? ● Constraint checking avoids errors – No Such Method / Field ● Basic statically typed OO – ClassCastException ● Generics – ??Exception ● SWTException("Invalid thread access") ● … ● NullPointerException Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 9

  10. 8 Let the Type System Handle Nullity ● Ideally: Java would force explicit choice – String defjnitely a String, never null – String? either a String or null – Type system ensures: no dereferencing of null ● Nullity as a language feature? – Heavy weight, incompatible change – Language change for each new constraint? Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 10

  11. 8 Pluggable Type System ● Make it easier to add new constraints – Only one new syntax for all kinds of constraints ● Make it easier to add new type checkers – Checker Framework (Michael Ernst – U of Washington) ● Examples – @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 # 11

  12. 8 Can't Java 5 Do All This? @Target(ElementType. PARAMETER ) @Target(ElementType. TYPE_USE ) @interface NonNull5 {} @interface NonNull8 {} void java5(@NonNull5 String arg); void java8(@NonNull8 String arg); arg is qualifjed to be non-null String is qualifjed to be non-null @Target(ElementType. METHOD ) @Target(ElementType. TYPE_USE ) @interface NonNull5 {} @interface NonNull8 {} @NonNull5 String java5(); @NonNull8 String java8(); java5 is qualifjed to be non-null String is qualifjed to be non-null ● We've been lying about the method result – but we can't lie about everything, e.g.: void letemBark(@NonNull List<Dog> dogs) { dogs.get(0).bark(); } NPE? Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 12

  13. 8 Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1 cannot contain l1.add("Hello"); null elements for (String elem : l1) System. out .println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain l2.add( null ); null elements for (String unknown : l2) if (unknown != null ) System. out .println(unknown.toUpperCase()); } void bad(List<String> unknown, List<@Nullable String> withNulls) { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1.add( null ); String first = l1.get(0); Null type mismatch: required '@NonNull String' Null type mismatch: required '@NonNull String' if (first == null ) return ; but the provided value is null but the provided value is null l1 = unknown; l1 = withNulls; String canNull = withNulls.get(0); System. out .println(canNull.toUpperCase()); } Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 13

  14. 8 Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1 cannot contain l1.add("Hello"); null elements for (String elem : l1) System. out .println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain l2.add( null ); null elements for (String unknown : l2) if (unknown != null ) System. out .println(unknown.toUpperCase()); } 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 ; Null comparison always yields false: Null comparison always yields false: l1 = unknown; The variable fjrst cannot be null at this location The variable fjrst cannot be null at this location l1 = withNulls; String canNull = withNulls.get(0); System. out .println(canNull.toUpperCase()); } Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 14

  15. 8 Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1 cannot contain l1.add("Hello"); null elements for (String elem : l1) System. out .println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain l2.add( null ); null elements for (String unknown : l2) if (unknown != null ) System. out .println(unknown.toUpperCase()); } 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; Null type safety (type annotations): The expression of type 'List<String>' Null type safety (type annotations): The expression of type 'List<String>' needs unchecked conversion to conform to '@NonNull List<@NonNull String>' needs unchecked conversion to conform to '@NonNull List<@NonNull String>' String canNull = withNulls.get(0); System. out .println(canNull.toUpperCase()); } Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 15

  16. 8 Annotated Generics void good() { @NonNull List<@NonNull String> l1 = new ArrayList<>(); l1 cannot contain l1.add("Hello"); null elements for (String elem : l1) System. out .println(elem.toUpperCase()); @NonNull List<@Nullable String> l2 = new ArrayList<>(); @NonNull List<@Nullable String> l2 = new ArrayList<>(); l2 can contain l2.add( null ); null elements for (String unknown : l2) if (unknown != null ) System. out .println(unknown.toUpperCase()); } 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; Null type mismatch (type annotations): Null type mismatch (type annotations): String canNull = withNulls.get(0); required '@NonNull List<@NonNull String>' required '@NonNull List<@NonNull String>' System. out .println(canNull.toUpperCase()); but this expression has type 'List<@Nullable String>' } but this expression has type 'List<@Nullable String>' Stephan Herrmann: JDT Embraces Type Annotations - EclipseCon North America 2014 # 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend