test/hotspot/jtreg/runtime/Nestmates/privateMethods/TestInvoke.java
changeset 50735 2f2af62dfac7
equal deleted inserted replaced
50734:0828a0f6676b 50735:2f2af62dfac7
       
     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-host
       
    28  *          using different flavours of named nested types
       
    29   * @run main TestInvoke
       
    30  */
       
    31 
       
    32 public class TestInvoke {
       
    33 
       
    34     // Private method of nest-host for nestmates to access
       
    35     private void priv_invoke() {
       
    36         System.out.println("TestInvoke::priv_invoke");
       
    37     }
       
    38 
       
    39     // public constructor so we aren't relying on private access
       
    40     public TestInvoke() {}
       
    41 
       
    42     // Methods that will access private methods of nestmates
       
    43 
       
    44     void access_priv(TestInvoke o) {
       
    45         o.priv_invoke();
       
    46     }
       
    47     void access_priv(InnerNested o) {
       
    48         o.priv_invoke();
       
    49     }
       
    50     void access_priv(StaticNested o) {
       
    51         o.priv_invoke();
       
    52     }
       
    53     void access_priv(StaticIface o) {
       
    54         o.priv_invoke();
       
    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(TestInvoke o) {
       
    68             o.priv_invoke();
       
    69         }
       
    70         default void access_priv(InnerNested o) {
       
    71             o.priv_invoke();
       
    72         }
       
    73         default void access_priv(StaticNested o) {
       
    74             o.priv_invoke();
       
    75         }
       
    76         default void access_priv(StaticIface o) {
       
    77             o.priv_invoke();
       
    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(TestInvoke o) {
       
    93             o.priv_invoke();
       
    94         }
       
    95         void access_priv(InnerNested o) {
       
    96             o.priv_invoke();
       
    97         }
       
    98         void access_priv(StaticNested o) {
       
    99             o.priv_invoke();
       
   100         }
       
   101         void access_priv(StaticIface o) {
       
   102             o.priv_invoke();
       
   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() {
       
   116             TestInvoke.this.priv_invoke(); // check this$0 access
       
   117         }
       
   118         void access_priv(TestInvoke o) {
       
   119             o.priv_invoke();
       
   120         }
       
   121         void access_priv(InnerNested o) {
       
   122             o.priv_invoke();
       
   123         }
       
   124         void access_priv(StaticNested o) {
       
   125             o.priv_invoke();
       
   126         }
       
   127         void access_priv(StaticIface o) {
       
   128             o.priv_invoke();
       
   129         }
       
   130     }
       
   131 
       
   132     public static void main(String[] args) {
       
   133         TestInvoke o = new TestInvoke();
       
   134         StaticNested s = new StaticNested();
       
   135         InnerNested i = o.new InnerNested();
       
   136         StaticIface intf = new StaticIface() {};
       
   137 
       
   138         o.access_priv(new TestInvoke());
       
   139         o.access_priv(i);
       
   140         o.access_priv(s);
       
   141         o.access_priv(intf);
       
   142 
       
   143         s.access_priv(o);
       
   144         s.access_priv(i);
       
   145         s.access_priv(new StaticNested());
       
   146         s.access_priv(intf);
       
   147 
       
   148         i.access_priv();
       
   149         i.access_priv(o);
       
   150         i.access_priv(o.new InnerNested());
       
   151         i.access_priv(s);
       
   152         i.access_priv(intf);
       
   153 
       
   154         intf.access_priv(o);
       
   155         intf.access_priv(i);
       
   156         intf.access_priv(s);
       
   157         intf.access_priv(new StaticIface(){});
       
   158     }
       
   159 }