Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] EMF and AOP (spring like?)

Hi,

currently, I am adding what some AOP like features
too my EMF (http://www.eclipse.org/modeling/emf/)
application. I realized I am reinventing aspectJ
and I wonder if I can use aspectJ.

A simplified view of EMF: a ECore model defines objects with
attributes:

  class Person {
     attr String name;
  }

EMF can run in two modes: generated code and reflective.
In case of code generation the following code would be
generated for the above ecore model:

  public class Person {
     protected String name;
     public String getName() {
        return name;
     }
     public setName(String newName) {
        name=newName;
    // some code to notify listeners
     }

  }

Here one could use normal aspectJ. What interests me more is
the reflective case. In the case of reflective EMF instance
would have to be used like this (simplified version):

   name=person.eGet("name"); // in reality the attribute name is not string
   person.eSet("name", "Michael");

Essentially, any access of EMF objects funneled through the eGet/eSet
methods.

I want to define some advices for some of the getters and setters.
E.g. an advice for supplying some calculated default value on the
getter:

@Aspect
public class DefaultExample {

  @Around("com.xyz.myapp.Person.getName()")
  public Object addDefault(ProceedingJoinPoint jp) throws Throwable {
     EObject target=(EObject)jp.getTarget();
     // I somehow need to get to the structurel feature representing
     // the "name" attribute
     EStructuralFeature attribute=EMFAspectJHelper.getAttribute(jp)
     if(!target.eIsSet(attribute)
        return "My calculated default Value";
     return jp.proceed();
  }
}

I have found the docu for spring AOP
  http://static.springframework.org/spring/docs/2.5.x/reference/aop.html
I have never used spring but it seems they use some aspectJ library
to implement AOP for spring.

So, here is my question: Is it possible to use AOP in way that I
could add aspects to some reflective API like EMF? I mean is there
some calls I could make in the eGet/eSet methods to weave in aspects?

Here is how envision how this could work: Suppose I create a subclass
of EObjectImpl (the class that defines eGet/eSet). All eSet/eGet
would be handeled by my class. This class would then be used
for all dynamic access eGet/eSet:

// a class that adds AOP to dynamic EMF
class AopEObjectImpl extends EObjectImpl {
    // the magic from aspectJ
    MagicAspectJ aspectj;
    @Override
    public Object eGet(EStructuralFeature feature) {
        // let aspecJ doo the call for me. In case
        // the original method is needed
        // (JointPoint.proceed()), call eOriginalGet.
        return aspectj.call(buildGetSignature(feature));
    }
    // this calls the original get method
    public Object eOriginalGet(EStructuralFeature feature) {
        return super.eGet(feature);
    }
    private Signature buildGetSignature(EStructuralFeature feature) {
        // somehow create the signature like "com.xyz.myapp.Person.getName()"
        return signature;
    }
}

The MagicAspectJ would then do the magic for me of looking for
aspects and call the aspects that I have registered with
MagicAspectJ...

Am I dreaming here? Does aspectJ provide a way of using
aspects for dynamically called methods?
Any docu or example I can start with?

Michael

--
Michael Scharf
Wind River Systems GmbH
http://www.WindRiver.com
http://MichaelScharf.blogspot.com/



Back to the top