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 import jdk.internal.reflect.PreviewAccessFlags;
35
36 public final class AccessFlagsImpl extends AbstractElement
37 implements AccessFlags {
38
39 private final AccessFlag.Location location;
40 private final int flagsMask;
41 private final ClassFileFormatVersion formatVersion; // null for preview
42 private Set<AccessFlag> flags;
43
44 public AccessFlagsImpl(AccessFlag.Location location, AccessFlag... flags) {
45 this.location = location;
46 this.flagsMask = Util.flagsToBits(location, flags);
47 this.flags = Set.of(flags);
48 this.formatVersion = ClassFileFormatVersion.latest();
49 }
50
51 public AccessFlagsImpl(AccessFlag.Location location, int mask) {
52 this(location, mask, ClassFileFormatVersion.latest());
53 }
54
55 public AccessFlagsImpl(AccessFlag.Location location, int mask, int version) {
56 int major = version & 0xFFFF;
57 int minor = version >>> Character.SIZE;
58
59 ClassFileFormatVersion cffv = minor == ClassFile.PREVIEW_MINOR_VERSION
60 ? null // Try to guess for older preview features
61 : VM.isSupportedClassFileVersion(major, minor) ? ClassFileFormatVersion.fromMajor(major)
62 : ClassFileFormatVersion.latest(); // Fallback
63 this(location, mask, cffv);
64 }
65
66 private AccessFlagsImpl(AccessFlag.Location location, int mask, ClassFileFormatVersion version) {
67 this.location = location;
68 this.flagsMask = Util.checkFlags(mask);
69 this.formatVersion = version;
70 }
71
72 @Override
73 public int flagsMask() {
74 return flagsMask;
75 }
76
77 @Override
78 public Set<AccessFlag> flags() {
79 if (flags == null)
80 flags = formatVersion == null ? PreviewAccessFlags.maskToAccessFlags(flagsMask, location)
81 : AccessFlag.maskToAccessFlags(flagsMask, location, formatVersion);
82 return flags;
83 }
84
85 @Override
86 public void writeTo(DirectClassBuilder builder) {
87 builder.setFlags(flagsMask);
88 }
89
90 @Override
91 public void writeTo(DirectMethodBuilder builder) {
92 builder.setFlags(flagsMask);
93 }
94
95 @Override
96 public void writeTo(DirectFieldBuilder builder) {
97 builder.setFlags(flagsMask);
98 }
99
100 @Override
101 public AccessFlag.Location location() {
102 return location;
103 }
104
105 @Override
106 public boolean has(AccessFlag flag) {
107 return Util.has(location, flagsMask, flag);
108 }
109
110 @Override
111 public String toString() {
112 return String.format("AccessFlags[flags=%d]", flagsMask);
113 }
114 }