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  * @run main UnifiedPrimitiveClassBytecodeTest
 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 UnifiedPrimitiveClassBytecodeTest {
 39 
 40     public primitive class X {
 41 
 42         X.ref xr = null;
 43 
 44         public void foo(X.ref[] xra, X[] xa) {
 45             xa = new X[10];
 46             xra = new X.ref[10];
 47             xra[0] = xa[0];
 48             xa[1] = xra[0];
 49             Class<?> c = X.val.class;
 50             c = X.ref.class;
 51         }
 52     }
 53 
 54     public static void main(String[] args) {
 55         new UnifiedPrimitiveClassBytecodeTest().run();
 56     }
 57 
 58     void run() {
 59         String [] params = new String [] { "-v",
 60                                             Paths.get(System.getProperty("test.classes"),
 61                                                 "UnifiedPrimitiveClassBytecodeTest$X.class").toString() };
 62         runCheck(params, new String [] {
 63 
 64         // check field
 65         "final UnifiedPrimitiveClassBytecodeTest$X xr;",
 66         "descriptor: LUnifiedPrimitiveClassBytecodeTest$X;",
 67         "flags: (0x0010) ACC_FINAL",
 68 
 69         // check method
 70         "public void foo(UnifiedPrimitiveClassBytecodeTest$X[], UnifiedPrimitiveClassBytecodeTest$X[]);",
 71         "descriptor: ([LUnifiedPrimitiveClassBytecodeTest$X;[QUnifiedPrimitiveClassBytecodeTest$X;)V",
 72         " 0: bipush        10",
 73         " 2: anewarray     #11                 // class \"QUnifiedPrimitiveClassBytecodeTest$X;\"",
 74         " 5: astore_2",
 75         " 6: bipush        10",
 76         " 8: anewarray     #1                  // class UnifiedPrimitiveClassBytecodeTest$X",
 77         "11: astore_1",
 78         "12: aload_1",
 79         "13: iconst_0",
 80         "14: aload_2",
 81         "15: iconst_0",
 82         "16: aaload",
 83         "17: checkcast     #1                  // class UnifiedPrimitiveClassBytecodeTest$X",
 84         "20: aastore",
 85         "21: aload_2",
 86         "22: iconst_1",
 87         "23: aload_1",
 88         "24: iconst_0",
 89         "25: aaload",
 90         "26: checkcast     #11                 // class \"QUnifiedPrimitiveClassBytecodeTest$X;\"",
 91         "29: aastore",
 92         "30: ldc           #11                 // class \"QUnifiedPrimitiveClassBytecodeTest$X;\"",
 93         "32: astore_3",
 94         "33: ldc           #1                  // class UnifiedPrimitiveClassBytecodeTest$X",
 95         "35: astore_3",
 96         "36: return",
 97          });
 98      }
 99 
100      void runCheck(String [] params, String [] expectedOut) {
101         StringWriter s;
102         String out;
103 
104         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
105             com.sun.tools.javap.Main.run(params, pw);
106             out = s.toString();
107         }
108         int errors = 0;
109         for (String eo: expectedOut) {
110             if (!out.contains(eo)) {
111                 System.err.println("Match not found for string: " + eo);
112                 errors++;
113             }
114         }
115          if (errors > 0) {
116              throw new AssertionError("Unexpected javap output: " + out);
117          }
118     }
119 }