7 Replies Latest reply on Sep 15, 2005 7:28 AM by freezy

    Method invocation's access to instance data

    aozoren

      The FieldInvocation typecast has a non-null reference to a targetObject but not the MethodInvocation. How can I access to the instance variables of the "aspected?" class instance from within a MethodInvocation? I would like to implement a security mechanism based on values on db tables and session variables not xml definition files. Thanks.

        • 1. Re: Method invocation's access to instance data
          freezy

          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


            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

              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


                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

                  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

                    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

                      thanks, nthx! It works now.

                      nice nice :-)