[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[aspectj-users] Re: [NEWSDELIVER] Help: how to get calling class for static methods?
|
Sunny,
The following:
package test.staticmethods;
public aspect Aspect {
pointcut
staticMethodCall() : call(static * *.*(..));
after()
: staticMethodCall()
{
Class callerClass
= thisEnclosingJoinPointStaticPart.getSignature().getDeclaringType();
Class calleeClass
= thisJoinPoint.getSignature().getDeclaringType();
System.out.println("Aspect.after()
" + callerClass + " -> " + calleeClass);
//
Object caller = thisJoinPoint.getThis();
//
//
if (caller == null)
//
//
caller = thisJoinPoint.getSourceLocation().getWithinType();
}
}
yields:
from B
Aspect.after() class test.staticmethods.A
-> class test.staticmethods.B
Aspect.after() class test.staticmethods.A
-> class test.staticmethods.A
You could also use thisJoinPointStaticPart
instead of thisJoinPoint.
Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester, SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/
To:
Matthew Webster/UK/IBM@IBMGB
cc:
Subject:
Re: [NEWSDELIVER]
Help: how to get calling class for static methods?
Hello Matthew,
Thank you very much for the tip. I just don't know why
I could not find the thisEnclosingJoinPointStaticPart variable in the AspectJ
API in my Language Guide but it is still legal to use this variable in
the advice (I am using AJDT for eclipse 3.0). I did got it through
the link:
http://www.eclipse.org/aspectj/doc/next/runtime-api/index.html?org/aspectj/lang/package-summary.html
Yet, I did not see any more methods than its parent, JoinPoint.StaticPart
. Is it the real case? If yes, may I still call
thisEnclosingJoinPointStaticPart
.getSourceLocation().getWithinType(), even if I don't have source code
available?
Best wishes,
SunnyDay
On 3/21/06, Matthew Webster <matthew_webster@xxxxxxxxxx>
wrote:
SunnyDay,
Use thisJoinPoint.getSignature().getDeclaringType() for the target class
and thisEnclosingJoinPointStaticpart... for the caller.
Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester, SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/
To:
undisclosed-recipients:;
cc:
Subject: [NEWSDELIVER]
Help: how to get calling class for static methods?
Hello,
I am having trouble in obtaining the caller and callee's classes if a
method call is made within the caller's static method. For example:
public class A
{
public static void
call1()
{
B.changeField();
}
}
public class B {
public static void
changeField()
{
System.out.println("from
B");
}
}
How can I know the caller is class A and the callee is class B? I tried
the following pointcut:
pointcut staticMethodCall() : call(static * *.*(..));
after() : staticMethodCall()
{
Class calleeClass
= thisJoinPoint.getSignature().getDeclaringType();
Object caller = thisJoinPoint.getThis();
if (caller == null)
caller = thisJoinPoint.getSourceLocation().getWithinType();
}
Here I used thisJoinPoint.getSignature().getDeclaringType() to obtain the
callee's class. But I am not sure what's the exact meaning of "Signature"
in the context of a method call. Does it always give me the type of the
callee?
For the caller, I used thisJoinPoint.getSourceLocation ().getWithinType();
But what if I test a program where no source code is available? Can I
still call thisJoinPoint.getSourceLocation()?
All these seem to work for now but I am really wondering if this is a good
solution. I appreciate any advice and code snippet!
Thank you,
SunnyDay