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
26 package java.lang.reflect;
27
28 import java.lang.classfile.ClassModel;
29 import java.lang.classfile.FieldModel;
30 import java.lang.classfile.MethodModel;
31 import java.lang.classfile.attribute.InnerClassInfo;
32 import java.lang.classfile.attribute.MethodParameterInfo;
33 import java.lang.classfile.attribute.ModuleAttribute;
34 import java.lang.classfile.attribute.ModuleExportInfo;
35 import java.lang.classfile.attribute.ModuleOpenInfo;
36 import java.lang.classfile.attribute.ModuleRequireInfo;
37 import java.lang.module.ModuleDescriptor;
38 import java.util.AbstractSet;
39 import java.util.Collection;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.NoSuchElementException;
44 import java.util.Objects;
45 import java.util.Set;
46 import java.util.function.Consumer;
47 import java.util.function.Predicate;
145 STATIC(Modifier.STATIC, true, Location.SET_FIELD_METHOD_INNER_CLASS,
146 List.of(Map.entry(RELEASE_0, Location.SET_FIELD_METHOD))),
147
148 /**
149 * The access flag {@code ACC_FINAL}, corresponding to the source
150 * modifier {@link Modifier#FINAL final}, with a mask
151 * value of <code>{@value "0x%04x" Modifier#FINAL}</code>.
152 */
153 FINAL(Modifier.FINAL, true,
154 Location.SET_FINAL_8,
155 List.of(Map.entry(RELEASE_7, Location.SET_CLASS_FIELD_METHOD_INNER_CLASS),
156 Map.entry(RELEASE_0, Location.SET_CLASS_FIELD_METHOD))),
157
158 /**
159 * The access flag {@code ACC_SUPER} with a mask value of {@code
160 * 0x0020}.
161 *
162 * @apiNote
163 * In Java SE 8 and above, the JVM treats the {@code ACC_SUPER}
164 * flag as set in every class file (JVMS {@jvms 4.1}).
165 */
166 SUPER(0x0000_0020, false, Location.SET_CLASS, List.of()),
167
168 /**
169 * The module flag {@code ACC_OPEN} with a mask value of {@code
170 * 0x0020}.
171 * @see java.lang.module.ModuleDescriptor#isOpen
172 */
173 OPEN(0x0000_0020, false, Location.SET_MODULE,
174 List.of(Map.entry(RELEASE_8, Location.EMPTY_SET))),
175
176 /**
177 * The module requires flag {@code ACC_TRANSITIVE} with a mask
178 * value of {@code 0x0020}.
179 * @see java.lang.module.ModuleDescriptor.Requires.Modifier#TRANSITIVE
180 */
181 TRANSITIVE(0x0000_0020, false, Location.SET_MODULE_REQUIRES,
182 List.of(Map.entry(RELEASE_8, Location.EMPTY_SET))),
183
184 /**
185 * The access flag {@code ACC_SYNCHRONIZED}, corresponding to the
186 * source modifier {@link Modifier#SYNCHRONIZED synchronized}, with
247 * value of <code>{@value "0x%04x" Modifier#ABSTRACT}</code>.
248 */
249 ABSTRACT(Modifier.ABSTRACT, true,
250 Location.SET_CLASS_METHOD_INNER_CLASS,
251 List.of(Map.entry(RELEASE_0, Location.SET_CLASS_METHOD))),
252
253 /**
254 * The access flag {@code ACC_STRICT}, corresponding to the source
255 * modifier {@link Modifier#STRICT strictfp}, with a mask value of
256 * <code>{@value "0x%04x" Modifier#STRICT}</code>.
257 *
258 * @apiNote
259 * The {@code ACC_STRICT} access flag is defined for class file
260 * major versions 46 through 60, inclusive (JVMS {@jvms 4.6}),
261 * corresponding to Java SE 1.2 through 16.
262 */
263 STRICT(Modifier.STRICT, true, Location.EMPTY_SET,
264 List.of(Map.entry(RELEASE_16, Location.SET_METHOD),
265 Map.entry(RELEASE_1, Location.EMPTY_SET))),
266
267 /**
268 * The access flag {@code ACC_SYNTHETIC} with a mask value of
269 * <code>{@value "0x%04x" Modifier#SYNTHETIC}</code>.
270 * @see Class#isSynthetic()
271 * @see Executable#isSynthetic()
272 * @see java.lang.module.ModuleDescriptor.Modifier#SYNTHETIC
273 */
274 SYNTHETIC(Modifier.SYNTHETIC, false, Location.SET_SYNTHETIC_9,
275 List.of(Map.entry(RELEASE_8, Location.SET_SYNTHETIC_8),
276 Map.entry(RELEASE_7, Location.SET_SYNTHETIC_5),
277 Map.entry(RELEASE_4, Location.EMPTY_SET))),
278
279 /**
280 * The access flag {@code ACC_ANNOTATION} with a mask value of
281 * <code>{@value "0x%04x" Modifier#ANNOTATION}</code>.
282 * @see Class#isAnnotation()
283 */
284 ANNOTATION(Modifier.ANNOTATION, false, Location.SET_CLASS_INNER_CLASS,
285 List.of(Map.entry(RELEASE_4, Location.EMPTY_SET))),
286
336 public int mask() {
337 return mask;
338 }
339
340 /**
341 * {@return whether or not this flag has a directly corresponding
342 * modifier in the Java programming language}
343 */
344 public boolean sourceModifier() {
345 return sourceModifier;
346 }
347
348 /**
349 * {@return locations this flag can be applied to in the current class file
350 * format version}
351 * <p>
352 * This method returns an empty set if this flag is not defined in
353 * the current class file format version.
354 */
355 public Set<Location> locations() {
356 return locations;
357 }
358
359 /**
360 * {@return locations this flag can be applied to in the given class file
361 * format version}
362 * <p>
363 * This method returns an empty set if this flag is not defined in
364 * the given {@code cffv}.
365 *
366 * @param cffv the class file format version to use
367 * @throws NullPointerException if the parameter is {@code null}
368 */
369 public Set<Location> locations(ClassFileFormatVersion cffv) {
370 return Location.findInHistory(locations, historicalLocations, cffv);
371 }
372
373 /**
374 * {@return an unmodifiable set of access flags for the given mask value
375 * appropriate for the location in the current class file format version}
376 *
377 * @param mask bit mask of access flags
378 * @param location context to interpret mask value
379 * @throws IllegalArgumentException if the mask contains bit
380 * positions not defined for the location in the current class file format
381 * @throws NullPointerException if {@code location} is {@code null}
382 */
383 public static Set<AccessFlag> maskToAccessFlags(int mask, Location location) {
384 var definition = findDefinition(location); // null checks location
385 int unmatchedMask = mask & (~location.flagsMask());
386 if (unmatchedMask != 0) {
387 throw new IllegalArgumentException("Unmatched bit position 0x" +
388 Integer.toHexString(unmatchedMask) +
389 " for location " + location);
390 }
391 return new AccessFlagSet(definition, mask);
392 }
393
394 /**
395 * {@return an unmodifiable set of access flags for the given mask value
396 * appropriate for the location in the given class file format version}
397 *
398 * @param mask bit mask of access flags
399 * @param location context to interpret mask value
400 * @param cffv the class file format to interpret mask value
401 * @throws IllegalArgumentException if the mask contains bit
402 * positions not defined for the location in the given class file format
403 * @throws NullPointerException if {@code location} or {@code cffv} is {@code null}
404 * @since 25
405 */
406 public static Set<AccessFlag> maskToAccessFlags(int mask, Location location, ClassFileFormatVersion cffv) {
407 var definition = findDefinition(location); // null checks location
408 int unmatchedMask = mask & (~location.flagsMask(cffv)); // null checks cffv
409 if (unmatchedMask != 0) {
410 throw new IllegalArgumentException("Unmatched bit position 0x" +
411 Integer.toHexString(unmatchedMask) +
412 " for location " + location +
413 " for class file format " + cffv);
414 }
415 return new AccessFlagSet(definition, mask);
416 }
417
418 /**
419 * A location within a {@code class} file where flags can be applied.
420 * <p>
421 * Note that since these locations represent {@code class} file structures
422 * rather than language structures, many language structures, such
423 * as constructors and interfaces, are <em>not</em> present.
424 * @since 20
425 */
426 public enum Location {
427 /**
428 * Class location.
429 *
430 * @see Class#accessFlags()
431 * @see ClassModel#flags()
432 * @see Modifier#classModifiers()
433 * @see Modifier#interfaceModifiers()
434 * @jvms 4.1 The {@code ClassFile} Structure
435 */
436 CLASS(ACC_PUBLIC | ACC_FINAL | ACC_SUPER |
437 ACC_INTERFACE | ACC_ABSTRACT |
438 ACC_SYNTHETIC | ACC_ANNOTATION |
439 ACC_ENUM | ACC_MODULE,
440 List.of(Map.entry(RELEASE_8, // no module
441 ACC_PUBLIC | ACC_FINAL | ACC_SUPER |
442 ACC_INTERFACE | ACC_ABSTRACT |
443 ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
444 Map.entry(RELEASE_4, // no synthetic, annotation, enum
445 ACC_PUBLIC | ACC_FINAL | ACC_SUPER |
446 ACC_INTERFACE | ACC_ABSTRACT))),
447
448 /**
449 * Field location.
450 *
451 * @see Field#accessFlags()
452 * @see FieldModel#flags()
453 * @see Modifier#fieldModifiers()
454 * @jvms 4.5 Fields
455 */
456 FIELD(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
457 ACC_STATIC | ACC_FINAL | ACC_VOLATILE |
458 ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM,
459 List.of(Map.entry(RELEASE_4, // no synthetic, enum
460 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
461 ACC_STATIC | ACC_FINAL | ACC_VOLATILE |
462 ACC_TRANSIENT))),
463
464 /**
465 * Method location.
466 *
467 * @see Executable#accessFlags()
468 * @see MethodModel#flags()
469 * @see Modifier#methodModifiers()
470 * @see Modifier#constructorModifiers()
471 * @jvms 4.6 Methods
472 */
473 METHOD(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
474 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
475 ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE |
476 ACC_ABSTRACT | ACC_SYNTHETIC,
477 List.of(Map.entry(RELEASE_16, // had strict
478 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
479 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
480 ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE |
481 ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC),
482 Map.entry(RELEASE_4, // no bridge, varargs, synthetic
483 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
484 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
485 ACC_NATIVE | ACC_ABSTRACT | ACC_STRICT),
486 Map.entry(RELEASE_1, // no strict
487 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
488 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
489 ACC_NATIVE | ACC_ABSTRACT))),
490
491 /**
492 * Inner class location.
493 *
494 * @see Class#accessFlags()
495 * @see InnerClassInfo#flags()
496 * @see Modifier#classModifiers()
497 * @see Modifier#interfaceModifiers()
498 * @jvms 4.7.6 The {@code InnerClasses} Attribute
499 */
500 INNER_CLASS(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
501 ACC_STATIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT |
502 ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM,
503 List.of(Map.entry(RELEASE_4, // no synthetic, annotation, enum
504 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
505 ACC_STATIC | ACC_FINAL | ACC_INTERFACE |
506 ACC_ABSTRACT),
507 Map.entry(RELEASE_0, 0))), // did not exist
508
509 /**
510 * Method parameter location.
511 *
512 * @see Parameter#accessFlags()
513 * @see MethodParameterInfo#flags()
514 * @see Modifier#parameterModifiers()
515 * @jvms 4.7.24 The {@code MethodParameters} Attribute
516 */
517 METHOD_PARAMETER(ACC_FINAL | ACC_SYNTHETIC | ACC_MANDATED,
518 List.of(Map.entry(RELEASE_7, 0))), // did not exist
519
520 /**
521 * Module location.
522 *
523 * @see ModuleDescriptor#accessFlags()
524 * @see ModuleAttribute#moduleFlags()
525 * @jvms 4.7.25 The {@code Module} Attribute
526 */
603 MODULE_EXPORTS, MODULE_OPENS);
604 private static final Set<Location> SET_MANDATED_9 =
605 Set.of(METHOD_PARAMETER, // From 8
606 // Starting in 9
607 MODULE, MODULE_REQUIRES,
608 MODULE_EXPORTS, MODULE_OPENS);
609
610 private final int flagsMask;
611 private final List<Map.Entry<ClassFileFormatVersion, Integer>> historicalFlagsMasks;
612
613 Location(int flagsMask,
614 List<Map.Entry<ClassFileFormatVersion, Integer>> historicalFlagsMasks) {
615 this.flagsMask = flagsMask;
616 this.historicalFlagsMasks = ensureHistoryOrdered(historicalFlagsMasks);
617 }
618
619 // Ensures the historical versions are from newest to oldest and do not include the latest
620 // These 2 utilities reside in Location because Location must be initialized before AccessFlag
621 private static <T> List<Map.Entry<ClassFileFormatVersion, T>> ensureHistoryOrdered(
622 List<Map.Entry<ClassFileFormatVersion, T>> history) {
623 ClassFileFormatVersion lastVersion = ClassFileFormatVersion.latest();
624 for (var e : history) {
625 var historyVersion = e.getKey();
626 if (lastVersion.compareTo(historyVersion) <= 0) {
627 throw new IllegalArgumentException("Versions out of order");
628 }
629 lastVersion = historyVersion;
630 }
631 return history;
632 }
633
634 private static <T> T findInHistory(T candidate, List<Map.Entry<ClassFileFormatVersion, T>> history,
635 ClassFileFormatVersion cffv) {
636 Objects.requireNonNull(cffv);
637 for (var e : history) {
638 if (e.getKey().compareTo(cffv) < 0) {
639 // last version found was valid
640 return candidate;
641 }
642 candidate = e.getValue();
643 }
644 return candidate;
645 }
646
647 /**
648 * {@return the union of masks of all access flags defined for
649 * this location in the current class file format version}
650 * <p>
651 * This method returns {@code 0} if this location does not exist in
652 * the current class file format version.
653 *
654 * @since 25
655 */
656 public int flagsMask() {
657 return flagsMask;
658 }
659
660 /**
661 * {@return the union of masks of all access flags defined for
662 * this location in the given class file format version}
663 * <p>
664 * This method returns {@code 0} if this location does not exist in
665 * the given {@code cffv}.
666 *
667 * @param cffv the class file format version
668 * @throws NullPointerException if {@code cffv} is {@code null}
669 * @since 25
670 */
671 public int flagsMask(ClassFileFormatVersion cffv) {
672 return findInHistory(flagsMask, historicalFlagsMasks, cffv);
673 }
674
675 /**
676 * {@return the set of access flags defined for this location in the
677 * current class file format version} The set is immutable.
678 * <p>
679 * This method returns an empty set if this location does not exist
680 * in the current class file format version.
681 *
682 * @since 25
683 */
684 public Set<AccessFlag> flags() {
685 return new AccessFlagSet(findDefinition(this), flagsMask());
686 }
687
688 /**
689 * {@return the set of access flags defined for this location in the
690 * given class file format version} The set is immutable.
691 * <p>
692 * This method returns an empty set if this location does not exist
693 * in the given {@code cffv}.
694 *
695 * @param cffv the class file format version
696 * @throws NullPointerException if {@code cffv} is {@code null}
697 * @since 25
698 */
699 public Set<AccessFlag> flags(ClassFileFormatVersion cffv) {
700 // implicit null check cffv
701 return new AccessFlagSet(findDefinition(this), flagsMask(cffv));
702 }
703 }
704
705 private static AccessFlag[] createDefinition(AccessFlag... known) {
706 var ret = new AccessFlag[Character.SIZE];
707 for (var flag : known) {
708 var mask = flag.mask;
709 int pos = Integer.numberOfTrailingZeros(mask);
710 assert ret[pos] == null : ret[pos] + " " + flag;
711 ret[pos] = flag;
712 }
713 return ret;
714 }
715
716 // Will take extra args in the future for valhalla switch
717 private static AccessFlag[] findDefinition(Location location) {
718 return switch (location) {
719 case CLASS -> CLASS_FLAGS;
720 case FIELD -> FIELD_FLAGS;
721 case METHOD -> METHOD_FLAGS;
722 case INNER_CLASS -> INNER_CLASS_FLAGS;
723 case METHOD_PARAMETER -> METHOD_PARAMETER_FLAGS;
724 case MODULE -> MODULE_FLAGS;
725 case MODULE_REQUIRES -> MODULE_REQUIRES_FLAGS;
726 case MODULE_EXPORTS -> MODULE_EXPORTS_FLAGS;
727 case MODULE_OPENS -> MODULE_OPENS_FLAGS;
728 };
729 }
730
731 private static final @Stable AccessFlag[] // Can use stable array and lazy init in the future
732 CLASS_FLAGS = createDefinition(PUBLIC, FINAL, SUPER, INTERFACE, ABSTRACT, SYNTHETIC, ANNOTATION, ENUM, MODULE),
733 FIELD_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, VOLATILE, TRANSIENT, SYNTHETIC, ENUM),
734 METHOD_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, SYNCHRONIZED, BRIDGE, VARARGS, NATIVE, ABSTRACT, STRICT, SYNTHETIC),
735 INNER_CLASS_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, INTERFACE, ABSTRACT, SYNTHETIC, ANNOTATION, ENUM),
736 METHOD_PARAMETER_FLAGS = createDefinition(FINAL, SYNTHETIC, MANDATED),
737 MODULE_FLAGS = createDefinition(OPEN, SYNTHETIC, MANDATED),
738 MODULE_REQUIRES_FLAGS = createDefinition(TRANSITIVE, STATIC_PHASE, SYNTHETIC, MANDATED),
739 MODULE_EXPORTS_FLAGS = createDefinition(SYNTHETIC, MANDATED),
740 MODULE_OPENS_FLAGS = createDefinition(SYNTHETIC, MANDATED);
741
742 private static int undefinedMask(AccessFlag[] definition, int mask) {
743 assert definition.length == Character.SIZE;
744 int definedMask = 0;
745 for (int i = 0; i < Character.SIZE; i++) {
746 if (definition[i] != null) {
747 definedMask |= 1 << i;
748 }
749 }
750 return mask & ~definedMask;
751 }
752
753 private static final class AccessFlagSet extends AbstractSet<AccessFlag> {
754 private final @Stable AccessFlag[] definition;
755 private final int mask;
|
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
26 package java.lang.reflect;
27
28 import jdk.internal.javac.PreviewFeature;
29
30 import java.lang.classfile.ClassModel;
31 import java.lang.classfile.FieldModel;
32 import java.lang.classfile.MethodModel;
33 import java.lang.classfile.attribute.InnerClassInfo;
34 import java.lang.classfile.attribute.MethodParameterInfo;
35 import java.lang.classfile.attribute.ModuleAttribute;
36 import java.lang.classfile.attribute.ModuleExportInfo;
37 import java.lang.classfile.attribute.ModuleOpenInfo;
38 import java.lang.classfile.attribute.ModuleRequireInfo;
39 import java.lang.module.ModuleDescriptor;
40 import java.util.AbstractSet;
41 import java.util.Collection;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.NoSuchElementException;
46 import java.util.Objects;
47 import java.util.Set;
48 import java.util.function.Consumer;
49 import java.util.function.Predicate;
147 STATIC(Modifier.STATIC, true, Location.SET_FIELD_METHOD_INNER_CLASS,
148 List.of(Map.entry(RELEASE_0, Location.SET_FIELD_METHOD))),
149
150 /**
151 * The access flag {@code ACC_FINAL}, corresponding to the source
152 * modifier {@link Modifier#FINAL final}, with a mask
153 * value of <code>{@value "0x%04x" Modifier#FINAL}</code>.
154 */
155 FINAL(Modifier.FINAL, true,
156 Location.SET_FINAL_8,
157 List.of(Map.entry(RELEASE_7, Location.SET_CLASS_FIELD_METHOD_INNER_CLASS),
158 Map.entry(RELEASE_0, Location.SET_CLASS_FIELD_METHOD))),
159
160 /**
161 * The access flag {@code ACC_SUPER} with a mask value of {@code
162 * 0x0020}.
163 *
164 * @apiNote
165 * In Java SE 8 and above, the JVM treats the {@code ACC_SUPER}
166 * flag as set in every class file (JVMS {@jvms 4.1}).
167 * If preview feature is enabled,
168 * the {@code 0x0020} access flag bit is {@linkplain #IDENTITY IDENTITY access flag}.
169 */
170 SUPER(0x0000_0020, false,
171 Location.EMPTY_SET,
172 List.of(Map.entry(latest(), Location.SET_CLASS))),
173
174 /**
175 * The access flag {@code ACC_IDENTITY}, corresponding to the
176 * modifier {@link Modifier#IDENTITY identity}, with a mask
177 * value of <code>{@value "0x%04x" Modifier#IDENTITY}</code>.
178 * @jvms 4.1 -B. Class access and property modifiers
179 *
180 * @since Valhalla
181 */
182 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
183 IDENTITY(Modifier.IDENTITY, false,
184 Location.SET_CLASS_INNER_CLASS,
185 List.of(Map.entry(latest(), Location.EMPTY_SET))),
186
187 /**
188 * The module flag {@code ACC_OPEN} with a mask value of {@code
189 * 0x0020}.
190 * @see java.lang.module.ModuleDescriptor#isOpen
191 */
192 OPEN(0x0000_0020, false, Location.SET_MODULE,
193 List.of(Map.entry(RELEASE_8, Location.EMPTY_SET))),
194
195 /**
196 * The module requires flag {@code ACC_TRANSITIVE} with a mask
197 * value of {@code 0x0020}.
198 * @see java.lang.module.ModuleDescriptor.Requires.Modifier#TRANSITIVE
199 */
200 TRANSITIVE(0x0000_0020, false, Location.SET_MODULE_REQUIRES,
201 List.of(Map.entry(RELEASE_8, Location.EMPTY_SET))),
202
203 /**
204 * The access flag {@code ACC_SYNCHRONIZED}, corresponding to the
205 * source modifier {@link Modifier#SYNCHRONIZED synchronized}, with
266 * value of <code>{@value "0x%04x" Modifier#ABSTRACT}</code>.
267 */
268 ABSTRACT(Modifier.ABSTRACT, true,
269 Location.SET_CLASS_METHOD_INNER_CLASS,
270 List.of(Map.entry(RELEASE_0, Location.SET_CLASS_METHOD))),
271
272 /**
273 * The access flag {@code ACC_STRICT}, corresponding to the source
274 * modifier {@link Modifier#STRICT strictfp}, with a mask value of
275 * <code>{@value "0x%04x" Modifier#STRICT}</code>.
276 *
277 * @apiNote
278 * The {@code ACC_STRICT} access flag is defined for class file
279 * major versions 46 through 60, inclusive (JVMS {@jvms 4.6}),
280 * corresponding to Java SE 1.2 through 16.
281 */
282 STRICT(Modifier.STRICT, true, Location.EMPTY_SET,
283 List.of(Map.entry(RELEASE_16, Location.SET_METHOD),
284 Map.entry(RELEASE_1, Location.EMPTY_SET))),
285
286 /**
287 * The access flag {@code ACC_STRICT_INIT}, with a mask value of
288 * <code>{@value "0x%04x" java.lang.classfile.ClassFile#ACC_STRICT_INIT}</code>.
289 *
290 * @jvms 4.5 Fields
291 * @since Valhalla
292 */
293 @PreviewFeature(feature = PreviewFeature.Feature.VALUE_OBJECTS, reflective=true)
294 STRICT_INIT(ACC_STRICT_INIT, false,
295 Location.SET_FIELD,
296 List.of(Map.entry(latest(), Location.EMPTY_SET))),
297
298 /**
299 * The access flag {@code ACC_SYNTHETIC} with a mask value of
300 * <code>{@value "0x%04x" Modifier#SYNTHETIC}</code>.
301 * @see Class#isSynthetic()
302 * @see Executable#isSynthetic()
303 * @see java.lang.module.ModuleDescriptor.Modifier#SYNTHETIC
304 */
305 SYNTHETIC(Modifier.SYNTHETIC, false, Location.SET_SYNTHETIC_9,
306 List.of(Map.entry(RELEASE_8, Location.SET_SYNTHETIC_8),
307 Map.entry(RELEASE_7, Location.SET_SYNTHETIC_5),
308 Map.entry(RELEASE_4, Location.EMPTY_SET))),
309
310 /**
311 * The access flag {@code ACC_ANNOTATION} with a mask value of
312 * <code>{@value "0x%04x" Modifier#ANNOTATION}</code>.
313 * @see Class#isAnnotation()
314 */
315 ANNOTATION(Modifier.ANNOTATION, false, Location.SET_CLASS_INNER_CLASS,
316 List.of(Map.entry(RELEASE_4, Location.EMPTY_SET))),
317
367 public int mask() {
368 return mask;
369 }
370
371 /**
372 * {@return whether or not this flag has a directly corresponding
373 * modifier in the Java programming language}
374 */
375 public boolean sourceModifier() {
376 return sourceModifier;
377 }
378
379 /**
380 * {@return locations this flag can be applied to in the current class file
381 * format version}
382 * <p>
383 * This method returns an empty set if this flag is not defined in
384 * the current class file format version.
385 */
386 public Set<Location> locations() {
387 return locations(latest());
388 }
389
390 /**
391 * {@return locations this flag can be applied to in the given class file
392 * format version}
393 * <p>
394 * This method returns an empty set if this flag is not defined in
395 * the given {@code cffv}.
396 *
397 * @param cffv the class file format version to use
398 * @throws NullPointerException if the parameter is {@code null}
399 */
400 public Set<Location> locations(ClassFileFormatVersion cffv) {
401 return Location.findInHistory(locations, historicalLocations, cffv);
402 }
403
404 /**
405 * {@return an unmodifiable set of access flags for the given mask value
406 * appropriate for the location in the current class file format version}
407 *
408 * @param mask bit mask of access flags
409 * @param location context to interpret mask value
410 * @throws IllegalArgumentException if the mask contains bit
411 * positions not defined for the location in the current class file format
412 * @throws NullPointerException if {@code location} is {@code null}
413 */
414 public static Set<AccessFlag> maskToAccessFlags(int mask, Location location) {
415 return maskToAccessFlags(mask, location, latest());
416 }
417
418 /**
419 * {@return an unmodifiable set of access flags for the given mask value
420 * appropriate for the location in the given class file format version}
421 *
422 * @param mask bit mask of access flags
423 * @param location context to interpret mask value
424 * @param cffv the class file format to interpret mask value
425 * @throws IllegalArgumentException if the mask contains bit
426 * positions not defined for the location in the given class file format
427 * @throws NullPointerException if {@code location} or {@code cffv} is {@code null}
428 * @since 25
429 */
430 public static Set<AccessFlag> maskToAccessFlags(int mask, Location location, ClassFileFormatVersion cffv) {
431 var definition = findDefinition(location, cffv); // null checks location
432 int unmatchedMask = mask & (~location.flagsMask(cffv)); // null checks cffv
433 if (unmatchedMask != 0) {
434 throw new IllegalArgumentException("Unmatched bit position 0x" +
435 Integer.toHexString(unmatchedMask) +
436 " for location " + location +
437 " for class file format " + cffv);
438 }
439 return new AccessFlagSet(definition, mask);
440 }
441
442 /**
443 * A location within a {@code class} file where flags can be applied.
444 * <p>
445 * Note that since these locations represent {@code class} file structures
446 * rather than language structures, many language structures, such
447 * as constructors and interfaces, are <em>not</em> present.
448 * @since 20
449 */
450 public enum Location {
451 /**
452 * Class location.
453 *
454 * @see Class#accessFlags()
455 * @see ClassModel#flags()
456 * @see Modifier#classModifiers()
457 * @see Modifier#interfaceModifiers()
458 * @jvms 4.1 The {@code ClassFile} Structure
459 */
460 CLASS(ACC_PUBLIC | ACC_FINAL | ACC_IDENTITY |
461 ACC_INTERFACE | ACC_ABSTRACT |
462 ACC_SYNTHETIC | ACC_ANNOTATION |
463 ACC_ENUM | ACC_MODULE,
464 List.of(Map.entry(RELEASE_8, // no module
465 ACC_PUBLIC | ACC_FINAL | ACC_SUPER |
466 ACC_INTERFACE | ACC_ABSTRACT |
467 ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
468 Map.entry(RELEASE_4, // no synthetic, annotation, enum
469 ACC_PUBLIC | ACC_FINAL | ACC_SUPER |
470 ACC_INTERFACE | ACC_ABSTRACT))),
471
472 /**
473 * Field location.
474 *
475 * @see Field#accessFlags()
476 * @see FieldModel#flags()
477 * @see Modifier#fieldModifiers()
478 * @jvms 4.5 Fields
479 */
480 FIELD(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
481 ACC_STATIC | ACC_FINAL | ACC_VOLATILE |
482 ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM | ACC_STRICT_INIT,
483 List.of(Map.entry(latest(), // no strict_init
484 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
485 ACC_STATIC | ACC_FINAL | ACC_VOLATILE |
486 ACC_TRANSIENT | ACC_SYNTHETIC | ACC_ENUM),
487 Map.entry(RELEASE_4, // no synthetic, enum
488 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
489 ACC_STATIC | ACC_FINAL | ACC_VOLATILE |
490 ACC_TRANSIENT))),
491
492 /**
493 * Method location.
494 *
495 * @see Executable#accessFlags()
496 * @see MethodModel#flags()
497 * @see Modifier#methodModifiers()
498 * @see Modifier#constructorModifiers()
499 * @jvms 4.6 Methods
500 */
501 METHOD(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
502 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
503 ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE |
504 ACC_ABSTRACT | ACC_SYNTHETIC,
505 List.of(Map.entry(RELEASE_16, // had strict
506 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
507 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
508 ACC_BRIDGE | ACC_VARARGS | ACC_NATIVE |
509 ACC_ABSTRACT | ACC_STRICT | ACC_SYNTHETIC),
510 Map.entry(RELEASE_4, // no bridge, varargs, synthetic
511 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
512 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
513 ACC_NATIVE | ACC_ABSTRACT | ACC_STRICT),
514 Map.entry(RELEASE_1, // no strict
515 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
516 ACC_STATIC | ACC_FINAL | ACC_SYNCHRONIZED |
517 ACC_NATIVE | ACC_ABSTRACT))),
518
519 /**
520 * Inner class location.
521 *
522 * @see Class#accessFlags()
523 * @see InnerClassInfo#flags()
524 * @see Modifier#classModifiers()
525 * @see Modifier#interfaceModifiers()
526 * @jvms 4.7.6 The {@code InnerClasses} Attribute
527 */
528 INNER_CLASS(ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED | ACC_IDENTITY |
529 ACC_STATIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT |
530 ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM,
531 List.of(Map.entry(latest(), // no identity
532 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
533 ACC_STATIC | ACC_FINAL | ACC_INTERFACE | ACC_ABSTRACT |
534 ACC_SYNTHETIC | ACC_ANNOTATION | ACC_ENUM),
535 Map.entry(RELEASE_4, // no synthetic, annotation, enum
536 ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED |
537 ACC_STATIC | ACC_FINAL | ACC_INTERFACE |
538 ACC_ABSTRACT),
539 Map.entry(RELEASE_0, 0))), // did not exist
540
541 /**
542 * Method parameter location.
543 *
544 * @see Parameter#accessFlags()
545 * @see MethodParameterInfo#flags()
546 * @see Modifier#parameterModifiers()
547 * @jvms 4.7.24 The {@code MethodParameters} Attribute
548 */
549 METHOD_PARAMETER(ACC_FINAL | ACC_SYNTHETIC | ACC_MANDATED,
550 List.of(Map.entry(RELEASE_7, 0))), // did not exist
551
552 /**
553 * Module location.
554 *
555 * @see ModuleDescriptor#accessFlags()
556 * @see ModuleAttribute#moduleFlags()
557 * @jvms 4.7.25 The {@code Module} Attribute
558 */
635 MODULE_EXPORTS, MODULE_OPENS);
636 private static final Set<Location> SET_MANDATED_9 =
637 Set.of(METHOD_PARAMETER, // From 8
638 // Starting in 9
639 MODULE, MODULE_REQUIRES,
640 MODULE_EXPORTS, MODULE_OPENS);
641
642 private final int flagsMask;
643 private final List<Map.Entry<ClassFileFormatVersion, Integer>> historicalFlagsMasks;
644
645 Location(int flagsMask,
646 List<Map.Entry<ClassFileFormatVersion, Integer>> historicalFlagsMasks) {
647 this.flagsMask = flagsMask;
648 this.historicalFlagsMasks = ensureHistoryOrdered(historicalFlagsMasks);
649 }
650
651 // Ensures the historical versions are from newest to oldest and do not include the latest
652 // These 2 utilities reside in Location because Location must be initialized before AccessFlag
653 private static <T> List<Map.Entry<ClassFileFormatVersion, T>> ensureHistoryOrdered(
654 List<Map.Entry<ClassFileFormatVersion, T>> history) {
655 ClassFileFormatVersion lastVersion = CURRENT_PREVIEW_FEATURES;
656 for (var e : history) {
657 var historyVersion = e.getKey();
658 if (lastVersion.compareTo(historyVersion) <= 0) {
659 throw new IllegalArgumentException("Versions out of order");
660 }
661 lastVersion = historyVersion;
662 }
663 return history;
664 }
665
666 private static <T> T findInHistory(T candidate, List<Map.Entry<ClassFileFormatVersion, T>> history,
667 ClassFileFormatVersion cffv) {
668 Objects.requireNonNull(cffv);
669 for (var e : history) {
670 if (e.getKey().compareTo(cffv) < 0) {
671 // last version found was valid
672 return candidate;
673 }
674 candidate = e.getValue();
675 }
676 return candidate;
677 }
678
679 /**
680 * {@return the union of masks of all access flags defined for
681 * this location in the current class file format version}
682 * <p>
683 * This method returns {@code 0} if this location does not exist in
684 * the current class file format version.
685 *
686 * @since 25
687 */
688 public int flagsMask() {
689 return flagsMask(latest());
690 }
691
692 /**
693 * {@return the union of masks of all access flags defined for
694 * this location in the given class file format version}
695 * <p>
696 * This method returns {@code 0} if this location does not exist in
697 * the given {@code cffv}.
698 *
699 * @param cffv the class file format version
700 * @throws NullPointerException if {@code cffv} is {@code null}
701 * @since 25
702 */
703 public int flagsMask(ClassFileFormatVersion cffv) {
704 return findInHistory(flagsMask, historicalFlagsMasks, cffv);
705 }
706
707 /**
708 * {@return the set of access flags defined for this location in the
709 * current class file format version} The set is immutable.
710 * <p>
711 * This method returns an empty set if this location does not exist
712 * in the current class file format version.
713 *
714 * @since 25
715 */
716 public Set<AccessFlag> flags() {
717 return flags(latest());
718 }
719
720 /**
721 * {@return the set of access flags defined for this location in the
722 * given class file format version} The set is immutable.
723 * <p>
724 * This method returns an empty set if this location does not exist
725 * in the given {@code cffv}.
726 *
727 * @param cffv the class file format version
728 * @throws NullPointerException if {@code cffv} is {@code null}
729 * @since 25
730 */
731 public Set<AccessFlag> flags(ClassFileFormatVersion cffv) {
732 // implicit null check cffv
733 return new AccessFlagSet(findDefinition(this, cffv), flagsMask(cffv));
734 }
735 }
736
737 private static AccessFlag[] createDefinition(AccessFlag... known) {
738 var ret = new AccessFlag[Character.SIZE];
739 for (var flag : known) {
740 var mask = flag.mask;
741 int pos = Integer.numberOfTrailingZeros(mask);
742 assert ret[pos] == null : ret[pos] + " " + flag;
743 ret[pos] = flag;
744 }
745 return ret;
746 }
747
748 private static AccessFlag[] findDefinition(Location location, ClassFileFormatVersion cffv) {
749 return switch (location) {
750 case CLASS -> cffv == CURRENT_PREVIEW_FEATURES ? CLASS_PREVIEW_FLAGS : CLASS_FLAGS;
751 case FIELD -> cffv == CURRENT_PREVIEW_FEATURES ? FIELD_PREVIEW_FLAGS : FIELD_FLAGS;
752 case METHOD -> METHOD_FLAGS;
753 case INNER_CLASS -> cffv == CURRENT_PREVIEW_FEATURES ? INNER_CLASS_PREVIEW_FLAGS : INNER_CLASS_FLAGS;
754 case METHOD_PARAMETER -> METHOD_PARAMETER_FLAGS;
755 case MODULE -> MODULE_FLAGS;
756 case MODULE_REQUIRES -> MODULE_REQUIRES_FLAGS;
757 case MODULE_EXPORTS -> MODULE_EXPORTS_FLAGS;
758 case MODULE_OPENS -> MODULE_OPENS_FLAGS;
759 };
760 }
761
762 private static final @Stable AccessFlag[] // Can use stable array and lazy init in the future
763 CLASS_FLAGS = createDefinition(PUBLIC, FINAL, SUPER, INTERFACE, ABSTRACT, SYNTHETIC, ANNOTATION, ENUM, MODULE),
764 CLASS_PREVIEW_FLAGS = createDefinition(PUBLIC, FINAL, IDENTITY, INTERFACE, ABSTRACT, SYNTHETIC, ANNOTATION, ENUM, MODULE), // identity
765 FIELD_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, VOLATILE, TRANSIENT, SYNTHETIC, ENUM),
766 FIELD_PREVIEW_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, VOLATILE, TRANSIENT, SYNTHETIC, ENUM, STRICT_INIT), // strict
767 METHOD_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, SYNCHRONIZED, BRIDGE, VARARGS, NATIVE, ABSTRACT, STRICT, SYNTHETIC),
768 INNER_CLASS_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, STATIC, FINAL, INTERFACE, ABSTRACT, SYNTHETIC, ANNOTATION, ENUM),
769 INNER_CLASS_PREVIEW_FLAGS = createDefinition(PUBLIC, PRIVATE, PROTECTED, IDENTITY, STATIC, FINAL, INTERFACE, ABSTRACT, SYNTHETIC, ANNOTATION, ENUM), // identity
770 METHOD_PARAMETER_FLAGS = createDefinition(FINAL, SYNTHETIC, MANDATED),
771 MODULE_FLAGS = createDefinition(OPEN, SYNTHETIC, MANDATED),
772 MODULE_REQUIRES_FLAGS = createDefinition(TRANSITIVE, STATIC_PHASE, SYNTHETIC, MANDATED),
773 MODULE_EXPORTS_FLAGS = createDefinition(SYNTHETIC, MANDATED),
774 MODULE_OPENS_FLAGS = createDefinition(SYNTHETIC, MANDATED);
775
776 private static int undefinedMask(AccessFlag[] definition, int mask) {
777 assert definition.length == Character.SIZE;
778 int definedMask = 0;
779 for (int i = 0; i < Character.SIZE; i++) {
780 if (definition[i] != null) {
781 definedMask |= 1 << i;
782 }
783 }
784 return mask & ~definedMask;
785 }
786
787 private static final class AccessFlagSet extends AbstractSet<AccessFlag> {
788 private final @Stable AccessFlag[] definition;
789 private final int mask;
|