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  * bug 8212563
 27  * @summary Check that javac emits Q types for values as needed
 28  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
 29  * @compile QTypedValue.java
 30  * @run main/othervm -Xverify:none QTypeTest
 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 QTypeTest {
 39 
 40     public static void main(String[] args) {
 41         new QTypeTest().run();
 42     }
 43 
 44     void run() {
 45         String [] params = new String [] { "-v",
 46                                             Paths.get(System.getProperty("test.classes"),
 47                                                 "QTypedValue.class").toString() };
 48         runCheck(params, new String [] {
 49               "final primitive class QTypedValue",
 50               "  flags: (0x0130) ACC_FINAL, ACC_SUPER, ACC_PRIMITIVE",
 51               "  this_class: #1                          // QTypedValue",
 52               "   #1 = Class              #2             // QTypedValue",
 53               "   #2 = Utf8               QTypedValue",
 54               "   #3 = Class              #4             // \"QQTypedValue;\"",
 55               "   #4 = Utf8               QQTypedValue;",
 56               "   #5 = Fieldref           #1.#6          // QTypedValue.f1:[QQTypedValue;",
 57               "   #8 = Utf8               [QQTypedValue;",
 58               "   #9 = Fieldref           #1.#10         // QTypedValue.f2:[QQTypedValue;",
 59               "  #12 = Class              #13            // \"[[[QQTypedValue;\"",
 60               "  #13 = Utf8               [[[QQTypedValue;",
 61               "  #14 = Fieldref           #1.#15         // QTypedValue.f3:[[[QQTypedValue;",
 62               "  #17 = Fieldref           #1.#18         // QTypedValue.f4:[[[QQTypedValue;",
 63               "  #27 = Utf8               (QQTypedValue;I)V",
 64               "  #21 = NameAndType        #22:#23        // \"<init>\":()QQTypedValue;",
 65               "  #25 = NameAndType        #26:#27        // foo:(QQTypedValue;I)V",
 66               "   #6 = NameAndType        #7:#8          // f1:[QQTypedValue;",
 67               "  #10 = NameAndType        #11:#8         // f2:[QQTypedValue;",
 68               "  #15 = NameAndType        #16:#13        // f3:[[[QQTypedValue;",
 69               "  #18 = NameAndType        #19:#13        // f4:[[[QQTypedValue;",
 70               " final QTypedValue[] f1;",
 71               "    descriptor: [QQTypedValue;",
 72               "    flags: (0x0010) ACC_FINAL",
 73               "  final QTypedValue[] f2;",
 74               "    descriptor: [QQTypedValue;",
 75               "    flags: (0x0010) ACC_FINAL",
 76               "  final QTypedValue[][][] f3;",
 77               "    descriptor: [[[QQTypedValue;",
 78               "    flags: (0x0010) ACC_FINAL",
 79               "  final QTypedValue[][][] f4;",
 80               "    descriptor: [[[QQTypedValue;",
 81               "    flags: (0x0010) ACC_FINAL",
 82               "  void foo(QTypedValue, int);",
 83               "    descriptor: (QQTypedValue;I)V",
 84               "    flags: (0x0000)",
 85               "    Code:",
 86               "      stack=3, locals=12, args_size=3",
 87               "         0: aload_0",
 88               "         1: invokestatic  #20                 // Method \"<init>\":()QQTypedValue;",
 89               "         4: bipush        10",
 90               "         6: invokevirtual #24                 // Method foo:(QQTypedValue;I)V",
 91               "         9: iload_2",
 92               "        10: ifne          34",
 93               "        13: iconst_0",
 94               "        14: istore        8",
 95               "        16: dconst_0",
 96               "        17: dstore        9",
 97               "        19: invokestatic  #20                 // Method \"<init>\":()QQTypedValue;",
 98               "        22: astore_3",
 99               "        23: iload         8",
100               "        25: ifne          29",
101               "        28: return",
102               "        29: invokestatic  #20                 // Method \"<init>\":()QQTypedValue;",
103               "        32: astore        11",
104               "        34: return",
105               "      StackMapTable: number_of_entries = 2",
106               "        frame_type = 255 /* full_frame */",
107               "          offset_delta = 29",
108               "          locals = [ class \"QQTypedValue;\", class \"QQTypedValue;\", int, class \"QQTypedValue;\", top, top, top, top, int, double ]",
109               "          stack = []",
110               "        frame_type = 255 /* full_frame */",
111               "          offset_delta = 4",
112               "          locals = [ class \"QQTypedValue;\", class \"QQTypedValue;\", int ]",
113               "          stack = []",
114               "static QTypedValue QTypedValue();",
115               "    descriptor: ()QQTypedValue;",
116               "    flags: (0x0008) ACC_STATIC",
117               "    Code:",
118               "      stack=2, locals=1, args_size=0",
119               "         0: defaultvalue  #1                  // class QTypedValue",
120               "         3: astore_0",
121               "         4: bipush        10",
122               "         6: anewarray     #3                  // class \"QQTypedValue;\"",
123               "         9: aload_0",
124               "        10: swap",
125               "        11: withfield     #5                  // Field f1:[QQTypedValue;",
126               "        14: astore_0",
127               "        15: bipush        10",
128               "        17: anewarray     #3                  // class \"QQTypedValue;\"",
129               "        20: aload_0",
130               "        21: swap",
131               "        22: withfield     #9                  // Field f2:[QQTypedValue;",
132               "        25: astore_0",
133               "        26: bipush        10",
134               "        28: bipush        10",
135               "        30: multianewarray #12,  2            // class \"[[[QQTypedValue;\"",
136               "        34: aload_0",
137               "        35: swap",
138               "        36: withfield     #14                 // Field f3:[[[QQTypedValue;",
139               "        39: astore_0",
140               "        40: bipush        10",
141               "        42: bipush        10",
142               "        44: multianewarray #12,  2            // class \"[[[QQTypedValue;\"",
143               "        48: aload_0",
144               "        49: swap",
145               "        50: withfield     #17                 // Field f4:[[[QQTypedValue;",
146               "        53: astore_0",
147               "        54: aload_0",
148               "        55: areturn",
149                          }, new String [] {
150                             });
151 
152      }
153 
154      void runCheck(String [] params, String [] expectedOut, String [] unexpectedOut) {
155         StringWriter s;
156         String out;
157 
158         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
159             com.sun.tools.javap.Main.run(params, pw);
160             out = s.toString();
161         }
162         int errors = 0;
163         for (String eo: expectedOut) {
164             if (!out.contains(eo)) {
165                 System.err.println("Match not found for string: " + eo);
166                 errors++;
167             }
168         }
169         for (String eo: unexpectedOut) {
170             if (out.contains(eo)) {
171                 System.err.println("Unexpected output found for string: " + eo);
172                 errors++;
173             }
174         }
175         if (errors > 0) {
176              throw new AssertionError("Unexpected javap output: " + out);
177         }
178     }
179 }