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 static methods between nestmates and nest-top
  28  *          using different flavours of named nested types
  29  * @compile -XDdisablePrivateAccessors TestInvokeStatic.java
  30  * @run main TestInvokeStatic
  31  */
  32 
  33 public class TestInvokeStatic {
  34 
  35     // Private static method of nest-top for nestmates to access
  36     private static void priv_static_invoke() {
  37         System.out.println("TestInvokeStatic::priv_static_invoke");
  38     }
  39 
  40     // public constructor so we aren't relying on private access
  41     public TestInvokeStatic() {}
  42 
  43     // Methods that will access private static methods of nestmates
  44     // We use the arguments for overloading purposes and use the fact
  45     // you can invoke a static method through an object reference for
  46     // convenience. Except for static interface methods of course.
  47 
  48     // NOTE: No InnerNested calls in this test because non-static nested types
  49     // can't have static method
  50 
  51     void access_priv(TestInvokeStatic o) {
  52         o.priv_static_invoke();
  53     }
  54     void access_priv(StaticNested o) {
  55         o.priv_static_invoke();
  56     }
  57     void access_priv(StaticIface o) {
  58         StaticIface.priv_static_invoke();
  59     }
  60 
  61     // The various nestmates
  62 
  63     static interface StaticIface {
  64 
  65         private static void priv_static_invoke() {
  66             System.out.println("StaticIface::priv_static_invoke");
  67         }
  68 
  69         // Methods that will access private static methods of nestmates
  70 
  71         default void access_priv(TestInvokeStatic o) {
  72             o.priv_static_invoke();
  73         }
  74         default void access_priv(StaticNested o) {
  75             o.priv_static_invoke();
  76         }
  77         default void access_priv(StaticIface o) {
  78             StaticIface.priv_static_invoke();
  79         }
  80     }
  81 
  82     static class StaticNested {
  83 
  84         private static void priv_static_invoke() {
  85             System.out.println("StaticNested::priv_static_invoke");
  86         }
  87 
  88         // public constructor so we aren't relying on private access
  89         public StaticNested() {}
  90 
  91         // Methods that will access private static methods of nestmates
  92 
  93         void access_priv(TestInvokeStatic o) {
  94             o.priv_static_invoke();
  95         }
  96         void access_priv(StaticNested o) {
  97             o.priv_static_invoke();
  98         }
  99         void access_priv(StaticIface o) {
 100             StaticIface.priv_static_invoke();
 101         }
 102     }
 103 
 104     class InnerNested {
 105 
 106         // public constructor so we aren't relying on private access
 107         public InnerNested() {}
 108 
 109         void access_priv(TestInvokeStatic o) {
 110             o.priv_static_invoke();
 111         }
 112         void access_priv(StaticNested o) {
 113             o.priv_static_invoke();
 114         }
 115         void access_priv(StaticIface o) {
 116             StaticIface.priv_static_invoke();
 117         }
 118     }
 119 
 120     public static void main(String[] args) {
 121         TestInvokeStatic o = new TestInvokeStatic();
 122         StaticNested s = new StaticNested();
 123         InnerNested i = o.new InnerNested();
 124         StaticIface intf = new StaticIface() {};
 125 
 126         o.access_priv(new TestInvokeStatic());
 127         o.access_priv(s);
 128         o.access_priv(intf);
 129 
 130         s.access_priv(o);
 131         s.access_priv(new StaticNested());
 132         s.access_priv(intf);
 133 
 134         i.access_priv(o);
 135         i.access_priv(s);
 136         i.access_priv(intf);
 137 
 138         intf.access_priv(o);
 139         intf.access_priv(s);
 140         intf.access_priv(new StaticIface(){});
 141     }
 142 }