Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Predefined Classes in Java Ellen Walker CPSC 201 Data Structures Hiram College.
Class Inheritance UNC-CHAPEL HILL COMP 401 BRIAN CRISTANTE 5 FEBRUARY 2015.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
P.R. James © P.Chalin et al.1 An Integrated Verification Environment for JML: Architecture and Early Results Patrice Chalin, Perry R. James, and George.
User-defined type checkers for error detection and prevention in Java Michael D. Ernst MIT Computer Science & AI Lab
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
Abstract Syntax Trees Compiler Baojian Hua
Introduction to Generics
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java 1 Introduction Why annotations?  Enhance ease-of-development  Shift some code generation from programmer to compiler What are annotations?
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Annotations. Annotations  Annotations are metadata or data about data. An annotation indicates that the declared element should be processed in.
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-Oriented Concepts
CS 536 / Fall 2017 Introduction to programming languages and compilers
Chapter 9 Inheritance and Polymorphism
Starting JavaProgramming
Extending Classes.
Java Programming Language
Conditional Statements
Interfaces.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Course
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 11 Inheritance and Polymorphism Part 2
In this class, we will cover:
Java Annotations.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Java Annotations for Invariant Specification
Review for Midterm 3.
Presentation transcript:

Java Annotations for Types and Expressions Mathias Ricken October 24, 2008 COMP 617 Seminar

1 // never null // p never null // never null class C { Object field; C(Object p) { field = p; } Object get() { return field; } Comments are Discarded Parser Source File Program with comments Program Comments Abstract Syntax Tree (AST) C Object field C(Object p) = fieldp Object get() return field

2 class C Object field; Object p) { field = p; Object get() { return field; } Annotations are Retained Parser Source File Abstract Syntax Tree C Object field C(Object p) = fieldp Object get() return field Program with annotations Program with Annotations are part of the AST  Can be processed automatically

3 Annotation Definition Structured Data  Comparable to records in MyAnnotation { String value(); // member int i() default 123; // member w. default value MarkerAnnotation { // annotation without any members }

4 Annotation i=456) void method() { … } // default value for i: void method2() { … } // special case for members called void method3() { … } // parenthesis can be omitted if no void method4() { … }

5 Annotation MyAnnotation { int intMember(); // primitives String stringMember(); // strings Class classMember(); // class literals SomeEnum enumMember(); // enums // annotions OnlyThreadWithName annotMember(); // arrays of the above OnlyThreadWithName[] arrayMember(); }

6 Annotation Targets in Java package class MyClass Object Object param) { field = param; Object method() Object localVar = field; return localVar; }

7 New Annotation Targets in JSR308 Types  Type parameters HashMap m;  Inheritance, bounds class MyClass SuperClass { … }  Object creation Integer(5);  Class literals Class c String.class;  Static MyClass.field  Type casts String s = String) myObject;

8 New Annotation Targets in JSR308 All Type Occurrences HashMap m; Arrays a = new Method Receivers public String { … }  As opposed to return type String toString() { … } Backward-Compatible to pre-JSR308  Annotations can be written as comments public String toString() { … }

9 class C Object field; Object p) { field = p; Object get() { return field; } Structure of Java Compiler Parser Source File Class File Writer Class File Error p Type Checker AST

10 class C Object field; Object p) { field = p; Object get() { return field; } Structure of JSR308 Compiler Parser Annotation Checker Plugins AST p Annotation Checker Class File Writer Class File Type Checker Error Source File

11 Annotation Checker Plugins Nullness Object foo = null; // error: null! Mutability checkers (Javari, int i = 0; i = 1; // error: mutation! Interning String a = "x".intern(); String b = "x"; if (a==b) … // error: identity vs. equality Defined using extensible framework

12 Nullness Checker Define {} Nullable { Nullable.class } NonNull { Date

13 Nullness Checker Override processing for certain AST nodes Flow analysis  Recognizes assignment of constants Integer i = null; // i always null from here on Integer j = 1; // j never null from here on i = 2; // i never null from here on  Recognizes simple conditionals if (i==null){ /* i must be null */ } else { /* i can't be null */ }

14 Suggested Extensions Annotations on Block { … } Annotations on Parenthetical ( … ) { int value() default 0; } +1; // (1)+1; or // +1; ???

15 Resolving Ambiguity Require parentheses for annotations { int value() default {} (1)+1; // +1; (1)+1; // (1)+1; (1)+1; // (1)+1;

16 class C Object field; Object p) { field = p; Object get() { return field; } Source Code Generation Parser Annotation Checker Class File Writer Class File Type Checker Error Source File Code Generator Annotation Checker Plugins Code Generator Plugins

17 class C Object field; Object p) { field = p; Object get() { return field; } AST Generation Parser Annotation Checker Plugins Annotation Checker Class File Writer Class File Type Checker Error Source File Code Generator Code Generator Plugins

18 Multi-Stage Java double double x, int n) { if (n==0) (1.0); else (x) (power(x, n-1)) ); } double square(double x) { (x), 2)); // generates x * x * 1.0; } Java splice run generated code.!x

19 Multi-Stage Java double double x, int n) { if (n==0) (1.0); else (x) (power(x, n-1)) ); } double square(double x) { (x), 2)); // generates x * x * 1.0; } let rec power(x, n) = match n with 0 ->.. | n ->..;; let square =.!..~(power (.., 2))>.;;

20 Summary Annotations can be processed automatically  Checker plugins for enhanced systems Add annotations on  Block { … }  Parenthetical ( … ) Provide code generator stage  Use for multi-stage programs