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  * @summary Check code generation for value creation ops
  27  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
  28  * @compile WithFieldOfExplicitSelector.java
  29  * @run main/othervm -Xverify:none -XX:+EnableValhalla WithFieldOfExplicitSelector
  30  * @modules jdk.compiler
  31  */
  32 
  33 import java.io.PrintWriter;
  34 import java.io.StringWriter;
  35 import java.nio.file.Paths;
  36 
  37 public class WithFieldOfExplicitSelector {
  38 
  39     final __ByValue class X {
  40 
  41         final int i;
  42 
  43         X() {
  44             i = 10;
  45         }
  46         
  47         X getX(int i, Integer in) {
  48             X xl = __WithField(this.i, i);
  49             xl = __WithField(xl.i, in);
  50             return xl;
  51         }
  52     }
  53 
  54     public static void main(String[] args) {
  55         new WithFieldOfExplicitSelector().run();
  56     }
  57 
  58     void run() {
  59         String [] params = new String [] { "-v",
  60                                             Paths.get(System.getProperty("test.classes"),
  61                                                 "WithFieldOfExplicitSelector$X.class").toString() };
  62         runCheck(params, new String [] {
  63 
  64          "0: aload_0",
  65          "1: iload_1",
  66          "2: withfield     #3                  // Field i:I",
  67          "5: astore_3",
  68          "6: aload_3",
  69          "7: aload_2",
  70         "8: invokevirtual #4                  // Method java/lang/Integer.intValue:()I",
  71         "11: withfield     #3                  // Field i:I",
  72         "14: astore_3",
  73         "15: aload_3",
  74         "16: areturn"
  75          });
  76      }
  77 
  78      void runCheck(String [] params, String [] expectedOut) {
  79         StringWriter s;
  80         String out;
  81 
  82         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
  83             com.sun.tools.javap.Main.run(params, pw);
  84             out = s.toString();
  85         }
  86         int errors = 0;
  87         for (String eo: expectedOut) {
  88             if (!out.contains(eo)) {
  89                 System.err.println("Match not found for string: " + eo);
  90                 errors++;
  91             }
  92         }
  93          if (errors > 0) {
  94              throw new AssertionError("Unexpected javap output: " + out);
  95          }
  96     }
  97 }