1 /*
2 * Copyright (c) 2008, 2025, 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 id=NoPreview
26 * @bug 4975569 6622215 8034861
27 * @summary javap doesn't print new flag bits
28 * @modules jdk.jdeps/com.sun.tools.javap
29 * @modules java.base/jdk.internal.misc
30 * @comment Ensure that that this test is skipped if the test is run on a preview enabled
31 VM as the compiled test class has not been forced into preview mode.
32 Valhalla affects the outcome.
33 * @requires !java.enablePreview
34 * @run main T4975569
35 */
36
37 /*
38 * @test id=Preview
39 * @bug 4975569 6622215 8034861
40 * @summary javap doesn't print new flag bits - Preview
41 * @modules jdk.jdeps/com.sun.tools.javap
42 * @modules java.base/jdk.internal.misc
43 * @enablePreview
44 * @compile -XDforcePreview T4975569.java
45 * @run main T4975569
46 */
47
48 import java.io.*;
49 import java.util.*;
50 import java.util.regex.Matcher;
51 import java.util.regex.Pattern;
52
53 import jdk.internal.misc.PreviewFeatures;
54
55 public class T4975569 {
56 private static final String NEW_LINE = System.getProperty("line.separator");
57 private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
58
59 public static void main(String... args) {
60 new T4975569().run();
61 }
62
63 T4975569() {
64 System.currentTimeMillis();
65 super(); // Trigger forced preview
66 }
67
68 void run() {
69 verify(Anno.class.getName(), "flags: \\(0x2600\\) ACC_INTERFACE, ACC_ABSTRACT, ACC_ANNOTATION");
70 verify(E.class.getName(), PreviewFeatures.isEnabled()
71 ? "flags: \\(0x4030\\) ACC_FINAL, ACC_IDENTITY, ACC_ENUM"
72 : "flags: \\(0x4030\\) ACC_FINAL, ACC_SUPER, ACC_ENUM");
73 verify(S.class.getName(), "flags: \\(0x1040\\) ACC_BRIDGE, ACC_SYNTHETIC",
74 "InnerClasses:\n static [# =\\w]+; +// ");
75 verify(V.class.getName(), "void m\\(java.lang.String...\\)",
76 "flags: \\(0x0080\\) ACC_VARARGS");
77 verify(Prot.class.getName(), "InnerClasses:\n protected [# =\\w]+; +// ");
78 verify(Priv.class.getName(), new String[]{"-p"},
79 "InnerClasses:\n private [# =\\w]+; +// ");
80
81 if (errors > 0)
82 throw new Error(errors + " found.");
83 }
84
85 void verify(String className, String[] flags, String... expects) {
86 String output = javap(className, Arrays.asList(flags));
87 for (String expect: expects) {
88 Pattern expectPattern = Pattern.compile(expect);
89 Matcher matcher = expectPattern.matcher(output);
90 if (!matcher.find()) {
91 error(expect + " not found");
92 }
93 }
94 }
95
96 void verify(String className, String... expects) {
97 verify(className, new String[0], expects);
98 }
99
100 int errors;
101 void error(String msg) {
102 System.err.println(msg.replace("\n", NEW_LINE));
103 errors++;
104 }
105
106 String javap(String className, List<String> flags) {
107 StringWriter sw = new StringWriter();
108 PrintWriter out = new PrintWriter(sw);
109 List<String> args = new ArrayList<>(flags);
110 args.addAll(Arrays.asList("-v", "-classpath", TEST_CLASSES, className));
111 int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), out);
112 out.close();
113 String output = sw.toString();
114 System.err.println("class " + className);
115 System.err.println(output);
116
117 if (rc != 0)
118 throw new Error("javap failed. rc=" + rc);
119 return output.replaceAll(NEW_LINE, "\n");
120 }
121
122 List x() { return null; }
123
124 class V { void m(String... args) { } }
125 enum E { e }
126 @interface Anno { }
127 static class S extends T4975569 {
128 ArrayList x() { return null; }
129 }
130
131 protected class Prot { }
132 private class Priv { int i; }
133 }