AOP Framed! Informatics Center Federal University of Pernambuco

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

JML and Aspects: The Benefits of

Programming AspectJ with Eclipse and AJDT, By Example. Chien-Tsun Chen Sep. 21, 2003

AJDT: Getting started with Aspect-Oriented Programming in Eclipse

Chapitre 6 Programmation orientée aspect (AOP)

AOP Tutorial. Written By: Muhammad Asif. Department of Computer Science, Virtual University of Pakistan

A Process for Separation of Crosscutting Grid Concerns

Course 6 7 November Adrian Iftene

c Copyright 2004, Vinicius Cardoso Garcia, Eduardo Kessler Piveta, Daniel Lucrédio, Alexandre Alvaro, Eduardo Santana de Almeida, Antonio Francisco

Chapter 32. Aspect-Oriented Software Development (AOSD) Ian Sommerville 2006 Software Engineering. Chapter 32 Slide 1

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

Aspect-Oriented Programming and Aspect-J

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

Mobile and Context-aware Interactive Systems

Aspect Oriented Programming

Using Aspects to Make Adaptive Object-Models Adaptable

Aspect-Oriented Programming. Danilo Caetano Matias dos Santos Henrique Alberto Brittes Potter Igor Conrado Alves de Lima

On the Impact of Aspect-Oriented Programming on Object-Oriented Metrics

Metrics for AOP: Do Aspects Help?

Using Aspects to Make Adaptive Object-Models Adaptable

Employing Query Technologies for Crosscutting Concern Comprehension

Analyzing effect of Aspect Oriented concepts in design and implementation of design patterns with case study of Observer Pattern

Comparative Evaluation of Programming Paradigms: Separation of Concerns with Object-, Aspect-, and Context-Oriented Programming

Assessing Aspect-Oriented Artifacts: Towards a Tool-Supported Quantitative Method

AOSA - Betriebssystemkomponenten und der Aspektmoderatoransatz

Enterprise Informatization LECTURE

Chapter 21 Aspect-Oriented Software Engineering (AOSE)

Assessing Aspect-Oriented Artifacts: Towards a Tool-Supported Quantitative Method

Applying Aspect Oriented Programming on Security

Course 1 October, 9, Adrian Iftene

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

Demo Proposal. 1 General Information

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

Aspect Oriented Java RMI Server

Implementing Producers/Consumers Problem Using Aspect-Oriented Framework

CSE 70 Final Exam Fall 2009

Copyright IBM Corporation 2004.All rights reserved.

Improving Software Modularity using AOP

AOP 101: Intro to Aspect Oriented Programming. Ernest Hill

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

Idioms for Building Software Frameworks in AspectJ

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

Integration of Application Business Logic and Business Rules with DSL and AOP

Sort-based Refactoring of Crosscutting Concerns to Aspects

SERG. Sort-based Refactoring of Crosscutting Concerns to Aspects

More Refactoring s: Aspect Oriented Programming with AspectJ

Aspect-Oriented Programming

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

Frustrated by all the hype?

On the Interplay of Exception Handling and Design by Contract: An Aspect-Oriented Recovery Approach

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

University of Huddersfield Repository

Information systems modeling. Tomasz Kubik

A Quantitative Assessment

DISCUSSING ASPECTS OF AOP

Towards Reusable Components with Aspects: An Empirical Study on Modularity and Obliviousness

Refactoring Aspect Oriented Software

A Query-Based Approach for the Analysis of Aspect-Oriented Systems by

Aspect Design Pattern for Non Functional Requirements

JAVA GUI PROGRAMMING REVISION TOUR III

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

Crosscutting Interfaces for Aspect-Oriented Modeling *

Introduction to Aspect-Oriented Programming

Application-Oriented System Design

Classes, subclasses, subtyping

Aspect-Orientation from Design to Code

International Association of Scientific Innovation and Research (IASIR) (An Association Unifying the Sciences, Engineering, and Applied Research)

Concurrency Control with Java and Relational Databases

Aspect-Oriented Programming

A short introduction to INF329. Spring AOP

Modeling Aspect-Oriented Change Realizations

Course 7 25 November Adrian Iftene

Evolving Software Product Lines with Aspects: An Empirical Study on Design Stability

Using and Extending AspectJ for Separating Concerns in Parallel Java Code

Code reuse for improving software productivity and quality in AOP

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

TRAP/J v2.1: An improvement for Transparent Adaptation

Language support for AOP

Evolving Software Product Lines with Aspects: An Empirical Study on Design Stability

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Content(2) Contribution of OOT in Software Engineering History of SE Technologies and Contribution of OOT JAIST Koichiro Ochimizu

The Object Oriented Paradigm

JML and Aspects: The Benefits of Instrumenting JML Features with AspectJ

Enforcing Information Hiding in Interface Specifications: with The AspectJML specification language. A Client Aware checking Approach

Identification of Differences Between Aspect-Oriented Programs. Marija Katic, PhD Student

Bugdel: An Aspect-Oriented Debugging System

A Model for Software Plans

Object-Oriented Design

Aspect-Oriented Software Development

Chapter 7. Modular Refactoring. 7.1 Introduction to Modular Refactoring

Concepts of Programming Languages

"Learn to do Verification with AOP? We've just learned OOP!"

TransJ: An Abstract Independent-Framework for Weaving Crosscutting Concern into Distributed Transactions

Unit-Testing Aspectual Behavior

Take Control with AspectJ

Language Oriented Modularity: From Theory to Practice

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

OS Customization versus OS Code Modularity

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Domain Knowledge Driven Program Analysis

Transcription:

AOP Framed! Henrique Rebêlo Informatics Center Federal University of Pernambuco Henrique Rebêlo 2009

Contacting Me Ph.D. student Henrique Rebêlo Specialist on AOSD, DbC, Static Metrics, JML Software Architecture, Product Lines, Empirical Studies email: hemr@cin.ufpe.br Informatics Center Federal University of Pernambuco, UFPE Brazil

Talk Material - Contributions Prof. Paulo Borba (expert on aspect-oriented languages and refactoring) Prof. Sérgio Soares (expert on aspect-oriented software development) Prof. Alessandro Garcia (expert on aspect-oriented software assessment metrics) Prof. Fernando Castor Filho (expert on exception handling)

talk timeline OO, concerns, tangle, scatter, crosscut AOP AOSD and AspectJ overview research with AOP AOP in industry conclusion UCF 2009

OO scatter tangle exploring some of the terms we use crosscut and all that Copyright 2009, Henrique Rebêlo.

Object-Oriented Paradigm (OOP) object oriented decomposition modularization objects classes encapsulated data and procedures problem:? 1960 1965 1970 1975 1980 1985 1990 1995 2000 2005 CLOS Simula 67 Smalltalk Eiffel C++ Java

Bad OO Design G D COMUNICATION BUSINESST A P bl t li f d ith Problem: tangling of code with different purposes

Good OO Design Graphical user interface (GUI) Comunication Business Data

Layered Architecture The various kinds of code should be written separately Separation of concepts concerns kinds of code separation of concerns

Need for Separation of Concerns Large complex distributed software systems Development requires focusing on one concern at a time

Separation of Concerns A concern is a specific requirement or consideration that must be addressed in order to satisfy the overall system goal The principle was coined by Edsger Dijkstra 1974 paper "On the Role of Scientific Thought" ht" he received the 1972 A. M. Turing Award for fundamental contributions in the area of programming languages

Effective Separation of Concerns improve... Quality attributtes stability reusability evolvability traceability composability maintainability reliability testability...

Good modularization without persistence, distribution,... public class Programa { Data [STAThread] public static void Main(string[] args) { Banco fachada = Banco.GetInstance(); Programa.menu(fachada); public static void menu(banco fachada) { string numero = null; double valor = 0.0; Conta conta = null; int opcao = 1; while (opcao!= 0) { try { System.Console.Out.WriteLine("Aperte <Enter> para continuar"); Util.Util.waitEnter(); System.Console.Out.WriteLine("\n\n\n\n\n\n\n"); System.Console.Out.WriteLine("Escolha uma das alternativas abaixo:"); System.Console.Out.WriteLine("1 - Cadastrar Conta"); System.Console.Out.WriteLine("2 - Creditar"); System.Console.Out.WriteLine("3 - Debitar"); System.Console.Out.WriteLine("4 - Transferir"); System.Console.Out.WriteLine("5 - Ver Saldo"); System.Console.Out.WriteLine("0 - Sair"); opcao = Util.Util.readInt(); switch (opcao) { Interface Business public class Banco { private CadastroContas contas; private Banco() { contas = new CadastroConta (new RepositorioContasAccess()); public class CadastroContas { private RepositorioContas contas; public void Debitar(string numero, double valor) { Conta c = contas.procurar(numero); c.debitar(valor); public class RepositorioContasArray : RepositorioContas { private Conta[] contas; private int indice; public RepositorioContasArray() { contas = new Conta[100]; indice = 0; public void Cadastrar(Conta conta) { public void Transferir(string numerode, string n contas.cadastrar(conta); umeropara, double valor) { public void Inserir(Conta conta) { Conta de = contas.procurar(numerode); contas[indice] = conta; Conta para = contas.procurar(numeropara); indice = indice + 1; de.debitar(valor); para.creditar(valor); public void Transferir(string numerode, string n umeropara, double valor) { contas.transferir(numerode, numeropara, valor); public double Saldo(string numero) { Conta c = contas.procurar(numero); return c.saldo; public void Atualizar(Conta conta) { int i = GetIndice(conta.Numero); if (i == indice) { throw new ContaNaoEncontradaException(conta.Numero); else { contas[i].atualizar(conta); [] public class Conta { private string numero; private double saldo; public void Creditar(double valor) { this.saldo = this.saldo + valor; public void Debitar(double valor) { if (valor > saldo) { throw new SaldoInsuficienteException)); else { this.saldo = this.saldo - valor; public void Atualizar(Conta c) { this.numero = c.numero; this.saldo = c.saldo;

We have problems with persistence using JDBC public class Banco { private CadastroContas contas; private Banco() { contas = new CadastroConta (new RepositorioContasAccess()); public void Cadastrar(Conta conta) { Persistence.DBHandler.StartTransaction(); try { contas.cadastrar(conta); Persistence.DBHandler.CommitTransaction(); catch (System.Exception ex){ Persistence.DBHandler.RollBackTransaction(); throw ex; public void Transferir(string numerode, string n umeropara, double valor) { Persistence.DBHandler.StartTransaction(); try { contas.transferir(numerode, numeropara, valor); Persistence.DBHandler.CommitTransaction(); catch (System.Exception ex){ Persistence.DBHandler.RollBackTransaction(); throw ex; public class CadastroContas { private RepositorioContas contas; public void Debitar(string numero, double valor) { Conta c = contas.procurar(numero); c.debitar(valor); contas.atualizar(c); public void Transferir(string numerode, string n umeropara, double valor) { Conta de = contas.procurar(numerode); Conta para = contas.procurar(numeropara); de.debitar(valor); para.creditar(valor); contas.atualizar(de); contas.atualizar(para); public double Saldo(string numero) { Conta c = contas.procurar(numero); return c.saldo; public class RepositorioContasAccess : RepositorioContas { public void Inserir(Conta conta) { string sql = "INSERT INTO Conta (NUMERO,SALDO) VALUES ('" + conta.numero + "'," + conta.saldo + ")"; OleDbCommand insertcommand = new OleDbCommand (sql,dbhandler.connection,dbhandler.transaction); insertcommand.executenonquery(); public void Atualizar(Conta conta) { string sql = "UPDATE Conta SET SALDO = (" + conta.as ldo + ") WHERE NUMERO = '" + conta.numero + "'"; OleDbCommand updatecommand = new OleDbCommand(s ql,dbhandler.connection,dbhandler.transaction); int linhasafetadas; linhasafetadas = updatecommand.executenonquery(); if (linhasafetadas == 0) { throw new ContaNaoEncontradaException(conta.Numero); public class DBHandler { private static OleDbConnection connection; public static OleDbConnection Connection { get { if (connection == null) { string datasource = "BancoCS.mdb"; string strconexao = "Provider= Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + datasource; connection = new OleDbConnection(strConexao); return connection; public class Conta { private string numero; private double saldo; public void Creditar(double valor) { this.saldo = this.saldo + valor; public void Debitar(double valor) { if (valor > saldo) { throw new SaldoInsuficienteException)); else { this.saldo = this.saldo - valor; public void Atualizar(Conta c) { this.numero = c.numero; this.saldo = c.saldo; JDBC code in red public static OleDbConnection GetOpenConnection() { Connection.Open(); return Connection; public static void StartTransaction() { Connection.Open(); transaction = Connection.BeginTransaction();

What is the problem? Bad modularization of system-wide, peripheral p concerns at the implementation level Symptoms Code tangling Code scattering

Problems with OO implementation Code tangling public class Banco { private CadastroContas contas; private Banco() { contas = new CadastroConta (new RepositorioContasAccess()); public void Cadastrar(Conta conta) { Persistence.DBHandler.StartTransaction(); try { contas.cadastrar(conta); Persistence.DBHandler.CommitTransaction(); catch (System.Exception ex){ Persistence.DBHandler.RollBackTransaction(); throw ex; public void Transferir(string numerode, string n umeropara, double valor) { Persistence.DBHandler.StartTransaction(); try { contas.transferir(numerode, numeropara, valor); Persistence.DBHandler.CommitTransaction(); catch (System.Exception ex){ Persistence.DBHandler.RollBackTransaction(); ll i throw ex; public class CadastroContas { private RepositorioContas contas; public void Debitar(string numero, double valor) { Conta c = contas.procurar(numero); c.debitar(valor); contas.atualizar(c); public void Transferir(string numerode, string n umeropara, double valor) { Conta de = contas.procurar(numerode); Conta para = contas.procurar(numeropara); de.debitar(valor); para.creditar(valor); contas.atualizar(de); contas.atualizar(para); public double Saldo(string numero) { Conta c = contas.procurar(numero); return c.saldo; public class DBHandler { private static OleDbConnection connection; public static OleDbConnection Connection { get { if (connection == null) { string datasource = "BancoCS.mdb"; string strconexao = "Provider= Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + datasource; connection = new OleDbConnection(strConexao); return connection; public static OleDbConnection GetOpenConnection() { Connection.Open(); return Connection; public static void StartTransaction() { Connection.Open(); transaction = Connection.BeginTransaction(); Code scattering

Problem with OO Code related to such concerns are crosscutting Scattered across several modules of implementation (classes) Each color is one different concern

Redundance More OO Problems several similar code fragments in several places of the source code Weak cohesion methods of the affected classes contain instructions that are not directed related to the core concern in which they implement Strong coupling methods of the affected classes need to be aware of methods from classes s which implement the scattered funcionality

The Problem of Crosscutting Concerns Certain properties naturally have global, pervasive effects over the system decompositions examples: exception handling, distribution, persistence OO Decomposition Crosscutting Concerns Classes Hamper software reusability, maintainability, and the like p y, y, Domain-specific programming languages started to emerge...

Persistence is a crosscutting concern public class Banco { private CadastroContas contas; public class CadastroContas { private RepositorioContas contas; public class RepositorioContasAccess : RepositorioContas { public void Inserir(Conta conta) { string sql = "INSERT INTO Conta (NUMERO,SALDO) private Banco() { public void Debitar(string numero, double valor) { VALUES ('" + conta.numero + "'," + conta.saldo + ")"; contas = new CadastroConta Conta c = contas.procurar(numero); OleDbCommand insertcommand = new OleDbCommand (new RepositorioContasAccess()); c.debitar(valor); (sql,dbhandler.connection,dbhandler.transaction); contas.atualizar(c); insertcommand.executenonquery(); public void Cadastrar(Conta conta) { Persistence.DBHandler.StartTransaction(); public void Transferir(string numerode, string n public void Atualizar(Conta conta) { try { umeropara, double valor) { string sql = "UPDATE Conta SET SALDO = (" + conta.as contas.cadastrar(conta); Conta de = contas.procurar(numerode); ldo + ") WHERE NUMERO = '" + conta.numero + "'"; Persistence.DBHandler.CommitTransaction(); Conta para = contas.procurar(numeropara); OleDbCommand updatecommand = new OleDbCommand(s catch (System.Exception ti ex){ de.debitar(valor); ql,dbhandler.connection,dbhandler.transaction); public void Atualizar(Conta c) Persistence.DBHandler.RollBackTransaction(); para.creditar(valor); int linhasafetadas; throw ex; contas.atualizar(de); linhasafetadas = updatecommand.executenonquery(); contas.atualizar(para); if (linhasafetadas == 0) { throw new ContaNaoEncontradaException(conta.Numero); public void Transferir(string numerode, string n public double Saldo(string numero) { umeropara, double valor) { Conta c = contas.procurar(numero); Persistence.DBHandler.StartTransaction(); return c.saldo; try { contas.transferir(numerode, numeropara, valor); Persistence.DBHandler.CommitTransaction(); public class DBHandler { catch (System.Exception ex){ Persistence.DBHandler.RollBackTransaction(); throw ex; Persistence code in red crosscuts business private static OleDbConnection connection; public static OleDbConnection Connection { get { if (connection == null) { string datasource = "BancoCS.mdb"; string strconexao = "Provider= Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + datasource; connection = new OleDbConnection(strConexao); return connection; public static OleDbConnection GetOpenConnection() { Connection.Open(); return Connection; public static void StartTransaction() { Connection.Open(); transaction = Connection.BeginTransaction(); public class Conta { private string numero; private double saldo; public void Creditar(double valor) { this.saldo = this.saldo + valor; public void Debitar(double valor) { if (valor > saldo) { throw new SaldoInsuficienteException)); else { this.saldo = this.saldo - valor; bli id At li (C t ) { this.numero = c.numero; this.saldo = c.saldo;

Crosscutting concerns... cannot be properly modularized by OO constructs because their realization affects the behavior of several methods, possibly in several classes, cutting across class structures

Tangling and scattering of... Concerns Persistence Use cases Concurrency Performance control Design by Logging Contract Security Events

Generalizing the Problem Primary Functionality Persistence distribution Data Classes Account Loan Customer User Interface ATM Web PC Terminal

Root of Cause Copyright 2002, Silvio do Lago Pereira. Persistence The concern is crosscutting to the dimension of the implementation

AOP AOSD and exploring another way to promote modularization AspectJ overview Copyright 2009, Henrique Rebêlo.

Aspect-oriented languages are quite popular... due to the promise of modularizing crosscutting concerns

Aspect-Oriented Programming (AOP) Distribution Security Data Management AOP tools, techniques and methodology Distribution Security Data Management

Revisiting the Example Primary Functionality Persistence Distribution Data Classes Account Loan Customer User Interface ATM Web PC Terminal

Wouldn t itbeniceif if Data Classes Account aspect Persistence Loan aspect Distribution Customer User Interface ATM Web PC Terminal

From your experience Which other system concerns are typically y crosscutting?

Quantification is the basic concept of AOP T m() T m() Advice = m() m() m() m() m() m() m() m() m() m() m() m()

The success of AOP Persistence code is localized, can be understood in isolation, part of it can be reused Business code can be reused to work with other persistence APIs Business code is not invaded by changes to persistence APIs Less code, more code units

Towards to Aspect Oriented Software Development (AOSD)

Step 1: Concern identification (decomposition) Concern Identification Concerns Requirements Requirements engineering

Examples of Identified Concerns Graphical user interface Distribution Data management Business rules Concurrency control

Step 2: Concern Implementation

Step 3: System Recomposition classes and interfaces aspects W E A V E R Executable System

Aspect Oriented Software Development Requisitos concern identification concerns W OOP Classes e a v AOP Aspects e r Program Business rules Concurrency control Data management Graphical user interface Distribution

OOP vs. AOP Business rules Concurrency control Data management Graphical user interface Distribution

Context: Objects vs. Aspects Aspect-oriented programming (AOP) modularizing crosscutting concerns (CCCs) OO Decomposition Crosscutting Concerns Aspect-Oriented Decomposition Aspect Join Points Classes enhancing reusability, maintainability, and the like AspectJ: most popular aspect-oriented language

Aspect-Oriented Programming (AOP): The AspectJ Approach Classes Class A Class B Aspect Weaver Composed Code Aspect X Aspects Aspect Y Legend Class Aspect Join Points: Reference points in classes used by aspects for specifying relationships with classes

Pointcuts define a set of join points join points can be: calls and executions of methods (and also constructors) field access exception handling static initialization joint point composition &&, e!

Advice in AspectJ provides extra behavior at join points Define additional code that should be executed... before after after returning after throwing or around join points

Persistence at facade public class Banco { private CadastroContas contas;... public void cadastrar(conta t conta) { try { getpm().starttransaction(); contas.cadastrar(conta);... getpm().committransaction(); catch (Exception ex){ getpm().rollbacktransaction();......

Pointcut (with a simple alternative) pointcut transmethods(): execution (public * Banco.cadastrar(..)) execution (public * Banco.creditar(..)) execution (public * Banco.deditar(..))... pointcut transmethods(): execution (public * Banco.*(..));

Advice for normal flow public aspect PersistenceAspect { before(): transmethods() { getpm().starttransaction(); after() returning(): transmethods() { getpm().committransaction();

Advice for exceptional flow Exception is not captured after() throwing(): transmethods() { getpm().rollbacktransaction();...

Besides dynamic crosscutting with advice... AspectJ also supports static crosscutting change the relation of subtypes add members into classes Inter-type declarations

Static crosscutting pointcut TransMethods(): execution (public * Trans.*(..)); private interface Trans { public void cadastrar(conta conta);... declare parents: Banco implements Trans; interface locall to the aspect changing the hierarchy

Research with discussing about some researches that use AOP AOP Copyright 2009, Henrique Rebêlo.

Research with AOP There are several other Exception handling improvement Exception handling refactoring AO refactoring...

AOP in discussing about the adoption of AOP in idustry industry Copyright 2009, Henrique Rebêlo.

AOP in Industry Used tools Spring AOP JBoss AOP AspectJ PostSharp (.NET) Compose* EOS (.NET) Glassbox

Aspects in some Brazilian companies In most cases this means Spring AOP, but AspectJ is used too Popular concerns Transactional control Access control Logging Security Business code with reduced Business code with reduced maintenance and more reuse

IBM Aspects in some known supports AspectJ Motorola WEAVR Microsoft companies Policy Injection Application Block

Conclusions OO appeared with Simula 67 became popular in the industry after... 25 years? design patterns AOP has 15 years some use in big companies

AOP Framed! Henrique Rebêlo Informatics Center Federal University of Pernambuco Henrique Rebêlo 2009