test/hotspot/jtreg/runtime/Nestmates/privateMethods/TestReflection.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 methods between nestmates and nest-host
       
    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-host 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     // Methods that will access private methods of nestmates
       
    49 
       
    50     void access_priv(TestReflection o) throws Throwable {
       
    51         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    52     }
       
    53     void access_priv(InnerNested o) throws Throwable {
       
    54         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    55     }
       
    56     void access_priv(StaticNested o) throws Throwable {
       
    57         o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    58     }
       
    59     void access_priv(StaticIface o) throws Throwable {
       
    60         // Can't use o.getClass() as the method is not in that class
       
    61         StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    62     }
       
    63 
       
    64     // The various nestmates
       
    65 
       
    66     static interface StaticIface {
       
    67 
       
    68         private void priv_invoke() {
       
    69             System.out.println("StaticIface::priv_invoke");
       
    70         }
       
    71 
       
    72         // Methods that will access private methods of nestmates
       
    73 
       
    74         default void access_priv(TestReflection o) throws Throwable {
       
    75             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    76         }
       
    77         default void access_priv(InnerNested o) throws Throwable {
       
    78             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    79         }
       
    80         default void access_priv(StaticNested o) throws Throwable {
       
    81             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    82         }
       
    83         default void access_priv(StaticIface o) throws Throwable {
       
    84             // Can't use o.getClass() as the method is not in that class
       
    85             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
    86         }
       
    87     }
       
    88 
       
    89     static class StaticNested {
       
    90 
       
    91         private void priv_invoke() {
       
    92             System.out.println("StaticNested::priv_invoke");
       
    93         }
       
    94 
       
    95         // public constructor so we aren't relying on private access
       
    96         public StaticNested() {}
       
    97 
       
    98         // Methods that will access private methods of nestmates
       
    99 
       
   100         void access_priv(TestReflection o) throws Throwable {
       
   101             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   102         }
       
   103         void access_priv(InnerNested o) throws Throwable {
       
   104             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   105         }
       
   106         void access_priv(StaticNested o) throws Throwable {
       
   107             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   108         }
       
   109         void access_priv(StaticIface o) throws Throwable {
       
   110             // Can't use o.getClass() as the method is not in that class
       
   111             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   112         }
       
   113     }
       
   114 
       
   115     class InnerNested {
       
   116 
       
   117         private void priv_invoke() {
       
   118             System.out.println("InnerNested::priv_invoke");
       
   119         }
       
   120 
       
   121         // public constructor so we aren't relying on private access
       
   122         public InnerNested() {}
       
   123 
       
   124         void access_priv(TestReflection o) throws Throwable {
       
   125             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   126         }
       
   127         void access_priv(InnerNested o) throws Throwable {
       
   128             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   129         }
       
   130         void access_priv(StaticNested o) throws Throwable {
       
   131             o.getClass().getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   132         }
       
   133         void access_priv(StaticIface o) throws Throwable {
       
   134             // Can't use o.getClass() as the method is not in that class
       
   135             StaticIface.class.getDeclaredMethod("priv_invoke", new Class<?>[0]).invoke(o, new Object[0]);
       
   136         }
       
   137     }
       
   138 
       
   139     public static void main(String[] args) throws Throwable {
       
   140         TestReflection o = new TestReflection();
       
   141         StaticNested s = new StaticNested();
       
   142         InnerNested i = o.new InnerNested();
       
   143         StaticIface intf = new StaticIface() {};
       
   144 
       
   145         o.access_priv(new TestReflection());
       
   146         o.access_priv(i);
       
   147         o.access_priv(s);
       
   148         o.access_priv(intf);
       
   149 
       
   150         s.access_priv(o);
       
   151         s.access_priv(i);
       
   152         s.access_priv(new StaticNested());
       
   153         s.access_priv(intf);
       
   154 
       
   155         i.access_priv(o);
       
   156         i.access_priv(o.new InnerNested());
       
   157         i.access_priv(s);
       
   158         i.access_priv(intf);
       
   159 
       
   160         intf.access_priv(o);
       
   161         intf.access_priv(i);
       
   162         intf.access_priv(s);
       
   163         intf.access_priv(new StaticIface(){});
       
   164     }
       
   165 }