-
1. Re: Method invocation's access to instance data
freezy Sep 13, 2005 5:17 PM (in response to aozoren)I asked myself the same question. Provides JBossAOP this feature?
I'm describing it once more: I want to write an Interceptor which gets an MethodInvocation. Within this Interceptor I want to read fields from the intercepted class instance...
How can I do this? Do you have a tutorial covering this question? -
2. Re: Method invocation's access to instance data
nthx Sep 14, 2005 6:21 AM (in response to aozoren)
Using reflection.
Not the prettiest one, but works.public Object invoke(Invocation invocation) { MethodInvocation methodInvocation = (MethodInvocation) invocation; CustomerManager customerManager = (CustomerManager) methodInvocation.getTargetObject(); Long newValue = (Long) HelperClass.getValueOfField(customerManager, "customerCounter"); }
public class HelperClass { public static Object getValueOfField(Object target, String fieldName) throws ManyExceptions { Field field = null; field = target.getClass().getDeclaredField(fieldName); field.setAccessible(true); return field.get(target); }
public class CustomerManager { private long customerCounter; ///.... }
Tomasz -
3. Re: Method invocation's access to instance data
freezy Sep 14, 2005 8:06 PM (in response to aozoren)Thanks for your answer. I already tried a similiar approach. Actually, what I'm trying to do is to aspectize some method's constructor in order to add some test data after the object was created. This seemed like a really cool idea. Unfortunately, I don't get it working :-(
My pointcut definition:<bind pointcut="execution(eshop.pojo.EShopApp->new())"> <interceptor class="eshop.aop.InsertTestData"/> </bind>
EShopApp has an empty constructor and is instantiated from another class "Test" (nothing special here): EShopApp app = new EShopApp();
And finally the interceptor "InsertTestData" itself:public java.lang.Object invoke(org.jboss.aop.joinpoint.Invocation invocation) throws java.lang.Throwable { System.out.println("InsertTestData->invoke!"); System.out.println("InsertTestData invokes next."); if (invocation instanceof ConstructorInvocation) { ConstructorInvocation ci = (ConstructorInvocation)invocation; System.out.println(ci.getTargetObject()); } try { return invocation.invokeNext(); } finally { ... } }
Output:InsertTestData->invoke! InsertTestData invokes next. null
Although the Interceptor gets instantiated and is even invoked, "constructorInvocation.getTargetObject()" returns "null" :-(
Any ideas?
btw: I ran also into the problem, that I created a "useless" construct statement (I did not further use the constructed object). So, the compiler thought "well, let's optimize this crappy code" and thus my Interceptor got never called. Took me some time to figure THAT out :-) -
4. Re: Method invocation's access to instance data
nthx Sep 15, 2005 4:58 AM (in response to aozoren)
Of course it will return null.
You have to:public java.lang.Object invoke(Invocation invocation) throws java.lang.Throwable { ConstructorInvocation ci = (ConstructorInvocation)invocation; Object targetObject = invocation.invokeNext(); //^^This is the reference to just created object by the constructor //it is the same as invoking: EShopApp app = new EShopApp() //so: targetObject == app }
-
5. Re: Method invocation's access to instance data
nthx Sep 15, 2005 5:49 AM (in response to aozoren)I mean the target object (the being constructed one) doesn't exist until you invoke: "invocation.invokeNext()" -- its constructor.
This applies to ConstructorInvocation only. In MethodInvocation you can get targetObject() as it has already been created.
Tomasz -
6. Re: Method invocation's access to instance data
kabirkhan Sep 15, 2005 5:58 AM (in response to aozoren)Correct.
However, I think getTargetObject() still returns null when the object has been constructed, i.e. on the way out. I've added JIRA issue:
http://jira.jboss.com/jira/browse/JBAOP-168 -
7. Re: Method invocation's access to instance data
freezy Sep 15, 2005 7:28 AM (in response to aozoren)thanks, nthx! It works now.
nice nice :-)