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 emission of ValueTypes attribute
  27  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
  28  * @compile MultiValues.java
  29  * @compile -g ValueTypesAttributeTest.java
  30  * @run main/othervm -Xverify:none -XX:+EnableValhalla ValueTypesAttributeTest
  31  * @modules jdk.compiler
  32  */
  33 
  34 import java.io.PrintWriter;
  35 import java.io.StringWriter;
  36 import java.nio.file.Paths;
  37 
  38 public class ValueTypesAttributeTest {
  39 
  40     static final __ByValue class X {
  41         final V1 [] v1 = null; // field descriptor 
  42         V2[] foo() {  // method descriptor encoding value type
  43             return null;
  44         }
  45         void foo(V3 v3) { // method descriptor encoding value type
  46         }
  47         void foo(int x) {
  48             V4 [] v4 = null; // local variable.
  49         }
  50         void goo() {
  51             V5 [] v5 = null;
  52             if (v5 == null) {
  53                V6 [] v61 = null;  // stack map table.
  54             } else {
  55                V5 [] v52 = null;
  56             }
  57         }
  58     }
  59 
  60     public static void main(String[] args) {
  61         new ValueTypesAttributeTest().run();
  62     }
  63 
  64     void run() {
  65         String [] params = new String [] { "-v",
  66                                             Paths.get(System.getProperty("test.classes"),
  67                                                 "ValueTypesAttributeTest$X.class").toString() };
  68         runCheck(params, new String [] {
  69 
  70          "ValueTypes:",
  71          "#36;                                    // value class V4",
  72          "#24;                                    // value class V2",
  73          "#6;                                     // value class ValueTypesAttributeTest$X",
  74          "#10;                                    // value class V1",
  75          "#27;                                    // value class V3",
  76          "#41;                                    // value class V5",
  77            
  78          }, new String [] {
  79          });
  80 
  81      }
  82 
  83      void runCheck(String [] params, String [] expectedOut, String [] unexpectedOut) {
  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         for (String eo: unexpectedOut) {
  99             if (out.contains(eo)) {
 100                 System.err.println("Unexpected output found for string: " + eo);
 101                 errors++;
 102             }
 103         }
 104         if (errors > 0) {
 105              throw new AssertionError("Unexpected javap output: " + out);
 106         }
 107     }
 108 }