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 constant propagation behavior
27  * @modules jdk.compiler/com.sun.tools.javac.util jdk.jdeps/com.sun.tools.javap
28  * @compile -XDenablePrimitiveClasses ConstantPropagationTest.java
29  * @run main/othervm -Xverify:none -XX:+EnableValhalla -XX:+EnablePrimitiveClasses ConstantPropagationTest
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 ConstantPropagationTest {
38 
39     static final primitive class X {
40         static final int sfif = 8888;
41         final int ifif = 9999;
42         static void foo(X x) {
43             System.out.println(sfif);
44             System.out.println(x.ifif);
45         }
46     }
47 
48     public static void main(String[] args) {
49         new ConstantPropagationTest().run();
50     }
51 
52     void run() {
53         String [] params = new String [] { "-v",
54                                             Paths.get(System.getProperty("test.classes"),
55                                                 "ConstantPropagationTest$X.class").toString() };
56         runCheck(params, new String [] {
57 
58          "ConstantValue: int 8888",
59          "3: sipush        8888",
60          }, new String [] {
61          "ConstantValue: int 9999"
62          });
63 
64      }
65 
66      void runCheck(String [] params, String [] expectedOut, String [] unexpectedOut) {
67         StringWriter s;
68         String out;
69 
70         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
71             com.sun.tools.javap.Main.run(params, pw);
72             out = s.toString();
73         }
74         int errors = 0;
75         for (String eo: expectedOut) {
76             if (!out.contains(eo)) {
77                 System.err.println("Match not found for string: " + eo);
78                 errors++;
79             }
80         }
81         for (String eo: unexpectedOut) {
82             if (out.contains(eo)) {
83                 System.err.println("Unexpected output found for string: " + eo);
84                 errors++;
85             }
86         }
87         if (errors > 0) {
88              throw new AssertionError("Unexpected javap output: " + out);
89         }
90     }
91 }