Muga Nishizawa and Shigeru Chiba (Tokyo Institute of Technology, Japan) 1 A Small Extension to Java for Class Refinement ACM SAC'08, March 18, 2008.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
1 An Aspect-Aware Outline Viewer Michihiro Horie and Shigeru Chiba Tokyo Institute of Technology, Japan.
Class Hierarchy (Inheritance)
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Lecture 11: Polymorphism and Dynamic Binding Polymorphism Static (compile-time) binding Dynamic (run-time) binding.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Object Oriented Programming
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
A Distributed Aspect-Oriented System for J2EE Applications Muga Nishizawa and Shigeru Chiba (Tokyo Institute of Technology, Japan) Background - As benefits.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
© Keren Kalif Advanced Java Topics Written by Keren Kalif, Edited by Liron Blecher.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
OOP: Inheritance. Inheritance A class can extend another class, inheriting all its data members and methods while redefining some of them and/or adding.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Comparison of Different AOP Approaches Presented by: Xiaojing Wang.
AOSD'04, Lancaster, UK 1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech) Shigeru Chiba (Tokyo Tech) Michiaki Tatsubori.
Classes, Interfaces and Packages
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Jun 14, 2004RAM-SE'04 Workshop, Oslo, Norway 1 Negligent Class Loaders for Software Evolution Yoshiki Sato, Shigeru Chiba (Tokyo Institute of Technology.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
PPL 2004, Gamagori, AICHI1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech, Japan) Shigeru Chiba (Tokyo Tech, Japan)
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Inheritance and Polymorphism
Java Programming Language
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Java Programming Language
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism Part 2
Exception Handling.
Exceptions and Exception Handling
Introduction to Classes and Objects
Presentation transcript:

Muga Nishizawa and Shigeru Chiba (Tokyo Institute of Technology, Japan) 1 A Small Extension to Java for Class Refinement ACM SAC'08, March 18, 2008

Class Refinement for Software Evolution ACM SAC'08, March 18, Extend an existing class without modifying its source code class Point { int x, y; void setX(int nx) { x = nx; } void setY(int ny) { y = ny; } refine Point { void setX(int x) { System.out.println(“x :” + x); super.setX(x); } void print() { System.out.println(x + “, “ + y); }

Dynamic Class Refinement ACM SAC'08, March 18, Turn refinement on and off during runtime refine WebApp when context.lang == “jp” { String price(int itemNo) { return toYen(super.price(itemNo)) + “ yen”; } class WebApp { static Session context; String price(int itemNo) {.. } : }

The GluonJ Language ACM SAC'08, March 18, Static/dynamic class refinement for Java Design goals A normal Java syntax + Java annotations No new language syntax Programming with a normal Java IDE + compiler Manageable runtime type errors Runtime type errors should occur at only limited places Compile-time type checking to a certain degree

Manageable Runtime Type Errors ACM SAC'08, March 18, Without this, dynamic-refinement causes runtime type errors at too many places refine Point when Main.debug { void print() { … } List observer; void add(Observer o) {.. } void notify() {.. } void setX(int ny) { notify(); … } } class Point { int x, y; int setX(int nx) { x = nx; } } Point p = … ; p.add(new Observer()); p.setX(0); p.print(); This causes a type error If Main.debug is false. appended.

Syntax of GluonJ ACM SAC'08, March 18, It is described as a subclass of the original class Logging static class BookLogger extends Book int getPrice() { System.out.println(price); return super.getPrice(); } class refines a Book class. class classes related to each other Indicates the Book class

Calling an Appended Method ACM SAC'08, March 18, To call a newly appended method, an instance of its target class must be cast Book b = …; ((BookPrinter)b).print(); From the type of an original class to the type of class Printing static class BookPrinter extends Book { void print() { System.out.println(“book[“ + title + “,” + price + “]”); }

Bytecode Instrumentation ACM SAC'08, March 18, A class refinement is directly applied to the bytecode of the original class definition. class Book { int getPrice() { System.out.println(price); return orig_getPrice(); } int orig_getPrice() { return price; } Overridden The original getPrice() is renamed. super.getPrice() is replaced

Programming with a Java IDE ACM SAC'08, March 18, class is a standard subclass Enjoy a code assist by a Java IDE A Java IDE recognizes methods/fields appended classes Appended by a class refinement

Dynamic-Refinement ACM SAC'08, March 18, Effective while the specified method is running. Methods/fields are appended to all instances of the target class during that class CflowPrinting static class BookPrinter extends Book { void print() { System.out.println(“book[“ + title + “,” + price + “]”); } Print() is appended only while getBookPrice() is running.

No NoSuchMethodError thrown ACM SAC'08, March 18, For manageable type errors Only explicit type casts may throw a type error. refine PointEx when Main.debug { void print() { … } List observer; void add(Observer o) {.. } void notify() {.. } void setX(int ny) { notify(); … } } class Point { int x, y; int setX(int nx) { x = nx; } } Point p = … ; PointEx pe = (PointEx)p; pe.add(new Observer()); pe.setX(0); pe.print(); May throw a type error Never throws a type error

Bytecode instrumentation (again) ACM SAC'08, March 18, All methods/fields in class are appended. Cast opeartor Before type cast, insert a code for checking whether or not class is effective. replace all occurrences of the type name of refinement classes with their target class names. Methods Throw NoSuchMethodError if condition is not satisfied.

Coding constraints ACM SAC'08, March 18, For manageable type errors Once an explicit type cast succeeds, a type error never happens because class is effective during the lifetime of the value. The value never escapes from the dynamic scope where class is effective. Constraints The value after the type cast cannot be saved in a non-local storage such as a field. This is statically checked.

Coding Constraints (cont.) ACM SAC'08, March 18, A field of the type R appears only within G. 2. The type R is not the return type or one of the parameter types of the method specified as the argument 3. R is not an exception type (i.e. a subclass of Throwable). It is not the parameter to a catch clause. 4. The refinement class R does not override a static method in its original class. Let G is class associated and let G include class R

Related Work ACM SAC'08, March 18, Class Refinement E.g. eJava [Warth’06], AspectJ [Kiczales’01] ContextJ [Costanza’06], CaesarJ [Aracic’06] Extended Java languages AspectJ5, JBoss AOP, AspectWerkz Ad-hoc design with Java annotations Thus such extensions are not understood by a normal Java IDE

Conclusion ACM SAC'08, March 18, GluonJ Static/dynamic class refinement for Java Class refinement is useful for software evolution The Design goals of GluonJ A normal Java syntax + Java annotations No new language syntax Programming with a normal Java IDE + compiler Manageable runtime type errors Runtime type errors should occur at only limited places Compile-time type checking to a certain degree

Thank you ACM SAC'08, March 18,

Class Refinement ACM SAC'08, March 18, An effective technology for software evolution Its concept is similar to subclassing, mixins It allows separating an extension to a class as a module E.g. Changing an existing method in its class Adding new methods, fields, interfaces to an existing class

The Benefit of Refinement ACM SAC'08, March 18, Unlike subclassing and mixins, it directly modifies the definition of a class Editing an original source code is NOT necessary A client does not need to explicitly create an instance of the extended version of that class Subclassing Refinement class A class SubA class A A refinement for A directly modify A a = new SubA();A a = new A();

Various Refinements ACM SAC'08, March 18, Static-refinement It allows statically changing the structure of a class E.g. eJava [Warth’06], AspectJ [Kiczales’01] E.g. ContextJ [Costanza’06], CaesarJ [Aracic’06] Dynamic-refinement It allows switching refinements to a class according to dynamic contexts These features are useful for modularly extending and customizing a program