1 
  2 /*
  3  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /*
 26  * @test
 27  * @bug 8265423
 28  * @summary Experimental support for generating a single class file per primitive class
 29  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
 30  * @compile -XDenablePrimitiveClasses UnifiedPrimitiveClassBytecodeTest.java
 31  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses UnifiedPrimitiveClassBytecodeTest
 32  * @modules jdk.compiler
 33  */
 34 
 35 import java.io.PrintWriter;
 36 import java.io.StringWriter;
 37 import java.nio.file.Paths;
 38 
 39 public class UnifiedPrimitiveClassBytecodeTest {
 40 
 41     public primitive class X {
 42 
 43         X.ref xr = null;
 44 
 45         public void foo(X.ref[] xra, X[] xa) {
 46             xa = new X[10];
 47             xra = new X.ref[10];
 48             xra[0] = xa[0];
 49             xa[1] = xra[0];
 50             Class<?> c = X.val.class;
 51             c = X.ref.class;
 52         }
 53     }
 54 
 55     public static void main(String[] args) {
 56         new UnifiedPrimitiveClassBytecodeTest().run();
 57     }
 58 
 59     void run() {
 60         String [] params = new String [] { "-v",
 61                                             Paths.get(System.getProperty("test.classes"),
 62                                                 "UnifiedPrimitiveClassBytecodeTest$X.class").toString() };
 63         runCheck(params, new String [] {
 64 
 65         // check field
 66         "final UnifiedPrimitiveClassBytecodeTest$X xr;",
 67         "descriptor: LUnifiedPrimitiveClassBytecodeTest$X;",
 68         "flags: (0x0010) ACC_FINAL",
 69 
 70         // check method
 71         "public void foo(UnifiedPrimitiveClassBytecodeTest$X[], UnifiedPrimitiveClassBytecodeTest$X[]);",
 72         "descriptor: ([LUnifiedPrimitiveClassBytecodeTest$X;[QUnifiedPrimitiveClassBytecodeTest$X;)V",
 73         " 0: bipush        10",
 74         " 2: anewarray     #7                  // class \"QUnifiedPrimitiveClassBytecodeTest$X;\"",
 75         " 5: astore_2",
 76         " 6: bipush        10",
 77         " 8: anewarray     #1                  // class UnifiedPrimitiveClassBytecodeTest$X",
 78         "11: astore_1",
 79         "12: aload_1",
 80         "13: iconst_0",
 81         "14: aload_2",
 82         "15: iconst_0",
 83         "16: aaload",
 84         "17: checkcast     #1                  // class UnifiedPrimitiveClassBytecodeTest$X",
 85         "20: aastore",
 86         "21: aload_2",
 87         "22: iconst_1",
 88         "23: aload_1",
 89         "24: iconst_0",
 90         "25: aaload",
 91         "26: checkcast     #7                  // class \"QUnifiedPrimitiveClassBytecodeTest$X;\"",
 92         "29: aastore",
 93         "30: ldc           #7                  // class \"QUnifiedPrimitiveClassBytecodeTest$X;\"",
 94         "32: astore_3",
 95         "33: ldc           #1                  // class UnifiedPrimitiveClassBytecodeTest$X",
 96         "35: astore_3",
 97         "36: return",
 98          });
 99      }
100 
101      void runCheck(String [] params, String [] expectedOut) {
102         StringWriter s;
103         String out;
104 
105         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
106             com.sun.tools.javap.Main.run(params, pw);
107             out = s.toString();
108         }
109         int errors = 0;
110         for (String eo: expectedOut) {
111             if (!out.contains(eo)) {
112                 System.err.println("Match not found for string: " + eo);
113                 errors++;
114             }
115         }
116          if (errors > 0) {
117              throw new AssertionError("Unexpected javap output: " + out);
118          }
119     }
120 }