1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8046171
  27  * @summary Test access to private methods between nestmates and nest-top
  28  *          using different flavours of named nested types using core reflection
  29  * @run main TestReflection
  30  */
  31 
  32 public class TestReflection {
  33 
  34     // Private method of nest-top for nestmates to access
  35     private void priv_invoke() {
  36         System.out.println("TestReflection::priv_invoke");
  37     }
  38 
  39     // public constructor so we aren't relying on private access
  40     public TestReflection() {}
  41 
  42     // Methods that will access private methods of nestmates
  43 
  44     void access_priv(TestReflection o) throws Throwable {
  45         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  46     }
  47     void access_priv(InnerNested o) throws Throwable {
  48         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  49     }
  50     void access_priv(StaticNested o) throws Throwable {
  51         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  52     }
  53     void access_priv(StaticIface o) throws Throwable {
  54         StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  55     }
  56 
  57     // The various nestmates
  58 
  59     static interface StaticIface {
  60 
  61         private void priv_invoke() {
  62             System.out.println("StaticIface::priv_invoke");
  63         }
  64 
  65         // Methods that will access private methods of nestmates
  66 
  67         default void access_priv(TestReflection o) throws Throwable {
  68             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  69         }
  70         default void access_priv(InnerNested o) throws Throwable {
  71             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  72         }
  73         default void access_priv(StaticNested o) throws Throwable {
  74             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  75         }
  76         default void access_priv(StaticIface o) throws Throwable {
  77             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  78         }
  79     }
  80 
  81     static class StaticNested {
  82 
  83         private void priv_invoke() {
  84             System.out.println("StaticNested::priv_invoke");
  85         }
  86 
  87         // public constructor so we aren't relying on private access
  88         public StaticNested() {}
  89 
  90         // Methods that will access private methods of nestmates
  91 
  92         void access_priv(TestReflection o) throws Throwable {
  93             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  94         }
  95         void access_priv(InnerNested o) throws Throwable {
  96             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  97         }
  98         void access_priv(StaticNested o) throws Throwable {
  99             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 100         }
 101         void access_priv(StaticIface o) throws Throwable {
 102             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 103         }
 104     }
 105 
 106     class InnerNested {
 107 
 108         private void priv_invoke() {
 109             System.out.println("InnerNested::priv_invoke");
 110         }
 111 
 112         // public constructor so we aren't relying on private access
 113         public InnerNested() {}
 114 
 115         void access_priv(TestReflection o) throws Throwable {
 116             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 117         }
 118         void access_priv(InnerNested o) throws Throwable {
 119             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 120         }
 121         void access_priv(StaticNested o) throws Throwable {
 122             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 123         }
 124         void access_priv(StaticIface o) throws Throwable {
 125             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 126         }
 127     }
 128 
 129     public static void main(String[] args) throws Throwable {
 130         TestReflection o = new TestReflection();
 131         StaticNested s = new StaticNested();
 132         InnerNested i = o.new InnerNested();
 133         StaticIface intf = new StaticIface() {};
 134 
 135         o.access_priv(new TestReflection());
 136         o.access_priv(i);
 137         o.access_priv(s);
 138         o.access_priv(intf);
 139 
 140         s.access_priv(o);
 141         s.access_priv(i);
 142         s.access_priv(new StaticNested());
 143         s.access_priv(intf);
 144 
 145         i.access_priv(o);
 146         i.access_priv(o.new InnerNested());
 147         i.access_priv(s);
 148         i.access_priv(intf);
 149 
 150         intf.access_priv(o);
 151         intf.access_priv(i);
 152         intf.access_priv(s);
 153         intf.access_priv(new StaticIface(){});
 154     }
 155 }