Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How? Reflective access on a private method introduced by an inter-type declaration

Hi Andy,

thanks a lot for your answer. I like the annotation based approach. This would allow me, not having to deal with the mangled names.
Is there any chance, that AjType gets enhanced to directly provide the stuff i would need?

best regards,

Thomas
On May 18, 2010, at 1:40 AM, Andy Clement wrote:

> Hi Thomas,
> 
> I actually thought the AjTypeSystem would allow this. But having just
> tried it, I see that it doesn't !
> 
> The name is stable.  (it is computed by AspectJs
> org.aspectj.weaver.NameMangler.interMethod() which is contained in the
> weaver jar).  So you could create it yourself or simply look for the
> method name that starts with 'ajc$' and ends with '$getName'.
> 
> However, there is also an annotation added to these mangled members,
> which captures the declared name.  Here is some code that shows how to
> access it:
> 
> public class Person {
>  private String name;
> 
>  public static void main(String[] args) {
> 	Method[] ms = Person.class.getDeclaredMethods();
> 	for (int i=0;i<ms.length;i++) {
> 		ajcITD ajcITD =
> ms[i].getAnnotation(org.aspectj.internal.lang.annotation.ajcITD.class);
> 		if (ajcITD!=null) {
> 		  System.out.println("method = "+ms[i].getName()+"  has real
> name="+ajcITD.name());
> 		}
> 	}
>  }
> }
> 
> privileged aspect Person_ITD {
> 	public String Person.getName() {
> 		return name;
> 	}
> }
> 
> 
> For the mangled method, that prints:
> 
> method = getName  has ajcitd=getName
> 
> 
> cheers,
> Andy
> 
> On 17 May 2010 15:26, Thomas Wieger <thomas.wieger@xxxxxx> wrote:
>> 
>> Hello!
>> 
>> I'm just wondering, how i could invoke a private method introduced by an inter-type declaration via java reflection (method#invoke(...)) using the aspectj runtime infrastructure, possibly the AjTypeSystem.
>> 
>> What i want to do is something like this. Suppose i have a class Person, which looks like this:
>> 
>> class Person {
>>   private String name;
>> }
>> 
>> now i declare an inter-type:
>> 
>> privileged aspect Person_IDT {
>>   private String Person.getName()  {
>>       return name;
>>   }
>> }
>> 
>> now i would like to do something like this:
>> 
>> Method readMethod = Person.class.getDeclaredMethod("getName");
>> readMethod.setAccessible(true);
>> readMethod.invoke(aTarget);
>> 
>> Actually i know, that i won't find the method in this way, because AspectJ mangles the name to something beginning with "ajc$interMethodDispatch2$". But my hope was, that somewhere in the aspectj runtime, there would be some way to achieve something like previously described. Unfortunately i haven't found a way.
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top