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
  29  * @compile -XDdisablePrivateAccessors TestInvoke.java
  30  * @run main TestInvoke
  31  */
  32 
  33 public class TestInvoke {
  34 
  35     // Private method of nest-top for nestmates to access
  36     private void priv_invoke() {
  37         System.out.println("TestInvoke::priv_invoke");
  38     }
  39 
  40     // public constructor so we aren't relying on private access
  41     public TestInvoke() {}
  42 
  43     // Methods that will access private methods of nestmates
  44 
  45     void access_priv(TestInvoke o) {
  46         o.priv_invoke();
  47     }
  48     void access_priv(InnerNested o) {
  49         o.priv_invoke();
  50     }
  51     void access_priv(StaticNested o) {
  52         o.priv_invoke();
  53     }
  54     void access_priv(StaticIface o) {
  55         o.priv_invoke();
  56     }
  57 
  58     // The various nestmates
  59 
  60     static interface StaticIface {
  61 
  62         private void priv_invoke() {
  63             System.out.println("StaticIface::priv_invoke");
  64         }
  65 
  66         // Methods that will access private methods of nestmates
  67 
  68         default void access_priv(TestInvoke o) {
  69             o.priv_invoke();
  70         }
  71         default void access_priv(InnerNested o) {
  72             o.priv_invoke();
  73         }
  74         default void access_priv(StaticNested o) {
  75             o.priv_invoke();
  76         }
  77         default void access_priv(StaticIface o) {
  78             o.priv_invoke();
  79         }
  80     }
  81 
  82     static class StaticNested {
  83 
  84         private void priv_invoke() {
  85             System.out.println("StaticNested::priv_invoke");
  86         }
  87 
  88         // public constructor so we aren't relying on private access
  89         public StaticNested() {}
  90 
  91         // Methods that will access private methods of nestmates
  92 
  93         void access_priv(TestInvoke o) {
  94             o.priv_invoke();
  95         }
  96         void access_priv(InnerNested o) {
  97             o.priv_invoke();
  98         }
  99         void access_priv(StaticNested o) {
 100             o.priv_invoke();
 101         }
 102         void access_priv(StaticIface o) {
 103             o.priv_invoke();
 104         }
 105     }
 106 
 107     class InnerNested {
 108 
 109         private void priv_invoke() {
 110             System.out.println("InnerNested::priv_invoke");
 111         }
 112 
 113         // public constructor so we aren't relying on private access
 114         public InnerNested() {}
 115 
 116         void access_priv(TestInvoke o) {
 117             o.priv_invoke();
 118         }
 119         void access_priv(InnerNested o) {
 120             o.priv_invoke();
 121         }
 122         void access_priv(StaticNested o) {
 123             o.priv_invoke();
 124         }
 125         void access_priv(StaticIface o) {
 126             o.priv_invoke();
 127         }
 128     }
 129 
 130     public static void main(String[] args) {
 131         TestInvoke o = new TestInvoke();
 132         StaticNested s = new StaticNested();
 133         InnerNested i = o.new InnerNested();
 134         StaticIface intf = new StaticIface() {};
 135 
 136         o.access_priv(new TestInvoke());
 137         o.access_priv(i);
 138         o.access_priv(s);
 139         o.access_priv(intf);
 140 
 141         s.access_priv(o);
 142         s.access_priv(i);
 143         s.access_priv(new StaticNested());
 144         s.access_priv(intf);
 145 
 146         i.access_priv(o);
 147         i.access_priv(o.new InnerNested());
 148         i.access_priv(s);
 149         i.access_priv(intf);
 150 
 151         intf.access_priv(o);
 152         intf.access_priv(i);
 153         intf.access_priv(s);
 154         intf.access_priv(new StaticIface(){});
 155     }
 156 }