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 8210351
  27  * @summary test nestmate access to a value type's public, protected and private final fields.
  28  * @compile -XDemitQtypes -XDallowWithFieldOperator WithFieldAccessorTest.java
  29  * @run main/othervm -XX:+EnableValhalla WithFieldAccessorTest
  30  */
  31 
  32 // This test is similar to javac's WithFieldAccessorTest but tests nestmate
  33 // access to public, protected, and private final fields in a value type.
  34 public class WithFieldAccessorTest {
  35 
  36     public static final value class V {
  37         public final char c;
  38         protected final long l;
  39         private final int i;
  40         V() {
  41             this.c = '0';
  42             this.l = 0;
  43             this.i = 0;
  44         }
  45 
  46         public static V make(char c, long l, int i) {
  47             V v = V.default;
  48             v = __WithField(v.c, c);
  49             v = __WithField(v.l, l);
  50             v = __WithField(v.i, i);
  51             return v;
  52         }
  53     }
  54 
  55     public static void main(String... args) throws Throwable {
  56         V v = __WithField(V.make('a', 5, 10).c, 'b');
  57         if (!v.toString().equals("[WithFieldAccessorTest$V c=b l=5 i=10]")) {
  58             throw new AssertionError("Withfield of 'c' didn't work!" + v.toString());
  59         }
  60         v = __WithField(V.make('a', 5, 10).l, 25);
  61         if (!v.toString().equals("[WithFieldAccessorTest$V c=a l=25 i=10]")) {
  62             throw new AssertionError("Withfield of 'l' didn't work!" + v.toString());
  63         }
  64         v = __WithField(V.make('a', 5, 10).i, 20);
  65         if (!v.toString().equals("[WithFieldAccessorTest$V c=a l=5 i=20]")) {
  66             throw new AssertionError("Withfield of 'i' didn't work!" + v.toString());
  67         }
  68     }
  69 }