Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Replacing an instantiated object

Hi Vinicius,

This is an area where Aspectj can be useful, although it can be a little
more difficult. There is a kind of advice, around advice (@Around), that can
return an alternative value from a join point. However, its contract is that
it will return a compatible type as the constructor, so it needs to return
the original class or a subclass thereof. So this won't work:
    @Around("call(MyClass+.new(..))")
    public Object replace(ProceedingJoinPoint jp) {
        Object o = jp.proceed();
        Return ProxyFactory.createProxy(o.getClass(),o);
    }

However, you could use a subclass proxy instead (e.g., using CGLib). You
also might find it possible to implement the semantics of the proxy with
AspectJ directly, possibly holding some state on construction using an
inter-type declaration (set a flag to indicate that this object should have
the additional behavior).

HTH,
Ron

-----Original Message-----
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Vinicius Carvalho
Sent: Tuesday, March 06, 2007 9:42 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Replacing an instantiated object

Hello there! I'd like to know if it is possibly to replace an instance
of a class by something else using aspects.
Here's what I've done so far:

@AfterReturning(pointcut="call (public MyClass+.new(..))",returning="o")
	public void proxyReturn(Object o){
		
		o = ProxyFactory.createProxy(o.getClass(),o);
	}

What I need to do is replace a call:

MyClass c = new MyClass();

and return the proxy instead of the original class. Altough the
afterreturning gets called, the user does not get back a proxy
reference. I've tried around, but it throws a null point (which makes
sense to me since the instance is not yet created).

Any way of doing this?

Best regards
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top