JSR 308: Type Annotations Making Java annotations more general and more useful Spec leads: Michael Ernst & Alex Buckley Implementation lead: Werner Dietl.

Slides:



Advertisements
Similar presentations
New features in JDK 1.5 Can these new and complex features simplify Java development?
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Preventing bugs with pluggable type checking Michael D. Ernst University of Washington Object x)
GUI Threading Explained and Verified Michael Ernst U. of Washington, Seattle, USA IMDEA Software Institute, Madrid, Spain joint work with Colin Gordon,
Detecting and preventing bugs with pluggable type-checking Michael D. Ernst University of Washington Joint work with Mahmood Ali, Werner Dietl, …
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Atomicity in Multi-Threaded Programs Prachi Tiwari University of California, Santa Cruz CMPS 203 Programming Languages, Fall 2004.
An overview of JML tools and applications Lilian Burdy Gemplus Yoonsik Cheon, Gary Leavens Iowa Univ. David Cok Kodak Michael Ernst MIT Rustan Leino Microsoft.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Encapsulation by Subprograms and Type Definitions
Visualizing Type Qualifier Inference with Eclipse David Greenfieldboyce Jeffrey S. Foster University of Maryland.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
Stimulating reuse with an automated active code search tool Júlio Lins – André Santos (Advisor) –
Javari: Adding Reference Immutability to Java Matthew Tschantz and Michael Ernst MIT CSAIL.
1 Documenting with Javadoc CS 3331 Fall 2009 How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
Usable code verification: balancing costs and benefits Two nuggets: User-defined verifiers Crowd-sourced verification Michael D. Ernst University of Washington.
Java 1.5 Annotations. Motivation Computer scientists and engineers are always trying to add new features to programming languages Sometimes they are genuine.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
C++ Code Analysis: an Open Architecture for the Verification of Coding Rules Paolo Tonella ITC-irst, Centro per la Ricerca Scientifica e Tecnologica
1 Static Type Analysis of Path Expressions in XQuery Using Rho-Calculus Wang Zhen (Selina) Oct 26, 2006.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
Java Implementation: Part 3 Software Construction Lecture 8.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Object x) { List lst; … } Detecting and preventing null pointer errors with pluggable type-checking CSE 331 University of Washington.
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Preventing bugs with pluggable type checking Michael Ernst and Mahmood Ali University of Washington and MIT OOPSLA 2009 tutorial Object.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
ABSTRACT The real world is concurrent. Several things may happen at the same time. Computer systems must increasingly contend with concurrent applications.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
User-defined type checkers for error detection and prevention in Java Michael D. Ernst MIT Computer Science & AI Lab
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Types in programming languages1 What are types, and why do we need them?
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
An Undergraduate Course on Software Bug Detection Tools and Techniques Eric Larson Seattle University March 3, 2006.
Javari: Adding Reference Immutability to Java Rauno Ots Matthew S. Tschantz Michael D. Ernst MIT CSAIL 2005.
Demo of Scalable Pluggable Types Michael Ernst MIT Dagstuhl Seminar “Scalable Program Analysis” April 17, 2008.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
Introduction Mehdi Einali Advanced Programming in Java 1.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
1 CSE 331 Hash codes; annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.
Detecting and preventing bugs with pluggable type-checking Michael D. Ernst University of Washington Joint work with Werner Dietl, and many others
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Preventing bugs with pluggable type checking Michael Ernst University of Washington Joint work with Mahmood Ali and Matthew Papi Object.
Preventing bugs with pluggable type-checking Michael Ernst MIT
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
1 Documenting with Javadoc How to Write Doc Comments for the Javadoc TM Tool available from java.sun.com.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 16 Checker Framework.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
for regular expressions
CSE 331 Software Design and Implementation
Functional Programming with Java
Java Programming Language
Java Programming Course
Bugs & Debugging - Testing
CSE 331 Annotations slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Java Annotations for Invariant Specification
Abstract Types Defined as Classes of Variables
Presentation transcript:

JSR 308: Type Annotations Making Java annotations more general and more useful Spec leads: Michael Ernst & Alex Buckley Implementation lead: Werner Dietl Object x) { List lst; … }

Java 7: declaration public class Date int compareTo(Date other) {... }... }

Type annotations JSR 308 permits annotating any use of a String query; List strings; myGraph = Graph) tmpGraph; class UnmodifiableList List {}

Syntax vs. semantics JSR 308 specifies syntax but not semantics – No change to Java compile-time, load-time, or run- time semantics Motivation: type qualifiers and pluggable type- checking – Prevent/detect errors, improve code quality Semantics: An annotation processor (compiler plug-in) can handle type annotations – Detect run-time errors at compile time – Compiler issues additional errors/warnings

Outline Java syntax Classfile representation Relationship to other projects: – Java SE 8 language changes – Checker Framework (pluggable type-checking) – JSR 269: annotation processing – JSR 305: standard annotations for code quality – New datatype libraries: JSR 310 Time, JSR 354 Money Logistics Conclusion

Prefix syntax: annotation appears before the type class Folder {... } class UnmodifiableList List {... } void monitorTemperature() TemperatureException {... } Map > files; Collection files; MyClass > unchangeable; Entry keyAndValue;... String) myObject... o. m("...")... List (myNonEmptyStringSet)... MyObject() class MyClass {... }

Array and receiver syntax Array annotations appear before the relevant part of the Document [][] docs1 [][] docs2 [] docs3 Explicit receiver (“ this ” parameter); these are equivalent: String toString() {... } String toString(MyClass this) {... } – Presence or absence of the this parameter has no effect on semantics – No special type annotation syntax

Distinguishing declaration annotations from type Override { NonNull String toString() {... }

Class file format Java stores declaraton annotations in classfile attributes – For formal parameters: RuntimeVisibleParameterAnnotations and RuntimeInvisibleParameterAnnotations – For fields, methods, and classes: RuntimeVisibleAnnotations and RuntimeInvisibleAnnotations JSR 308 defines two new classfile attributes: RuntimeVisibleTypeAnnotations and RuntimeInvisibleTypeAnnotations – structurally identical to corresponding declaration annotations Also stores local variable declaration annotations End of JSR 308 specification. Now, discuss relationship to other projects.

Java SE 8 language changes Lambda expressions – 2 forms of argument list: normal and compact – compact form (#{ x -> x.toString }): no type annotations compiler copies annotations during inference (like diamond) – normal form: permits type annotations – No annotations on lambda expression, which is not a type Extension methods: deferred until that spec is stable Modules: no annotations at present Annotation changes unrelated to JSR 308: – Repeated/duplicate/multiple annotations annotation

JSR 269: Annotation processing API 1.Iterator/visitor Designed for declaration annotations Visits each declaration (but not their bodies) Type annotations appear on signatures and in bodies JSR 308 makes no change 2.API for accessing information about signatures JSR 308 updates this Update reflection and serialization APIs similarly

Pluggable type-checking Goal: prevent run-time errors Technique: detect the errors at compile time – Create a new type system – Annotate your program with the type system Checker Framework: – A useful third-party tool – Not part of JSR 308 or Java SE

Java’s type checking is too weak Type checking prevents many bugs int i = “hello”; // type error Type checking doesn’t prevent enough bugs System.console().readLine();  NullPointerException Collections.emptyList().add(“One”);  UnsupportedOperationException 13

Some errors are silent Date date = new Date(0); myMap.put(date, “Java epoch”); date.setYear(70); myMap.put(date, “Linux epoch”);  Corrupted map dbStatement.executeQuery(userInput);  SQL injection attack Initialization, data formatting, equality tests, …

Solution: Pluggable type systems Design a type system to solve a specific problem Write type qualifiers in code (or, use type Date date = new Date(0); date.setTime(70); // compile-time error Type checker warns about violations (bugs) % javac -processor NullnessChecker MyFile.java MyFile.java:149: dereference of possibly-null reference bb2 allVars = bb2.vars; ^

Pluggable types in practice In use academically and industrially – not imposed on any user Case studies at U. of Washington > 3,000,000 lines of code annotated > 200 errors found also, improved the design < 1 annotation per 50 lines of code, often much less much less overhead than generic types

@Regex annotation Goal: prevent a string that is a legal regular expression – Pattern.compile will not throw PatternSyntaxException – "a*(bc)?" – "1) first point; 2) second point" is An undergraduate annotated 4 programs plume-lib, Daikon, Apache Lucene, Chukwa > 500K LOC – Wrote 62 annotations – Detected over 20 bugs Suppressed 16 false warnings

Standard type annotations in Java SE 8? A small number of standard annotations may help users – The mandate of JSR 305 (now defunct) Annotations are stylized documentation – Terser than Javadoc (shorter code) – Machine-checked to detect bugs – etc. Semantics are defined independently of any annotation processor – No effect on compilation unless an annotation processor is specified Needs JDK annotations

Candidate standard @FullyQualifiedName Property file keys Concurrency annotations (JCIP book) Fake enumerations

New data structure APIs JSR 310 Time JSR 354 Money Can define annotations to avoid run-time errors

Logistics JCP process version 2.6 Spec is complete and stable – some changes are scheduled for release in early 2012 – most recent previous changes were in 2009 Implementation complete except: – parsing of annotations on nested classes – renumbering an enum (trivial) – reflection & serialization (waiting for finalized spec) EDR planned in early 2012, when reference implementation is complete

JSR 308: annotations on types Extends Java annotations – permits annotations to be written on any use of a type – defines syntax but not semantics – simple prefix syntax Enables pluggable type-checking – detects and prevents errors, improves code quality Specificaton and implementation status – publicly available: – operational and in daily use