A Deep Dive into the Void

Similar documents
Using null type annotations in practice

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Advances in Programming Languages

The Hack programming language:

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

Operators and Expressions

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

The Checker Framework: pluggable static analysis for Java

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Exercise 8 Parametric polymorphism November 18, 2016

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

1 Shyam sir JAVA Notes

Java Generics -- an introduction. Based on

Advances in Programming Languages

Mixed projects: Java + Kotlin. Svetlana Isakova

Srikanth Sankaran IBM India. Stephan Herrmann GK Software. Noopur Gupta. IBM India. EclipseCon NA 2014 JDT Embraces Lambda Expressions

Metadata Features in Java SE 8

Code verification. CSE 331 University of Washington. Michael Ernst

Objects and Iterators

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

Status Report. JSR-305: Annotations for Software Defect Detection. William Pugh Professor

Program Verification (6EC version only)

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CSE 331. Generics (Parametric Polymorphism)

Types and Type Inference

Principles of Software Construction: Objects, Design, and Concurrency (Part 2: Designing (Sub )Systems)

JSR-305: Annotations for Software Defect Detection

Java Design Goals. Lecture 32: Java. Java Original implementations slow! Exceptions & Subtyping. - void method readfiles() throws IOException {...}!

Checked and Unchecked Exceptions in Java

Oracle Corporation

Static program checking and verification

CSE 331 Software Design & Implementation

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Lecture 13: Subtyping

Exercise 13 Self-Study Exercise Sheet

Concepts of Object-Oriented Programming Peter Müller

Using Type Annotations to Improve Your Code

Collections, Maps and Generics

DD2460 Software Safety and Security: Part III Exercises session 2: Type + Jif

Principles of Programming Languages

Polymorphic (Generic) Programming in Java

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Data Structure. Recitation IV

CS/ENGRD 2110 SPRING 2018

Static Analysis in C/C++ code with Polyspace

Exercise 12 Initialization December 15, 2017

Rules and syntax for inheritance. The boring stuff

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Engineering Abstractions in Model Checking and Testing. Michael Achenbach Klaus Ostermann

Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail)

Types and Type Inference

Concepts of Object-Oriented Programming Peter Müller

CSCE 314 Programming Languages

Super-Classes and sub-classes

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

8 Understanding Subtyping

Javarifier: inference of reference immutability

ESC/Java2 Warnings David Cok, Joe Kiniry, and Erik Poll Eastman Kodak Company, University College Dublin, and Radboud University Nijmegen

CSCI 261 Computer Science II

Preventing Errors Before They Happen The Checker Framework

CS108, Stanford Handout #8. Java Generics

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

CS558 Programming Languages

5/23/2015. Core Java Syllabus. VikRam ShaRma

Exercise 13 Self-Study Exercise Sheet

Advances in Programming Languages

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Java Programming Best Practices Part V. Timothy Fagan Ishi Systems, Inc.

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Eclipse and Java 8. Daniel Megert Platform and JDT Lead Eclipse PMC Member IBM Rational Zurich Research Lab

Java Overview An introduction to the Java Programming Language

Experimental New Directions for JavaScript

Index COPYRIGHTED MATERIAL

Java Programming. Atul Prakash

Detecting and preventing null pointer errors with pluggable type-checking

COMP 110/L Lecture 24. Kyle Dewey

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

An introduction to formal specifications and JML. Invariant properties

Detecting and preventing null pointer errors with pluggable type-checking

CSE 331 Software Design and Implementation. Lecture 16 Checker Framework

Understanding C/C++ Strict Aliasing

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

Implements vs. Extends When Defining a Class

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Lecture 19 Checker Framework

Data Structures. 02 Exception Handling

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

Thinking Functionally

Declarations and Access Control SCJP tips

Comp215: Thinking Generically

Java Review: Objects

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Transcription:

A Deep Dive into the Void Java ready Advanced null Type Annotations Stephan Herrmann GK Software

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 2 An Old Problem 1965 Tony Hoare introduced Null references in ALGOL W simply because it was so easy to implement Photograph by Rama, Wikimedia Commons, Cc-by-sa-2.0-fr

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 3 Still Leading the Charts

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 4 An Expensive Problem The Billion Dollar Mistake Photograph by Rama, Wikimedia Commons, Cc-by-sa-2.0-fr

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 5 Many Steps to a Solution do nothing: booooooooom recognize patterns booooooom local flow analysis Since ALGOL W Since Eclipse 3.1 1965 2005 boooom method contracts boom type checking Since Eclipse Juno / Java 5 2012 Since Eclipse Luna / Java 2014

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 7 Alternative Strategy Optional a polite way of saying: this value could be missing supports null-less programming doesn't help against existing nulls viable option for from-scratch clean-room new software requires all software to be re-written

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # Obviously Correct Typechecking for null analysis Two kinds of types those that include null those that don't This is OK: These are type errors: Dog? barkandreturn(dog dog) { dog.bark(); return null; } Dog barkandreturn(dog? dog) { dog.bark(); return null; }

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 9 Typesystem Construction Kit Strategy Richer types add more information Richer type checking new typing rules Stronger guarantees proven absence of errors no methodnotunderstood no ClassCastException JSR 30 Type annotations @NonNull @Nullable org.eclipse.jdt.annotation_2.0 ECJ checks nullness compatibility & flow Stronger guarantees no NPE no SWTExecption(thread)

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 10 Ternary or Boolean? Buy @NonNull & @Nullable, get 3 kinds of types green, red, legacy Interfacing with legacy code like using raw types in Java 5+ Null Null type type safety (type (type annotations): The The expression of of type type 'List<String>' needs needs unchecked conversion to to conform to to '@NonNull List<@NonNull String>' Nirwana: ubiquitous @NonNullByDefault per class / interface per package (use package-info.java) let the compiler complain about missing @NNBD And back to 2014 reality @NonNullByDefault({})

Where is: Everywhere? DefaultLocation for @NonNullByDefault declarations TYPE_USE PARAMETER RETURN_TYPE FIELD TYPE_PARAMETER TYPE_BOUND TYPE_ARGUMENT ARRAY_CONTENTS @NonNullByDefault({ }) class Foo <T extends Bar> { Foo<String> stringfoo; Bar[][] bars; Bar getbar(string name) { return bar; } } Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 11

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 12 Where is: Everywhere? DefaultLocation for @NonNullByDefault Not supported non-null by construction new throws, catch argument this receiver in method reference would need runtime evaluation instanceof & cast void void make(@a Foo Foo this) {} {} @A @A Object::toString no usefull interpretation prefix of static member constructor @A @A Integer.MAX_VALUE future type declaration

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 13 Where is: Everywhere?...... Wildcards Wildcards and and the the use use of of type type variables variables are are always always excluded excluded from from NonNullByDefault. NonNullByDefault. DefaultLocation for @NonNullByDefault Not supported No default local variables better covered by flow analysis on type variables & wildcards these obtain nullness via other channels

Where is: Everywhere? DefaultLocation for @NonNullByDefault declarations TYPE_USE PARAMETER RETURN_TYPE FIELD TYPE_PARAMETER TYPE_BOUND TYPE_ARGUMENT ARRAY_CONTENTS @NonNullByDefault({ }) class Foo <T extends Bar> { Foo<String> stringfoo; Bar[][] bars; Bar getbar(string name) { return bar; } } Default default Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 14

Where is: Everywhere? DefaultLocation for @NonNullByDefault declarations TYPE_USE PARAMETER RETURN_TYPE FIELD TYPE_PARAMETER TYPE_BOUND TYPE_ARGUMENT ARRAY_CONTENTS @NonNullByDefault({ }) class Foo <T extends Bar> { Foo<String> stringfoo; Bar[][] bars; Bar getbar(string name) { return bar; } } Default default Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 15

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 16 Great Expectations ECJ will not make your program null safe It will hurt pin-point design decisions you failed to make contradictory assumptions type checker will need your help a false positive? rules & obligations Application development free to move obligations around Tension in API design strict for effective null checking flexible for wide range of re-use

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 17 Generic API The classic declaration Unconstrained type parameter: public interface List<T> { } Client side Free to choose the type argument: List<@NonNull Person> List<@Nullable Person> Implementer No knowledge about free type variable T Must assume the worst need to check on dereference cannot assign null to a T variable

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 1 Constraining Type Parameters Admit only @NonNull types: annotated type bound class MyGenericClass1<T extends @NonNull Object> legal substitutes: @NonNull Object, @NonNull String illegal: @Nullable Object because @NonNull X <: @Nullable X annotated Admit only @Nullable types: type parameter (declaration) we can't say <T super @Nullable Object> class MyGenericClass2<@Nullable T> (interpreted as both upper and lower bound)

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 19 Puzzle 1 Legal or not? @NonNullByDefault class Tricky<T> { } void foo(t t) {} void test() { foo(null); } where does @NonNullByDefault apply type parameter? no method parameter? generally yes but not to a type variable Null type mismatch (type annotations): 'null' is is not not compatible to to the the free type variable 'T' 'T'...... Wildcards Wildcards and and the the use use of of type type variables variables are are always always excluded excluded from from NonNullByDefault. NonNullByDefault.

Puzzle 2 What is this? @NonNullByDefault class Tricky<T> { void foo(@nullable T t) {} void test() { foo(null); } } You said 'T' could mean anything so what is @Nullable T? e.g., if T := @NonNull String? the @Nullable variant of whatever T represents e.g., @Nullable String Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 20

Stephan Herrmann: A Deep Dive into the Void - EclipseCon Europe 2014 # 21 DEMO Double Inference External Annotations