Object and Reference Immutability using Java Generics Yoav Zibin(1), Alex Potanin(2), Shay Artzi(1), Adam Kiezun(1), and Michael D. Ernst(1) 1) MIT Computer.

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

CPSC 388 – Compiler Design and Construction
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Object and Reference Immutability using Java Generics Yoav Zibin, Alex Potanin(*), Mahmood Ali, Shay Artzi, Adam Kiezun, and Michael D. Ernst MIT Computer.
Generic programming in Java
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
Java Generics.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Topic 6 Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Javari: Adding Reference Immutability to Java Matthew Tschantz and Michael Ernst MIT CSAIL.
Chapter 19 Java Data Structures
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Ownership and Immutability in Generic Java (OIGJ) Yoav Zibin +, Alex Potanin * Paley Li *, Mahmood Ali ^, and Michael Ernst $ Presenter: Yossi Gil + +
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
User-defined type checkers for error detection and prevention in Java Michael D. Ernst MIT Computer Science & AI Lab
Peyman Dodangeh Sharif University of Technology Fall 2013.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Javari: Adding Reference Immutability to Java Rauno Ots Matthew S. Tschantz Michael D. Ernst MIT CSAIL 2005.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Generic Data Structures "Get your data structures correct first, and the rest of the program will write itself." - David Jones EE 422CGenerics 1.
CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Polymorphism (generics) CSE 331 University of Washington.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Object and Reference Immutability using Java Generics Yoav Zibin(1), Alex Potanin(2), Shay Artzi(1), Adam Kiezun(1), and Michael D. Ernst(1) 1) MIT Computer.
1 Javari: Adding Reference Immutability to Java Matthew Tschantz & Michael Ernst (MIT) OOPSLA’05 Presented by Tali Shragai.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
UMBC CMSC 331 Java Review of objects and variables in Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Preventing bugs with pluggable type-checking Michael Ernst MIT
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
12-CRS-0106 REVISED 8 FEB 2013 CSG2H3 Object Oriented Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes
JAVA COLLECTIONS LIBRARY
Object and Reference Immutability using Java Generics
Constructors and Other Tools
Java Programming Language
Chapter 9 Objects and Classes
Presentation transcript:

Object and Reference Immutability using Java Generics Yoav Zibin(1), Alex Potanin(2), Shay Artzi(1), Adam Kiezun(1), and Michael D. Ernst(1) 1) MIT Computer Science and Artificial Intelligence Lab, USA 2) Victoria University of Wellington, New-Zealand Presenting: Ori Arad –

2/23 Immutability – What for?... Program comprehension Verification Compile & Run-time optimizations Invariant detection Refactoring Test input generation Regression oracle creation Specification mining Modeling

3/23 Immutability varieties Class immutability No instance of an immutable class may be change after creation (e.g. String, Integer etc.) Object immutability Immutable instances may not be changed, while other instances (of the same class) may… Reference immutability a given reference cannot be used to modify its referent.

4/23 IGJ - Immutability Generic Java Object immutability: An object: mutable or immutable. Reference immutability: A reference: Immutable, Mutable, or ReadOnly Class immutability Also support Class immutability

5/23 IGJ - Immutability Generic Java one new generic parameter (at the beginning of the list of generic parameters) was added 1: // An immutable reference to an immutable date; side effects are prohibited 2: Date immutD = new Date (); 3: // A mutable reference to a mutable date; side effects are permitted 4: Date mutD = new Date (); 5: // A readonly reference to any date; side effects are prohibited 6: Date roD =... ? immutD : mutD; 1: // An immutable reference to an immutable date; side effects are prohibited 2: Date immutD = new Date (); 3: // A mutable reference to a mutable date; side effects are permitted 4: Date mutD = new Date (); 5: // A readonly reference to any date; side effects are prohibited 6: Date roD =... ? immutD : mutD;

6/23 IGJ main design principles Transitivity: The design must provide transitive (deep) immutability Purely static: There should be no runtime representation for immutability Polymorphism: It must be possible to abstract over immutability without code duplication Simplicity: The design should not change Java's syntax and have a small set of typing rules

7/23 Type Hierarchies for IGJ The type hierarchy for immutability parameters The top of the type hierarchy for all other classes

8/23 IGJ Subtype hierarchy // Demonstrates how to copy value of readonly roObj to mutable mutableObj Object roObj =...; List > l = new List >(); ((List >) l).add(roObj); Object mutableObj = l.get(0); // mutableObj equals roObj // Demonstrates how to copy value of readonly roObj to mutable mutableObj Object roObj =...; List > l = new List >(); ((List >) l).add(roObj); Object mutableObj = l.get(0); // mutableObj equals roObj Legend: L List, O object, R ReadOnly IM Immutable, M Mutable For example: L > means List >

9/23 Java Array Co-Variance Problem Read from Array: OK! Write to Array: Problem! // A is a single-element array of String. String[] a = new String[1]; // B is an array of Object Object[] b = a; // Assign an Integer to b. This would be possible if b really were // an array of Object, but since it really is an array of String, // we will get a java.lang.ArrayStoreException. b[0] = new Integer (1); // A is a single-element array of String. String[] a = new String[1]; // B is an array of Object Object[] b = a; // Assign an Integer to b. This would be possible if b really were // an array of Object, but since it really is an array of String, // we will get a java.lang.ArrayStoreException. b[0] = new Integer (1); Solution with IGJ – immutability promise us only reading  no Co-Variance problem

10/23 The Field Rule legal iff I(o) = Mutable. Example : Employee roE =...; roE.address =...; // Compilation error! Employee roE =...; roE.address =...; // Compilation error! o.someField = exp; Typing is guaranteed by several rules. Let I(x) denote the immutability of x Rule I: Field Assignment Rule

11/23 "Immutability" of Methods 4 @Immutable void m() {...this...} Here: I( this) is Mutable In general: in any method m, I( this ) is the same as I( m ) “ this ” immutability depends on the context

12/23 Reference-Immutability Rules Method Invocation Rule: (not necessarily I(o) = I(m) ) Employee o =...; o.setAddress(...); // OK since I(o) = Mutable and I(setAddress) = Mutable o.getAddress(); // OK since I(o) = Mutable and I(getAddress) = ReadOnly ((Employee ) o).setAddress(...); // Compilation error! Employee o =...; o.setAddress(...); // OK since I(o) = Mutable and I(setAddress) = Mutable o.getAddress(); // OK since I(o) = Mutable and I(getAddress) = ReadOnly ((Employee ) o).setAddress(...); // Compilation error! o.m(...) is legal if I(o) is subtype of I(m).

13/23 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id; } 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: } 10: class Graph { 11: public Edge lastN; 12: public List > l; 13: Graph(List > l) { this.l = l; } 14: void addEdge(Edge n) { 15: this.l.add(n); this.lastN = n; } 16: Edge getLast() { return this.lastN; } 17: public static 18: Edge findEdge(Graph nl, long id) {... } 19: } 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id; } 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: } 10: class Graph { 11: public Edge lastN; 12: public List > l; 13: Graph(List > l) { this.l = l; } 14: void addEdge(Edge n) { 15: this.l.add(n); this.lastN = n; } 16: Edge getLast() { return this.lastN; } 17: public static 18: Edge findEdge(Graph nl, long id) {... } 19: } Example IGJ classes Edge and Graph, with the immutability parameters (and annotations, for this) underlined. For now: is is

14/23 Example: Line 5: the assignment into this.id is OK If this.id = …; was on line 6 – illegal… Line 3: OK due to Method Rule Line 8: static  no annotation, Edge of any immutability could be pass here… 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id; } 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: } 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id; } 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: }

15/23 Example: Line 11: a class can pass its immutability parameter to its fields Line 16: Also return type – no need for overloading Line 12: Transitivity - in an immutable Graph the field l will contain an immutable list of immutable edges 10: class Graph { 11: public Edge lastN; 12: public List > l; 13: Graph(List > l) { this.l = l; } 14: void addEdge(Edge n) { 15: this.l.add(n); this.lastN = n; } 16: Edge getLast() { return this.lastN; } 17: public static 18: Edge findEdge(Graph nl, long id) {... } 19: } 10: class Graph { 11: public Edge lastN; 12: public List > l; 13: Graph(List > l) { this.l = l; } 14: void addEdge(Edge n) { 15: this.l.add(n); this.lastN = n; } 16: Edge getLast() { return this.lastN; } 17: public static 18: Edge findEdge(Graph nl, long id) {... } 19: }

16/23 Object Immutability & constructors What annotation should CTOR problem: Graph(List > l) { this.l = l; this.addEdge( new Edge (0) ); //  } … List > imList =...; new Graph (imList); // An element was added to imList Graph(List > l) { this.l = l; this.addEdge( new Edge (0) ); //  } … List > imList =...; new Graph (imList); // An element was added to Guess not…

17/23 Object Immutability & constructors Solution: Forth kind of reference immutability: AssignFields Permit to perform limited side-effects without permitting modification of immutable method can assign & method can only assign

18/23 Immutability & Assignability MyClass myObject = new MyClass(); myObject = anotherObject; Assignability myObject.setField(4); immutability

19/23 Revised rules Field Rule revised (relaxed): AssignField is not transitive… Method Rule revised (restricted): e.g. this.l.get(0).setId(42); // will be illegal o.someField =...; is legal if I(o) = Mutable or (I(o) = AssignFields and o=this) o.m(...) is legal if I(o) is subtype of I(m) and (if I(m) is AssignFields o must be this)

20/23 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id; } 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: } 10: class Graph { 11: public Edge lastN; 12: public List > l; 13: Graph(List > l) { this.l = l; } 14: void addEdge(Edge n) { 15: this.l.add(n); this.lastN = n; } 16: Edge getLast() { return this.lastN; } 17: public static 18: Edge findEdge(Graph nl, long id) {... } 19: } 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id; } 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: } 10: class Graph { 11: public Edge lastN; 12: public List > l; 13: Graph(List > l) { this.l = l; } 14: void addEdge(Edge n) { 15: this.l.add(n); this.lastN = n; } 16: Edge getLast() { return this.lastN; } 17: public static 18: Edge findEdge(Graph nl, long id) {... } 19: } Example The original code –

21/23 Example: Line 5: the assignment into this.id is still OK setId is annotated  I(this)=AssignFields Line 3: I(this) = AssignFields & I(setId) = AssignFields 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id;} 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: } 1: class Edge { 2: private long id; 3: Edge(long id) { this.setId(id); } 4: synchronized void setId(long id) { 5: this.id = id; } 6: synchronized long getId() { return id;} 7: long getIdImmutable() { return id; } 8: public static void print(Edge n) {... } 9: }

22/23 Example: Line 13: If we will add the following code – it will be illegal in the revised rule (and legal in the old one) Graph(List > l) { this.l = l; this.l.get(0).setId(42); //  } Graph(List > l) { this.l = l; this.l.get(0).setId(42); //  } New Rule: new SomeClass (…) is legal only if X = Mutable and this CTOR is not marked or X = Imuutable and this CTOR is not marked (X = readOnly & X=AssignFileds are illegal)

23/23 Future Work Plug-in for IDE (e.g. Eclipse) A WriteOnly immutability notation for field class Vector int int size() { return size; void add(T t) {... void removeLast() {... void remove(T t) {... void get(int index) {... } } class Vector int int size() { return size; void add(T t) {... void removeLast() {... void remove(T t) {... void get(int index) {... } }

24/23 Future Work (cont.) Add default immutability class Graph An alternative syntax (Java 7?...) Runtime support (e.g. ArrayList ArrayList (...)

25/23 Questions?