Private member access from an Around joinpoint on a method

View: New views
2 Messages — Rating Filter:   Alert me  

Private member access from an Around joinpoint on a method

by gags_78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.



Re: Private member access from an Around joinpoint on a method

by gags_78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.