test/hotspot/jtreg/runtime/Nestmates/privateFields/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 fields 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     // Private field of nest-host for nestmates to access
       
    36     private int priv_field;
       
    37 
       
    38     static final String FIELD = "priv_field";
       
    39 
       
    40     // public constructor so we aren't relying on private access
       
    41     public TestJNI() {}
       
    42 
       
    43     // Methods that will access private fields of nestmates
       
    44 
       
    45     // NOTE: No StaticIface variants as interfaces can't have instance fields
       
    46 
       
    47     void access_priv(TestJNI o) {
       
    48         this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
    49     }
       
    50     void access_priv(InnerNested o) {
       
    51         this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
    52     }
       
    53     void access_priv(StaticNested o) {
       
    54         this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
    55     }
       
    56 
       
    57     // The various nestmates
       
    58 
       
    59     static interface StaticIface {
       
    60 
       
    61         // Methods that will access private fields of nestmates
       
    62 
       
    63         default void access_priv(TestJNI o) {
       
    64             int priv_field = getAndInc(o, o.getClass(), FIELD);
       
    65         }
       
    66         default void access_priv(InnerNested o) {
       
    67             int priv_field = getAndInc(o, o.getClass(), FIELD);
       
    68         }
       
    69         default void access_priv(StaticNested o) {
       
    70             int priv_field = getAndInc(o, o.getClass(), FIELD);
       
    71         }
       
    72     }
       
    73 
       
    74     static class StaticNested {
       
    75 
       
    76         private int priv_field;
       
    77 
       
    78         // public constructor so we aren't relying on private access
       
    79         public StaticNested() {}
       
    80 
       
    81         // Methods that will access private fields of nestmates
       
    82 
       
    83         void access_priv(TestJNI o) {
       
    84             this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
    85         }
       
    86         void access_priv(InnerNested o) {
       
    87             this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
    88         }
       
    89         void access_priv(StaticNested o) {
       
    90             this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
    91         }
       
    92     }
       
    93 
       
    94     class InnerNested {
       
    95 
       
    96         private int priv_field;
       
    97 
       
    98         // public constructor so we aren't relying on private access
       
    99         public InnerNested() {}
       
   100 
       
   101         void access_priv(TestJNI o) {
       
   102             this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
   103         }
       
   104         void access_priv(InnerNested o) {
       
   105             this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
   106         }
       
   107         void access_priv(StaticNested o) {
       
   108             this.priv_field = getAndInc(o, o.getClass(), FIELD);
       
   109         }
       
   110     }
       
   111 
       
   112     public static void main(String[] args) {
       
   113         TestJNI o = new TestJNI();
       
   114         StaticNested s = new StaticNested();
       
   115         InnerNested i = o.new InnerNested();
       
   116         StaticIface intf = new StaticIface() {};
       
   117 
       
   118         o.access_priv(new TestJNI());
       
   119         o.access_priv(i);
       
   120         o.access_priv(s);
       
   121 
       
   122         s.access_priv(o);
       
   123         s.access_priv(i);
       
   124         s.access_priv(new StaticNested());
       
   125 
       
   126         i.access_priv(o);
       
   127         i.access_priv(o.new InnerNested());
       
   128         i.access_priv(s);
       
   129 
       
   130         intf.access_priv(o);
       
   131         intf.access_priv(i);
       
   132         intf.access_priv(s);
       
   133     }
       
   134 
       
   135     static int getAndInc(Object target, Class<?> klass, String field) {
       
   136         String definingClass = klass.getName();
       
   137         String desc = "Access to field " +
       
   138                        definingClass + "." + field + " on instance of class " +
       
   139                        target.getClass().getName();
       
   140         int first, second;
       
   141         try {
       
   142             first = NestmatesJNI.getIntField(target, definingClass, field);
       
   143             NestmatesJNI.setIntField(target, definingClass, field, (first + 1));
       
   144             second = NestmatesJNI.getIntField(target, definingClass, field);
       
   145         }
       
   146         catch (Throwable t) {
       
   147             throw new Error(desc + ": Unexpected exception: " + t, t);
       
   148         }
       
   149         if (second != first + 1) {
       
   150             throw new Error(desc + ": wrong field values: first=" + first +
       
   151                             ", second=" + second + " (should equal first+1)");
       
   152         }
       
   153         System.out.println(desc + " - passed");
       
   154         return first;
       
   155     }
       
   156 }