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