1 /*
  2  * Copyright (c) 2018, Google LLC. All rights reserved.
  3  * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 /**
 26  * @test
 27  * @bug 8211138 8362885
 28  * @summary Missing Flag enum constants
 29  * @library /tools/javac/lib
 30  * @modules jdk.compiler/com.sun.tools.javac.code
 31  * @compile FlagsTest.java
 32  * @run main/manual FlagsTest
 33  */
 34 import com.sun.tools.javac.code.Flags;
 35 import com.sun.tools.javac.code.Flags.FlagTarget;
 36 import com.sun.tools.javac.code.Flags.NotFlag;
 37 import com.sun.tools.javac.code.Flags.Use;
 38 import java.lang.reflect.Field;
 39 import java.util.ArrayList;
 40 import java.util.HashMap;
 41 import java.util.List;
 42 import java.util.Map;
 43 
 44 public class FlagsTest {
 45 
 46     private static final int U2_SIZE = 16;
 47 
 48     public static void main(String[] args) throws Throwable {
 49         findFreeFlags();
 50     }
 51 
 52     private static void findFreeFlags() throws Throwable {
 53         Map<FlagTarget, Map<Long, List<Field>>> target2Flag2Fields = computeTarget2Flag2Fields();
 54 
 55         for (FlagTarget target : FlagTarget.values()) {
 56             long freeFlags = ~collectFlags(target2Flag2Fields, target);
 57 
 58             printFreeFlags(target.name(), freeFlags);
 59         }
 60     }
 61 
 62     private static Map<FlagTarget, Map<Long, List<Field>>> computeTarget2Flag2Fields() throws Throwable {
 63         Map<FlagTarget, Map<Long, List<Field>>> target2Flag2Fields = new HashMap<>();
 64         for (Field f : Flags.class.getFields()) {
 65             if (f.isAnnotationPresent(NotFlag.class)) {
 66                 continue;
 67             }
 68 
 69             Use use = f.getAnnotation(Use.class);
 70 
 71             if (use == null) {
 72                 throw new AssertionError("No @Use and no @NotFlag for: " + f.getName());
 73             }
 74 
 75             long flagValue = ((Number) f.get(null)).longValue();
 76 
 77             for (FlagTarget target : use.value()) {
 78                 target2Flag2Fields.computeIfAbsent(target, _ -> new HashMap<>())
 79                         .computeIfAbsent(flagValue, _ -> new ArrayList<>())
 80                         .add(f);
 81             }
 82         }
 83         return target2Flag2Fields;
 84     }
 85 
 86     private static void printFreeFlags(String comment, long freeFlags) {
 87             System.err.print("free flags for " + comment + ": ");
 88             for (int bit = U2_SIZE; bit < Long.SIZE; bit++) { //lowest 16 bits are used in classfiles, never suggest adding anything there
 89                 if ((freeFlags & (1L << bit)) != 0) {
 90                     System.err.print("1L<<" + bit + " ");
 91                 }
 92             }
 93             System.err.println();
 94     }
 95 
 96     private static long collectFlags(Map<FlagTarget, Map<Long, List<Field>>> target2Flag2Fields, FlagTarget... forTargets) {
 97         long flags = 0;
 98 
 99         for (FlagTarget target : forTargets) {
100             for (long used : target2Flag2Fields.get(target).keySet()) {
101                 flags |= used;
102             }
103         }
104 
105         return flags;
106     }
107 }