Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Terms and Rules Professor Evan Korth New York University (All rights reserved)
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 8 More Object Concepts
Intro to OOP with Java, C. Thomas Wu
4.1 Instance Variables, Constructors, and Methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Reviewing Recent ICSE Proceedings For:.  Defining and Continuous Checking of Structural Program Dependencies  Automatic Inference of Structural Changes.
A Meta-Level Specification and Profile for AspectJ in UML Joerg Evermann School of Information Management Victoria University of Wellington.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Topic 4 Inheritance.
Programming in Java CSCI-2220 Object Oriented Programming.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
AspectJ – AOP for Java Tom Janofsky. Instructor at Penn State Abington Consultant with Chariot Solutions JUG Member.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Introduction to Object-Oriented Programming Lesson 2.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
1 Aspectual Caml an Aspect-Oriented Functional Language Hideaki Tatsuzawa Hidehiko Masuhara Akinori Yonezawa University of Tokyo.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
Conclusão Parte 1 – Qualidade de Software por Construção Alessandro Garcia © Alessandro Garcia, 2016 Departamento de Informática.
Lecture 12 Inheritance.
Inheritance and Polymorphism
Java Programming Language
Packages and Interfaces
Aspect Oriented Programming
Presentation transcript:

Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009

So far … We have looked at augmenting behaviour of classes using aspects –Aspects are defined with reference to the nodes in the object call graph (the join points) Now we will look at another feature of AspectJ –Ability to add new fields, methods, constructors as well as inheritance relationships

Aims Provide an overview of AspectJ support for inter-type declarations – Introduce methods and attributes – Add relationships to the classes – Specify warnings or detect errors with respect to program constraints – Deal with aspect interactions

Inter-type Declarations Also known as introductions Means to inject additional code into classes –The introduced fields, methods, etc. belong to the class though are declared in an aspect Which means that a dereferencing of the Java special variable this returns ……? ?

Inter-type Field Declarations public Customer Account.owner; Visibility: public, or private. Cannot be protected. Type of the introduced field Type in which the field is injected The name of the field being introduced public Customer Account.owner = new Customer(“John Doe”); Can also initialise the introduced field

Inter-type Method Declarations private void Account.addMoney(int amount) { // code } Visibility: public, or private. Cannot be protected. Type in which the method is injected Signature of the method. May include a throws clause Note: this here will be bound to the Account instance on which the method is being executed and not to the aspect from which the introduction is done.

Inter-type Constructor Declarations Similar to inter-type method declarations public Account. new (int accoutID) { // code } Visibility: public, private or default. Cannot be protected. Type in which the constructor is injected Signature of the constructor. May include a throws clause Note: Would probably be good to introduce a field to capture the account ID supplied as an argument to the constructor.

Declaring Parents You can also use inter-type declarations to add extends or implements relationships to your classes Takes the form –declare parents: extends ; –declare parents: implements ;

Declaring Parents declare parents: AnInterface extends AnotherInterface; declare parents: com.lancs.* implements Serializable; declare parents: (CheckingAccount && SavingsAccount) extends Account; declare parents: (CheckingAccount || SavingsAccount) extends Account; ?

Declaring Warnings A compile-time mechanism to warn developers of constraints on a program Takes the form: –declare warning: : “ ”; declare warning: call(String *.toString(..)): “Not a good idea to call toString( ) methods”;

Declaring Errors A compile-time mechanism to ensure that developers don’t violate certain constraints Takes the form: –declare error: : “ ”; declare error: call(String *.toString(..)): “Not allowed to call toString( ) methods”;

Declaring Warnings and Errors A very good way to find relevant code for refactoring –Suppose you wanted to factor out all calls to addListener( ) methods from your code aspect HelpRefactor { declare warning: call(* *.addListener(..)): “Point out all calls to addListener methods”; } When you build the project with Eclipse the compiler will warn you of all the place where the above warning applies. You can navigate to those points and factor out the calls into an aspect. Later you can declare it as an error so that other developers only put calls to addListener in an aspect.

Declaring Errors and Warnings Only suitable with pointcuts specification that can be evaluated at compile-time –Cannot be used with this( ), args( ), target( ), cflow( ), etc. Depend on dynamic context

Aspect Interaction aspect Security { pointcut getCalls( ): call(* get*(..)) && target(Account); before( ): getCalls( ) { // do security } aspect ShowBalance { pointcut getCalls( ): call(* get*(..)) && target(Account); before( ): getCalls( ) { // show remaining balance } There is no guarantee which advice is going to be executed first!

Declaring Precedence We would want the Security check to happen before the balance is shown by the ShowBalance aspect We can declare such precedence explicitly –Takes the form: declare precedence: ; –Can have a declare precedence statement in any aspect –Good practice to have it as a separate aspect Precedence cuts across aspects!

Declaring Precedence aspect AspectPrecedence { declare precedence: Security, ShowBalance; } aspect AspectPrecedence { declare precedence: Security, *; } Always do Security check before anything else.

Exercise In the Tracing example, include an aspect that: –registers the history of the previous balances of Account object Use inter-type declarations The History aspect must always have precedence over the Tracing aspect Include a third aspect that introduces the same elements (same signatures) into the same classes –Check what happens!

In Summary We have looked at a range of features of the AspectJ language –Notion of join point –Pointcuts and the various pointcut designators –Advice –Aspect inheritance –Inter-type declarations –Aspect interaction resolution

In Summary There are a number of features of AspectJ we have not covered –Some pointcut designators Initialisation, advice execution, etc. –Aspect instantiation models Our aspects have been singletons Aspects can be instantiated based on join point matching –Exception softening –Etc… Refer to the course texts and the AspectJ documentation for details of these