I've been experimenting with programmatic creation of proxied objects, with the use of AspectJProxyFactory.
I've created a simple example where i have the following:
A simple target class that looks like the following:
Code:public class Target { public String methodReturningString(){return "StringReturn";} }
An interface defining a business method:
An interceptor of that business method:Code:public interface AspectDefiningMethod { String anotherMethodReturningString(); }
An interface defining additional values on a given target:Code:@Aspect public class AspectJInterceptor{ @Around("execution(* anotherMethodReturningString())") public String pointCut(ProceedingJoinPoint joinPoint){ return "anotherMethodReturningString"; } }
And an implementation for that, plus an interceptor that should intercept all calls to a proxied object and change the values accordingly:Code:public interface AspectDefiningValues { void setBooleanValue(Boolean boolean1); Boolean getBooleanValue(); }
Then, I create my proxy like that:Code:@Aspect public class AspectDefininingBooleanValueImpl implements AspectDefiningValues, Ordered { @DeclareParents(value="com.foo..*", defaultImpl=AspectDefininingBooleanValueImpl.class) AspectDefiningValues values; private Boolean booleanValue = Boolean.FALSE; @Override public Boolean getBooleanValue() { return booleanValue; } @Override public void setBooleanValue(Boolean boolean1) { this.booleanValue = boolean1; } @Around("execution(* anotherMethodReturning*())") public void pointCut(ProceedingJoinPoint joinPoint) throws Throwable{ ((AspectDefiningValues)joinPoint.getThis()).setBooleanValue(true); joinPoint.proceed(); } @Override public int getOrder() { return Ordered.HIGHEST_PRECEDENCE; } }
Given the above, the expected behaviour would be that, in all circumstances, AspectDefininingBooleanValueImpl.pointCut() should kick in first, since it has Ordered.HIGHEST_PRECEDENCE. But, as i realized, the order is never respected.Code:public class Test { @org.junit.Test public void test(){ AspectJProxyFactory factory = new AspectJProxyFactory(); factory.setTarget(new Target()); factory.setOptimize(true); factory.setProxyTargetClass(true); factory.addInterface(AspectDefiningMethod.class); Object target = factory.getProxy(); factory = new AspectJProxyFactory(); factory.setTarget(target); factory.setOptimize(true); factory.addAspect(AspectJInterceptor.class); factory.addAspect(AspectDefininingBooleanValueImpl.class); target = factory.getProxy(); Assert.assertTrue(target instanceof Target); Assert.assertTrue(target instanceof AspectDefiningMethod); Assert.assertTrue(target instanceof AspectDefiningValues); Assert.assertEquals("anotherMethodReturningString", ((AspectDefiningMethod)target).anotherMethodReturningString()); Assert.assertTrue(((AspectDefiningValues)target).getBooleanValue()); }
Indeed, after searching a bit, i found out that the chain creation for a given proxy does not take under consideration the ordering, as I've seen inside DefaultAdvisorChainFactory.getInterceptorsAndDynam icInterceptionAdvice().
Is that the default expected behavior or am i missing something?


Reply With Quote
