Implement Wormhole Diagram Using AspectJ

I am looking for an example to implement a wormhole template using AspectJ (it would be interesting if Guice AOP can implement this).

The worm hole essentially allows you to pass additional parameters along the call flow, for example:

// say we have class foo { public int m0 int a, int b) { return m1(a,b); } public int m1 int a, int b) { return m2(a,b); } public int m2 int a, int b) { return a+b; } } // and I wanted in a non-invasive manner to pass a third parameter of type class context { String userName; long timeCalled; String path; } // I could use an advise to say print the context information // to trace what was going on without mucking up my method signatures 

I believe this Ramnivas Laddad has such an example in his book AspectJ in Action.

Thanks in advance.

+4
source share
3 answers

Indeed, there is an example in AspectJ in action . If you look at the table of contents , you will notice that chapter 12.2 is what you are looking for. It would be nice to buy a book. I can recommend it warmly. Because I'm not sure that you can simply copy and paste parts of the book, I will just give a template here:

 public aspect WormholeAspect { pointcut callerSpace(<caller context>) : <caller pointcut>; pointcut calleeSpace(<callee context>) : <callee pointcut>; pointcut wormhole(<caller context>, <callee context>) : cflow(callerSpace(<caller context>)) && calleeSpace(<callee context>); // advice to wormhole before(<caller context>, <callee context>) : wormhole(<caller context>, <callee context>) { ... advice body } } 

There is an older Laddad article on TheServerSide.com with a more specific example. This is not the same from the book, but it seems.

As you can see, this is easy to do in AspectJ, because there is pointcut cflow() . I have never used Guice, but the AOP introduction page mentions that their implementation is part of the AOP Alliance . Looking at the AOP Alliance API , there is nothing that looks like point tcut> t21, it's all about calling the constructor and method plus access to the field.

So what can you do in Spring (without AspectJ) or Guice if you want to avoid going through all levels? The obvious solution is the declared and managed ThreadLocal variable (i.e. the Assigned, but also cleared) calling program and available to the called party. This is not nice, just a workaround so as not to inflate the API. But this requires that both the caller and the callee have a common understanding of what they want to share and how. In a way, this implementation is more of an anti-pattern than a pattern. If you can, use AspectJ to solve this in a clean and modular way, encapsulating a problem that needs to be solved in one module (aspect).

+4
source

Do not use the wormhole pattern ... Actually, use AOP only if you are really sure that you need it, otherwise leave it.

The disadvantage of the wormhole template is that you skip many layers ... is that what you really want? :)

Grtz,

Christoph

+1
source

A simple example. Imagine that you have contextual and target objects that provide functionality that somehow depends on the state of the context:

 class T { public void foo() { System.out.println("T.foo()"); } } class Context { public boolean isValid = true; public void doStuff() { T t = new T(); t.foo(); } } public class Main { public static void main(String[] args) { Context c = new Context(); c.doStuff(); } } 

an aspect that ensures that the Context instance can call foo() on the T instance only if the isValid element is set to true , can look like this:

 public aspect ContextStateValidation { pointcut MyContext(Context c) : execution(* Context.*()) && this(c); pointcut FooCalls(T t) : call(* T.foo()) && target(t); pointcut FooCallsInMyContext(Context c, T t) : cflow(MyContext(c)) && FooCalls(t); void around(Context c, T t) : FooCallsInMyContext(c, t) { if (c.isValid) proceed(c, t); } } 
+1
source

All Articles