So, What is an Aspect?

Similar documents
Course 6 7 November Adrian Iftene

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Information systems modeling. Tomasz Kubik

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Aspect-Oriented Programming and Aspect-J

Aspect-Oriented Programming and AspectJ

Aspect Oriented Programming with AspectJ. Ted Leung Sauria Associates, LLC

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Chapter 5 Object-Oriented Programming

A short introduction to INF329. Spring AOP

AOP 101: Intro to Aspect Oriented Programming. Ernest Hill

Aspect-Oriented Programming

Polymorphism. return a.doublevalue() + b.doublevalue();

Aspect-Oriented Programming with C++ and AspectC++

Objects and Iterators

Object-Oriented Programming

Separation of Concerns. AspectJ. What if the concerns are Cross-Cutting? SoC: Programming Paradigms. Key theme: Modularity and Encapsulation

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Conversions and Casting

CMSC 132: Object-Oriented Programming II

15CS45 : OBJECT ORIENTED CONCEPTS

Object Oriented Programming

Programming II (CS300)

AspectC++ A Language Overview

JML and Aspects: The Benefits of

Introduction to Aspect-Oriented Programming

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Introduction to Programming Using Java (98-388)

6.Introducing Classes 9. Exceptions

Practice for Chapter 11

A Brief Introduction to Aspect-Oriented Programming. Historical View Of Languages. Procedural language Functional language Object-Oriented language

A Brief Introduction to Aspect-Oriented Programming" Historical View Of Languages"

What is AOP? Business Logic Requirements Concern Identifier Security Logging (Laddad, 2003, p. 9) What is AOP? Non-AOP implementation of crosscutting

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Around Weaving in abc

Program Instrumentation for Debugging and Monitoring with AspectC++

Advanced Java Programming

Introduction to Aspect-Oriented Programming

Programming II (CS300)

Programming Languages

A Basis for AspectJ Refactoring

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Specifying Pointcuts in AspectJ

Motivation. Ability is what you're capable of doing. Motivation determines what you do. Attitude determines how well you do it.

Comp215: Thinking Generically

CH. 2 OBJECT-ORIENTED PROGRAMMING

Lecture 9 : Basics of Reflection in Java

Compaq Interview Questions And Answers

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Introduction to. Bruno Harbulot. ESNW, the University of Manchester.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Bugdel: An Aspect-Oriented Debugging System

Introflection. Dave Landers BEA Systems, Inc.

Chapter 14 Abstract Classes and Interfaces

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Idioms for Building Software Frameworks in AspectJ

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Aspect Oriented Programming

Type Hierarchy. Lecture 6: OOP, autumn 2003

Some language elements described in this text are not yet supported in the current JAsCo version (0.8.x). These are: Multiple hook constructors

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Java Object Oriented Design. CSC207 Fall 2014

What is Inheritance?

Java: introduction to object-oriented features

Class, Variable, Constructor, Object, Method Questions

Method Slots: Supporting Methods, Events, and Advices by a Single Language Construct

Inheritance and Interfaces

Chapitre 6 Programmation orientée aspect (AOP)

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Advanced AspectJ. This chapter covers

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Using and Extending AspectJ for Separating Concerns in Parallel Java Code

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Object-Oriented Design. March 2005 Object Oriented Design 1

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Logistics. Final Exam on Friday at 3pm in CHEM 102

CS 11 java track: lecture 3

An Aspect-Oriented Approach. Henrique Rebêlo Informatics Center

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Using Aspect-Oriented Programming to extend Protégé. Henrik Eriksson Linköping University

Programming Paradigms, Fall 06

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Refactoring Aspect Oriented Software

Chapter 7. Modular Refactoring. 7.1 Introduction to Modular Refactoring

Language-Independent Aspect-Oriented Programming

SCALA AND ASPECTJ. Approaching Modularizing of Crosscutting. Ramnivas Laddad. Concerns. ramnivas

XFindBugs: extended FindBugs for AspectJ

Notes on Java Programming IST JAV 2012 S. Frénot

CMSC 132: Object-Oriented Programming II

AJDT: Getting started with Aspect-Oriented Programming in Eclipse

Transcription:

Introduction to AspectJ Aspect-oriented paradigm AspectJ constructs Types of Join Points Primitive Lexical designators Type designators Control flow Types of Advice Before After Around Receptions Join Points Java method dispatch Vs. executions join points Exposing Context with thisjoinpoint, thisstaticjoinpoint Other ways to expose context modularly Aspect Instantiation The of clause Lexical Introduction Additional members Extension & implementation Private introduction Aspect Extension Abstract aspects Aspect Privilege Aspect Composition Priority & domination Recursion Additional Topics Throwing checked exceptions Current compiler limitations

AspectJ & AOP General-purpose aspect-oriented extension to Java Grew out of coordination library (COOL) Aspect-oriented programming allows you to modularize concerns that would otherwise cut across object-oriented program logic Logging/Tracing Session Management Coordination Why use aspects? MODULARITY Conditional compilation made easy Implementation of pluggable features debugging Aspects can implement features necessary for correctness of programs synchronization reactivity Aspects can introduce space or time optimizations caching

So, What is an Aspect? Modular unit of crosscutting implementation An AspectJ aspect is a crosscutting type consisting of advice on pointcuts lexical introduction of behavior into other types Like classes, aspects can have internal state and behavior, can extend other aspects and classes, and can implement interfaces

Advice on Join Points Join point : a well-defined location at a point in the execution of a program the execution of the method public void A.foo(int) the static initialization of class A Pointcut : a set of join points all method calls to class B within class A all mutations of fields of class A outside of A's subclasses Advice : code designed to run automatically at all join points in a particular pointcut can be marked as before, after,or around (in place of) the join points in the pointcut Lexical introduction : adding functionality to a class in place (as opposed to extending it) For example, making class A implement Cloneable

Composition of Join Points Use &&! Use defined pointcuts in other pointcuts pointcut foocalls(): calls(int Bar.foo()) && within(myclass); pointcut interestingclasses(): instanceof(mypackage..*); pointcut interestingreceptions(): ( receptions(* *(..)) receptions(new(..)) ) && interestingclasses(); pointcut nonstaticmethods(): executions(!static *(..));

Types of Join Points Primitive: initializations( GTN ) staticinitializations( GTN ) receptions( Signature ) executions( Signature ) calls( Signature ) callsto( PCD ) Lexical extents: within( GTN ) withinall( GTN ) withincode( Signature ) Type designators: instanceof( GTN ) hasaspect( GAN ) sets( Signature )[oldval ][newval ] gets( Signature )[value ] handlers( throwable type name ) Control Flow: cflow( PCD ) cflowtop( PCD )

Types of Advice before() : pointcut { advice } after() returning() : pointcut { advice } advice runs if join point computation concludes successfully after() throwing() : pointcut { advice } advice runs if join point computation throws an exception after() : pointcut { advice } advice runs in either case, and after the others around() returns type : pointcut { advice } return type widening advice must return a value advice must explicitly act to proceed with join point computation if the computation is to continue at all Because the flow of control dips through the advice, it can modify method arguments and/or the return value Implements a middle wrapping layer that is completely modular -- neither caller or receiver need to know about it

Advice Priority If more than one advice block affects the same join point, they operate in this order: around advice is run most specific first before advice is run most specific first after advice is run least specific first Of course, if any around advice executes that does not continue with join point computation, no other advice runs for the join point

Receptions Join Points Related to the idea of object-oriented message-passing Java method dispatch There are two ways to execute public, non-static methods in Java: a.foo() - dispatch occurs at runtime based on runtime type of a super.foo() receptions A receptions A receptions - the implementation to use is known at compile time join points occur at runtime dispatch join point never catches superclass calls join point does not occur at a place in the code - cannot be used with lexical constructs like within! receptions vs. executions join points receptions vs. calls join points

Exposing Context - Part I thisjoinpoint is statically typed as JoinPoint but is actually a MethodExecutionJoinPoint,a HandlerJoinPoint, or whatever JoinPoint is actually an interface hierarchy Cast thisjoinpoint to the proper type (if necessary for the information you need) thisstaticjoinpoint a lightweight join point object similar to thisjoinpoint but only static information is available a StaticJoinPointException is thrown if you ask for more Package org.aspectj.lang contains: JoinPoint Signature SourceLocation Package org.aspectj.lang.reflect JoinPoint subinterfaces Signature subinterfaces contains: StaticJoinPointException These packages are not automatically imported for you

Exposing Context - Part II If we have a pointcut: pointcut foocalls() : calls(bar.foo(int));... but we really want to know what that int is, we can write: pointcut foocalls(int i) : calls(int Bar.foo(i)); We then write advice constructs like these: before(int i) : foocalls(i) { System.out.println("The int is " + i + "!"); } after(int i) returning(int j) : calls(int Bar.foo(i)) { System.out.println("Bar.foo(" + i + ") returned " + j); } around(int i) returns int : receptions(int Bar.foo(i)) { // double the argument, halve the result return proceed(2*i)/2; }

Exposing Context - Part III Exposing context can be very useful pointcut guardedints(int oldval, int val) : sets(int Foo.*)[oldval][val]; around(int oldval, int val) returns int : guardedints(oldval, val) { if(math.abs(oldval - val) > 5) throw new RuntimeException("Delta too big -> " + oldval + " to " + val); return proceed(oldval, val); }

Aspect Instances Aspects cannot be instantiated with new and may only have nullary constructors, even if they extend classes of clauses of eachjvm() This is the default, one aspect instance for the whole virtual machine You can use FooAspect.aspectOf() to get the singleton instance of FooAspect of eachobject( PCD ) Associate a shadow aspect instance with every object in the Each pointcut has an implicit hasaspect() PCD You can use FooAspect.aspectOf(obj) to get the instance of FooAspect associated with obj throws an NoAspectBoundException of eachclass( PCD ) on error Part of the AspectJ language, but not yet implemented in the compiler of eachcflowroot( PCD ) Control flow entering each join point in the PCD get an aspect instance

Lexical Introduction Making a class extend another or implement an interface Foo +extends Bar; Foo +implements Cloneable; Introduction of state and behavior protected static int Foo.i; public Vector (Foo Bar).aVector = new Vector(); Or, if you have a lot of classes to introduce into... interface I { } String I.foo() { return "some string"; } int I.someInt = 5; (Foo Bar Bat Bam SomePackage..*) +implements I; Private introduction Private to the aspect, not to the class Guaranteed not to cause conflicts Currently a problem with making classes Serializable since private writeobject() and readobject() methods are required

Aspect Extension Aspects can extend classes other aspects that are explicitly labelled abstract pointcuts are inherited abstract pointcuts can be extended of clause inherited

Aspect Privilege Way too powerful right now, may be more controlled later Declare an aspect privileged and it has access to all private members of all classes class C { private int i; C() { i = 3; } } privileged aspect A { after(c c) : executions(c.new(..)) { c.i = 4; } }

Composition of Aspects Watch out! Aspect priority and domination Recursion -- aspects affecting themselves

Additional Notes Compiler Limitations Throwing checked exceptions within advice of eachclass() preprocessing -- source level only! introducing Serializable (for now)