Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Accessing private members from advice

Not sure if this is what you mean but you can always access private state using reflection. Something like

Field myPrivateField = ClassWithPrivateField.class.getDeclaredMethod("myPrivateFeildName");
myPrivateField.setAccessible(true);
Object result = myPrivateField.get(myInstanceOfClassWithPrivateMethod);

Of course this is not type-safe so if you refactor the field-name you'll have to update the advice as well.

/Johan

Cosmin Mogos wrote:
Hi,

Is it possible to access private members of class from an advice that is in another package?

I have a class in package a and I want to create an aspect in package a.test that adds a getState function to the class in package a. For that I wold need access to some private/protected members that I wold not like to make public (or add getters).

This aspect wold be used only for testing purposes so I don't want to put it in package a to get access to protected members.

Thanks,
Cosmin Mogos
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top