Re: [aspectwerkz-user] Private member access from an Around joinpoint on a method
gags_78 <[email protected]> Mon, 16 Jun 2008 07:14:28 -0700 (PDT)
| Newsgroups | gmane.comp.java.aspectwerkz.user |
|---|---|
| Message-ID | <[email protected]> |
Hey folks,
Got around this using reflection within the aspect.
with the Around aspect advice :-
public Object aroundSayHello(JoinPoint joinPoint) {
MethodRtti rtti = (MethodRtti)joinPoint.getRtti();
MemberAccess ma = (MemberAccessrtti.getThis();
String name= null;
Object parameter = null;
try {
Field nameField = MemberAccess .class.getDeclaredField("name");
nameField .setAccessible(true);
name = (String) nameField .get(ma );
Field parameterField = MemberAccess
.class.getDeclaredField("parameter");
parameterField.setAccessible(true);
parameter = parameterField.get(ma );
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
System.out.println("The name is " + name);
System.out.println("The parameter is " + parameter);
:
: proceed to do things. Very baaaad things.
:
return "Please press any key to move funds to offshore Caymen
account"; // Ahem.
}
When using the Accessible.setAccessible method you may very well encounter
problems with SecurityPermissions. This will usually happen in some kind of
enterprise environment. In this case
you need to probably look into java policy and grant access.
HTH someone else,
Mark.
gags_78 wrote:
>
> Hey folks,
>
> I'm fairly new to AOP so forgive me for any major gaffes.
>
> I have the following class. It has two private members initialised in the
> constructor.
>
> package com.mystuff;
>
> public class MemberAccess{
>
> private String name;
> private Object parameter;
>
> public MemberAccess(String name, Object parameter) {
> this.name = name;
> this.parameter = parameter;
> }
>
> public String sayHello() {
> return "Hello to " + name + " with object of type " +
> parameter.getClass();
> }
>
> }
>
> I have an Around advice declared around the sayHello method and in it I'm
> using the MethodRtti to
> be able to access information about the method and the class, alas as the
> members are declared private
> in the aspected class I can't seem to access them. Is it possible to
> access private members from within the scope of a method advice?
>
> Thanks,
> Mark.
>
>
>
>
--
View this message in context: http://www.nabble.com/Private-member-access-from-an-Around-joinpoint-on-a-method-tp17862543p17865349.html
Sent from the AspectWerkz - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email