Pluggable Aspect Instantiation
           Models
        Victor Trakhtenberg
    The Open University of Israel

           Joint Work With
          David H. Lorenz
Aspect Instantiation Models (AIMs)
• What is an aspect?
  – Crosscutting behavior + crosscutting state
• What is an AIM?
  – 6 keyword in AspectJ: issingleton, pertarget, perthis,
    percflow, percflowbelow, pertypewithin.
  – Added incrementally to AspectJ
  – More AIMs in other AOP languages
• There is a need for custom AIMs
  – Challenge: decouple the definition of AIMs from the
    evolution of the language

                                                  The Open University of Israel
Contribution of this Work
• Pluggable Aspect Instantiation Models
    – Make AIMs pluggable!
❶   Language extension
    – We introduce a new keyword: ‘perscope’
       • Replaces the exiting 6 keywords
       • Eliminates need for new keywords (hopefully)
❷   Interface for defining AIMs
    – Third parties can design and implement new AIMs
❸   Library of AIM Implementations
    – Fully implemented in ajc
                                                 The Open University of Israel
Outline
•   Introduction
•   Requirements
•   Example
•   Pluggable Mechanism
•   Conclusion




                            The Open University of Israel
Motivation: “caching and auditing”
• Industrial Case Study
  – Web application development with AspectJ
  – Need for ‘persession’ and ‘perconversation’ AIMs

                                    Cache
                 Cache



                            Cache           Cache
                Cache




                                                    The Open University of Israel
Current Solution
• Wait for a new release of AspectJ
  – May take awhile…
• Modify the current ajc compiler
  – Sisyphean, complex task
• Manage the state manually
  – Tangled and scattered code




                                      The Open University of Israel
Desired Solution


    1.           Pluggable          5.
Extensible          AIM          Flexible
                 Mechanism

    2.
Declarative                            4.
                       3.          Efficient
                   Expressive



                                 The Open University of Israel
Requirement 1: Extensible
• New AIMs can be introduced
  – Perthread                                        Perthread
  – Persession
• Pre-defined AIMs can be refined
                                                     Per Recent
  – Perthread that was started in the last hour
                                                       Thread
• Generic AIMs can be offered by
  – Third-party providers
  – AspectJ developers



                                                  The Open University of Israel
Requirement 2: Declarative
• Easy to use
  – Like AspectJ
• AIMs implementation may vary
  independently of their use
  – Unlike AspectJ
                                      AspectJ
  public aspect MyAspect
        perthis(myPointcut()){
  }

                                     perscope
 public aspect MyAspect
       perscope(Perthis, myPointcut()){
 }
                                          The Open University of Israel
Requirement 2: Declarative
• Easy to use
  – Like AspectJ
• AIMs implementation may vary
  independently of their use
  – Unlike AspectJ



   New keyword       Class implements AIM

                                            perscope
 public aspect MyAspect
       perscope(Perthis, myPointcut()){
 }
                                               The Open University of Israel
Requirement 3: Expressive
• Practical
  – Realistically complex AIMs can be defined
  – E.g., perconversation
• AspectJ compatible
  – Can implement all 6 AspectJ AIMs




                                                The Open University of Israel
Requirement 4: Efficient
• Weaving
  – Implemented by bytecode weaving
• Runtime performance
  – Generate the same bytecode as AspectJ
    for the 6 AspectJ AIMs




                                            The Open University of Israel
Requirement 5: Flexible
• Static AIM implementation
  – Same woven performance as AspectJ
• Dynamic AIM implementation
  – Encourages experimenting with AIMs




                                         The Open University of Israel
Outline
•   Introduction
•   Requirements
•   Example
•   Pluggable Mechanism
•   Evaluation
•   Conclusion



                            The Open University of Israel
Example: Global Caching

                        Aspect




public aspect GlobalCaching      AspectJ and perscope
       extends SimpleCaching {
}


                                             The Open University of Israel
Example: Perclient Caching
              Aspect        Aspect        Aspect



             Aspect



                                          AspectJ
public aspect PerClientCaching extends SimpleCaching
       perthis (cachedOperation(Object)) {
}

                                          perscope
public aspect PerClientCaching extends SimpleCaching
       perscope(Perthis, cachedOperation(Object)) {
}
                                          The Open University of Israel
Example: Perconversation Caching
                                 Aspect
                 Aspect




               Aspect           Aspect



public aspect PerConversationCaching
       extends SimpleCaching              perscope
       perscope(Perconversation,
              cachedOperation(Object)){
}


                                           AspectJ



                                          The Open University of Israel
Outline
•   Introduction
•   Requirements
•   Example
•   Pluggable Mechanism
    – Dynamic
    – Static
• Evaluation
• Conclusion

                            The Open University of Israel
Dynamic Implementation
• Implement the DynamicPerscope interface
• Specify how the aspect instance should be
  bound, retrieved, and queried
 public interface DynamicPerscope {
        void bindAspect(Object aspekt, Object object);
        Object aspectOf(Class<?> aspectType, Object
 object);
        boolean hasAspect(Class<?> aspectType, Object
 object);
 }




                                             The Open University of Israel
DynamicPerscope: usage

public aspect PerThreadCaching extends SimpleCaching
       perscope(Perthread, cachedOperation(Object)) {
}




                                            The Open University of Israel
DynamicPerscope: implementation
public class Perthread implements DynamicPerscope {
  private static ThreadLocal<Object> theAspect =
       new ThreadLocal<Object>();

    public void bindAspect(Object aspekt, Object o) {
      if (theAspect.get() == null) {
        theAspect.set(aspekt);
    }
         }
    public Object aspectOf(Class<?> aspectType, Object o) {
         return theAspect.get();
    }

    public boolean hasAspect(Class<?> aspectType, Object o){
      return theAspect.get() != null;
    }
}


                                                 The Open University of Israel
Static Implementation
• Bytecode manipulation
  – Harder to implement
  – Capable of producing efficient code
• StaticPerscope interface
  – Hard-wired weaving code replaced with calls to the
    StaticPerscope interface




                                                The Open University of Israel
DynamicPerscope vs. StaticPerscope
• DynamicPerscope
  – Regular Java code
  – Intended for regular infrastructure programmers
• StaticPerscope
  – More complex
  – More efficient
  – Intended for advanced infrastructure programmers


Note: these trade-offs are transparent to the
end user of AIM
                                               The Open University of Israel
Outline
•   Introduction
•   Requirements
•   Example
•   Pluggable Mechanism
•   Evaluation
•   Conclusion



                            The Open University of Israel
Evaluation
• Implementing AspectJ built-in AIMs
  – Caching example
  – Law of Demeter (AspectBench compiler distribution)
• Implementing Non-AspectJ AIMs
  – AspectS cflow advice activations
  – JAsCo perthread AIM
  – persession and perconversation




                                               The Open University of Israel
Threats to Validity
• Implemented only for the ajc compiler
  – Language feature
• AspectJ AIMs were tested on two examples
  – Same bytecode is generated
•  AIMs that cannot be implemented using
  perscope
  – Always true
                       AspectJ       Other AOP
                                     Languages


                  perscope       Custom


                                                 The Open University of Israel
Outline
•   Introduction
•   Requirements
•   Example
•   Pluggable Mechanism
•   Evaluation
•   Conclusion



                            The Open University of Israel
Related Work
• Perscope vs. JAsCo
  – IAspectFactory is used at runtime for aspect instantiation
  – With perscope two choices: DynamicPerscope or
    StaticPerscope
• Perscope vs. Caesar
  – In Caesar aspect instantiated explicitly
  – With perscope – implicitly according to aspect declaration
    as in AspectJ




                                                  The Open University of Israel
Conclusion
• With perscope AIMs are pluggable
• New keyword ‘perscope’ is introduced
• Usage of AIMs is declarative
  – As in AspectJ
• AIMs may be provided by third-parties
  – Different than AspectJ




                                    The Open University of Israel
Thank You!
• Questions?

• Contact information:
  Software Engineering Research Lab
  Open University of Israel
  Email:
      • lorenz@openu.ac.il
      • victortr75@gmail.com
  URL:
      • http://aop.cslab.openu.ac.il/research/perscope/
                                                The Open University of Israel

Sc11 presentation 2001_06_28

  • 1.
    Pluggable Aspect Instantiation Models Victor Trakhtenberg The Open University of Israel Joint Work With David H. Lorenz
  • 2.
    Aspect Instantiation Models(AIMs) • What is an aspect? – Crosscutting behavior + crosscutting state • What is an AIM? – 6 keyword in AspectJ: issingleton, pertarget, perthis, percflow, percflowbelow, pertypewithin. – Added incrementally to AspectJ – More AIMs in other AOP languages • There is a need for custom AIMs – Challenge: decouple the definition of AIMs from the evolution of the language The Open University of Israel
  • 3.
    Contribution of thisWork • Pluggable Aspect Instantiation Models – Make AIMs pluggable! ❶ Language extension – We introduce a new keyword: ‘perscope’ • Replaces the exiting 6 keywords • Eliminates need for new keywords (hopefully) ❷ Interface for defining AIMs – Third parties can design and implement new AIMs ❸ Library of AIM Implementations – Fully implemented in ajc The Open University of Israel
  • 4.
    Outline • Introduction • Requirements • Example • Pluggable Mechanism • Conclusion The Open University of Israel
  • 5.
    Motivation: “caching andauditing” • Industrial Case Study – Web application development with AspectJ – Need for ‘persession’ and ‘perconversation’ AIMs Cache Cache Cache Cache Cache The Open University of Israel
  • 6.
    Current Solution • Waitfor a new release of AspectJ – May take awhile… • Modify the current ajc compiler – Sisyphean, complex task • Manage the state manually – Tangled and scattered code The Open University of Israel
  • 7.
    Desired Solution 1. Pluggable 5. Extensible AIM Flexible Mechanism 2. Declarative 4. 3. Efficient Expressive The Open University of Israel
  • 8.
    Requirement 1: Extensible •New AIMs can be introduced – Perthread Perthread – Persession • Pre-defined AIMs can be refined Per Recent – Perthread that was started in the last hour Thread • Generic AIMs can be offered by – Third-party providers – AspectJ developers The Open University of Israel
  • 9.
    Requirement 2: Declarative •Easy to use – Like AspectJ • AIMs implementation may vary independently of their use – Unlike AspectJ AspectJ public aspect MyAspect perthis(myPointcut()){ } perscope public aspect MyAspect perscope(Perthis, myPointcut()){ } The Open University of Israel
  • 10.
    Requirement 2: Declarative •Easy to use – Like AspectJ • AIMs implementation may vary independently of their use – Unlike AspectJ New keyword Class implements AIM perscope public aspect MyAspect perscope(Perthis, myPointcut()){ } The Open University of Israel
  • 11.
    Requirement 3: Expressive •Practical – Realistically complex AIMs can be defined – E.g., perconversation • AspectJ compatible – Can implement all 6 AspectJ AIMs The Open University of Israel
  • 12.
    Requirement 4: Efficient •Weaving – Implemented by bytecode weaving • Runtime performance – Generate the same bytecode as AspectJ for the 6 AspectJ AIMs The Open University of Israel
  • 13.
    Requirement 5: Flexible •Static AIM implementation – Same woven performance as AspectJ • Dynamic AIM implementation – Encourages experimenting with AIMs The Open University of Israel
  • 14.
    Outline • Introduction • Requirements • Example • Pluggable Mechanism • Evaluation • Conclusion The Open University of Israel
  • 15.
    Example: Global Caching Aspect public aspect GlobalCaching AspectJ and perscope extends SimpleCaching { } The Open University of Israel
  • 16.
    Example: Perclient Caching Aspect Aspect Aspect Aspect AspectJ public aspect PerClientCaching extends SimpleCaching perthis (cachedOperation(Object)) { } perscope public aspect PerClientCaching extends SimpleCaching perscope(Perthis, cachedOperation(Object)) { } The Open University of Israel
  • 17.
    Example: Perconversation Caching Aspect Aspect Aspect Aspect public aspect PerConversationCaching extends SimpleCaching perscope perscope(Perconversation, cachedOperation(Object)){ } AspectJ The Open University of Israel
  • 18.
    Outline • Introduction • Requirements • Example • Pluggable Mechanism – Dynamic – Static • Evaluation • Conclusion The Open University of Israel
  • 19.
    Dynamic Implementation • Implementthe DynamicPerscope interface • Specify how the aspect instance should be bound, retrieved, and queried public interface DynamicPerscope { void bindAspect(Object aspekt, Object object); Object aspectOf(Class<?> aspectType, Object object); boolean hasAspect(Class<?> aspectType, Object object); } The Open University of Israel
  • 20.
    DynamicPerscope: usage public aspectPerThreadCaching extends SimpleCaching perscope(Perthread, cachedOperation(Object)) { } The Open University of Israel
  • 21.
    DynamicPerscope: implementation public classPerthread implements DynamicPerscope { private static ThreadLocal<Object> theAspect = new ThreadLocal<Object>(); public void bindAspect(Object aspekt, Object o) { if (theAspect.get() == null) { theAspect.set(aspekt); } } public Object aspectOf(Class<?> aspectType, Object o) { return theAspect.get(); } public boolean hasAspect(Class<?> aspectType, Object o){ return theAspect.get() != null; } } The Open University of Israel
  • 22.
    Static Implementation • Bytecodemanipulation – Harder to implement – Capable of producing efficient code • StaticPerscope interface – Hard-wired weaving code replaced with calls to the StaticPerscope interface The Open University of Israel
  • 23.
    DynamicPerscope vs. StaticPerscope •DynamicPerscope – Regular Java code – Intended for regular infrastructure programmers • StaticPerscope – More complex – More efficient – Intended for advanced infrastructure programmers Note: these trade-offs are transparent to the end user of AIM The Open University of Israel
  • 24.
    Outline • Introduction • Requirements • Example • Pluggable Mechanism • Evaluation • Conclusion The Open University of Israel
  • 25.
    Evaluation • Implementing AspectJbuilt-in AIMs – Caching example – Law of Demeter (AspectBench compiler distribution) • Implementing Non-AspectJ AIMs – AspectS cflow advice activations – JAsCo perthread AIM – persession and perconversation The Open University of Israel
  • 26.
    Threats to Validity •Implemented only for the ajc compiler – Language feature • AspectJ AIMs were tested on two examples – Same bytecode is generated •  AIMs that cannot be implemented using perscope – Always true AspectJ Other AOP Languages perscope Custom The Open University of Israel
  • 27.
    Outline • Introduction • Requirements • Example • Pluggable Mechanism • Evaluation • Conclusion The Open University of Israel
  • 28.
    Related Work • Perscopevs. JAsCo – IAspectFactory is used at runtime for aspect instantiation – With perscope two choices: DynamicPerscope or StaticPerscope • Perscope vs. Caesar – In Caesar aspect instantiated explicitly – With perscope – implicitly according to aspect declaration as in AspectJ The Open University of Israel
  • 29.
    Conclusion • With perscopeAIMs are pluggable • New keyword ‘perscope’ is introduced • Usage of AIMs is declarative – As in AspectJ • AIMs may be provided by third-parties – Different than AspectJ The Open University of Israel
  • 30.
    Thank You! • Questions? •Contact information: Software Engineering Research Lab Open University of Israel Email: • lorenz@openu.ac.il • victortr75@gmail.com URL: • http://aop.cslab.openu.ac.il/research/perscope/ The Open University of Israel