0

I'm attempting to extend a class in order to polymorphically run its functions as privileged. I want to avoid modifying the base class, Fooer in the example, and I definitely want to avoid reflection. I've been working mainly with javascript and python lately so my mind keeps repeating, "Use a function pointer" but since functions are not first class objects in java that is not an option. This seems like a valid goal so I'm assuming someone know a good design pattern for this that I'm missing.

public class SecureFooer extends Fooer()
{
    @Override
    public Object foo()
    {
        PrivilegedAction<Object> action = new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                // This isn't going to work, i'm inside PrivilegedAction
                return super.foo()
            }
        };
        return AccessController.doPrivileged(action);
    }
}
Kevin Brey
  • 1,233
  • 1
  • 12
  • 18

1 Answers1

4

The syntax is

return SecureFooer.super.foo();

This works because the PrivilegedAction anonymous class is an inner class of SecureFooer and all inner classes have access to their enclosing instance, and to that instance's super implementation.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I tried this after reading [this](http://stackoverflow.com/questions/1084112/access-this-from-java-anonymous-class?rq=1) just after posting this question. I verified it worked and was just about to post the answer but it appears you beat me to it. Thanks! – Kevin Brey Jun 26 '14 at 20:48