Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Context passing problem

I'm trying to extend and modify classes that are part of a third-party
platform that we're using in our project.
I need to modify the implementation of a method in the platform. The new
custom implementation would need some information that is only available
higher up in the same call chain but not in the intercepted method.
Additionally the platform has a parameter class that is declared final,
that I'd want to extend so that I could make my custom classes
explicitly pass extra data down the call chain that could be used in the
interceptor implementation.

What would be the best way to implement this using AspectJ 1.6?

Here's a simplified definition of the problem in terms of code:

// Class A is developed for the project and the source can be modified.
// Types B, I and MyParameter are third-party code and can't be
modified.

class A {
  I i = new B();

  void doA() {
    i.doI(new MyParameter());
  }
}

class B implements I {
  C c = new C();
  
  void doI(MyParameter p) {
    c.doC();
  }
}

interface I {
  void doI(MyParameter p);
}

final class MyParameter {
}

class C {
  D d = new D();

  void doC() {
    String s = d.doD(someParameter, "aa"); // Need to intercept this
call
  }
}

I've developed the following aspect for passing the context down the
chain. Does this work correctly?
I haven't figured out how I could add data to MyParameter instances in
class A.

aspect MyAspect percflow(aDoI(MyParameter)) {
  MyParameter p;

  pointcut aDoI(MyParameter parameter):
    cflow(execution(* A.doA()) &&
    execution(void B.doI(MyParameter) &&
    args(parameter);

  before(MyParameter p): aDoI(p) {
    this.p = p; // save context
  }

  pointcut cDoD(SomeParameter someParameter, String str):
    cflow(execution(* A.doA()) &&
    cflow(execution(void C.doC()) &&
    call(String D.doD(SomeParameter, String)) &&
    args(someParameter, str);

  String around(SomeParameter someParameter, String str):
    cDoD(someParameter, str) {
    // The advice implementation needs the MyParameter instance passed
to B.
    // Some additional data required to be added to MyParameter by
methods in A
    // ...
  }

}




Back to the top