// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.lang.reflect.Field;

public class AccessFun {
    public static void main(String[] args) {
        //System.setSecurityManager(new SecurityManager());
        Outer o = new Outer();
        o.test();
    }
}

class Base {
    private static void test(Base o, boolean setaccess, String fieldname) {
        System.out.print(o.getClass().getName() + (setaccess ? " (with)" : "") + " : ");
        try {
            Field f = o.getClass().getDeclaredField(fieldname);
            if(setaccess) {
                f.setAccessible(true);
            }
            int v = f.getInt(o);
            System.out.println(v);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            System.out.println("no access");
        }
    }
    private static void test(Base o) {
        test(o, false, "v");
    }
    private static void testWith(Base o) {
        test(o, true, "v");
    }
    private static void test2(Base o) {
        test(o, false, "v2");
    }
    private static void test2With(Base o) {
        test(o, true, "v3");
    }
    private static void test3(Base o) {
        test(o, false, "v2");
    }
    private static void test3With(Base o) {
        test(o, true, "v3");
    }
    public void test() {
        Outer o = (Outer)this;
        test(o);
        test(o.one);
        test(o.two);
        testWith(o);
        testWith(o.one);
        testWith(o.two);
        test2(o);
        test2(o.one);
        test2(o.two);
        test2With(o);
        test2With(o.one);
        test2With(o.two);
        test3(o);
        test3(o.one);
        test3(o.two);
        test3With(o);
        test3With(o.one);
        test3With(o.two);
    }
}

class Outer extends Base {
    private static Base hack;
    public Base one;
    public Base two;
    @SuppressWarnings("unused")
    private int v = 0;
    public int v2 = 0;
    protected int v3 = 0;
    static class InnerOne extends Base {
        @SuppressWarnings("unused")
        private int v = 1;
        public int v2 = 1;
        protected int v3 = 1;
    }
    static {
       class InnerTwo extends Base {
           @SuppressWarnings("unused")
           private int v = 2;
           @SuppressWarnings("unused")
           public int v2 = 2;
           @SuppressWarnings("unused")
           protected int v3 = 2;
       }
       hack = new InnerTwo();
    }
    public Outer() {
        one = new InnerOne();
        two = hack;
    }
}
