Introduccion a AspectJ
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Introduccion a AspectJ

  • 1,925 views
Uploaded on

Algunos ejemplos utilizando el lenguaje de aspectos para java AspectJ ...

Algunos ejemplos utilizando el lenguaje de aspectos para java AspectJ

An introduction to AspectJ
Some examples using the aspect's language AspectJ

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,925
On Slideshare
1,925
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
11
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introducción a AspectJ Mauricio Quezada 07/01/11
  • 2. Lo que queremos publicImportantcomplexMethod() {// applicationlogiconly returnanImportantResult; }
  • 3. Como es en realidad… publicImportantcomplexMethod() { LOG.log(“Enteringthemethod..”); // complexlogic... mutex.lock(); // more complexlogic... cipher.encrypt(message); // ... Connection con = getConnection(); // ... returnanImportantResult; }
  • 4. Aspectos Modularizancross-cuttingconcerns
  • 5. Aspectos Modularizancross-cuttingconcerns Logging, seguridad, sincronización, control de acceso …
  • 6. Aspectos Modularizancross-cuttingconcerns Logging, seguridad, sincronización, control de acceso … before(): call(complexMethod(..)) { LOG.log(“Callingtehmethod”); }
  • 7. AspectJ Lenguaje orientado a aspectos para Java Define su propio Join Point Model: Joinpoints + pointcuts + advices = aspect
  • 8. ¿Qué es el Join Point Model? Es el modelo que define cuáles son los: JoinPoints: Puntos de ejecución Ej. Llamada de un método, lanzamiento de una excepción Pointcut: Predicados que “atrapan” a los joinpoints Ej. Llamar al método foo(), lanzar una excepción de tipo Exception Advice: Acción a realizar dado un pointcut
  • 9. En AspectJ pointcutmove(): call(publicvoidFigureElement.setXY(int,int)); before(): move() { System.out.println(“abouttomove”); } after(): move() { System.out.println(“moved”); }
  • 10. Wildcards *Account= UserAccount, AdminAccount, ... java.*.Date = java.util.Date, java.sql.Date, ... javax..*Model+ = TableModel + subclasses+ TreeModel + subclasses + ...
  • 11. Pointcuts call(publicvoidAccount.*(int)); call(* *.*(int)) ||call(* *.*(double)); execution(public !final * *.*(..)) &&within(Dao+);
  • 12. Pointcuts pointcutcreditOp(Accountacc, floatamt) :call (voidAccount.credit(float)) && target (acc) && args (amt); pointcut creditOp2(Accountacc, floatamt) : execution (voidAccount.credit(float)) && this(acc) && args (amt);
  • 13. Advices before(): call(* Account.*(..)) { ... } after(): call(...) { ... } Objectaround(): move() { // ... Objectret = proceed(); // ... returnret; }
  • 14. Sí capitán, estamos listos! Ejemplos
  • 15. Log abstractaspect Log { abstractpointcutmyClass(); pointcutmyConstr(): myClass() && execution(new(..)); pointcutmyMethod(): myClass() && execution(* *(..));
  • 16. Log (2) after() throwing (Error e): myMethod() { LOG.getInstance().write(e); } before(): myConstr() { LOG.getInstance().write (“Creatingobject”); } }
  • 17. Log (3) publicaspectLogMyClassesextends Log { pointcutmyClass(): within( Account ) || within( Dao+ ) || within( Controller ); }
  • 18. TimerLog publicabstractaspectTimerLog { abstractpointcutmyMethod(); before(): myMethod () { Timer.start(); System.out.println( “Timerstarted at “ + t.startTime); } }
  • 19. Transparent RMI public interface Receiver extendsRemote { publicvoidreceive(Stringname, Object[] params) throwsRemoteException; } abstractaspectTransparentRMI { abstractpointcutmyInterceptedClass(); pointcutclientCall(Object[] arg): myInterceptedClass() && execution(void *.*(..)) && args(arg);
  • 20. Transparent RMI (2) voidaround(Object[] arg): clientCall(arg) { Receiver r = null; r = (Receiver) Naming.lookup(“rmi://...”); r.receive( thisJoinPoint.getSignature().getName(), arg); } }
  • 21. Access Control aspectIdentificationperthis(this(Client)) { publicSubjectsubject = null; } aspectAuthenticationpercflow(serviceRequest()) { privateSubjectsubject; pointcutserviceRequest(): call(* ServerInterface+.service(..));
  • 22. Access Control (2) pointcutauthenticationCall(Objectcaller): this(caller) && serviceRequest() && if(Identification.hasAspect(caller)); publicSubjectgetSubject() { returnsubject; }
  • 23. Access Control (3) before(Objectcaller): authenticationCall(caller) { Identification id = Identification.aspectOf(caller); if(id.subject == null) { // askforlogin subject = id.subject; } else { throwanException; } } }
  • 24. Access Control (4) aspectAuthorization { pointcutcheckedMethods(): within(Server) &&execution(* service(..)); Objectaround(): checkedMethods() { Authenticationauth = Authen.aspectOf(); Subjectsubject = auth.getSubject(); if(checkForPermission) returnproceed();} }
  • 25. Inter-Typedeclarations declare parents: banking.entities* implementsIdentifiable; declare warning: : get(public !final *.*) || set(public *.*) : “Should use a getteror setter method”;
  • 26. Cache publicabstractaspect Cache {Map _cache = new HashMap();abstractpointcutcacheMe(Object o);Objectaround(Object o): cacheMe(o) {ObjectinCache = _cache.get(o);if(inCache == null) {inCache = proceed(o); _cache.put(o, inCache); }returninCache;} }
  • 27. Pero yo no programo en Java…
  • 28. Pero yo no programo en Java… <script type=“text/javascript”> varpointcut = AspectScript.Pointcuts.call(foo); varadvice = function() { alert("Callingfoo"); }; AspectScript.before(pointcut, advice); </script>
  • 29. Pero yo no programo en Java… require ‘aquarium’ classAccount includeAquarium::DSL before :calls_to=> [:credit, :debit] do |join_point, object, *args| object.balance= read_from_database… end
  • 30. Otras implementaciones de aspectos AspectC / AspectC++ AspectScript* (Javascript) Aquarium (Ruby) MetaclassTalk (Smalltalk) Spring AOP JBoss AOP etc * hecho en Chile
  • 31. Conclusiones Los aspectos permiten modularizarcross-cuttingconcerns AspectJ es un lenguaje bastante expresivo, pero limitado Muy útiles en ambientes de desarrollo
  • 32. 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 Howaspectorientedprogramming can helptobuildsecure software http://people.cs.kuleuven.be/~bart.dedecker/pubs/aspects.pdf AspectScripthttp://www.pleiad.cl/aspectscript/ Mis tareas de CC71P :-P