Aspect Oriented Programming in .NET with CodeBricks - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Aspect Oriented Programming in .NET with CodeBricks

Description:

Milan, 2004. Universit di Pisa. Supported by Microsoft Research grant. Introduction ... AOP tools are responsible for synthesizing the program by combining ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 27
Provided by: downloadM
Category:

less

Transcript and Presenter's Notes

Title: Aspect Oriented Programming in .NET with CodeBricks


1
Aspect Oriented Programming in .NET with
CodeBricks
  • Antonio Cisternino
  • Academic Days
  • Milan, 2004

Supported by Microsoft Research grant
2
Introduction
  • Aspect Oriented Programming is a way of
    modularize crosscutting concerns in computer
    programs
  • AOP tools are responsible for synthesizing the
    program by combining different aspects
  • CodeBricks provides support for meta-programming
    within CLR how can we use the code generation
    facilities to support AOP?

3
Agenda
  • AOP and Code Generation
  • CodeBricks quick intro
  • aC annotated join points
  • ARE a generic program rewriting system
  • Conclusions

4
Aspect Oriented Programming
  • Traditional code organization privileges
    functional aspect of code other aspects, like
    logging, security, concurrency, tend to crosscut
    the program.
  • Aspect Oriented Programming aims to develop tools
    modularizing crosscutting concerns.
  • AOP languages (like AspectJ) are based on the
    single notion of join points positions in the
    source code where the code of a crosscutting
    aspect should join the program
  • AOP is a form of generative programming programs
    are synthesized from aspects and code

5
AspectJ a shallow approach to AOP
  • AspectJ is one of the most popular systems for
    AOP
  • It extends the Java language with constructs to
    specify join points and how aspects are weaved
    into them
  • Pointcuts are defined by means of pattern
    matching
  • It is possible to insert code before and after a
    given pointcut
  • Inter-type declarations allow to interact with
    the static structure of a program (i.e. types)

6
AspectJ a shallow approach to AOP
  • public aspect AutoLog
  • pointcut publicMethods()
  • execution(public org.apache.cactus..(..))
  • pointcut logObjectCalls()
  • execution( Logger.(..))
  • pointcut loggableCalls()
  • publicMethods() ! logObjectCalls()
  • before() loggableCalls()
  • Logger.entry(thisJoinPoint.getSignature().toStrin
    g())
  • after() loggableCalls()
  • Logger.exit(thisJoinPoint.getSignature().toStrin
    g())

7
Aspectwerkz runtime bytecode generation
  • AspectWerkz provides AOP facilities in the form
    of Java library rather than language
  • The library is capable of manipulating bytecode
    inside the class loader for injecting aspect code
    into classes
  • Aspects and join points are connected through XML
    files
  • The library supports matching on code attributes,
    the new feature of Java 1.5 (custom attributes)

8
Agenda
  • AOP and Code Generation
  • CodeBricks quick intro
  • aC annotated join points
  • ARE a generic program rewriting system
  • Conclusions

9
Code Manipulation based on methods
Perceived transformation
Programming language
Type T
Real transformation
Intermediate (or machine) language
Type T
Source program
Target program
10
Partial application inlining
  • CodeBricks library is built around the notion of
    partial application
  • Code generation is expressed as partial
    application assuming that code is generated
    relying on an inlining strategy
  • Code bricks are built around methods and
    represent high order values

11
Examples
  • delegate int F(int v)
  • delegate int G(int i, int j, int k)
  • public int add(int x, int y) return xy
  • //
  • Code c new Code(typeof(Add).GetMethod(add))
  • Code inc c.Bind(1, new Free()) // add(1, _)
  • Free x new Free()
  • Code twice c.Bind(x, x) // add(_0, _0)
  • Code threeAdd c.Bind(c.Bind(new Free(), new
    Free()), new Free()) // add(add(_, _), _)
  • F f (F)inc.MakeDelegate(typeof(F)) f(2)
  • G g (G)threeAdd.MakeDelegate(typeof(G)) g(1,
    2, 3)

12
High order splice
  • delegate void Cmd()
  • void Log(string info, Cmd c)
  • Console.WriteLine(Enter 0, info)
  • c()
  • Console.WriteLine(Exit 0, info)
  • void SomeWork() ...
  • //...
  • Code log new Code(typeof(Exmp).GetMethod(Log))
  • Code tgt new Code(typeof(Exmp).GetMethod(SomeWo
    rk))
  • Cmd norm new Cmd(SomeWork) // Log(info,
    SomeWork)
  • Cmd wlog (Cmd)log.Bind(SomeWork,
    tgt).MakeDelegate(typeof(Cmd))

13
AOP with CodeBricks
  • Previous example shows how CodeBricks can provide
    before and after code injections
  • But you always need to abstract the code you want
    to surround into a method
  • The compiler can generate patterns based on
    CodeBricks for aspect waving (this allows for
    dynamic weaving of aspects)

14
Agenda
  • AOP and Code Generation
  • CodeBricks quick intro
  • aC annotated join points
  • ARE a generic program rewriting system
  • Conclusions

15
Annotations for better join points?
  • AspectWerkz shows that code with annotations may
    help to define better join points
  • The annotation model of CLR and JVM is limited to
    methods
  • Join points are inside methods
  • We have extended C to allow annotations inside
    method bodies

16
aC
  • public void m()
  • Console.WriteLine("Parallelizable code
    sample")
  • Parallel("Begin of a parallelizable block")
  • Console.WriteLine("Code exec by the main
    thread")
  • Process("First process")
  • // Computation here
  • Process
  • // Computation here
  • // Join of processes here
  • Console.WriteLine("Here is sequential")

17
aC runtime operations
  • Operations available at runtime are
  • Annotation tree inspection
  • Extrusion
  • Injection
  • Replacement

18
aC and AOP
  • Annotations may help to define pointcuts
  • With code-level annotations programmers may
    declare join points
  • AOP systems may get benefit from annotations
  • The main source code becomes a little aware of
    the subsequent code transformation
  • aC transformations are performed at runtime
    but what does it means exactly runtime?

19
Agenda
  • AOP and Code Generation
  • CodeBricks quick intro
  • aC annotated join points
  • ARE a generic program rewriting system
  • Conclusions

20
Assembly Rewriting Engine
  • It is possible to encode information into
    binaries so that the programmer perceives that
    the information is provided into its own language
  • A general rewriting system would support several
    applications in a standard way complementing the
    reflection support already available in STEEs
  • Because the target program and the transformation
    system are expressed in the same language the
    program can be specialized by executing its own
    code
  • AOP systems can rely on this general
    infrastructure for generating their own code

21
Schema
AR
AR
AR
Stage name
Stage name
Stage name
22
The Algorithm
  • We assume that a program that wants to perform a
    computation at stage n provide some metadata to
    indicate where and what to do
  • Methods handled by the rewriting engine should
    have a known signature Code m(Context c)
  • The basic operation is type safe code replacement
    of explicitly (annotations) or implicitly (method
    calls) annotated code
  • foreach (Method m in Assembly)
  • if (annotated(m, stagename)) replaceCalls(m,
    a)
  • else if (annotatedbody(m, stagename))
    replace(m,a)

23
The logger example revised
  • class LogAspect Transformation
  • MethodCall(SomeWork)
  • void Log(Context c)
  • Console.WriteLine(Entering SomeWork)
  • c.MethodCall()
  • Console.WriteLine(Exiting SomeWork)
  • //...
  • void SomeWork() ...
  • //...
  • SomeWork()

24
And pattern matching?
  • Transformations are expressed on the methods
    using custom attributes
  • The transformation should be type-safe
  • ARE will not provide general support for pattern
    matching (maybe some restricted form)
  • An AOP compiler can anticipate pattern matching
    and output an appropriate transformation class.

25
Agenda
  • AOP and Code Generation
  • CodeBricks quick intro
  • aC annotated join points
  • ARE a generic program rewriting system
  • Conclusions

26
Conclusions
  • CodeBricks aims to become a general CLR support
    for meta-programming at runtime
  • We have discussed how the typical transformations
    of AOP could be mapped on CodeBricks
  • Annotations can help AOP systems to define better
    join points
  • A multi-stage code transformation system is
    required to have before runtime stages
Write a Comment
User Comments (0)
About PowerShow.com