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