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 TestReflection.java
  30  * @run main TestReflection
  31  */
  32 
  33 import java.lang.reflect.Field;
  34 
  35 public class TestReflection {
  36 
  37     // private static field of nest-top for nestmates to access
  38     private static int priv_field;
  39 
  40     // public constructor so we aren't relying on private access
  41     public TestReflection() {}
  42 
  43     // Methods that will access private static fields of nestmates
  44 
  45     // NOTE: No InnerNested calls in this test because non-static nested types
  46     // can't have static fields. Also no StaticIface calls as static interface
  47     // fields must be public (and final)
  48 
  49     void access_priv(TestReflection o) throws Throwable {
  50         Field f = o.getClass().getDeclaredField("priv_field");
  51         priv_field = f.getInt(null);
  52         f.setInt(null, 2);
  53     }
  54     void access_priv(StaticNested o) throws Throwable {
  55         Field f = o.getClass().getDeclaredField("priv_field");
  56         priv_field = f.getInt(null);
  57         f.setInt(null, 2);
  58     }
  59 
  60     // The various nestmates
  61 
  62     static interface StaticIface {
  63 
  64         // Methods that will access private static fields of nestmates
  65 
  66         default void access_priv(TestReflection o) throws Throwable {
  67             Field f = o.getClass().getDeclaredField("priv_field");
  68             int priv_field = f.getInt(null);
  69             f.setInt(null, 2);
  70         }
  71         default void access_priv(StaticNested o) throws Throwable {
  72             Field f = o.getClass().getDeclaredField("priv_field");
  73             int priv_field = f.getInt(null);
  74             f.setInt(null, 2);
  75         }
  76     }
  77 
  78     static class StaticNested {
  79 
  80         private static int priv_field;
  81 
  82         // public constructor so we aren't relying on private access
  83         public StaticNested() {}
  84 
  85         // Methods that will access private static fields of nestmates
  86 
  87         void access_priv(TestReflection o) throws Throwable {
  88             Field f = o.getClass().getDeclaredField("priv_field");
  89             priv_field = f.getInt(null);
  90             f.setInt(null, 2);
  91         }
  92         void access_priv(StaticNested o) throws Throwable {
  93             Field f = o.getClass().getDeclaredField("priv_field");
  94             priv_field = f.getInt(null);
  95             f.setInt(null, 2);
  96         }
  97     }
  98 
  99     class InnerNested {
 100 
 101         // public constructor so we aren't relying on private access
 102         public InnerNested() {}
 103 
 104         void access_priv(TestReflection o) throws Throwable {
 105             Field f = o.getClass().getDeclaredField("priv_field");
 106             priv_field = f.getInt(null);
 107             f.setInt(null, 2);
 108         }
 109         void access_priv(StaticNested o) throws Throwable {
 110             Field f = o.getClass().getDeclaredField("priv_field");
 111             priv_field = f.getInt(null);
 112             f.setInt(null, 2);
 113         }
 114     }
 115 
 116     public static void main(String[] args) throws Throwable {
 117         TestReflection o = new TestReflection();
 118         StaticNested s = new StaticNested();
 119         InnerNested i = o.new InnerNested();
 120         StaticIface intf = new StaticIface() {};
 121 
 122         o.access_priv(new TestReflection());
 123         o.access_priv(s);
 124 
 125         s.access_priv(o);
 126         s.access_priv(new StaticNested());
 127 
 128         i.access_priv(o);
 129         i.access_priv(s);
 130 
 131         intf.access_priv(o);
 132         intf.access_priv(s);
 133     }
 134 }