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-top
  28  *          using different flavours of named nested types using core reflection
  29  * @run main TestReflection
  30  * @run main/othervm -Dsun.reflect.noInflation=true TestReflection
  31  */
  32 
  33 // The first run will use NativeMethodAccessor and due to the limited number
  34 // of calls we will not reach the inflation threshold.
  35 // The second run disables inflation so we will use the GeneratedMethodAccessor
  36 // instead. In this way both sets of Reflection classes are tested.
  37 
  38 public class TestReflection {
  39 
  40     // Private method of nest-top for nestmates to access
  41     private void priv_invoke() {
  42         System.out.println("TestReflection::priv_invoke");
  43     }
  44 
  45     // public constructor so we aren't relying on private access
  46     public TestReflection() {}
  47 
  48     // We test access via getClass() as well as Foo.class
  49 
  50     // Methods that will access private methods of nestmates
  51 
  52     void access_priv(TestReflection o) throws Throwable {
  53         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  54         TestReflection.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  55     }
  56     void access_priv(InnerNested o) throws Throwable {
  57         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  58         InnerNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  59     }
  60     void access_priv(StaticNested o) throws Throwable {
  61         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  62         StaticNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  63     }
  64     void access_priv(StaticIface o) throws Throwable {
  65         // Can't use o.getClass() as the method is not in that class
  66         StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  67     }
  68 
  69     // The various nestmates
  70 
  71     static interface StaticIface {
  72 
  73         private void priv_invoke() {
  74             System.out.println("StaticIface::priv_invoke");
  75         }
  76 
  77         // Methods that will access private methods of nestmates
  78 
  79         default void access_priv(TestReflection o) throws Throwable {
  80             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  81             TestReflection.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  82         }
  83         default void access_priv(InnerNested o) throws Throwable {
  84             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  85             InnerNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  86         }
  87         default void access_priv(StaticNested o) throws Throwable {
  88             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  89             StaticNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  90         }
  91         default void access_priv(StaticIface o) throws Throwable {
  92             // Can't use o.getClass() as the method is not in that class
  93             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
  94         }
  95     }
  96 
  97     static class StaticNested {
  98 
  99         private void priv_invoke() {
 100             System.out.println("StaticNested::priv_invoke");
 101         }
 102 
 103         // public constructor so we aren't relying on private access
 104         public StaticNested() {}
 105 
 106         // Methods that will access private methods of nestmates
 107 
 108         void access_priv(TestReflection o) throws Throwable {
 109             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 110             TestReflection.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 111         }
 112         void access_priv(InnerNested o) throws Throwable {
 113             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 114             InnerNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 115         }
 116         void access_priv(StaticNested o) throws Throwable {
 117             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 118             StaticNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 119         }
 120         void access_priv(StaticIface o) throws Throwable {
 121             // Can't use o.getClass() as the method is not in that class
 122             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 123         }
 124     }
 125 
 126     class InnerNested {
 127 
 128         private void priv_invoke() {
 129             System.out.println("InnerNested::priv_invoke");
 130         }
 131 
 132         // public constructor so we aren't relying on private access
 133         public InnerNested() {}
 134 
 135         void access_priv(TestReflection o) throws Throwable {
 136             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 137             TestReflection.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 138         }
 139         void access_priv(InnerNested o) throws Throwable {
 140             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 141             InnerNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 142         }
 143         void access_priv(StaticNested o) throws Throwable {
 144             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 145             StaticNested.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 146         }
 147         void access_priv(StaticIface o) throws Throwable {
 148             // Can't use o.getClass() as the method is not in that class
 149             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
 150         }
 151     }
 152 
 153     public static void main(String[] args) throws Throwable {
 154         TestReflection o = new TestReflection();
 155         StaticNested s = new StaticNested();
 156         InnerNested i = o.new InnerNested();
 157         StaticIface intf = new StaticIface() {};
 158 
 159         o.access_priv(new TestReflection());
 160         o.access_priv(i);
 161         o.access_priv(s);
 162         o.access_priv(intf);
 163 
 164         s.access_priv(o);
 165         s.access_priv(i);
 166         s.access_priv(new StaticNested());
 167         s.access_priv(intf);
 168 
 169         i.access_priv(o);
 170         i.access_priv(o.new InnerNested());
 171         i.access_priv(s);
 172         i.access_priv(intf);
 173 
 174         intf.access_priv(o);
 175         intf.access_priv(i);
 176         intf.access_priv(s);
 177         intf.access_priv(new StaticIface(){});
 178     }
 179 }