Introduccion a AspectJ
-
Upload
mauricio-quezada -
Category
Technology
-
view
1.539 -
download
0
description
Transcript of Introduccion a AspectJ
![Page 1: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/1.jpg)
Introducción a AspectJ
Mauricio Quezada07/01/11
![Page 2: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/2.jpg)
Lo que queremos
public Important complexMethod() {// application logic onlyreturn anImportantResult;
}
![Page 3: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/3.jpg)
Como es en realidad…
public Important complexMethod() {LOG.log(“Entering the method..”);// complex logic...mutex.lock();// more complex logic...cipher.encrypt(message);
// ...Connection con = getConnection();// ...return anImportantResult;
}
![Page 4: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/4.jpg)
Aspectos
• Modularizan cross-cutting concerns
![Page 5: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/5.jpg)
Aspectos
• Modularizan cross-cutting concerns– Logging, seguridad, sincronización, control de acceso …
![Page 6: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/6.jpg)
Aspectos
• Modularizan cross-cutting concerns– Logging, seguridad, sincronización, control de acceso …
before(): call(complexMethod(..)) {
LOG.log(“Calling teh method”);
}
![Page 7: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/7.jpg)
AspectJ
• Lenguaje orientado a aspectos para Java
• Define su propio Join Point Model:
Join points + pointcuts + advices = aspect
![Page 8: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/8.jpg)
¿Qué es el Join Point Model?
• Es el modelo que define cuáles son los:
– Join Points: Puntos de ejecución• Ej. Llamada de un método, lanzamiento de una excepción
– Pointcut: Predicados que “atrapan” a los join points• Ej. Llamar al método foo(), lanzar una excepción de tipo Exception
– Advice: Acción a realizar dado un pointcut
![Page 9: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/9.jpg)
En AspectJ
pointcut move(): call(public void FigureElement.setXY(int,int));
before(): move() {System.out.println(“about to move”);
}
after(): move() { System.out.println(“moved”);
}
![Page 10: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/10.jpg)
Wildcards
*Account = UserAccount, AdminAccount, ...
java.*.Date = java.util.Date, java.sql.Date, ...
javax..*Model+ = TableModel + subclasses + TreeModel + subclasses + ...
![Page 11: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/11.jpg)
Pointcuts
call(public void Account.*(int));
call(* *.*(int)) || call(* *.*(double));
execution(public !final * *.*(..)) && within(Dao+);
![Page 12: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/12.jpg)
Pointcuts
pointcut creditOp(Account acc, float amt) :call (void Account.credit(float))&& target (acc)&& args (amt);
pointcut creditOp2(Account acc, float amt) :execution (void Account.credit(float))&& this(acc)&& args (amt);
![Page 13: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/13.jpg)
Advices
before(): call(* Account.*(..)) { ... }
after(): call(...) { ... }
Object around(): move() {// ...Object ret = proceed();// ...return ret;
}
![Page 14: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/14.jpg)
Sí capitán, estamos listos!
Ejemplos
![Page 15: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/15.jpg)
Log
abstract aspect Log {abstract pointcut myClass();
pointcut myConstr():myClass() && execution(new(..));
pointcut myMethod():myClass() && execution(* *(..));
![Page 16: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/16.jpg)
Log (2)
after() throwing (Error e):myMethod() {
LOG.getInstance().write(e);}
before(): myConstr() {LOG.getInstance().write
(“Creating object”);}
}
![Page 17: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/17.jpg)
Log (3)
public aspect LogMyClasses extends Log {
pointcut myClass():within( Account ) ||within( Dao+ ) ||within( Controller );
}
![Page 18: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/18.jpg)
TimerLog
public abstract aspect TimerLog {abstract pointcut myMethod();
before(): myMethod () {
Timer.start();System.out.println(“Timer started at “ + t.startTime);
}
}
![Page 19: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/19.jpg)
Transparent RMI
public interface Receiver extends Remote {public void receive(String name, Object[] params) throws RemoteException;
}
abstract aspect TransparentRMI {abstract pointcut myInterceptedClass();
pointcut clientCall(Object[] arg):myInterceptedClass() && execution(void *.*(..)) && args(arg);
![Page 20: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/20.jpg)
Transparent RMI (2)
void around(Object[] arg): clientCall(arg) {Receiver r = null;
r = (Receiver) Naming.lookup(“rmi://...”);r.receive(
thisJoinPoint.getSignature().getName(),arg);
}}
![Page 21: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/21.jpg)
Access Control
aspect Identification perthis(this(Client)) {public Subject subject = null;
}
aspect Authentication percflow(serviceRequest()) {private Subject subject;
pointcut serviceRequest():call(* ServerInterface+.service(..));
![Page 22: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/22.jpg)
Access Control (2)
pointcut authenticationCall(Object caller):this(caller) &&serviceRequest() &&if(Identification.hasAspect(caller));
public Subject getSubject() {return subject;
}
![Page 23: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/23.jpg)
Access Control (3)
before(Object caller): authenticationCall(caller) {
Identification id = Identification.aspectOf(caller);
if(id.subject == null) {// ask for loginsubject = id.subject;
} else {throw anException;
}}
}
![Page 24: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/24.jpg)
Access Control (4)
aspect Authorization {pointcut checkedMethods():
within(Server) &&execution(* service(..));
Object around(): checkedMethods() {Authentication auth =
Authen.aspectOf();Subject subject = auth.getSubject();if(checkForPermission)
return proceed();}
}
![Page 25: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/25.jpg)
Inter-Type declarations
declare parents: banking.entities* implements Identifiable;
declare warning:: get(public !final *.*) || set(public *.*): “Should use a getter or setter method”;
![Page 26: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/26.jpg)
Cachepublic abstract aspect Cache {Map _cache = new HashMap();
abstract pointcut cacheMe(Object o);
Object around(Object o): cacheMe(o) {Object inCache = _cache.get(o);
if(inCache == null) {inCache = proceed(o);_cache.put(o, inCache);
}return inCache;
}}
![Page 27: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/27.jpg)
Pero yo no programo en Java…
![Page 28: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/28.jpg)
Pero yo no programo en Java…
<script type=“text/javascript”>
var pointcut = AspectScript.Pointcuts.call(foo);
var advice = function() { alert("Calling foo");
};
AspectScript.before(pointcut, advice);
</script>
![Page 29: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/29.jpg)
Pero yo no programo en Java…
require ‘aquarium’class Account include Aquarium::DSL before :calls_to => [:credit, :debit] \ do |join_point, object, *args| object.balance = read_from_database…
end
![Page 30: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/30.jpg)
Otras implementaciones de aspectos
• AspectC / AspectC++• AspectScript* (Javascript)• Aquarium (Ruby)• MetaclassTalk (Smalltalk)• Spring AOP• JBoss AOP• etc
* hecho en Chile
![Page 31: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/31.jpg)
Conclusiones
• Los aspectos permiten modularizar cross-cutting concerns
• AspectJ es un lenguaje bastante expresivo, pero limitado
• Muy útiles en ambientes de desarrollo
![Page 32: Introduccion a AspectJ](https://reader036.fdocuments.in/reader036/viewer/2022062513/5563a293d8b42a2d538b5336/html5/thumbnails/32.jpg)
Referencias• AspectJ Project http://www.eclipse.org/aspectj/• AJDT (Eclipse plugin) http://www.eclipse.org/ajdt/• Slides de CC71P – Objetos y Aspectos
http://pleiad.dcc.uchile.cl/teaching/cc71p• How aspect oriented programming can help to build secure
software http://people.cs.kuleuven.be/~bart.dedecker/pubs/aspects.pdf
• AspectScripthttp://www.pleiad.cl/aspectscript/
• Mis tareas de CC71P :-P