Pluggable Aspect Instantiation Models
Victor Trakhtenberg
The Open University of Israel
Joint Work With
David H. Lorenz
The Open University of Israel
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
CacheCache 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
2. Declarative
3. Expressive
1. Extensible
5. Flexible
4. Efficient
Pluggable AIM
Mechanism
The Open University of Israel
Requirement 1: Extensible
• New AIMs can be introduced– Perthread
– Persession
• Pre-defined AIMs can be refined– Perthread that was started in the last hour
• Generic AIMs can be offered by – Third-party providers
– AspectJ developers
Perthread
Per RecentThread
The Open University of Israel
Requirement 2: Declarative
• Easy to use– Like AspectJ
• AIMs implementation may vary independently of their use– Unlike AspectJ
public aspect MyAspect
perthis(myPointcut()){
}
public aspect MyAspect
perscope(Perthis, myPointcut()){
}
AspectJ
perscope
The Open University of Israel
Requirement 2: Declarative
• Easy to use– Like AspectJ
• AIMs implementation may vary independently of their use– Unlike AspectJ
public aspect MyAspect
perscope(Perthis, myPointcut()){
}
perscope
New keyword Class implements AIM
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
public aspect GlobalCaching
extends SimpleCaching {
}
Aspect
AspectJ and perscope
The Open University of Israel
Example: Perclient Caching
public aspect PerClientCaching extends SimpleCaching
perthis (cachedOperation(Object)) {
}
Aspect
Aspect
Aspect Aspect
AspectJ
public aspect PerClientCaching extends SimpleCaching
perscope(Perthis, cachedOperation(Object)) {
}
perscope
The Open University of Israel
Example: Perconversation Caching
public aspect PerConversationCaching
extends SimpleCaching
perscope(Perconversation,
cachedOperation(Object)){
}
Aspect
Aspect Aspect
Aspect
perscope
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
Other AOP Languages
Custom
AspectJ
perscope
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:
URL:
• http://aop.cslab.openu.ac.il/research/perscope/