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