From 93c19ac73c2feb8d6191bc5da98b4a9c8e2b5590 Mon Sep 17 00:00:00 2001 From: Chen Liang Date: Tue, 30 Jul 2024 17:41:49 +0000 Subject: [PATCH] 8337219: AccessFlags factories do not require necessary arguments Reviewed-by: asotona --- .../java/lang/classfile/AccessFlags.java | 50 +---------------- .../java/lang/classfile/ClassBuilder.java | 6 ++- .../java/lang/classfile/FieldBuilder.java | 6 ++- .../java/lang/classfile/MethodBuilder.java | 6 ++- .../classfile/impl/BufferedFieldBuilder.java | 3 +- .../classfile/impl/BufferedMethodBuilder.java | 2 +- .../internal/classfile/impl/ClassImpl.java | 2 +- .../internal/classfile/impl/FieldImpl.java | 3 +- .../internal/classfile/impl/MethodImpl.java | 3 +- .../com/sun/tools/javap/ClassWriter.java | 35 ++++++------ test/jdk/jdk/classfile/AccessFlagsTest.java | 54 ++++++++++++++----- test/jdk/jdk/classfile/BuilderBlockTest.java | 17 +++--- .../jdk/classfile/BuilderTryCatchTest.java | 13 ++--- test/jdk/jdk/classfile/LDCTest.java | 8 +-- test/jdk/jdk/classfile/LowAdaptTest.java | 6 ++- test/jdk/jdk/classfile/LvtTest.java | 12 ++--- test/jdk/jdk/classfile/OneToOneTest.java | 9 ++-- test/jdk/jdk/classfile/SwapTest.java | 11 ++-- test/jdk/jdk/classfile/WriteTest.java | 14 ++--- .../classfile/examples/ExampleGallery.java | 2 +- .../openjdk/bench/jdk/classfile/Write.java | 9 ++-- 21 files changed, 131 insertions(+), 140 deletions(-) diff --git a/src/java.base/share/classes/java/lang/classfile/AccessFlags.java b/src/java.base/share/classes/java/lang/classfile/AccessFlags.java index 283094b81d6..6c6ac75e648 100644 --- a/src/java.base/share/classes/java/lang/classfile/AccessFlags.java +++ b/src/java.base/share/classes/java/lang/classfile/AccessFlags.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -64,52 +64,4 @@ public sealed interface AccessFlags * method, or field} */ AccessFlag.Location location(); - - /** - * {@return an {@linkplain AccessFlags} for a class} - * @param mask the flags to be set, as a bit mask - */ - static AccessFlags ofClass(int mask) { - return new AccessFlagsImpl(AccessFlag.Location.CLASS, mask); - } - - /** - * {@return an {@linkplain AccessFlags} for a class} - * @param flags the flags to be set - */ - static AccessFlags ofClass(AccessFlag... flags) { - return new AccessFlagsImpl(AccessFlag.Location.CLASS, flags); - } - - /** - * {@return an {@linkplain AccessFlags} for a field} - * @param mask the flags to be set, as a bit mask - */ - static AccessFlags ofField(int mask) { - return new AccessFlagsImpl(AccessFlag.Location.FIELD, mask); - } - - /** - * {@return an {@linkplain AccessFlags} for a field} - * @param flags the flags to be set - */ - static AccessFlags ofField(AccessFlag... flags) { - return new AccessFlagsImpl(AccessFlag.Location.FIELD, flags); - } - - /** - * {@return an {@linkplain AccessFlags} for a method} - * @param mask the flags to be set, as a bit mask - */ - static AccessFlags ofMethod(int mask) { - return new AccessFlagsImpl(AccessFlag.Location.METHOD, mask); - } - - /** - * {@return an {@linkplain AccessFlags} for a method} - * @param flags the flags to be set - */ - static AccessFlags ofMethod(AccessFlag... flags) { - return new AccessFlagsImpl(AccessFlag.Location.METHOD, flags); - } } diff --git a/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java b/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java index a55b93e7c07..7ad6e94df3f 100644 --- a/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java +++ b/src/java.base/share/classes/java/lang/classfile/ClassBuilder.java @@ -34,6 +34,8 @@ import java.util.function.Consumer; import java.lang.classfile.constantpool.ClassEntry; import java.lang.classfile.constantpool.Utf8Entry; + +import jdk.internal.classfile.impl.AccessFlagsImpl; import jdk.internal.classfile.impl.ChainedClassBuilder; import jdk.internal.classfile.impl.DirectClassBuilder; import jdk.internal.classfile.impl.Util; @@ -73,7 +75,7 @@ public sealed interface ClassBuilder * @return this builder */ default ClassBuilder withFlags(int flags) { - return with(AccessFlags.ofClass(flags)); + return with(new AccessFlagsImpl(AccessFlag.Location.CLASS, flags)); } /** @@ -82,7 +84,7 @@ public sealed interface ClassBuilder * @return this builder */ default ClassBuilder withFlags(AccessFlag... flags) { - return with(AccessFlags.ofClass(flags)); + return with(new AccessFlagsImpl(AccessFlag.Location.CLASS, flags)); } /** diff --git a/src/java.base/share/classes/java/lang/classfile/FieldBuilder.java b/src/java.base/share/classes/java/lang/classfile/FieldBuilder.java index c0e733676f7..d9a8f6b2fc3 100644 --- a/src/java.base/share/classes/java/lang/classfile/FieldBuilder.java +++ b/src/java.base/share/classes/java/lang/classfile/FieldBuilder.java @@ -26,6 +26,8 @@ package java.lang.classfile; import java.lang.classfile.constantpool.Utf8Entry; + +import jdk.internal.classfile.impl.AccessFlagsImpl; import jdk.internal.classfile.impl.ChainedFieldBuilder; import jdk.internal.classfile.impl.TerminalFieldBuilder; import java.lang.reflect.AccessFlag; @@ -55,7 +57,7 @@ public sealed interface FieldBuilder * @return this builder */ default FieldBuilder withFlags(int flags) { - return with(AccessFlags.ofField(flags)); + return with(new AccessFlagsImpl(AccessFlag.Location.FIELD, flags)); } /** @@ -64,7 +66,7 @@ public sealed interface FieldBuilder * @return this builder */ default FieldBuilder withFlags(AccessFlag... flags) { - return with(AccessFlags.ofField(flags)); + return with(new AccessFlagsImpl(AccessFlag.Location.FIELD, flags)); } } diff --git a/src/java.base/share/classes/java/lang/classfile/MethodBuilder.java b/src/java.base/share/classes/java/lang/classfile/MethodBuilder.java index 4cf34c95ed0..7c230760f69 100644 --- a/src/java.base/share/classes/java/lang/classfile/MethodBuilder.java +++ b/src/java.base/share/classes/java/lang/classfile/MethodBuilder.java @@ -28,6 +28,8 @@ package java.lang.classfile; import java.util.function.Consumer; import java.lang.classfile.constantpool.Utf8Entry; + +import jdk.internal.classfile.impl.AccessFlagsImpl; import jdk.internal.classfile.impl.ChainedMethodBuilder; import jdk.internal.classfile.impl.TerminalMethodBuilder; import java.lang.reflect.AccessFlag; @@ -55,7 +57,7 @@ public sealed interface MethodBuilder * @return this builder */ default MethodBuilder withFlags(int flags) { - return with(AccessFlags.ofMethod(flags)); + return with(new AccessFlagsImpl(AccessFlag.Location.METHOD, flags)); } /** @@ -64,7 +66,7 @@ public sealed interface MethodBuilder * @return this builder */ default MethodBuilder withFlags(AccessFlag... flags) { - return with(AccessFlags.ofMethod(flags)); + return with(new AccessFlagsImpl(AccessFlag.Location.METHOD, flags)); } /** diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedFieldBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedFieldBuilder.java index d9732a0a14d..d165ddd3928 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedFieldBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedFieldBuilder.java @@ -24,6 +24,7 @@ */ package jdk.internal.classfile.impl; +import java.lang.reflect.AccessFlag; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -50,7 +51,7 @@ public final class BufferedFieldBuilder this.context = context; this.name = name; this.desc = type; - this.flags = AccessFlags.ofField(); + this.flags = new AccessFlagsImpl(AccessFlag.Location.FIELD); } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java index d8c4ba5f951..4a9e9b111ce 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java @@ -66,7 +66,7 @@ public final class BufferedMethodBuilder this.context = context; this.name = nameInfo; this.desc = typeInfo; - this.flags = AccessFlags.ofMethod(flags); + this.flags = new AccessFlagsImpl(AccessFlag.Location.METHOD, flags); this.original = original; } diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java index eab283a8c60..c6b9be5c76a 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java @@ -101,7 +101,7 @@ public final class ClassImpl @Override public AccessFlags flags() { - return AccessFlags.ofClass(reader.flags()); + return new AccessFlagsImpl(AccessFlag.Location.CLASS, reader.flags()); } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java index ab42b96084a..a71593a5712 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java @@ -24,6 +24,7 @@ */ package jdk.internal.classfile.impl; +import java.lang.reflect.AccessFlag; import java.util.List; import java.util.Optional; import java.util.function.Consumer; @@ -48,7 +49,7 @@ public final class FieldImpl @Override public AccessFlags flags() { - return AccessFlags.ofField(reader.readU2(startPos)); + return new AccessFlagsImpl(AccessFlag.Location.FIELD, reader.readU2(startPos)); } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java index 40223d58d6b..032b18600a8 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java @@ -28,6 +28,7 @@ import java.lang.constant.MethodTypeDesc; import java.lang.classfile.*; import java.lang.classfile.constantpool.Utf8Entry; +import java.lang.reflect.AccessFlag; import java.util.List; import java.util.Optional; import java.util.function.Consumer; @@ -51,7 +52,7 @@ public final class MethodImpl @Override public AccessFlags flags() { - return AccessFlags.ofMethod(reader.readU2(startPos)); + return new AccessFlagsImpl(AccessFlag.Location.METHOD, reader.readU2(startPos)); } @Override diff --git a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java index 2483e99e49a..764f93bd54a 100644 --- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java +++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,10 +25,12 @@ package com.sun.tools.javap; +import java.lang.reflect.AccessFlag; import java.net.URI; import java.text.DateFormat; import java.util.Collection; import java.util.Date; +import java.util.EnumSet; import java.util.List; import java.util.Set; @@ -149,7 +151,7 @@ public class ClassWriter extends BasicWriter { indent(-1); } - writeModifiers(getClassModifiers(cm.flags().flagsMask())); + writeModifiers(getClassModifiers(cm.flags())); if ((classModel.flags().flagsMask() & ACC_MODULE) != 0) { var attr = classModel.findAttribute(Attributes.module()); @@ -210,7 +212,7 @@ public class ClassWriter extends BasicWriter { println("minor version: " + classModel.minorVersion()); println("major version: " + classModel.majorVersion()); writeList(String.format("flags: (0x%04x) ", cm.flags().flagsMask()), - getClassFlags(cm.flags().flagsMask()), "\n"); + getClassFlags(cm.flags()), "\n"); print("this_class: #");print(() -> classModel.thisClass().index()); tab(); print(() -> "// " + classModel.thisClass().asInternalName()); @@ -416,7 +418,7 @@ public class ClassWriter extends BasicWriter { if (!options.checkAccess(f.flags().flagsMask())) return; - var flags = AccessFlags.ofField(f.flags().flagsMask()); + var flags = f.flags(); writeModifiers(flagsReportUnknown(flags).stream().filter(fl -> fl.sourceModifier()) .map(fl -> Modifier.toString(fl.mask())).toList()); print(() -> sigPrinter.print( @@ -794,9 +796,16 @@ public class ClassWriter extends BasicWriter { } } - private Set getClassModifiers(int mask) { - return getModifiers(flagsReportUnknown(AccessFlags.ofClass((mask & ACC_INTERFACE) != 0 - ? mask & ~ACC_ABSTRACT : mask))); + private Set getClassModifiers(AccessFlags flags) { + var flagSet = flagsReportUnknown(flags); + Set set; + if (flagSet.contains(AccessFlag.INTERFACE)) { + set = EnumSet.copyOf(flagSet); + set.remove(AccessFlag.ABSTRACT); + } else { + set = flagSet; + } + return getModifiers(set); } private static Set getModifiers(Set flags) { @@ -806,16 +815,8 @@ public class ClassWriter extends BasicWriter { return s; } - private Set getClassFlags(int mask) { - return getFlags(mask, flagsReportUnknown(AccessFlags.ofClass(mask))); - } - - private Set getMethodFlags(int mask) { - return getFlags(mask, flagsReportUnknown(AccessFlags.ofMethod(mask))); - } - - private Set getFieldFlags(int mask) { - return getFlags(mask, flagsReportUnknown(AccessFlags.ofField(mask))); + private Set getClassFlags(AccessFlags flags) { + return getFlags(flags.flagsMask(), flagsReportUnknown(flags)); } private static Set getFlags(int mask, Set flags) { diff --git a/test/jdk/jdk/classfile/AccessFlagsTest.java b/test/jdk/jdk/classfile/AccessFlagsTest.java index 97c0d97f8f9..379dcddb9c8 100644 --- a/test/jdk/jdk/classfile/AccessFlagsTest.java +++ b/test/jdk/jdk/classfile/AccessFlagsTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -26,6 +26,9 @@ * @summary Testing ClassFile AccessFlags. * @run junit AccessFlagsTest */ +import java.lang.classfile.ClassFile; +import java.lang.constant.ClassDesc; +import java.util.Arrays; import java.util.EnumSet; import java.util.Random; import java.util.Set; @@ -34,6 +37,10 @@ import java.util.function.Function; import java.util.function.IntFunction; import java.lang.reflect.AccessFlag; import java.lang.classfile.AccessFlags; + +import static java.lang.classfile.ClassFile.ACC_STATIC; +import static java.lang.constant.ConstantDescs.CD_int; +import static java.lang.constant.ConstantDescs.MTD_void; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.EnumSource; @@ -45,15 +52,38 @@ class AccessFlagsTest { @EnumSource(names = { "CLASS", "METHOD", "FIELD" }) void testRandomAccessFlagsConverions(AccessFlag.Location ctx) { IntFunction intFactory = switch (ctx) { - case CLASS -> AccessFlags::ofClass; - case METHOD -> AccessFlags::ofMethod; - case FIELD -> AccessFlags::ofField; + case CLASS -> v -> { + var bytes = ClassFile.of().build(ClassDesc.of("Test"), clb -> clb.withFlags(v)); + return ClassFile.of().parse(bytes).flags(); + }; + case METHOD -> v -> { + var bytes = ClassFile.of().build(ClassDesc.of("Test"), clb -> + clb.withMethod("test", MTD_void, v & ACC_STATIC, mb -> mb.withFlags(v))); + return ClassFile.of().parse(bytes).methods().getFirst().flags(); + }; + case FIELD -> v -> { + var bytes = ClassFile.of().build(ClassDesc.of("Test"), clb -> + clb.withField("test", CD_int, fb -> fb.withFlags(v))); + return ClassFile.of().parse(bytes).fields().getFirst().flags(); + }; default -> null; }; Function flagsFactory = switch (ctx) { - case CLASS -> AccessFlags::ofClass; - case METHOD -> AccessFlags::ofMethod; - case FIELD -> AccessFlags::ofField; + case CLASS -> v -> { + var bytes = ClassFile.of().build(ClassDesc.of("Test"), clb -> clb.withFlags(v)); + return ClassFile.of().parse(bytes).flags(); + }; + case METHOD -> v -> { + boolean hasStatic = Arrays.stream(v).anyMatch(f -> f == AccessFlag.STATIC); + var bytes = ClassFile.of().build(ClassDesc.of("Test"), clb -> + clb.withMethod("test", MTD_void, hasStatic ? ACC_STATIC : 0, mb -> mb.withFlags(v))); + return ClassFile.of().parse(bytes).methods().getFirst().flags(); + }; + case FIELD -> v -> { + var bytes = ClassFile.of().build(ClassDesc.of("Test"), clb -> + clb.withField("test", CD_int, fb -> fb.withFlags(v))); + return ClassFile.of().parse(bytes).fields().getFirst().flags(); + }; default -> null; }; @@ -72,11 +102,11 @@ class AccessFlagsTest { @Test void testInvalidFlagsUse() { - assertAll( - () -> assertThrowsForInvalidFlagsUse(AccessFlags::ofClass), - () -> assertThrowsForInvalidFlagsUse(AccessFlags::ofField), - () -> assertThrowsForInvalidFlagsUse(AccessFlags::ofMethod) - ); + ClassFile.of().build(ClassDesc.of("Test"), clb -> { + assertThrowsForInvalidFlagsUse(clb::withFlags); + clb.withMethod("test", MTD_void, ACC_STATIC, mb -> assertThrowsForInvalidFlagsUse(mb::withFlags)); + clb.withField("test", CD_int, fb -> assertThrowsForInvalidFlagsUse(fb::withFlags)); + }); } void assertThrowsForInvalidFlagsUse(Consumer factory) { diff --git a/test/jdk/jdk/classfile/BuilderBlockTest.java b/test/jdk/jdk/classfile/BuilderBlockTest.java index a2fb361ca2c..30a44491e6c 100644 --- a/test/jdk/jdk/classfile/BuilderBlockTest.java +++ b/test/jdk/jdk/classfile/BuilderBlockTest.java @@ -28,7 +28,6 @@ * @run junit BuilderBlockTest */ import java.io.IOException; -import java.lang.classfile.AccessFlags; import java.lang.classfile.Attributes; import java.lang.classfile.ClassFile; import java.lang.classfile.ClassTransform; @@ -53,6 +52,8 @@ import helpers.ByteArrayClassLoader; import jdk.internal.classfile.impl.LabelImpl; import org.junit.jupiter.api.Test; +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.CD_int; import static java.lang.constant.ConstantDescs.CD_void; @@ -116,7 +117,7 @@ class BuilderBlockTest { byte[] bytes = ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> xb.iload(0) .ifThen(xxb -> xxb.iconst_1().ireturn()) .iconst_2() @@ -135,7 +136,7 @@ class BuilderBlockTest { byte[] bytes = ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> xb.iload(0) .ifThenElse(xxb -> xxb.iconst_1().ireturn(), xxb -> xxb.iconst_2().ireturn()))); @@ -153,7 +154,7 @@ class BuilderBlockTest { ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> { xb.iload(0); xb.iload(1); @@ -173,7 +174,7 @@ class BuilderBlockTest { byte[] bytes = ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> xb.iload(0) .ifThenElse(xxb -> xxb.iconst_1().istore(2), xxb -> xxb.iconst_2().istore(2)) @@ -193,7 +194,7 @@ class BuilderBlockTest { byte[] bytes = ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> xb.iload(0) .ifThenElse(xxb -> xxb.iconst_1().istore(2).goto_(xxb.breakLabel()), xxb -> xxb.iconst_2().istore(2).goto_(xxb.breakLabel())) @@ -212,7 +213,7 @@ class BuilderBlockTest { byte[] bytes = ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> xb.iload(0) .iload(1) @@ -237,7 +238,7 @@ class BuilderBlockTest { ClassFile.of().build(ClassDesc.of("Foo"), cb -> { cb.withFlags(AccessFlag.PUBLIC); cb.withMethod("foo", MethodTypeDesc.of(CD_int, CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> { xb.iload(0); xb.iload(1); diff --git a/test/jdk/jdk/classfile/BuilderTryCatchTest.java b/test/jdk/jdk/classfile/BuilderTryCatchTest.java index 666b36e11a7..192125c4bb6 100644 --- a/test/jdk/jdk/classfile/BuilderTryCatchTest.java +++ b/test/jdk/jdk/classfile/BuilderTryCatchTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,7 +27,6 @@ * @run junit BuilderTryCatchTest */ -import java.lang.classfile.AccessFlags; import java.lang.classfile.ClassFile; import java.lang.classfile.CodeBuilder; import java.lang.classfile.CompoundElement; @@ -35,6 +34,9 @@ import java.lang.classfile.Opcode; import java.lang.classfile.TypeKind; import java.lang.classfile.instruction.BranchInstruction; import java.lang.classfile.instruction.ExceptionCatch; + +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; @@ -43,7 +45,6 @@ import java.lang.constant.MethodTypeDesc; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; -import java.lang.reflect.AccessFlag; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; @@ -184,7 +185,7 @@ class BuilderTryCatchTest { void testEmptyTry() { byte[] bytes = ClassFile.of().build(ClassDesc.of("C"), cb -> { cb.withMethod("main", MethodTypeDesc.of(CD_String, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), mb -> { + ACC_PUBLIC | ACC_STATIC, mb -> { mb.withCode(xb -> { int stringSlot = xb.allocateLocal(TypeKind.ReferenceType); xb.loadConstant("S"); @@ -215,7 +216,7 @@ class BuilderTryCatchTest { void testLocalAllocation() throws Throwable { byte[] bytes = ClassFile.of().build(ClassDesc.of("C"), cb -> { cb.withMethod("main", MethodTypeDesc.of(CD_String, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), mb -> { + ACC_PUBLIC | ACC_STATIC, mb -> { mb.withCode(xb -> { int stringSlot = xb.allocateLocal(TypeKind.ReferenceType); xb.loadConstant("S"); @@ -278,7 +279,7 @@ class BuilderTryCatchTest { static byte[] generateTryCatchMethod(Consumer c) { byte[] bytes = ClassFile.of().build(ClassDesc.of("C"), cb -> { cb.withMethod("main", MethodTypeDesc.of(CD_String, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), mb -> { + ACC_PUBLIC | ACC_STATIC, mb -> { mb.withCode(xb -> { int stringSlot = xb.allocateLocal(TypeKind.ReferenceType); xb.loadConstant("S"); diff --git a/test/jdk/jdk/classfile/LDCTest.java b/test/jdk/jdk/classfile/LDCTest.java index 1e7639de4bb..7379218840e 100644 --- a/test/jdk/jdk/classfile/LDCTest.java +++ b/test/jdk/jdk/classfile/LDCTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,6 +27,9 @@ * @run junit LDCTest */ import java.lang.constant.ClassDesc; + +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.*; import java.lang.constant.MethodTypeDesc; @@ -38,7 +41,6 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import static helpers.TestConstants.MTD_VOID; import static java.lang.classfile.Opcode.*; -import static java.lang.classfile.TypeKind.VoidType; import java.lang.classfile.instruction.ConstantInstruction; class LDCTest { @@ -56,7 +58,7 @@ class LDCTest { ) .withMethod("main", MethodTypeDesc.of(CD_void, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(c0 -> { ConstantPoolBuilder cpb = cb.constantPool(); for (int i = 0; i <= 256/2 + 2; i++) { // two entries per String diff --git a/test/jdk/jdk/classfile/LowAdaptTest.java b/test/jdk/jdk/classfile/LowAdaptTest.java index 9fa2620913b..d47141a885a 100644 --- a/test/jdk/jdk/classfile/LowAdaptTest.java +++ b/test/jdk/jdk/classfile/LowAdaptTest.java @@ -27,6 +27,9 @@ * @run junit LowAdaptTest */ import java.lang.constant.ClassDesc; + +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.*; import java.lang.constant.DirectMethodHandleDesc; import java.lang.constant.DynamicCallSiteDesc; @@ -35,7 +38,6 @@ import java.lang.constant.MethodTypeDesc; import java.net.URI; import java.nio.file.Paths; -import java.lang.classfile.AccessFlags; import java.lang.reflect.AccessFlag; import java.lang.classfile.ClassModel; import java.lang.classfile.ClassFile; @@ -75,7 +77,7 @@ class LowAdaptTest { cl.methods().forEach(cb::with); cb.withMethod("doit", MethodTypeDesc.of(CD_int, CD_int), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(xb -> { xb.invokedynamic(indy); xb.astore(1); diff --git a/test/jdk/jdk/classfile/LvtTest.java b/test/jdk/jdk/classfile/LvtTest.java index 3d789809798..d95a4fb7b29 100644 --- a/test/jdk/jdk/classfile/LvtTest.java +++ b/test/jdk/jdk/classfile/LvtTest.java @@ -35,13 +35,10 @@ import java.io.*; import java.lang.constant.ClassDesc; import java.net.URI; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.lang.classfile.AccessFlags; import java.lang.classfile.Attributes; import java.lang.classfile.attribute.SourceFileAttribute; import java.lang.classfile.constantpool.ConstantPoolBuilder; @@ -58,11 +55,10 @@ import static helpers.TestConstants.MTD_INT_VOID; import static helpers.TestConstants.MTD_VOID; import static helpers.TestUtil.ExpectedLvRecord; import static helpers.TestUtil.ExpectedLvtRecord; +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.*; import java.lang.constant.MethodTypeDesc; -import static java.lang.classfile.Opcode.*; -import static java.lang.classfile.Opcode.INVOKEVIRTUAL; -import static java.lang.classfile.TypeKind.VoidType; import static org.junit.jupiter.api.Assertions.*; class LvtTest { @@ -128,7 +124,7 @@ class LvtTest { ) ) .withMethod("main", MethodTypeDesc.of(CD_void, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb .withCode(c0 -> { ConstantPoolBuilder cpb = cb.constantPool(); @@ -243,7 +239,7 @@ class LvtTest { ) .withMethod("m", MethodTypeDesc.of(CD_Object, CD_Object.arrayType()), - ClassFile.ACC_PUBLIC, + ACC_PUBLIC, mb -> mb.withFlags(AccessFlag.PUBLIC) .withCode(c0 -> { ConstantPoolBuilder cpb = cb.constantPool(); diff --git a/test/jdk/jdk/classfile/OneToOneTest.java b/test/jdk/jdk/classfile/OneToOneTest.java index b20d07025e6..4e21cbebbe3 100644 --- a/test/jdk/jdk/classfile/OneToOneTest.java +++ b/test/jdk/jdk/classfile/OneToOneTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,18 +27,19 @@ * @run junit OneToOneTest */ import java.lang.constant.ClassDesc; + +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.*; import java.lang.constant.MethodTypeDesc; import java.util.List; -import java.lang.classfile.AccessFlags; import java.lang.reflect.AccessFlag; import java.lang.classfile.ClassModel; import java.lang.classfile.ClassFile; import java.lang.classfile.Instruction; import java.lang.classfile.Label; import java.lang.classfile.MethodModel; -import java.lang.classfile.TypeKind; import java.lang.classfile.attribute.SourceFileAttribute; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; @@ -74,7 +75,7 @@ class OneToOneTest { ) ) .withMethod("main", MethodTypeDesc.of(CD_void, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.STATIC, AccessFlag.PUBLIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(c0 -> { Label loopTop = c0.newLabel(); Label loopEnd = c0.newLabel(); diff --git a/test/jdk/jdk/classfile/SwapTest.java b/test/jdk/jdk/classfile/SwapTest.java index 10c0d66d513..71ea6189aa6 100644 --- a/test/jdk/jdk/classfile/SwapTest.java +++ b/test/jdk/jdk/classfile/SwapTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -27,8 +27,10 @@ * @run junit SwapTest */ -import java.lang.classfile.AccessFlags; import java.lang.classfile.ClassFile; + +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; @@ -38,9 +40,6 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; -import static java.lang.reflect.AccessFlag.PUBLIC; -import static java.lang.reflect.AccessFlag.STATIC; - class SwapTest { @Test void testTryCatchCatchAll() throws Throwable { @@ -48,7 +47,7 @@ class SwapTest { MethodTypeDesc mtd = mt.describeConstable().get(); byte[] bytes = ClassFile.of().build(ClassDesc.of("C"), cb -> { - cb.withMethodBody("m", mtd, AccessFlags.ofMethod(PUBLIC, STATIC).flagsMask(), xb -> { + cb.withMethodBody("m", mtd, ACC_PUBLIC | ACC_STATIC, xb -> { xb.aload(0); // 0 xb.aload(1); // 1, 0 xb.swap(); // 0, 1 diff --git a/test/jdk/jdk/classfile/WriteTest.java b/test/jdk/jdk/classfile/WriteTest.java index b4af9135d12..72d847c474c 100644 --- a/test/jdk/jdk/classfile/WriteTest.java +++ b/test/jdk/jdk/classfile/WriteTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,20 +30,16 @@ import java.lang.constant.ClassDesc; import java.lang.constant.MethodTypeDesc; import helpers.TestConstants; -import java.lang.classfile.AccessFlags; import java.lang.reflect.AccessFlag; import java.lang.classfile.ClassFile; -import java.lang.classfile.TypeKind; import java.lang.classfile.Label; import java.lang.classfile.attribute.SourceFileAttribute; import org.junit.jupiter.api.Test; import static helpers.TestConstants.MTD_VOID; +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.*; -import static java.lang.classfile.Opcode.*; -import static java.lang.classfile.TypeKind.IntType; -import static java.lang.classfile.TypeKind.ReferenceType; -import static java.lang.classfile.TypeKind.VoidType; class WriteTest { @@ -61,7 +57,7 @@ class WriteTest { ) ) .withMethod("main", MethodTypeDesc.of(CD_void, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(c0 -> { Label loopTop = c0.newLabel(); Label loopEnd = c0.newLabel(); @@ -102,7 +98,7 @@ class WriteTest { ) ) .withMethod("main", MethodTypeDesc.of(CD_void, CD_String.arrayType()), - AccessFlags.ofMethod(AccessFlag.PUBLIC, AccessFlag.STATIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(c0 -> { Label loopTop = c0.newLabel(); Label loopEnd = c0.newLabel(); diff --git a/test/jdk/jdk/classfile/examples/ExampleGallery.java b/test/jdk/jdk/classfile/examples/ExampleGallery.java index 7de4a78ffbf..f37f6b1b74d 100644 --- a/test/jdk/jdk/classfile/examples/ExampleGallery.java +++ b/test/jdk/jdk/classfile/examples/ExampleGallery.java @@ -207,7 +207,7 @@ public class ExampleGallery { public byte[] changeFieldFlags(ClassModel cm) { return ClassFile.of().transformClass(cm, ClassTransform.transformingFields((fb, fe) -> { switch (fe) { - case AccessFlags a -> fb.with(AccessFlags.ofField(a.flagsMask() & ~ClassFile.ACC_PUBLIC & ~ClassFile.ACC_PROTECTED)); + case AccessFlags a -> fb.withFlags(a.flagsMask() & ~ClassFile.ACC_PUBLIC & ~ClassFile.ACC_PROTECTED); default -> fb.with(fe); } })); diff --git a/test/micro/org/openjdk/bench/jdk/classfile/Write.java b/test/micro/org/openjdk/bench/jdk/classfile/Write.java index 59d07b05927..e5304f0e51c 100644 --- a/test/micro/org/openjdk/bench/jdk/classfile/Write.java +++ b/test/micro/org/openjdk/bench/jdk/classfile/Write.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,7 +22,6 @@ */ package org.openjdk.bench.jdk.classfile; -import java.lang.classfile.AccessFlags; import java.lang.reflect.AccessFlag; import java.lang.classfile.ClassFile; import java.lang.classfile.TypeKind; @@ -30,6 +29,8 @@ import java.lang.classfile.attribute.SourceFileAttribute; import jdk.internal.org.objectweb.asm.*; import org.openjdk.jmh.annotations.*; import java.io.FileOutputStream; +import static java.lang.classfile.ClassFile.ACC_PUBLIC; +import static java.lang.classfile.ClassFile.ACC_STATIC; import static java.lang.constant.ConstantDescs.*; import java.nio.file.Files; @@ -148,7 +149,7 @@ public class Write { ); for (int xi = 0; xi < 40; ++xi) { cb.withMethod("main" + ((xi == 0) ? "" : "" + xi), MTD_void_StringArray, - AccessFlags.ofMethod(AccessFlag.STATIC, AccessFlag.PUBLIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(c0 -> { java.lang.classfile.Label loopTop = c0.newLabel(); java.lang.classfile.Label loopEnd = c0.newLabel(); @@ -196,7 +197,7 @@ public class Write { ); for (int xi = 0; xi < 40; ++xi) { cb.withMethod("main" + ((xi == 0) ? "" : "" + xi), MTD_void_StringArray, - AccessFlags.ofMethod(AccessFlag.STATIC, AccessFlag.PUBLIC).flagsMask(), + ACC_PUBLIC | ACC_STATIC, mb -> mb.withCode(c0 -> { java.lang.classfile.Label loopTop = c0.newLabel(); java.lang.classfile.Label loopEnd = c0.newLabel();