Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Annotation Style: Ways to access the target of an inter-type declaration inside the interface implementation?

Hi,

I am currently implementing a lot of static cross-cutting with annotation style and have a hard time working around
the current limitations. My main problem is the inability to directly access the target object within the implementation of an interface
when making an inter-type declaration like

@DeclareParents(value="target", defaultImpl=Implementation.class)
public SomeInterface SomeInterfaceImpl;

public static Implementation implements SomeInterface
{
    //   "this" returns a reference to an object of this implementation class, not the target object
}

This topic has been already covered to some extent (like in  http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg05884.html),
but I couldn't find any direct hints or best practices how to implement a decent workaround for this situat

I started working with AspectJ some weeks ago, so my understanding for its internals might still be lacking,
but there seems to be no obvious way to access the target from inside the implementation class or vice versa.
(Although there is some kind of stub inside a targets class-file for each interface-method and some code that
creates the implementation object, that might indicate some field holding a reference ...)

My best "solution" so far is storing and retrieving a reference to the target object within the implementation class via get/set methods.
Setting the reference can be conveniently handled by an advice during creation of the target object. This is relatively easy to use,
but as all externally accessible methods must be declared in the interface, this approach is horribly invasive and I am not very happy with it.

sample code :

//-----defaultImpl-----
public static Implementation implements SomeInterface
{  
   private Object target=null;

    public void setTarget(Object ob) {
        this.target=ob;
    }

    public Object getTarget() {
        return this.target;
    }

   public void someMethod(){
      // do something with the target object via getTarget()
   }
}

//-----Advice-----
@After(" execution( new(..)) && @target(SomeAnnotation) && target (obj))")
        public void storeReference(Object obj )
        {
            ((SomeInterface)obj).setTarget(obj);
        }

I was experimenting with awkward things like multiple interfaces on the implementation class (in order to keep the important interface clean
and "hide" the additional methods in another interface)but with no success. It either didn't compile or AspectJ generated a separate object
of the implementation class for each interface.

Are there any different/better solutions to this problem?

Regards,

Michael




Get news, entertainment and everything you care about at Live.com. Check it out!

Back to the top