4 Replies Latest reply on Feb 4, 2004 7:45 PM by rythos

    Caller-pointcut question

    susanin Newbie

       

      "susanin" wrote:
      "susanin" wrote:
      Hi!

      I have defined caller-pointcut using the following definition:
      <caller-pointcut class="mypackage.POJO.*"
      calledClass="POJO1"
      calledMethodName="print">
      <interceptors>
      <interceptor class="TracingInterceptor"/>
      </interceptors>
      </caller-pointcut>

      Everything seems to work. I'm able to access the information about the called method using the Advisor of the targetObject field and calledMethodHash field. But in my TracingInterceptor class I would also like to access the information about the real caller, i.e. the name of the real caller class and the name of the caller-method. So far, I have found callingMethodHash field in the CallerInvocation class. But I don't know what to dot with it, to get the information about the class of the caller and the name (and prototype, e.g. parameter types) of the calling method. Do you have any ideas, how to get this information?

      BTW, I think it is generally very useful to be able to get the information about the caller for all types of invocations: ConstructorInvocations, MethodInvocations, etc. Is it possible? If not, are there any plans to implement it?

      One more thing I want to ask is if it is possible to use a wildcard for the calledMethodName in a caller-pointcut definition, so that I can give a mask not only for the name of the method, but also for the types of its parameters. I think AspectJ allows this type of wildcards.

      Thanks in advance for any help,
      Ivan


        • 1. Re: Caller-pointcut question
          Sean Radford Novice

           

          "sradford" wrote:
          "sradford" wrote:
          Hi,

          Anyone know any answers to the above?

          Regards,

          Sean


          • 2. Re: Caller-pointcut question
            rythos Newbie

             

            "rythos" wrote:
            "rythos" wrote:
            Everything seems to work. I'm able to access the information about the called method using the Advisor of the targetObject field and calledMethodHash field. But in my TracingInterceptor class I would also like to access the information about the real caller, i.e. the name of the real caller class and the name of the caller-method. So far, I have found callingMethodHash field in the CallerInvocation class. But I don't know what to dot with it, to get the information about the class of the caller and the name (and prototype, e.g. parameter types) of the calling method. Do you have any ideas, how to get this information?


            I have a partial solution to this problem. Using the ClassAdvisor (you get from the Advised class) you can call ClassAdvisor.getAdvisedMethods(), which is a Trove hashtable thing, and using the calledMethodHash and calleeMethodHash, pull out the two methods you want to get information on.

            Problems: not every method in your system is being advised. If I'm using a caller-pointcut on a class Run, which calls a method Test.method(), I get a ClassCastException when trying to cast invocation.targetObject (which is an instead of Test) because it isn't being advised, Run is.


            One more thing I want to ask is if it is possible to use a wildcard for the calledMethodName in a caller-pointcut definition, so that I can give a mask not only for the name of the method, but also for the types of its parameters. I think AspectJ allows this type of wildcards.


            XML for my caller pointcut looks like this:

            <caller-pointcut class="aspect.Run" calledClass=".*">

            It does what I expect it to (except for the ClassCastException mentioned above).

            Anyone have more information on this little issue, I'd appreciate it greatly.

            Craig


            • 3. Re: Caller-pointcut question
              Bill Burke Master

              Another way to do it is:

              org.jboss.util.MethodHashing.findMethodByHash(Class clazz, long hash)

              Can you expain your ClassCastException? You cannot cast the targetObject to Test? The targetObject is supposed to be the object you are invoking on, not the object doing the invoking.

              Bill

              • 4. Re: Caller-pointcut question
                rythos Newbie

                Been a while since I looked at this, but I believe this was the example i was playing with.

                class Run {
                 public static void main( String[] args ) {
                 Test t = new Test();
                 t.method();
                 }
                }
                
                class Test {
                 public void method() {
                 }
                }
                


                Have a caller pointcut picking out the call to Test.method() in from Run.main. invocation.targetObject is a Test instance. But Test isn't an Advised, Run is. So if you try to cast targetObject to an Advised you get a ClassCast. Reason I wanted to cast it to an Advised was to get all that other junk I needed, but your solution by passes all of my junk quite smoothly. Thanks :)