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