test/hotspot/jtreg/runtime/Nestmates/privateStaticMethods/TestJNI.java
changeset 50735 2f2af62dfac7
equal deleted inserted replaced
50734:0828a0f6676b 50735:2f2af62dfac7
       
     1 /*
       
     2  * Copyright (c) 2018, 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 JNI access to private methods between nestmates and nest-host
       
    28  *          using different flavours of named nested types
       
    29  * @compile ../NestmatesJNI.java
       
    30  * @run main/othervm/native TestJNI
       
    31  * @run main/othervm/native -Xcheck:jni TestJNI
       
    32  */
       
    33 public class TestJNI {
       
    34 
       
    35     // Unlike reflection, the calling context is not relevant to JNI
       
    36     // calls, but we keep the same structure as the reflection tests.
       
    37 
       
    38     // Private static method of nest-host for nestmates to access
       
    39     private static void priv_static_invoke() {
       
    40         System.out.println("TestJNI::priv_static_invoke");
       
    41     }
       
    42 
       
    43     static final String METHOD = "priv_static_invoke";
       
    44 
       
    45     // public constructor so we aren't relying on private access
       
    46     public TestJNI() {}
       
    47 
       
    48     // Methods that will access private static methods of nestmates
       
    49     // We use the arguments for overloading purposes and use the fact
       
    50     // you can invoke a static method through an object reference for
       
    51     // convenience. Except for static interface methods of course.
       
    52 
       
    53     // NOTE: No InnerNested calls in this test because non-static nested types
       
    54     // can't have static method
       
    55 
       
    56     void access_priv(TestJNI o) {
       
    57         doCall(o.getClass(), METHOD);
       
    58     }
       
    59     void access_priv(StaticNested o) {
       
    60         doCall(o.getClass(), METHOD);
       
    61     }
       
    62     void access_priv(StaticIface o) {
       
    63         // Can't use o.getClass() as the method is not in that class
       
    64         doCall(StaticIface.class, METHOD);
       
    65     }
       
    66 
       
    67     // The various nestmates
       
    68 
       
    69     static interface StaticIface {
       
    70 
       
    71         private static void priv_static_invoke() {
       
    72             System.out.println("StaticIface::priv_static_invoke");
       
    73         }
       
    74 
       
    75         // Methods that will access private static methods of nestmates
       
    76 
       
    77         default void access_priv(TestJNI o) {
       
    78             doCall(o.getClass(), METHOD);
       
    79         }
       
    80         default void access_priv(StaticNested o) {
       
    81             doCall(o.getClass(), METHOD);
       
    82         }
       
    83         default void access_priv(StaticIface o) {
       
    84             // Can't use o.getClass() as the method is not in that class
       
    85             doCall(StaticIface.class, METHOD);
       
    86         }
       
    87     }
       
    88 
       
    89     static class StaticNested {
       
    90 
       
    91         private static void priv_static_invoke() {
       
    92             System.out.println("StaticNested::priv_static_invoke");
       
    93         }
       
    94 
       
    95         // public constructor so we aren't relying on private access
       
    96         public StaticNested() {}
       
    97 
       
    98         // Methods that will access private static methods of nestmates
       
    99 
       
   100         void access_priv(TestJNI o) {
       
   101             doCall(o.getClass(), METHOD);
       
   102         }
       
   103         void access_priv(StaticNested o) {
       
   104             doCall(o.getClass(), METHOD);
       
   105         }
       
   106         void access_priv(StaticIface o) {
       
   107             // Can't use o.getClass() as the method is not in that class
       
   108             doCall(StaticIface.class, METHOD);
       
   109         }
       
   110     }
       
   111 
       
   112     class InnerNested {
       
   113 
       
   114         // public constructor so we aren't relying on private access
       
   115         public InnerNested() {}
       
   116 
       
   117         void access_priv(TestJNI o) {
       
   118             doCall(o.getClass(), METHOD);
       
   119         }
       
   120         void access_priv(StaticNested o) {
       
   121             doCall(o.getClass(), METHOD);
       
   122         }
       
   123         void access_priv(StaticIface o) {
       
   124             // Can't use o.getClass() as the method is not in that class
       
   125             doCall(StaticIface.class, METHOD);
       
   126         }
       
   127     }
       
   128 
       
   129     public static void main(String[] args) {
       
   130         TestJNI o = new TestJNI();
       
   131         StaticNested s = new StaticNested();
       
   132         InnerNested i = o.new InnerNested();
       
   133         StaticIface intf = new StaticIface() {};
       
   134 
       
   135         o.access_priv(new TestJNI());
       
   136         o.access_priv(s);
       
   137         o.access_priv(intf);
       
   138 
       
   139         s.access_priv(o);
       
   140         s.access_priv(new StaticNested());
       
   141         s.access_priv(intf);
       
   142 
       
   143         i.access_priv(o);
       
   144         i.access_priv(s);
       
   145         i.access_priv(intf);
       
   146 
       
   147         intf.access_priv(o);
       
   148         intf.access_priv(s);
       
   149         intf.access_priv(new StaticIface(){});
       
   150     }
       
   151 
       
   152     static void doCall(Class<?> klass, String method) {
       
   153         String definingClass = klass.getName();
       
   154         String desc = "Static invocation of " + definingClass + "." + method;
       
   155         try {
       
   156             NestmatesJNI.callStaticVoidVoid(definingClass, method);
       
   157             System.out.println(desc + " - passed");
       
   158         }
       
   159         catch (Throwable t) {
       
   160             throw new Error(desc + ": Unexpected exception: " + t, t);
       
   161         }
       
   162     }
       
   163 }