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