Java Annotations for Invariant Specification

Slides:



Advertisements
Similar presentations
COSC 2006 Data Structures I Instructor: S. Xu URL:
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Preventing bugs with pluggable type checking Michael D. Ernst University of Washington Object x)
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Road Map Introduction to object oriented programming. Classes
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Effective Java: Generics Last Updated: Spring 2009.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Object x) { List lst; … } Detecting and preventing null pointer errors with pluggable type-checking CSE 331 University of Washington.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
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
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Extended Static Checking for Java Cormac Flanagan Joint work with: Rustan Leino, Mark Lillibridge, Greg Nelson, Jim Saxe, and Raymie Stata.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Attributes C#.Net Software Development Version 1.0.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Types, Implementing, Extending, Interfaces, Superclasses, Subclasses, Casting, and Access Modifiers.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Today’s lecture Review of chapter 8 Go over examples.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Basic Syntax อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 2.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Java Bytecode Verification Types Chris Male, David J. Pearce, Alex Potanin and Constantine Dymnikov Victoria University of Wellington, New.
Preventing bugs with pluggable type-checking Michael Ernst MIT
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 16 Checker Framework.
Variable Scope & Lifetime
More on Arrays Review of Arrays of ints, doubles, chars
Content Coverity Static Analysis Use cases of Coverity Examples
Object-Oriented Concepts
Intro to Java L. Grewe.
Some Eclipse shortcuts
Lecture 7 Designing Classes
CS230 Tutorial Week 3.
Accessible Formal Methods A Study of the Java Modeling Language
Chapter 9 Inheritance and Polymorphism
Enumerations & Annotations
Variables and Their scope
The Building Blocks Classes: Java class library, over 1,800 classes:
Building Java Programs
Generic programming in Java
Units with – James tedder
Java Programming Course
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
Introduction to Static Analyzer
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Barb Ericson Georgia Institute of Technology June 2005
CS520 Web Programming Java Annotations
Java Programming Language
Java Modeling Language (JML)
Java Basics Data Types in Java.
Chapter 11 Inheritance and Polymorphism Part 2
Java Annotations.
Subtype Substitution Principle
CS 240 – Advanced Programming Concepts
Presentation transcript:

Java Annotations for Invariant Specification Mathias Ricken September 22, 2008 COMP 617 Seminar

NullPointerException at runtime: Comments are dumb class HashMap { // returns null if no mapping for key Object get(Object key) { … } } HashMap m = new HashMap(); Object o = m.get("foo"); String s = o.toString(); NullPointerException at runtime: o is null

Return type Object|null incompatible with type Object Types are smart class HashMap { // returns null if no mapping for key Object|null get(Object key) { … } } HashMap m = new HashMap(); Object o = m.get("foo"); String s = o.toString(); Compiler Error: Return type Object|null incompatible with type Object This is not Java!

Annotations can make Java smarter class HashMap { // returns null if no mapping for key @Nullable Object get(Object key) { … } } HashMap m = new HashMap(); Object o = m.get("foo"); String s = o.toString(); Compiler Warning: Return value may be null, assigned to non-null variable. Pluggable type systems in Java 5 7?

Annotation Targets in Java 5 @A package some.package.name; @B class MyClass { @NonNull Object field; @C MyClass(@NonNull Object param) { field = param; } @NonNull Object method() { @NonNull Object localVar = field; return localVar; Note: Local variable annotations are completely ignored.

Concurrency Invariants interface TableModel { // may only be called from event thread void setValueAt(…); } TableModel m; // from outside event thread… m.setValueAt(…); Possible race condition.

Invariant Specification interface TableModel { @OnlyEventThread void setValueAt(…); } TableModel m; // from outside event thread… m.setValueAt(…); Invariant Violation Warning at Runtime (but still possible race condition)

Comparison to assert Similarity void setValueAt(…) { assert (EventQueue.isDispatchThread()); … Similarity Debug mode – disabled in production code @OnlyEventThread void setValueAt(…) { … }

Annotations are Easier to Find Javadoc produces invariant index

Inherited Invariants TableModel Object getValueAt(…) @OnlyEventThread void setValueAt(…) Implied @OnlyEventThread AbstractTableModel Object getValueAt(…) void setValueAt(…) DefaultTableModel MySpecialTableModel Object getValueAt(…) void setValueAt(…) Object getValueAt(…) void setValueAt(…) Implied @OnlyEventThread Implied @OnlyEventThread

@OnlyEventThread TableModel Inherited Invariants @OnlyEventThread TableModel Object getValueAt(…) void setValueAt(…) Implied @OnlyEventThread AbstractTableModel Object getValueAt(…) void setValueAt(…) Implied @OnlyEventThread DefaultTableModel MySpecialTableModel Object getValueAt(…) void setValueAt(…) Object getValueAt(…) void setValueAt(…) Implied @OnlyEventThread Implied @OnlyEventThread

Limited Universality A few supplied invariant annotations @OnlyEventThread @OnlyThreadWithName @OnlySynchronizedThis @NotEventThread @NotThreadWithName @NotSynchronizedThis assert can test an arbitrary predicate assert (someComplexPredicate());

Predicate Invariant Annotations @PredicateLink(value=Predicates.class, method="eval") public @interface OnlyThreadWithName { String value; } Find predicate method 1. @OnlyThreadWithName("main") void myMethod() { … } 2. Call predicate method and pass as arguments: this (nor null if static) data in invariant annotation Return true or false to indicate violation 3. public class Predicates { public static boolean eval(Object this0, String name) { return Thread.currentThread().getName(). equals(name); }

Further Limitation of Annotations One occurrence of an annotation class per target @OnlyThreadWithName("main") // illegal; and is @OnlyThreadWithName("other") // this "and" or "or"? void myMethod() { … } Suggestion @Or({ @OnlyThreadWithName("main"), @OnlyThreadWithName("other") }) void myMethod() { … }

Annotation Members @interface MyAnnotation { extends OtherAnnotation not allowed  no subtyping @interface MyAnnotation { int intMember; // primitives String stringMember; // strings Class classMember; // class literals SomeEnum enumMember; // enums // annotions OnlyThreadWithName annotMember; // arrays of the above OnlyThreadWithName[] arrayMember; }

No Annotation Subtyping in Java @interface Or { OnlyThreadWithName[] value; } @Or({@OnlyThreadWithName("main"), @OnlyThreadWithName("other")}) void myMethod() { … } // legal @NotThreadWithName("other")}) void otherMethod() { … } // illegal No common supertype for annotations

xajavac Modified Compiler @interface Base {} @interface OnlyThreadWithName extends Base { String value; } @interface NotThreadWithName extends Base { @interface Or extends Base { Base[] value;

Results Annotations with Subtyping Minimal changes to the compiler No changes to class file format Reduced invariant checker by ~1500 lines Improved code reuse

Results Invariant Annotations Annotated part of Swing and DrJava Discovered and fixed some bugs in DrJava Hard to do retroactively and without inside knowledge

Future Work Reflection library for annotations with subtyping Annotation getAnnotation(Class c) currently returns the annotation of class c Annotation[] getAnnotations(Class c) should be added to return all annotations of class c and its subclasses Modify JSR 308 prototype compiler to support subtyping

More Information and Download Invariant Specification http://www.concutest.org/tc/ Annotations with Subtyping http://www.cs.rice.edu/~mgricken/research/xajavac/