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 ValueCreationTest.java
  29  * @run main/othervm -Xverify:none -XX:+EnableValhalla ValueCreationTest
  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 ValueCreationTest {
  38 
  39     __ByValue
  40     static final class Point {
  41 
  42         final int x;
  43         final int y;
  44 
  45         Point () {
  46             x = 10;
  47             y = 10;
  48         }
  49 
  50         static Point makePoint(int x, int y) {
  51            Point p = __MakeDefault Point();
  52            p = __WithField(p.x, x);
  53            return __WithField(p.y, y);
  54         }
  55 
  56         public static void main(String [] args) {
  57             Point p = makePoint(10, 20);
  58         }
  59     }
  60 
  61     public static void main(String[] args) {
  62         new ValueCreationTest().run();
  63     }
  64 
  65     void run() {
  66         String [] params = new String [] { "-v",
  67                                             Paths.get(System.getProperty("test.classes"),
  68                                                 "ValueCreationTest$Point.class").toString() };
  69         runCheck(params, new String [] {
  70 
  71          "0: defaultvalue  #4                  // class ValueCreationTest$Point",
  72          "3: astore_2",
  73          "4: aload_2",
  74          "5: iload_0",
  75          "6: withfield     #2                  // Field x:I",
  76          "9: astore_2",
  77         "10: aload_2",
  78         "11: iload_1",
  79         "12: withfield     #3                  // Field y:I",
  80         "15: areturn"
  81            
  82          });
  83 
  84      }
  85 
  86      void runCheck(String [] params, String [] expectedOut) {
  87         StringWriter s;
  88         String out;
  89 
  90         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
  91             com.sun.tools.javap.Main.run(params, pw);
  92             out = s.toString();
  93         }
  94         int errors = 0;
  95         for (String eo: expectedOut) {
  96             if (!out.contains(eo)) {
  97                 System.err.println("Match not found for string: " + eo);
  98                 errors++;
  99             }
 100         }
 101          if (errors > 0) {
 102              throw new AssertionError("Unexpected javap output: " + out);
 103          }
 104     }
 105 }