1 /*
  2  * Copyright (c) 2022, 2026, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package jdk.internal.classfile.impl;
 26 
 27 import java.lang.classfile.AccessFlags;
 28 import java.lang.classfile.ClassFile;
 29 import java.lang.reflect.AccessFlag;
 30 import java.lang.reflect.ClassFileFormatVersion;
 31 import java.util.Set;
 32 
 33 import jdk.internal.misc.VM;
 34 
 35 public final class AccessFlagsImpl extends AbstractElement
 36         implements AccessFlags {
 37 
 38     private final AccessFlag.Location location;
 39     private final int flagsMask;
 40     private final ClassFileFormatVersion formatVersion;
 41     private Set<AccessFlag> flags;
 42 
 43     public AccessFlagsImpl(AccessFlag.Location location, AccessFlag... flags) {
 44         this.location = location;
 45         this.flagsMask = Util.flagsToBits(location, flags);
 46         this.flags = Set.of(flags);
 47         this.formatVersion = ClassFileFormatVersion.latest();
 48     }
 49 
 50     public AccessFlagsImpl(AccessFlag.Location location, int mask) {
 51         this(location, mask, ClassFileFormatVersion.latest());
 52     }
 53 
 54     public AccessFlagsImpl(AccessFlag.Location location, int mask, int version) {
 55         int major = version & 0xFFFF;
 56         int minor = version >>> Character.SIZE;
 57 
 58         ClassFileFormatVersion cffv = minor == ClassFile.PREVIEW_MINOR_VERSION
 59                 ? ClassFileFormatVersion.CURRENT_PREVIEW_FEATURES // Try to guess for older preview features
 60                 : VM.isSupportedClassFileVersion(major, minor) ? ClassFileFormatVersion.fromMajor(major)
 61                                                                : ClassFileFormatVersion.latest(); // Fallback
 62         this(location, mask, cffv);
 63     }
 64 
 65     private AccessFlagsImpl(AccessFlag.Location location, int mask, ClassFileFormatVersion version) {
 66         this.location = location;
 67         this.flagsMask = Util.checkFlags(mask);
 68         this.formatVersion = version;
 69     }
 70 
 71     @Override
 72     public int flagsMask() {
 73         return flagsMask;
 74     }
 75 
 76     @Override
 77     public Set<AccessFlag> flags() {
 78         if (flags == null)
 79             flags = AccessFlag.maskToAccessFlags(flagsMask, location, formatVersion);
 80         return flags;
 81     }
 82 
 83     @Override
 84     public void writeTo(DirectClassBuilder builder) {
 85         builder.setFlags(flagsMask);
 86     }
 87 
 88     @Override
 89     public void writeTo(DirectMethodBuilder builder) {
 90         builder.setFlags(flagsMask);
 91     }
 92 
 93     @Override
 94     public void writeTo(DirectFieldBuilder builder) {
 95         builder.setFlags(flagsMask);
 96     }
 97 
 98     @Override
 99     public AccessFlag.Location location() {
100         return location;
101     }
102 
103     @Override
104     public boolean has(AccessFlag flag) {
105         return Util.has(location, flagsMask, flag);
106     }
107 
108     @Override
109     public String toString() {
110         return String.format("AccessFlags[flags=%d]", flagsMask);
111     }
112 }