test/hotspot/jtreg/runtime/Nestmates/privateStaticFields/TestMethodHandles.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 static fields between nestmates and nest-host
       
    28  *          using different flavours of named nested types using MethodHandles
       
    29  * @run main TestMethodHandles
       
    30  */
       
    31 
       
    32 import java.lang.invoke.*;
       
    33 import static java.lang.invoke.MethodHandles.*;
       
    34 
       
    35 import java.lang.reflect.Field;
       
    36 
       
    37 public class TestMethodHandles {
       
    38 
       
    39     // private static field of nest-host for nestmates to access
       
    40     private static int priv_field;
       
    41 
       
    42     // public constructor so we aren't relying on private access
       
    43     public TestMethodHandles() {}
       
    44 
       
    45     // Methods that will access private static fields of nestmates
       
    46 
       
    47     // NOTE: No InnerNested calls in this test because non-static nested types
       
    48     // can't have static fields. Also no StaticIface calls as static interface
       
    49     // fields must be public (and final)
       
    50 
       
    51     void access_priv(TestMethodHandles o) throws Throwable {
       
    52         MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
    53         priv_field = (int) mh.invoke();
       
    54         priv_field = (int) mh.invokeExact();
       
    55         mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
    56         mh.invoke(2);
       
    57         mh.invokeExact(3);
       
    58     }
       
    59     void access_priv(StaticNested o) throws Throwable {
       
    60         MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
    61         priv_field = (int) mh.invoke();
       
    62         priv_field = (int) mh.invokeExact();
       
    63         mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
    64         mh.invoke(2);
       
    65         mh.invokeExact(3);
       
    66     }
       
    67 
       
    68     // The various nestmates
       
    69 
       
    70     static interface StaticIface {
       
    71 
       
    72         // Methods that will access private static fields of nestmates
       
    73 
       
    74         default void access_priv(TestMethodHandles o) throws Throwable {
       
    75             MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
    76             int priv_field = (int) mh.invoke();
       
    77             priv_field = (int) mh.invokeExact();
       
    78             mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
    79             mh.invoke(2);
       
    80             mh.invokeExact(3);
       
    81         }
       
    82         default void access_priv(StaticNested o) throws Throwable {
       
    83             MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
    84             int priv_field = (int) mh.invoke();
       
    85             priv_field = (int) mh.invokeExact();
       
    86             mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
    87             mh.invoke(2);
       
    88             mh.invokeExact(3);
       
    89         }
       
    90     }
       
    91 
       
    92     static class StaticNested {
       
    93 
       
    94         private static int priv_field;
       
    95 
       
    96         // public constructor so we aren't relying on private access
       
    97         public StaticNested() {}
       
    98 
       
    99         // Methods that will access private static fields of nestmates
       
   100 
       
   101         void access_priv(TestMethodHandles o) throws Throwable {
       
   102             MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
   103             priv_field = (int) mh.invoke();
       
   104             priv_field = (int) mh.invokeExact();
       
   105             mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
   106             mh.invoke(2);
       
   107             mh.invokeExact(3);
       
   108         }
       
   109         void access_priv(StaticNested o) throws Throwable {
       
   110             MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
   111             priv_field = (int) mh.invoke();
       
   112             priv_field = (int) mh.invokeExact();
       
   113             mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
   114             mh.invoke(2);
       
   115             mh.invokeExact(3);
       
   116         }
       
   117     }
       
   118 
       
   119     class InnerNested {
       
   120 
       
   121         // public constructor so we aren't relying on private access
       
   122         public InnerNested() {}
       
   123 
       
   124         void access_priv(TestMethodHandles o) throws Throwable {
       
   125             MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
   126             priv_field = (int) mh.invoke();
       
   127             priv_field = (int) mh.invokeExact();
       
   128             mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
   129             mh.invoke(2);
       
   130             mh.invokeExact(3);
       
   131         }
       
   132         void access_priv(StaticNested o) throws Throwable {
       
   133             MethodHandle mh = lookup().findStaticGetter(o.getClass(), "priv_field", int.class);
       
   134             priv_field = (int) mh.invoke();
       
   135             priv_field = (int) mh.invokeExact();
       
   136             mh = lookup().findStaticSetter(o.getClass(), "priv_field", int.class);
       
   137             mh.invoke(2);
       
   138             mh.invokeExact(3);
       
   139         }
       
   140     }
       
   141 
       
   142     public static void main(String[] args) throws Throwable {
       
   143         TestMethodHandles o = new TestMethodHandles();
       
   144         StaticNested s = new StaticNested();
       
   145         InnerNested i = o.new InnerNested();
       
   146         StaticIface intf = new StaticIface() {};
       
   147 
       
   148         o.access_priv(new TestMethodHandles());
       
   149         o.access_priv(s);
       
   150 
       
   151         s.access_priv(o);
       
   152         s.access_priv(new StaticNested());
       
   153 
       
   154         i.access_priv(o);
       
   155         i.access_priv(s);
       
   156 
       
   157         intf.access_priv(o);
       
   158         intf.access_priv(s);
       
   159     }
       
   160 }