create your own type system in 1 hour
play

Create your own type system in 1 hour Michael Ernst University of - PowerPoint PPT Presentation

Create your own type system in 1 hour Michael Ernst University of Washington http://CheckerFramework.org/ Motivation java.lang.NullPointerException java.lang.NullPointerException Java's type system is too weak Type checking prevents many


  1. Create your own type system in 1 hour Michael Ernst University of Washington http://CheckerFramework.org/

  2. Motivation java.lang.NullPointerException java.lang.NullPointerException

  3. Java's type system is too weak Type checking prevents many errors int i = "hello"; Type checking doesn't prevent enough errors System.console().readLine();

  4. Java's type system is too weak Type checking prevents many errors int i = "hello"; Type checking doesn't prevent enough errors NullPointerException System.console().readLine();

  5. Prevent null pointer exceptions Java 8 introduces the Optional<T> type ● Wrapper; content may be present or absent ● Constructor: of(T value) ● Methods: boolean isPresent() , T get() Optional<String> maidenName;

  6. Optional reminds you to check Without Optional: With Optional: possible possible NullPointerException NoSuchElementException String mName; Optional<String> omName; mName.equals(...); omName.get().equals(...); if (mName != null) { if (omName.isPresent()) { mName.equals(...); omName.get().equals(...); } } possible NullPointerException Complex rules for using Optional correctly!

  7. Other guidelines from: Stephen Colebourne, Edwin How not to use Optional Dalorzo, Vasco Ferreira C., Brian Goetz, Daniel Olszewski, Nicolai Parlog, Oleg Shelajev, ... Stuart Marks’s rules: Never, ever, use null for an Optional variable or return value. 1. 2. Never use Optional.get() unless you can prove that the Optional is present. 3. Prefer alternative APIs over Optional.isPresent() and Optional.get(). It’s generally a bad idea to create an Optional for the specific purpose of 4. Let’s enforce the chaining methods from it to get a value. rules with a tool. 5. If an Optional chain has a nested Optional chain, or has an intermediate result of Optional, it’s probably too complex. Avoid using Optional in fields, method parameters, and collections. 6. Don’t use an Optional to wrap any collection type (List, Set, Map). Instead, 7. use an empty collection to represent the absence of values.

  8. Which rules to enforce with a tool Stuart Marks’s rules: Never , ever, use null for an Optional variable or return value. 1. 2. Never use Optional.get() unless you can prove that the Optional is present. 3. Prefer alternative APIs over Optional.isPresent() and Optional.get(). It’s generally a bad idea to create an Optional for the specific purpose of 4. These are chaining methods from it to get a value. type system properties. 5. If an Optional chain has a nested Optional chain, or has an intermediate result of Optional, it’s probably too complex . Avoid using Optional in fields, method parameters, and collections. 6. Don’t use an Optional to wrap any collection type (List, Set, Map). Instead, 7. use an empty collection to represent the absence of values.

  9. Define a type system

  10. Define a type system 1. Type hierarchy (subtyping) 2. Type rules (what operations are illegal) 3. Type introduction (what types for literals, … ) 4. Dataflow (run-time tests) We will define two type systems: nullness and Optional

  11. Define a type system 1. Type hierarchy (subtyping) 2. Type rules (what operations are illegal) 3. Type introduction (what types for literals, … ) 4. Dataflow (run-time tests)

  12. 1. Type hierarchy Animal Object Reptile Mammal List Number Giraffe Human Integer Float 2 pieces of information: ● the types ● their relationships

  13. Type hierarchy for nullness @Nullable @NonNull 2 pieces of information: ● the types ● their relationships

  14. Type hierarchy for Optional “Never use Optional.get() unless you can prove that the Optional is present.” @Maybe @Nullable Present @NonNull @Present 2 pieces of information: ● the types ● their relationships

  15. Type = type qualifier + Java basetype @Present Optional<String> mName; Type qualifier Java basetype @MaybePresent Type Optional<String> Default qualifier = @MaybePresent so, these types are equivalent: @Present Optional<String> ● @MaybePresent Optional<String> ● Optional<String>

  16. Define a type system 1. Type hierarchy (subtyping) 2. Type rules (what operations are illegal) 3. Type introduction (what types for literals, … ) 4. Dataflow (run-time tests)

  17. 2. Type rules To prevent null pointer exceptions: ● expr.field expr.getValue() receiver must be non-null ● synchronized (expr) { … } monitor must be non-null ● ...

  18. @MaybePresent Type rules for Optional @Present “Never use Optional.get() unless you can prove that the Optional is present.” Only call Optional.get() on a receiver of type @Present Optional . example call: myOptional.get() class Optional<T> { T get(Optional<T> this) { … } example call: a.equals(b) }

  19. @MaybePresent Type rules for Optional @Present “Never use Optional.get() unless you can prove that the Optional is present.” Only call Optional.get() on a receiver of type @Present Optional . example call: myOptional.get() class Optional<T> { T get(@Present Optional<T> this) { … } }

  20. @MaybePresent Type rules for Optional @Present “Never use Optional.get() unless you can prove that the Optional is present.” Only call Optional.get() on a receiver of type @Present Optional . example call: myOptional.get() class Optional<T> { T get(@Present Optional<T> this) { … } T orElseThrow(@Present this, … ) { … } }

  21. Define a type system 1. Type hierarchy (subtyping) 2. Type rules (what operations are illegal) 3. Type introduction (what types for literals … ) 4. Dataflow (run-time tests)

  22. Type introduction rules For Nullness type system: ● null : @Nullable ● "Hello World" : @NonNull

  23. @MaybePresent Type introduction for Optional @Present “Never use Optional.get() unless you can prove that the Optional is present.” Optional<T> of(T value) { … } Optional<T> ofNullable(T value){ … }

  24. @MaybePresent Type introduction for Optional @Present “Never use Optional.get() unless you can prove that the Optional is present.” @Present Optional<T> of(T value) { … } Optional<T> ofNullable(@Nullable T value){ … }

  25. Define a type system 1. Type hierarchy (subtyping) 2. Type rules (what operations are illegal) 3. Type introduction (what types for literals, … ) 4. Dataflow (run-time tests)

  26. Flow-sensitive type refinement After an operation, give an expression a more specific type @Nullable Object x; @Nullable Object y; if (x != null) { y = new SomeType(); ... ... x is @NonNull here y is @NonNul l here } y = unknownValue; ... ... x is @Nullable again y is @Nullable again

  27. @MaybePresent Type refinement for Optional @Present “Never use Optional.get() unless you can prove that the Optional is present.” After receiver.isPresent() returns true, the receiver’s type is @Present @MaybePresent Optional<String> x; if (x.isPresent()) { ... x is @Present here } x is @MaybePresent again ...

  28. Now, let’s implement it Follow the instructions in the Checker Framework Manual https://checkerframework.org/manual/#creating-a-checker

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