Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/java.base/share/classes/java/lang/classfile/AccessFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package java.lang.classfile;

import java.lang.reflect.AccessFlag;
import java.lang.reflect.ClassFileFormatVersion;
import java.util.Set;

import jdk.internal.classfile.impl.AccessFlagsImpl;
Expand All @@ -39,6 +40,10 @@
* {@code AccessFlags} cannot be created via a factory method directly; it can
* be created with {@code withFlags} methods on the respective builders.
* <p>
* The interpretation of access flags is version dependent. Flags defined in
* some versions of the {@code class} file format may be undefined in another
* version, and all flags are considered undefined if the version is erroneous.
* <p>
* A {@link MethodBuilder} throws an {@link IllegalArgumentException} if it is
* supplied an {@code AccessFlags} object that changes the preexisting
* {@link ClassFile#ACC_STATIC ACC_STATIC} flag of the builder, because the
Expand Down Expand Up @@ -72,15 +77,18 @@ public sealed interface AccessFlags
/**
* {@return the access flags, as a set of flag enums}
*
* @throws IllegalArgumentException if the flags mask has any undefined bit set
* @throws IllegalArgumentException if the class file version of this flag
* is unsupported, or if in the class file version, the flags mask
* has any undefined bit set
* @see #location()
*/
Set<AccessFlag> flags();

/**
* {@return whether the specified flag is set} If the specified flag
* is not available to this {@linkplain #location() location}, returns
* {@code false}.
* {@return whether the specified flag is set} If the class file version
* of this flag is unsupported, or if in the class file version, the
* specified flag is not {@linkplain AccessFlag#locations(ClassFileFormatVersion)
* available} to this {@linkplain #location() location}, returns {@code false}.
*
* @param flag the flag to test
* @see #location()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import jdk.internal.classfile.impl.AccessFlagsImpl;
import jdk.internal.classfile.impl.ChainedClassBuilder;
import jdk.internal.classfile.impl.ClassFileVersionAware;
import jdk.internal.classfile.impl.DirectClassBuilder;
import jdk.internal.classfile.impl.Util;

Expand Down Expand Up @@ -81,7 +82,7 @@ default ClassBuilder withVersion(int major, int minor) {
* @see AccessFlag.Location#CLASS
*/
default ClassBuilder withFlags(int flags) {
return with(new AccessFlagsImpl(AccessFlag.Location.CLASS, flags));
return with(new AccessFlagsImpl((ClassFileVersionAware) this, AccessFlag.Location.CLASS, flags));
}

/**
Expand All @@ -95,7 +96,7 @@ default ClassBuilder withFlags(int flags) {
* @see AccessFlag.Location#CLASS
*/
default ClassBuilder withFlags(AccessFlag... flags) {
return with(new AccessFlagsImpl(AccessFlag.Location.CLASS, flags));
return with(new AccessFlagsImpl((ClassFileVersionAware) this, AccessFlag.Location.CLASS, flags));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import jdk.internal.classfile.impl.AccessFlagsImpl;
import jdk.internal.classfile.impl.ChainedFieldBuilder;
import jdk.internal.classfile.impl.ClassFileVersionAware;
import jdk.internal.classfile.impl.TerminalFieldBuilder;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public sealed interface FieldBuilder
* @see ClassBuilder#withField(String, ClassDesc, int)
*/
default FieldBuilder withFlags(int flags) {
return with(new AccessFlagsImpl(AccessFlag.Location.FIELD, flags));
return with(new AccessFlagsImpl((ClassFileVersionAware) this, AccessFlag.Location.FIELD, flags));
}

/**
Expand All @@ -74,7 +75,7 @@ default FieldBuilder withFlags(int flags) {
* @see ClassBuilder#withField(String, ClassDesc, int)
*/
default FieldBuilder withFlags(AccessFlag... flags) {
return with(new AccessFlagsImpl(AccessFlag.Location.FIELD, flags));
return with(new AccessFlagsImpl((ClassFileVersionAware) this, AccessFlag.Location.FIELD, flags));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import jdk.internal.classfile.impl.AccessFlagsImpl;
import jdk.internal.classfile.impl.ChainedMethodBuilder;
import jdk.internal.classfile.impl.ClassFileVersionAware;
import jdk.internal.classfile.impl.TerminalMethodBuilder;

/**
Expand Down Expand Up @@ -64,7 +65,7 @@ public sealed interface MethodBuilder
* @see AccessFlag.Location#METHOD
*/
default MethodBuilder withFlags(int flags) {
return with(new AccessFlagsImpl(AccessFlag.Location.METHOD, flags));
return with(new AccessFlagsImpl((ClassFileVersionAware) this, AccessFlag.Location.METHOD, flags));
}

/**
Expand All @@ -79,7 +80,7 @@ default MethodBuilder withFlags(int flags) {
* @see AccessFlag.Location#METHOD
*/
default MethodBuilder withFlags(AccessFlag... flags) {
return with(new AccessFlagsImpl(AccessFlag.Location.METHOD, flags));
return with(new AccessFlagsImpl((ClassFileVersionAware) this, AccessFlag.Location.METHOD, flags));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ default Set<AccessFlag> flags() {
* @see AccessFlag.Location#INNER_CLASS
*/
default boolean has(AccessFlag flag) {
return Util.has(AccessFlag.Location.INNER_CLASS, flagsMask(), flag);
return Util.hasFlagVersionAgnostic(AccessFlag.Location.INNER_CLASS, flagsMask(), flag);
}

/**
Expand Down Expand Up @@ -137,6 +137,6 @@ static InnerClassInfo of(ClassDesc innerClass, Optional<ClassDesc> outerClass, O
* the {@link AccessFlag.Location#INNER_CLASS} location
*/
static InnerClassInfo of(ClassDesc innerClass, Optional<ClassDesc> outerClass, Optional<String> innerName, AccessFlag... flags) {
return of(innerClass, outerClass, innerName, Util.flagsToBits(AccessFlag.Location.INNER_CLASS, flags));
return of(innerClass, outerClass, innerName, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.INNER_CLASS, flags));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ default Set<AccessFlag> flags() {
* @see AccessFlag.Location#METHOD_PARAMETER
*/
default boolean has(AccessFlag flag) {
return Util.has(AccessFlag.Location.METHOD_PARAMETER, flagsMask(), flag);
return Util.hasFlagVersionAgnostic(AccessFlag.Location.METHOD_PARAMETER, flagsMask(), flag);
}

/**
Expand All @@ -98,7 +98,7 @@ static MethodParameterInfo of(Optional<Utf8Entry> name, int flags) {
* {@link AccessFlag.Location#METHOD_PARAMETER} location
*/
static MethodParameterInfo of(Optional<String> name, AccessFlag... flags) {
return of(name.map(TemporaryConstantPool.INSTANCE::utf8Entry), Util.flagsToBits(AccessFlag.Location.METHOD_PARAMETER, flags));
return of(name.map(TemporaryConstantPool.INSTANCE::utf8Entry), Util.flagsToBitsVersionAgnostic(AccessFlag.Location.METHOD_PARAMETER, flags));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ default Set<AccessFlag> moduleFlags() {
* @see AccessFlag.Location#MODULE
*/
default boolean has(AccessFlag flag) {
return Util.has(AccessFlag.Location.MODULE, moduleFlagsMask(), flag);
return Util.hasFlagVersionAgnostic(AccessFlag.Location.MODULE, moduleFlagsMask(), flag);
}

/**
Expand Down Expand Up @@ -242,7 +242,7 @@ public sealed interface ModuleAttributeBuilder
* {@link AccessFlag.Location#MODULE} location
*/
default ModuleAttributeBuilder moduleFlags(AccessFlag... moduleFlags) {
return moduleFlags(Util.flagsToBits(AccessFlag.Location.MODULE, moduleFlags));
return moduleFlags(Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE, moduleFlags));
}

/**
Expand Down Expand Up @@ -274,7 +274,7 @@ default ModuleAttributeBuilder moduleFlags(AccessFlag... moduleFlags) {
* {@link AccessFlag.Location#MODULE_REQUIRES} location
*/
default ModuleAttributeBuilder requires(ModuleDesc module, Collection<AccessFlag> requiresFlags, String version) {
return requires(module, Util.flagsToBits(AccessFlag.Location.MODULE_REQUIRES, requiresFlags), version);
return requires(module, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_REQUIRES, requiresFlags), version);
}

/**
Expand Down Expand Up @@ -306,7 +306,7 @@ default ModuleAttributeBuilder requires(ModuleDesc module, Collection<AccessFlag
* {@link AccessFlag.Location#MODULE_EXPORTS} location
*/
default ModuleAttributeBuilder exports(PackageDesc pkge, Collection<AccessFlag> exportsFlags, ModuleDesc... exportsToModules) {
return exports(pkge, Util.flagsToBits(AccessFlag.Location.MODULE_EXPORTS, exportsFlags), exportsToModules);
return exports(pkge, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_EXPORTS, exportsFlags), exportsToModules);
}

/**
Expand Down Expand Up @@ -348,7 +348,7 @@ default ModuleAttributeBuilder exports(PackageDesc pkge, Collection<AccessFlag>
* {@link AccessFlag.Location#MODULE_OPENS} location
*/
default ModuleAttributeBuilder opens(PackageDesc pkge, Collection<AccessFlag> opensFlags, ModuleDesc... opensToModules) {
return opens(pkge, Util.flagsToBits(AccessFlag.Location.MODULE_OPENS, opensFlags), opensToModules);
return opens(pkge, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_OPENS, opensFlags), opensToModules);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ default Set<AccessFlag> exportsFlags() {
* @see AccessFlag.Location#MODULE_EXPORTS
*/
default boolean has(AccessFlag flag) {
return Util.has(AccessFlag.Location.MODULE_EXPORTS, exportsFlagsMask(), flag);
return Util.hasFlagVersionAgnostic(AccessFlag.Location.MODULE_EXPORTS, exportsFlagsMask(), flag);
}

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ static ModuleExportInfo of(PackageEntry exports, int exportFlags,
*/
static ModuleExportInfo of(PackageEntry exports, Collection<AccessFlag> exportFlags,
List<ModuleEntry> exportsTo) {
return of(exports, Util.flagsToBits(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
return of(exports, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
}

/**
Expand Down Expand Up @@ -151,7 +151,7 @@ static ModuleExportInfo of(PackageEntry exports,
static ModuleExportInfo of(PackageEntry exports,
Collection<AccessFlag> exportFlags,
ModuleEntry... exportsTo) {
return of(exports, Util.flagsToBits(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
return of(exports, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
}

/**
Expand Down Expand Up @@ -181,7 +181,7 @@ static ModuleExportInfo of(PackageDesc exports, int exportFlags,
*/
static ModuleExportInfo of(PackageDesc exports, Collection<AccessFlag> exportFlags,
List<ModuleDesc> exportsTo) {
return of(exports, Util.flagsToBits(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
return of(exports, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
}

/**
Expand Down Expand Up @@ -211,6 +211,6 @@ static ModuleExportInfo of(PackageDesc exports,
static ModuleExportInfo of(PackageDesc exports,
Collection<AccessFlag> exportFlags,
ModuleDesc... exportsTo) {
return of(exports, Util.flagsToBits(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
return of(exports, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_EXPORTS, exportFlags), exportsTo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ default Set<AccessFlag> opensFlags() {
* @see AccessFlag.Location#MODULE_OPENS
*/
default boolean has(AccessFlag flag) {
return Util.has(AccessFlag.Location.MODULE_OPENS, opensFlagsMask(), flag);
return Util.hasFlagVersionAgnostic(AccessFlag.Location.MODULE_OPENS, opensFlagsMask(), flag);
}

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ static ModuleOpenInfo of(PackageEntry opens, int opensFlags,
*/
static ModuleOpenInfo of(PackageEntry opens, Collection<AccessFlag> opensFlags,
List<ModuleEntry> opensTo) {
return of(opens, Util.flagsToBits(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
return of(opens, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
}

/**
Expand Down Expand Up @@ -157,7 +157,7 @@ static ModuleOpenInfo of(PackageEntry opens,
static ModuleOpenInfo of(PackageEntry opens,
Collection<AccessFlag> opensFlags,
ModuleEntry... opensTo) {
return of(opens, Util.flagsToBits(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
return of(opens, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
}

/**
Expand Down Expand Up @@ -185,7 +185,7 @@ static ModuleOpenInfo of(PackageDesc opens, int opensFlags,
*/
static ModuleOpenInfo of(PackageDesc opens, Collection<AccessFlag> opensFlags,
List<ModuleDesc> opensTo) {
return of(opens, Util.flagsToBits(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
return of(opens, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
}

/**
Expand Down Expand Up @@ -213,6 +213,6 @@ static ModuleOpenInfo of(PackageDesc opens,
static ModuleOpenInfo of(PackageDesc opens,
Collection<AccessFlag> opensFlags,
ModuleDesc... opensTo) {
return of(opens, Util.flagsToBits(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
return of(opens, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_OPENS, opensFlags), opensTo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ default Set<AccessFlag> requiresFlags() {
* @see AccessFlag.Location#MODULE_REQUIRES
*/
default boolean has(AccessFlag flag) {
return Util.has(AccessFlag.Location.MODULE_REQUIRES, requiresFlagsMask(), flag);
return Util.hasFlagVersionAgnostic(AccessFlag.Location.MODULE_REQUIRES, requiresFlagsMask(), flag);
}

/**
Expand All @@ -112,7 +112,7 @@ static ModuleRequireInfo of(ModuleEntry requires, int requiresFlags, Utf8Entry r
* {@link AccessFlag.Location#MODULE_REQUIRES} location
*/
static ModuleRequireInfo of(ModuleEntry requires, Collection<AccessFlag> requiresFlags, Utf8Entry requiresVersion) {
return of(requires, Util.flagsToBits(AccessFlag.Location.MODULE_REQUIRES, requiresFlags), requiresVersion);
return of(requires, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_REQUIRES, requiresFlags), requiresVersion);
}

/**
Expand All @@ -136,6 +136,6 @@ static ModuleRequireInfo of(ModuleDesc requires, int requiresFlags, String requi
* {@link AccessFlag.Location#MODULE_REQUIRES} location
*/
static ModuleRequireInfo of(ModuleDesc requires, Collection<AccessFlag> requiresFlags, String requiresVersion) {
return of(requires, Util.flagsToBits(AccessFlag.Location.MODULE_REQUIRES, requiresFlags), requiresVersion);
return of(requires, Util.flagsToBitsVersionAgnostic(AccessFlag.Location.MODULE_REQUIRES, requiresFlags), requiresVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ private static MethodHandle generateTypeSwitch(MethodHandles.Lookup caller, Clas

byte[] classBytes = ClassFile.of(ClassFile.StackMapsOption.DROP_STACK_MAPS).build(ConstantUtils.binaryNameToDesc(typeSwitchClassName(caller.lookupClass())),
clb -> {
clb.withFlags(AccessFlag.FINAL, (PreviewFeatures.isEnabled()) ? AccessFlag.IDENTITY : AccessFlag.SUPER, AccessFlag.SYNTHETIC)
clb.withFlags(AccessFlag.FINAL, AccessFlag.SUPER, AccessFlag.SYNTHETIC)
.withMethodBody("typeSwitch",
addExtraInfo ? MTD_TYPE_SWITCH_EXTRA : MTD_TYPE_SWITCH,
ClassFile.ACC_FINAL | ClassFile.ACC_PUBLIC | ClassFile.ACC_STATIC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@
public final class AccessFlagsImpl extends AbstractElement
implements AccessFlags {

private final ClassFileVersionAware versionContext;
private final AccessFlag.Location location;
private final int flagsMask;
private Set<AccessFlag> flags;

public AccessFlagsImpl(AccessFlag.Location location, AccessFlag... flags) {
public AccessFlagsImpl(ClassFileVersionAware versionContext, AccessFlag.Location location, AccessFlag... flags) {
this.versionContext = versionContext;
this.location = location;
this.flagsMask = Util.flagsToBits(location, flags);
this.flagsMask = Util.flagsToBits(location, flags, Util.requireFormatVersion(versionContext.classFileVersion()));
this.flags = Set.of(flags);
}

public AccessFlagsImpl(AccessFlag.Location location, int mask) {
public AccessFlagsImpl(ClassFileVersionAware versionContext, AccessFlag.Location location, int mask) {
this.versionContext = versionContext;
this.location = location;
this.flagsMask = mask;
}
Expand All @@ -55,7 +58,7 @@ public int flagsMask() {
@Override
public Set<AccessFlag> flags() {
if (flags == null)
flags = AccessFlag.maskToAccessFlags(flagsMask, location, ClassFileFormatVersion.CURRENT_PREVIEW_FEATURES);
flags = AccessFlag.maskToAccessFlags(flagsMask, location, Util.requireFormatVersion(versionContext.classFileVersion()));
return flags;
}

Expand All @@ -81,7 +84,8 @@ public AccessFlag.Location location() {

@Override
public boolean has(AccessFlag flag) {
return Util.has(location, flagsMask, flag);
var cffv = Util.findFormatVersion(versionContext.classFileVersion());
return cffv != null && Util.hasFlag(location, flagsMask, flag, cffv);
}

@Override
Expand Down
Loading