Discussion:
[aspectwerkz-user] Private member access from an Around joinpoint on a method
gags_78
2008-06-16 11:40:37 UTC
Permalink
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-tp17862543p17862543.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
gags_78
2008-06-16 14:14:28 UTC
Permalink
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.
Post by gags_78
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
Loading...