1 /* 2 * Copyright (c) 2022, 2024, 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.*; 28 import java.lang.classfile.attribute.*; 29 import java.lang.classfile.constantpool.ClassEntry; 30 import java.lang.classfile.constantpool.ConstantValueEntry; 31 import java.lang.classfile.constantpool.ModuleEntry; 32 import java.lang.classfile.constantpool.NameAndTypeEntry; 33 import java.lang.classfile.constantpool.PackageEntry; 34 import java.lang.classfile.constantpool.Utf8Entry; 35 import java.util.Collection; 36 import java.util.List; 37 import java.util.Optional; 38 39 import jdk.internal.access.SharedSecrets; 40 41 import static java.util.Objects.requireNonNull; 42 43 public abstract sealed class UnboundAttribute<T extends Attribute<T>> 44 extends AbstractElement 45 implements Attribute<T>, Util.Writable { 46 protected final AttributeMapper<T> mapper; 47 48 public UnboundAttribute(AttributeMapper<T> mapper) { 49 this.mapper = mapper; 50 } 51 52 @Override 53 public AttributeMapper<T> attributeMapper() { 54 return mapper; 55 } 56 57 @Override 58 public String attributeName() { 59 return mapper.name(); 60 } 61 62 @Override 63 @SuppressWarnings("unchecked") 64 public void writeTo(BufWriterImpl buf) { 65 mapper.writeAttribute(buf, (T) this); 66 } 67 68 @Override 69 public void writeTo(DirectClassBuilder builder) { 70 builder.writeAttribute(this); 71 } 72 73 @Override 74 public void writeTo(DirectCodeBuilder builder) { 75 builder.writeAttribute(this); 76 } 77 78 @Override 79 public void writeTo(DirectMethodBuilder builder) { 80 builder.writeAttribute(this); 81 } 82 83 @Override 84 public void writeTo(DirectFieldBuilder builder) { 85 builder.writeAttribute(this); 86 } 87 88 @Override 89 public String toString() { 90 return String.format("Attribute[name=%s]", mapper.name()); 91 } 92 public static final class UnboundConstantValueAttribute 93 extends UnboundAttribute<ConstantValueAttribute> 94 implements ConstantValueAttribute { 95 96 private final ConstantValueEntry entry; 97 98 public UnboundConstantValueAttribute(ConstantValueEntry entry) { 99 super(Attributes.constantValue()); 100 this.entry = requireNonNull(entry); 101 } 102 103 @Override 104 public ConstantValueEntry constant() { 105 return entry; 106 } 107 108 } 109 110 public static final class UnboundDeprecatedAttribute 111 extends UnboundAttribute<DeprecatedAttribute> 112 implements DeprecatedAttribute { 113 public UnboundDeprecatedAttribute() { 114 super(Attributes.deprecated()); 115 } 116 } 117 118 public static final class UnboundSyntheticAttribute 119 extends UnboundAttribute<SyntheticAttribute> 120 implements SyntheticAttribute { 121 public UnboundSyntheticAttribute() { 122 super(Attributes.synthetic()); 123 } 124 } 125 126 public static final class UnboundSignatureAttribute 127 extends UnboundAttribute<SignatureAttribute> 128 implements SignatureAttribute { 129 private final Utf8Entry signature; 130 131 public UnboundSignatureAttribute(Utf8Entry signature) { 132 super(Attributes.signature()); 133 this.signature = requireNonNull(signature); 134 } 135 136 @Override 137 public Utf8Entry signature() { 138 return signature; 139 } 140 } 141 142 public static final class UnboundExceptionsAttribute 143 extends UnboundAttribute<ExceptionsAttribute> 144 implements ExceptionsAttribute { 145 private final List<ClassEntry> exceptions; 146 147 public UnboundExceptionsAttribute(List<ClassEntry> exceptions) { 148 super(Attributes.exceptions()); 149 this.exceptions = List.copyOf(exceptions); 150 } 151 152 @Override 153 public List<ClassEntry> exceptions() { 154 return exceptions; 155 } 156 } 157 158 public static final class UnboundAnnotationDefaultAttribute 159 extends UnboundAttribute<AnnotationDefaultAttribute> 160 implements AnnotationDefaultAttribute { 161 private final AnnotationValue annotationDefault; 162 163 public UnboundAnnotationDefaultAttribute(AnnotationValue annotationDefault) { 164 super(Attributes.annotationDefault()); 165 this.annotationDefault = requireNonNull(annotationDefault); 166 } 167 168 @Override 169 public AnnotationValue defaultValue() { 170 return annotationDefault; 171 } 172 } 173 174 public static final class UnboundSourceFileAttribute extends UnboundAttribute<SourceFileAttribute> 175 implements SourceFileAttribute { 176 private final Utf8Entry sourceFile; 177 178 public UnboundSourceFileAttribute(Utf8Entry sourceFile) { 179 super(Attributes.sourceFile()); 180 this.sourceFile = requireNonNull(sourceFile); 181 } 182 183 @Override 184 public Utf8Entry sourceFile() { 185 return sourceFile; 186 } 187 188 } 189 190 public static final class UnboundStackMapTableAttribute extends UnboundAttribute<StackMapTableAttribute> 191 implements StackMapTableAttribute { 192 private final List<StackMapFrameInfo> entries; 193 194 public UnboundStackMapTableAttribute(List<StackMapFrameInfo> entries) { 195 super(Attributes.stackMapTable()); 196 this.entries = List.copyOf(entries); 197 } 198 199 @Override 200 public List<StackMapFrameInfo> entries() { 201 return entries; 202 } 203 } 204 205 public static final class UnboundInnerClassesAttribute 206 extends UnboundAttribute<InnerClassesAttribute> 207 implements InnerClassesAttribute { 208 private final List<InnerClassInfo> innerClasses; 209 210 public UnboundInnerClassesAttribute(List<InnerClassInfo> innerClasses) { 211 super(Attributes.innerClasses()); 212 this.innerClasses = List.copyOf(innerClasses); 213 } 214 215 @Override 216 public List<InnerClassInfo> classes() { 217 return innerClasses; 218 } 219 } 220 221 public static final class UnboundRecordAttribute 222 extends UnboundAttribute<RecordAttribute> 223 implements RecordAttribute { 224 private final List<RecordComponentInfo> components; 225 226 public UnboundRecordAttribute(List<RecordComponentInfo> components) { 227 super(Attributes.record()); 228 this.components = List.copyOf(components); 229 } 230 231 @Override 232 public List<RecordComponentInfo> components() { 233 return components; 234 } 235 } 236 237 public static final class UnboundEnclosingMethodAttribute 238 extends UnboundAttribute<EnclosingMethodAttribute> 239 implements EnclosingMethodAttribute { 240 private final ClassEntry classEntry; 241 private final NameAndTypeEntry method; 242 243 public UnboundEnclosingMethodAttribute(ClassEntry classEntry, NameAndTypeEntry method) { 244 super(Attributes.enclosingMethod()); 245 this.classEntry = requireNonNull(classEntry); 246 this.method = method; 247 } 248 249 @Override 250 public ClassEntry enclosingClass() { 251 return classEntry; 252 } 253 254 @Override 255 public Optional<NameAndTypeEntry> enclosingMethod() { 256 return Optional.ofNullable(method); 257 } 258 } 259 260 public static final class UnboundMethodParametersAttribute 261 extends UnboundAttribute<MethodParametersAttribute> 262 implements MethodParametersAttribute { 263 private final List<MethodParameterInfo> parameters; 264 265 public UnboundMethodParametersAttribute(List<MethodParameterInfo> parameters) { 266 super(Attributes.methodParameters()); 267 this.parameters = List.copyOf(parameters); 268 } 269 270 @Override 271 public List<MethodParameterInfo> parameters() { 272 return parameters; 273 } 274 } 275 276 public static final class UnboundModuleTargetAttribute 277 extends UnboundAttribute<ModuleTargetAttribute> 278 implements ModuleTargetAttribute { 279 final Utf8Entry moduleTarget; 280 281 public UnboundModuleTargetAttribute(Utf8Entry moduleTarget) { 282 super(Attributes.moduleTarget()); 283 this.moduleTarget = requireNonNull(moduleTarget); 284 } 285 286 @Override 287 public Utf8Entry targetPlatform() { 288 return moduleTarget; 289 } 290 } 291 292 public static final class UnboundModuleMainClassAttribute 293 extends UnboundAttribute<ModuleMainClassAttribute> 294 implements ModuleMainClassAttribute { 295 final ClassEntry mainClass; 296 297 public UnboundModuleMainClassAttribute(ClassEntry mainClass) { 298 super(Attributes.moduleMainClass()); 299 this.mainClass = requireNonNull(mainClass); 300 } 301 302 @Override 303 public ClassEntry mainClass() { 304 return mainClass; 305 } 306 } 307 308 public static final class UnboundModuleHashesAttribute 309 extends UnboundAttribute<ModuleHashesAttribute> 310 implements ModuleHashesAttribute { 311 private final Utf8Entry algorithm; 312 private final List<ModuleHashInfo> hashes; 313 314 public UnboundModuleHashesAttribute(Utf8Entry algorithm, List<ModuleHashInfo> hashes) { 315 super(Attributes.moduleHashes()); 316 this.algorithm = requireNonNull(algorithm); 317 this.hashes = List.copyOf(hashes); 318 } 319 320 @Override 321 public Utf8Entry algorithm() { 322 return algorithm; 323 } 324 325 @Override 326 public List<ModuleHashInfo> hashes() { 327 return hashes; 328 } 329 } 330 331 public static final class UnboundModulePackagesAttribute 332 extends UnboundAttribute<ModulePackagesAttribute> 333 implements ModulePackagesAttribute { 334 private final Collection<PackageEntry> packages; 335 336 public UnboundModulePackagesAttribute(Collection<PackageEntry> packages) { 337 super(Attributes.modulePackages()); 338 this.packages = List.copyOf(packages); 339 } 340 341 @Override 342 public List<PackageEntry> packages() { 343 return List.copyOf(packages); 344 } 345 } 346 347 public static final class UnboundModuleResolutionAttribute 348 extends UnboundAttribute<ModuleResolutionAttribute> 349 implements ModuleResolutionAttribute { 350 private final int resolutionFlags; 351 352 public UnboundModuleResolutionAttribute(int flags) { 353 super(Attributes.moduleResolution()); 354 resolutionFlags = flags; 355 } 356 357 @Override 358 public int resolutionFlags() { 359 return resolutionFlags; 360 } 361 } 362 363 public static final class UnboundPermittedSubclassesAttribute 364 extends UnboundAttribute<PermittedSubclassesAttribute> 365 implements PermittedSubclassesAttribute { 366 private final List<ClassEntry> permittedSubclasses; 367 368 public UnboundPermittedSubclassesAttribute(List<ClassEntry> permittedSubclasses) { 369 super(Attributes.permittedSubclasses()); 370 this.permittedSubclasses = List.copyOf(permittedSubclasses); 371 } 372 373 @Override 374 public List<ClassEntry> permittedSubclasses() { 375 return permittedSubclasses; 376 } 377 } 378 379 public static final class UnboundNestMembersAttribute 380 extends UnboundAttribute<NestMembersAttribute> 381 implements NestMembersAttribute { 382 private final List<ClassEntry> memberEntries; 383 384 public UnboundNestMembersAttribute(List<ClassEntry> memberEntries) { 385 super(Attributes.nestMembers()); 386 this.memberEntries = List.copyOf(memberEntries); 387 } 388 389 @Override 390 public List<ClassEntry> nestMembers() { 391 return memberEntries; 392 } 393 } 394 395 public static final class UnboundNestHostAttribute 396 extends UnboundAttribute<NestHostAttribute> 397 implements NestHostAttribute { 398 private final ClassEntry hostEntry; 399 400 public UnboundNestHostAttribute(ClassEntry hostEntry) { 401 super(Attributes.nestHost()); 402 this.hostEntry = requireNonNull(hostEntry); 403 } 404 405 @Override 406 public ClassEntry nestHost() { 407 return hostEntry; 408 } 409 } 410 411 public static final class UnboundCompilationIDAttribute 412 extends UnboundAttribute<CompilationIDAttribute> 413 implements CompilationIDAttribute { 414 private final Utf8Entry idEntry; 415 416 public UnboundCompilationIDAttribute(Utf8Entry idEntry) { 417 super(Attributes.compilationId()); 418 this.idEntry = requireNonNull(idEntry); 419 } 420 421 @Override 422 public Utf8Entry compilationId() { 423 return idEntry; 424 } 425 } 426 427 public static final class UnboundSourceIDAttribute 428 extends UnboundAttribute<SourceIDAttribute> 429 implements SourceIDAttribute { 430 private final Utf8Entry idEntry; 431 432 public UnboundSourceIDAttribute(Utf8Entry idEntry) { 433 super(Attributes.sourceId()); 434 this.idEntry = requireNonNull(idEntry); 435 } 436 437 @Override 438 public Utf8Entry sourceId() { 439 return idEntry; 440 } 441 } 442 443 public static final class UnboundSourceDebugExtensionAttribute 444 extends UnboundAttribute<SourceDebugExtensionAttribute> 445 implements SourceDebugExtensionAttribute { 446 private final byte[] contents; 447 448 public UnboundSourceDebugExtensionAttribute(byte[] contents) { 449 super(Attributes.sourceDebugExtension()); 450 this.contents = requireNonNull(contents); 451 } 452 453 @Override 454 public byte[] contents() { 455 return contents; 456 } 457 } 458 459 public static final class UnboundCharacterRangeTableAttribute 460 extends UnboundAttribute<CharacterRangeTableAttribute> 461 implements CharacterRangeTableAttribute { 462 private final List<CharacterRangeInfo> ranges; 463 464 public UnboundCharacterRangeTableAttribute(List<CharacterRangeInfo> ranges) { 465 super(Attributes.characterRangeTable()); 466 this.ranges = List.copyOf(ranges); 467 } 468 469 @Override 470 public List<CharacterRangeInfo> characterRangeTable() { 471 return ranges; 472 } 473 } 474 475 public static final class UnboundLineNumberTableAttribute 476 extends UnboundAttribute<LineNumberTableAttribute> 477 implements LineNumberTableAttribute { 478 private final List<LineNumberInfo> lines; 479 480 public UnboundLineNumberTableAttribute(List<LineNumberInfo> lines) { 481 super(Attributes.lineNumberTable()); 482 this.lines = List.copyOf(lines); 483 } 484 485 @Override 486 public List<LineNumberInfo> lineNumbers() { 487 return lines; 488 } 489 } 490 491 public static final class UnboundLocalVariableTableAttribute 492 extends UnboundAttribute<LocalVariableTableAttribute> 493 implements LocalVariableTableAttribute { 494 private final List<LocalVariableInfo> locals; 495 496 public UnboundLocalVariableTableAttribute(List<LocalVariableInfo> locals) { 497 super(Attributes.localVariableTable()); 498 this.locals = List.copyOf(locals); 499 } 500 501 @Override 502 public List<LocalVariableInfo> localVariables() { 503 return locals; 504 } 505 } 506 507 public static final class UnboundLocalVariableTypeTableAttribute 508 extends UnboundAttribute<LocalVariableTypeTableAttribute> 509 implements LocalVariableTypeTableAttribute { 510 private final List<LocalVariableTypeInfo> locals; 511 512 public UnboundLocalVariableTypeTableAttribute(List<LocalVariableTypeInfo> locals) { 513 super(Attributes.localVariableTypeTable()); 514 this.locals = List.copyOf(locals); 515 } 516 517 @Override 518 public List<LocalVariableTypeInfo> localVariableTypes() { 519 return locals; 520 } 521 } 522 523 public static final class UnboundRuntimeVisibleAnnotationsAttribute 524 extends UnboundAttribute<RuntimeVisibleAnnotationsAttribute> 525 implements RuntimeVisibleAnnotationsAttribute { 526 private final List<Annotation> elements; 527 528 public UnboundRuntimeVisibleAnnotationsAttribute(List<Annotation> elements) { 529 super(Attributes.runtimeVisibleAnnotations()); 530 this.elements = List.copyOf(elements); 531 } 532 533 @Override 534 public List<Annotation> annotations() { 535 return elements; 536 } 537 } 538 539 public static final class UnboundRuntimeInvisibleAnnotationsAttribute 540 extends UnboundAttribute<RuntimeInvisibleAnnotationsAttribute> 541 implements RuntimeInvisibleAnnotationsAttribute { 542 private final List<Annotation> elements; 543 544 public UnboundRuntimeInvisibleAnnotationsAttribute(List<Annotation> elements) { 545 super(Attributes.runtimeInvisibleAnnotations()); 546 this.elements = List.copyOf(elements); 547 } 548 549 @Override 550 public List<Annotation> annotations() { 551 return elements; 552 } 553 } 554 555 public static final class UnboundRuntimeVisibleParameterAnnotationsAttribute 556 extends UnboundAttribute<RuntimeVisibleParameterAnnotationsAttribute> 557 implements RuntimeVisibleParameterAnnotationsAttribute { 558 private final List<List<Annotation>> elements; 559 560 public UnboundRuntimeVisibleParameterAnnotationsAttribute(List<List<Annotation>> elements) { 561 super(Attributes.runtimeVisibleParameterAnnotations()); 562 // deep copy 563 var array = elements.toArray().clone(); 564 for (int i = 0; i < array.length; i++) { 565 array[i] = List.copyOf((List<?>) array[i]); 566 } 567 568 this.elements = SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(array); 569 } 570 571 @Override 572 public List<List<Annotation>> parameterAnnotations() { 573 return elements; 574 } 575 } 576 577 public static final class UnboundRuntimeInvisibleParameterAnnotationsAttribute 578 extends UnboundAttribute<RuntimeInvisibleParameterAnnotationsAttribute> 579 implements RuntimeInvisibleParameterAnnotationsAttribute { 580 private final List<List<Annotation>> elements; 581 582 public UnboundRuntimeInvisibleParameterAnnotationsAttribute(List<List<Annotation>> elements) { 583 super(Attributes.runtimeInvisibleParameterAnnotations()); 584 // deep copy 585 var array = elements.toArray().clone(); 586 for (int i = 0; i < array.length; i++) { 587 array[i] = List.copyOf((List<?>) array[i]); 588 } 589 590 this.elements = SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(array); 591 } 592 593 @Override 594 public List<List<Annotation>> parameterAnnotations() { 595 return elements; 596 } 597 } 598 599 public static final class UnboundRuntimeVisibleTypeAnnotationsAttribute 600 extends UnboundAttribute<RuntimeVisibleTypeAnnotationsAttribute> 601 implements RuntimeVisibleTypeAnnotationsAttribute { 602 private final List<TypeAnnotation> elements; 603 604 public UnboundRuntimeVisibleTypeAnnotationsAttribute(List<TypeAnnotation> elements) { 605 super(Attributes.runtimeVisibleTypeAnnotations()); 606 this.elements = List.copyOf(elements); 607 } 608 609 @Override 610 public List<TypeAnnotation> annotations() { 611 return elements; 612 } 613 } 614 615 public static final class UnboundRuntimeInvisibleTypeAnnotationsAttribute 616 extends UnboundAttribute<RuntimeInvisibleTypeAnnotationsAttribute> 617 implements RuntimeInvisibleTypeAnnotationsAttribute { 618 private final List<TypeAnnotation> elements; 619 620 public UnboundRuntimeInvisibleTypeAnnotationsAttribute(List<TypeAnnotation> elements) { 621 super(Attributes.runtimeInvisibleTypeAnnotations()); 622 this.elements = List.copyOf(elements); 623 } 624 625 @Override 626 public List<TypeAnnotation> annotations() { 627 return elements; 628 } 629 } 630 631 public record UnboundCharacterRangeInfo(int startPc, int endPc, 632 int characterRangeStart, 633 int characterRangeEnd, 634 int flags) 635 implements CharacterRangeInfo { } 636 637 public record UnboundInnerClassInfo(ClassEntry innerClass, 638 Optional<ClassEntry> outerClass, 639 Optional<Utf8Entry> innerName, 640 int flagsMask) 641 implements InnerClassInfo { 642 public UnboundInnerClassInfo { 643 requireNonNull(innerClass); 644 requireNonNull(outerClass); 645 requireNonNull(innerName); 646 } 647 } 648 649 public record UnboundLineNumberInfo(int startPc, int lineNumber) 650 implements LineNumberInfo { } 651 652 public record UnboundLocalVariableInfo(int startPc, int length, 653 Utf8Entry name, 654 Utf8Entry type, 655 int slot) 656 implements LocalVariableInfo { 657 public UnboundLocalVariableInfo { 658 requireNonNull(name); 659 requireNonNull(type); 660 } 661 } 662 663 public record UnboundLocalVariableTypeInfo(int startPc, int length, 664 Utf8Entry name, 665 Utf8Entry signature, 666 int slot) 667 implements LocalVariableTypeInfo { 668 public UnboundLocalVariableTypeInfo { 669 requireNonNull(name); 670 requireNonNull(signature); 671 } 672 } 673 674 public record UnboundMethodParameterInfo(Optional<Utf8Entry> name, int flagsMask) 675 implements MethodParameterInfo { 676 public UnboundMethodParameterInfo { 677 requireNonNull(name); 678 } 679 } 680 681 public record UnboundModuleExportInfo(PackageEntry exportedPackage, 682 int exportsFlagsMask, 683 List<ModuleEntry> exportsTo) 684 implements ModuleExportInfo { 685 public UnboundModuleExportInfo { 686 requireNonNull(exportedPackage); 687 exportsTo = List.copyOf(exportsTo); 688 } 689 } 690 691 public record UnboundModuleHashInfo(ModuleEntry moduleName, 692 byte[] hash) implements ModuleHashInfo { 693 public UnboundModuleHashInfo { 694 requireNonNull(moduleName); 695 requireNonNull(hash); 696 } 697 } 698 699 public record UnboundModuleOpenInfo(PackageEntry openedPackage, int opensFlagsMask, 700 List<ModuleEntry> opensTo) 701 implements ModuleOpenInfo { 702 public UnboundModuleOpenInfo { 703 requireNonNull(openedPackage); 704 opensTo = List.copyOf(opensTo); 705 } 706 } 707 708 public record UnboundModuleProvideInfo(ClassEntry provides, 709 List<ClassEntry> providesWith) 710 implements ModuleProvideInfo { 711 public UnboundModuleProvideInfo { 712 requireNonNull(provides); 713 providesWith = List.copyOf(providesWith); 714 } 715 } 716 717 public record UnboundModuleRequiresInfo(ModuleEntry requires, int requiresFlagsMask, 718 Optional<Utf8Entry> requiresVersion) 719 implements ModuleRequireInfo { 720 public UnboundModuleRequiresInfo { 721 requireNonNull(requires); 722 requireNonNull(requiresVersion); 723 } 724 } 725 726 public record UnboundRecordComponentInfo(Utf8Entry name, 727 Utf8Entry descriptor, 728 List<Attribute<?>> attributes) 729 implements RecordComponentInfo { 730 public UnboundRecordComponentInfo { 731 requireNonNull(name); 732 requireNonNull(descriptor); 733 attributes = List.copyOf(attributes); 734 } 735 } 736 737 public record UnboundTypeAnnotation(TargetInfo targetInfo, 738 List<TypePathComponent> targetPath, 739 Annotation annotation) implements TypeAnnotation { 740 741 public UnboundTypeAnnotation { 742 requireNonNull(targetInfo); 743 targetPath = List.copyOf(targetPath); 744 requireNonNull(annotation); 745 } 746 } 747 748 public record TypePathComponentImpl(TypeAnnotation.TypePathComponent.Kind typePathKind, int typeArgumentIndex) 749 implements TypeAnnotation.TypePathComponent {} 750 751 public static final class UnboundModuleAttribute extends UnboundAttribute<ModuleAttribute> implements ModuleAttribute { 752 private final ModuleEntry moduleName; 753 private final int moduleFlags; 754 private final Utf8Entry moduleVersion; 755 private final List<ModuleRequireInfo> requires; 756 private final List<ModuleExportInfo> exports; 757 private final List<ModuleOpenInfo> opens; 758 private final List<ClassEntry> uses; 759 private final List<ModuleProvideInfo> provides; 760 761 public UnboundModuleAttribute(ModuleEntry moduleName, 762 int moduleFlags, 763 Utf8Entry moduleVersion, 764 Collection<ModuleRequireInfo> requires, 765 Collection<ModuleExportInfo> exports, 766 Collection<ModuleOpenInfo> opens, 767 Collection<ClassEntry> uses, 768 Collection<ModuleProvideInfo> provides) 769 { 770 super(Attributes.module()); 771 this.moduleName = requireNonNull(moduleName); 772 this.moduleFlags = moduleFlags; 773 this.moduleVersion = moduleVersion; 774 this.requires = List.copyOf(requires); 775 this.exports = List.copyOf(exports); 776 this.opens = List.copyOf(opens); 777 this.uses = List.copyOf(uses); 778 this.provides = List.copyOf(provides); 779 } 780 781 @Override 782 public ModuleEntry moduleName() { 783 return moduleName; 784 } 785 786 @Override 787 public int moduleFlagsMask() { 788 return moduleFlags; 789 } 790 791 @Override 792 public Optional<Utf8Entry> moduleVersion() { 793 return Optional.ofNullable(moduleVersion); 794 } 795 796 @Override 797 public List<ModuleRequireInfo> requires() { 798 return requires; 799 } 800 801 @Override 802 public List<ModuleExportInfo> exports() { 803 return exports; 804 } 805 806 @Override 807 public List<ModuleOpenInfo> opens() { 808 return opens; 809 } 810 811 @Override 812 public List<ClassEntry> uses() { 813 return uses; 814 } 815 816 @Override 817 public List<ModuleProvideInfo> provides() { 818 return provides; 819 } 820 } 821 822 public abstract static non-sealed class AdHocAttribute<T extends Attribute<T>> 823 extends UnboundAttribute<T> { 824 825 public AdHocAttribute(AttributeMapper<T> mapper) { 826 super(mapper); 827 } 828 829 public abstract void writeBody(BufWriterImpl b); 830 831 @Override 832 public void writeTo(BufWriterImpl b) { 833 b.writeIndex(b.constantPool().utf8Entry(mapper.name())); 834 int lengthIndex = b.skip(4); 835 writeBody(b); 836 int written = b.size() - lengthIndex - 4; 837 b.patchInt(lengthIndex, written); 838 } 839 } 840 841 public static final class EmptyBootstrapAttribute 842 extends UnboundAttribute<BootstrapMethodsAttribute> 843 implements BootstrapMethodsAttribute { 844 public EmptyBootstrapAttribute() { 845 super(Attributes.bootstrapMethods()); 846 } 847 848 @Override 849 public int bootstrapMethodsSize() { 850 return 0; 851 } 852 853 @Override 854 public List<BootstrapMethodEntry> bootstrapMethods() { 855 return List.of(); 856 } 857 } 858 }