test/hotspot/jtreg/runtime/Nestmates/privateConstructors/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 constructors between nestmates and nest-host
       
    28  *          using different flavours of named nested types using core reflection
       
    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 
       
    39     // All constructors are private to ensure nestmate access checks apply
       
    40 
       
    41     // All doConstruct methods are public so they don't involve nestmate access
       
    42 
       
    43     private TestJNI() {}
       
    44 
       
    45     // The various nestmates
       
    46 
       
    47     // Note: No constructor on interfaces so no StaticIface variants
       
    48 
       
    49     static interface StaticIface {
       
    50 
       
    51         // Methods that will access private constructors of nestmates.
       
    52 
       
    53         default void doConstruct(TestJNI o) throws Throwable {
       
    54             Object obj = newInstance(o.getClass());
       
    55         }
       
    56         default void doConstruct(TestJNI outerThis, InnerNested o) throws Throwable {
       
    57             Object obj = newInstance(o.getClass(), outerThis);
       
    58         }
       
    59         default void doConstruct(StaticNested o) throws Throwable {
       
    60             Object obj = newInstance(o.getClass());
       
    61         }
       
    62     }
       
    63 
       
    64     static class StaticNested {
       
    65 
       
    66         private StaticNested() {}
       
    67 
       
    68         // Methods that will access private constructors of nestmates.
       
    69         // The arg is a dummy for overloading purposes
       
    70 
       
    71         public void doConstruct(TestJNI o) throws Throwable {
       
    72             Object obj = newInstance(o.getClass());
       
    73         }
       
    74         public  void doConstruct(TestJNI outerThis, InnerNested o) throws Throwable {
       
    75             Object obj = newInstance(o.getClass(), outerThis);
       
    76         }
       
    77         public void doConstruct(StaticNested o) throws Throwable {
       
    78             Object obj = newInstance(o.getClass());
       
    79         }
       
    80     }
       
    81 
       
    82     class InnerNested {
       
    83 
       
    84         private InnerNested() {}
       
    85 
       
    86         // Methods that will access private constructors of nestmates.
       
    87         // The arg is a dummy for overloading purposes
       
    88 
       
    89         public void doConstruct(TestJNI o) throws Throwable {
       
    90             Object obj = newInstance(o.getClass());
       
    91         }
       
    92         public  void doConstruct(TestJNI outerThis, InnerNested o) throws Throwable {
       
    93             Object obj = newInstance(o.getClass(), outerThis);
       
    94         }
       
    95         public void doConstruct(StaticNested o) throws Throwable {
       
    96             Object obj = newInstance(o.getClass());
       
    97         }
       
    98     }
       
    99 
       
   100     public static void main(String[] args) throws Throwable {
       
   101         // These initial constructions test nest-host access to members
       
   102 
       
   103         TestJNI o = newInstance(TestJNI.class);
       
   104         StaticNested s = (StaticNested) newInstance(StaticNested.class);
       
   105         InnerNested i = (InnerNested) newInstance(InnerNested.class, o);
       
   106 
       
   107         // We need a StaticIface instance to call doConstruct on
       
   108         StaticIface intf = new StaticIface() {};
       
   109 
       
   110         s.doConstruct(o);
       
   111         s.doConstruct(o, i);
       
   112         s.doConstruct(s);
       
   113 
       
   114         i.doConstruct(o);
       
   115         i.doConstruct(o, i);
       
   116         i.doConstruct(s);
       
   117 
       
   118         intf.doConstruct(o);
       
   119         intf.doConstruct(o, i);
       
   120         intf.doConstruct(s);
       
   121     }
       
   122 
       
   123     static <T> T newInstance(Class<T> klass) {
       
   124         return newInstance(klass, null);
       
   125     }
       
   126 
       
   127     static <T> T newInstance(Class<T> klass, Object outerThis) {
       
   128         String sig = (outerThis == null) ?
       
   129             "()V" :
       
   130             "(L" + outerThis.getClass().getName() + ";)V";
       
   131         String definingClass = klass.getName();
       
   132         String desc = " Invocation of constructor " + definingClass + sig;
       
   133         try {
       
   134             T ret = (T) NestmatesJNI.newInstance(definingClass, sig, outerThis);
       
   135             System.out.println(desc + " - passed");
       
   136             return ret;
       
   137         }
       
   138         catch (Throwable t) {
       
   139             throw new Error(desc + ": Unexpected exception: " + t, t);
       
   140         }
       
   141     }
       
   142 }