1 /* 2 * Copyright (c) 2022, 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 java.lang.classfile; 26 27 import java.util.Collections; 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31 import java.util.Set; 32 33 import java.lang.classfile.attribute.AnnotationDefaultAttribute; 34 import java.lang.classfile.attribute.BootstrapMethodsAttribute; 35 import java.lang.classfile.attribute.CharacterRangeInfo; 36 import java.lang.classfile.attribute.CharacterRangeTableAttribute; 37 import java.lang.classfile.attribute.CodeAttribute; 38 import java.lang.classfile.attribute.CompilationIDAttribute; 39 import java.lang.classfile.attribute.ConstantValueAttribute; 40 import java.lang.classfile.attribute.DeprecatedAttribute; 41 import java.lang.classfile.attribute.EnclosingMethodAttribute; 42 import java.lang.classfile.attribute.ExceptionsAttribute; 43 import java.lang.classfile.attribute.InnerClassInfo; 44 import java.lang.classfile.attribute.InnerClassesAttribute; 45 import java.lang.classfile.attribute.LineNumberInfo; 46 import java.lang.classfile.attribute.LineNumberTableAttribute; 47 import java.lang.classfile.attribute.LocalVariableInfo; 48 import java.lang.classfile.attribute.LocalVariableTableAttribute; 49 import java.lang.classfile.attribute.LocalVariableTypeInfo; 50 import java.lang.classfile.attribute.LocalVariableTypeTableAttribute; 51 import java.lang.classfile.attribute.MethodParameterInfo; 52 import java.lang.classfile.attribute.MethodParametersAttribute; 53 import java.lang.classfile.attribute.ModuleAttribute; 54 import java.lang.classfile.attribute.ModuleExportInfo; 55 import java.lang.classfile.attribute.ModuleHashInfo; 56 import java.lang.classfile.attribute.ModuleHashesAttribute; 57 import java.lang.classfile.attribute.ModuleMainClassAttribute; 58 import java.lang.classfile.attribute.ModuleOpenInfo; 59 import java.lang.classfile.attribute.ModulePackagesAttribute; 60 import java.lang.classfile.attribute.ModuleProvideInfo; 61 import java.lang.classfile.attribute.ModuleRequireInfo; 62 import java.lang.classfile.attribute.ModuleResolutionAttribute; 63 import java.lang.classfile.attribute.ModuleTargetAttribute; 64 import java.lang.classfile.attribute.NestHostAttribute; 65 import java.lang.classfile.attribute.NestMembersAttribute; 66 import java.lang.classfile.attribute.PermittedSubclassesAttribute; 67 import java.lang.classfile.attribute.PreloadAttribute; 68 import java.lang.classfile.attribute.RecordAttribute; 69 import java.lang.classfile.attribute.RecordComponentInfo; 70 import java.lang.classfile.attribute.RuntimeInvisibleAnnotationsAttribute; 71 import java.lang.classfile.attribute.RuntimeInvisibleParameterAnnotationsAttribute; 72 import java.lang.classfile.attribute.RuntimeInvisibleTypeAnnotationsAttribute; 73 import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute; 74 import java.lang.classfile.attribute.RuntimeVisibleParameterAnnotationsAttribute; 75 import java.lang.classfile.attribute.RuntimeVisibleTypeAnnotationsAttribute; 76 import java.lang.classfile.attribute.SignatureAttribute; 77 import java.lang.classfile.attribute.SourceDebugExtensionAttribute; 78 import java.lang.classfile.attribute.SourceFileAttribute; 79 import java.lang.classfile.attribute.SourceIDAttribute; 80 import java.lang.classfile.attribute.StackMapTableAttribute; 81 import java.lang.classfile.attribute.SyntheticAttribute; 82 import java.lang.classfile.constantpool.Utf8Entry; 83 import jdk.internal.classfile.impl.AbstractAttributeMapper; 84 import jdk.internal.classfile.impl.BoundAttribute; 85 import jdk.internal.classfile.impl.CodeImpl; 86 import jdk.internal.classfile.impl.AbstractPoolEntry; 87 import jdk.internal.classfile.impl.StackMapDecoder; 88 import jdk.internal.javac.PreviewFeature; 89 90 /** 91 * Attribute mappers for standard classfile attributes. 92 * 93 * @see AttributeMapper 94 * 95 * @since 22 96 */ 97 @PreviewFeature(feature = PreviewFeature.Feature.CLASSFILE_API) 98 public class Attributes { 99 100 /** AnnotationDefault */ 101 public static final String NAME_ANNOTATION_DEFAULT = "AnnotationDefault"; 102 103 /** BootstrapMethods */ 104 public static final String NAME_BOOTSTRAP_METHODS = "BootstrapMethods"; 105 106 /** CharacterRangeTable */ 107 public static final String NAME_CHARACTER_RANGE_TABLE = "CharacterRangeTable"; 108 109 /** Code */ 110 public static final String NAME_CODE = "Code"; 111 112 /** CompilationID */ 113 public static final String NAME_COMPILATION_ID = "CompilationID"; 114 115 /** ConstantValue */ 116 public static final String NAME_CONSTANT_VALUE = "ConstantValue"; 117 118 /** Deprecated */ 119 public static final String NAME_DEPRECATED = "Deprecated"; 120 121 /** EnclosingMethod */ 122 public static final String NAME_ENCLOSING_METHOD = "EnclosingMethod"; 123 124 /** Exceptions */ 125 public static final String NAME_EXCEPTIONS = "Exceptions"; 126 127 /** InnerClasses */ 128 public static final String NAME_INNER_CLASSES = "InnerClasses"; 129 130 /** LineNumberTable */ 131 public static final String NAME_LINE_NUMBER_TABLE = "LineNumberTable"; 132 133 /** LocalVariableTable */ 134 public static final String NAME_LOCAL_VARIABLE_TABLE = "LocalVariableTable"; 135 136 /** LocalVariableTypeTable */ 137 public static final String NAME_LOCAL_VARIABLE_TYPE_TABLE = "LocalVariableTypeTable"; 138 139 /** MethodParameters */ 140 public static final String NAME_METHOD_PARAMETERS = "MethodParameters"; 141 142 /** Module */ 143 public static final String NAME_MODULE = "Module"; 144 145 /** ModuleHashes */ 146 public static final String NAME_MODULE_HASHES = "ModuleHashes"; 147 148 /** ModuleMainClass */ 149 public static final String NAME_MODULE_MAIN_CLASS = "ModuleMainClass"; 150 151 /** ModulePackages */ 152 public static final String NAME_MODULE_PACKAGES = "ModulePackages"; 153 154 /** ModuleResolution */ 155 public static final String NAME_MODULE_RESOLUTION = "ModuleResolution"; 156 157 /** ModuleTarget */ 158 public static final String NAME_MODULE_TARGET = "ModuleTarget"; 159 160 /** NestHost */ 161 public static final String NAME_NEST_HOST = "NestHost"; 162 163 /** NestMembers */ 164 public static final String NAME_NEST_MEMBERS = "NestMembers"; 165 166 /** PermittedSubclasses */ 167 public static final String NAME_PERMITTED_SUBCLASSES = "PermittedSubclasses"; 168 169 /** Preload */ 170 public static final String NAME_PRELOAD = "Preload"; 171 172 /** Record */ 173 public static final String NAME_RECORD = "Record"; 174 175 /** RuntimeInvisibleAnnotations */ 176 public static final String NAME_RUNTIME_INVISIBLE_ANNOTATIONS = "RuntimeInvisibleAnnotations"; 177 178 /** RuntimeInvisibleParameterAnnotations */ 179 public static final String NAME_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = "RuntimeInvisibleParameterAnnotations"; 180 181 /** RuntimeInvisibleTypeAnnotations */ 182 public static final String NAME_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = "RuntimeInvisibleTypeAnnotations"; 183 184 /** RuntimeVisibleAnnotations */ 185 public static final String NAME_RUNTIME_VISIBLE_ANNOTATIONS = "RuntimeVisibleAnnotations"; 186 187 /** RuntimeVisibleParameterAnnotations */ 188 public static final String NAME_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = "RuntimeVisibleParameterAnnotations"; 189 190 /** RuntimeVisibleTypeAnnotations */ 191 public static final String NAME_RUNTIME_VISIBLE_TYPE_ANNOTATIONS = "RuntimeVisibleTypeAnnotations"; 192 193 /** Signature */ 194 public static final String NAME_SIGNATURE = "Signature"; 195 196 /** SourceDebugExtension */ 197 public static final String NAME_SOURCE_DEBUG_EXTENSION = "SourceDebugExtension"; 198 199 /** SourceFile */ 200 public static final String NAME_SOURCE_FILE = "SourceFile"; 201 202 /** SourceID */ 203 public static final String NAME_SOURCE_ID = "SourceID"; 204 205 /** StackMapTable */ 206 public static final String NAME_STACK_MAP_TABLE = "StackMapTable"; 207 208 /** Synthetic */ 209 public static final String NAME_SYNTHETIC = "Synthetic"; 210 211 private Attributes() { 212 } 213 214 /** Attribute mapper for the {@code AnnotationDefault} attribute */ 215 public static final AttributeMapper<AnnotationDefaultAttribute> 216 ANNOTATION_DEFAULT = new AbstractAttributeMapper<>(NAME_ANNOTATION_DEFAULT) { 217 @Override 218 public AnnotationDefaultAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 219 return new BoundAttribute.BoundAnnotationDefaultAttr(cf, this, p); 220 } 221 222 @Override 223 protected void writeBody(BufWriter buf, AnnotationDefaultAttribute attr) { 224 attr.defaultValue().writeTo(buf); 225 } 226 227 @Override 228 public AttributeMapper.AttributeStability stability() { 229 return AttributeStability.CP_REFS; 230 } 231 }; 232 233 /** Attribute mapper for the {@code BootstrapMethods} attribute */ 234 public static final AttributeMapper<BootstrapMethodsAttribute> 235 BOOTSTRAP_METHODS = new AbstractAttributeMapper<>(NAME_BOOTSTRAP_METHODS) { 236 @Override 237 public BootstrapMethodsAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 238 return new BoundAttribute.BoundBootstrapMethodsAttribute(cf, this, p); 239 } 240 241 @Override 242 protected void writeBody(BufWriter buf, BootstrapMethodsAttribute attr) { 243 buf.writeList(attr.bootstrapMethods()); 244 } 245 246 @Override 247 public AttributeMapper.AttributeStability stability() { 248 return AttributeStability.CP_REFS; 249 } 250 }; 251 252 /** Attribute mapper for the {@code CharacterRangeTable} attribute */ 253 public static final AttributeMapper<CharacterRangeTableAttribute> 254 CHARACTER_RANGE_TABLE = new AbstractAttributeMapper<>(NAME_CHARACTER_RANGE_TABLE, true) { 255 @Override 256 public CharacterRangeTableAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 257 return new BoundAttribute.BoundCharacterRangeTableAttribute(cf, this, p); 258 } 259 260 @Override 261 protected void writeBody(BufWriter buf, CharacterRangeTableAttribute attr) { 262 List<CharacterRangeInfo> ranges = attr.characterRangeTable(); 263 buf.writeU2(ranges.size()); 264 for (CharacterRangeInfo info : ranges) { 265 buf.writeU2(info.startPc()); 266 buf.writeU2(info.endPc()); 267 buf.writeInt(info.characterRangeStart()); 268 buf.writeInt(info.characterRangeEnd()); 269 buf.writeU2(info.flags()); 270 } 271 } 272 273 @Override 274 public AttributeMapper.AttributeStability stability() { 275 return AttributeStability.LABELS; 276 } 277 }; 278 279 /** Attribute mapper for the {@code Code} attribute */ 280 public static final AttributeMapper<CodeAttribute> 281 CODE = new AbstractAttributeMapper<>(NAME_CODE) { 282 @Override 283 public CodeAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 284 return new CodeImpl(e, cf, this, p); 285 } 286 287 @Override 288 protected void writeBody(BufWriter buf, CodeAttribute attr) { 289 throw new UnsupportedOperationException("Code attribute does not support direct write"); 290 } 291 292 @Override 293 public AttributeMapper.AttributeStability stability() { 294 return AttributeStability.CP_REFS; 295 } 296 }; 297 298 299 /** Attribute mapper for the {@code CompilationID} attribute */ 300 public static final AttributeMapper<CompilationIDAttribute> 301 COMPILATION_ID = new AbstractAttributeMapper<>(NAME_COMPILATION_ID, true) { 302 @Override 303 public CompilationIDAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 304 return new BoundAttribute.BoundCompilationIDAttribute(cf, this, p); 305 } 306 307 @Override 308 protected void writeBody(BufWriter buf, CompilationIDAttribute attr) { 309 buf.writeIndex(attr.compilationId()); 310 } 311 312 @Override 313 public AttributeMapper.AttributeStability stability() { 314 return AttributeStability.CP_REFS; 315 } 316 }; 317 318 /** Attribute mapper for the {@code ConstantValue} attribute */ 319 public static final AttributeMapper<ConstantValueAttribute> 320 CONSTANT_VALUE = new AbstractAttributeMapper<>(NAME_CONSTANT_VALUE) { 321 @Override 322 public ConstantValueAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 323 return new BoundAttribute.BoundConstantValueAttribute(cf, this, p); 324 } 325 326 @Override 327 protected void writeBody(BufWriter buf, ConstantValueAttribute attr) { 328 buf.writeIndex(attr.constant()); 329 } 330 331 @Override 332 public AttributeMapper.AttributeStability stability() { 333 return AttributeStability.CP_REFS; 334 } 335 }; 336 337 /** Attribute mapper for the {@code Deprecated} attribute */ 338 public static final AttributeMapper<DeprecatedAttribute> 339 DEPRECATED = new AbstractAttributeMapper<>(NAME_DEPRECATED, true) { 340 @Override 341 public DeprecatedAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 342 return new BoundAttribute.BoundDeprecatedAttribute(cf, this, p); 343 } 344 345 @Override 346 protected void writeBody(BufWriter buf, DeprecatedAttribute attr) { 347 // empty 348 } 349 350 @Override 351 public AttributeMapper.AttributeStability stability() { 352 return AttributeStability.STATELESS; 353 } 354 }; 355 356 /** Attribute mapper for the {@code EnclosingMethod} attribute */ 357 public static final AttributeMapper<EnclosingMethodAttribute> 358 ENCLOSING_METHOD = new AbstractAttributeMapper<>(NAME_ENCLOSING_METHOD) { 359 @Override 360 public EnclosingMethodAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 361 return new BoundAttribute.BoundEnclosingMethodAttribute(cf, this, p); 362 } 363 364 @Override 365 protected void writeBody(BufWriter buf, EnclosingMethodAttribute attr) { 366 buf.writeIndex(attr.enclosingClass()); 367 buf.writeIndexOrZero(attr.enclosingMethod().orElse(null)); 368 } 369 370 @Override 371 public AttributeMapper.AttributeStability stability() { 372 return AttributeStability.CP_REFS; 373 } 374 }; 375 376 /** Attribute mapper for the {@code Exceptions} attribute */ 377 public static final AttributeMapper<ExceptionsAttribute> 378 EXCEPTIONS = new AbstractAttributeMapper<>(NAME_EXCEPTIONS) { 379 @Override 380 public ExceptionsAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 381 return new BoundAttribute.BoundExceptionsAttribute(cf, this, p); 382 } 383 384 @Override 385 protected void writeBody(BufWriter buf, ExceptionsAttribute attr) { 386 buf.writeListIndices(attr.exceptions()); 387 } 388 389 @Override 390 public AttributeMapper.AttributeStability stability() { 391 return AttributeStability.CP_REFS; 392 } 393 }; 394 395 /** Attribute mapper for the {@code InnerClasses} attribute */ 396 public static final AttributeMapper<InnerClassesAttribute> 397 INNER_CLASSES = new AbstractAttributeMapper<>(NAME_INNER_CLASSES) { 398 @Override 399 public InnerClassesAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 400 return new BoundAttribute.BoundInnerClassesAttribute(cf, this, p); 401 } 402 403 @Override 404 protected void writeBody(BufWriter buf, InnerClassesAttribute attr) { 405 List<InnerClassInfo> classes = attr.classes(); 406 buf.writeU2(classes.size()); 407 for (InnerClassInfo ic : classes) { 408 buf.writeIndex(ic.innerClass()); 409 buf.writeIndexOrZero(ic.outerClass().orElse(null)); 410 buf.writeIndexOrZero(ic.innerName().orElse(null)); 411 buf.writeU2(ic.flagsMask()); 412 } 413 } 414 415 @Override 416 public AttributeMapper.AttributeStability stability() { 417 return AttributeStability.CP_REFS; 418 } 419 }; 420 421 /** Attribute mapper for the {@code LineNumberTable} attribute */ 422 public static final AttributeMapper<LineNumberTableAttribute> 423 LINE_NUMBER_TABLE = new AbstractAttributeMapper<>(NAME_LINE_NUMBER_TABLE, true) { 424 @Override 425 public LineNumberTableAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 426 return new BoundAttribute.BoundLineNumberTableAttribute(cf, this, p); 427 } 428 429 @Override 430 protected void writeBody(BufWriter buf, LineNumberTableAttribute attr) { 431 List<LineNumberInfo> lines = attr.lineNumbers(); 432 buf.writeU2(lines.size()); 433 for (LineNumberInfo line : lines) { 434 buf.writeU2(line.startPc()); 435 buf.writeU2(line.lineNumber()); 436 } 437 } 438 439 @Override 440 public AttributeMapper.AttributeStability stability() { 441 return AttributeStability.LABELS; 442 } 443 }; 444 445 /** Attribute mapper for the {@code LocalVariableTable} attribute */ 446 public static final AttributeMapper<LocalVariableTableAttribute> 447 LOCAL_VARIABLE_TABLE = new AbstractAttributeMapper<>(NAME_LOCAL_VARIABLE_TABLE, true) { 448 @Override 449 public LocalVariableTableAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 450 return new BoundAttribute.BoundLocalVariableTableAttribute(e, cf, this, p); 451 } 452 453 @Override 454 protected void writeBody(BufWriter buf, LocalVariableTableAttribute attr) { 455 List<LocalVariableInfo> infos = attr.localVariables(); 456 buf.writeU2(infos.size()); 457 for (LocalVariableInfo info : infos) { 458 buf.writeU2(info.startPc()); 459 buf.writeU2(info.length()); 460 buf.writeIndex(info.name()); 461 buf.writeIndex(info.type()); 462 buf.writeU2(info.slot()); 463 } 464 } 465 466 @Override 467 public AttributeMapper.AttributeStability stability() { 468 return AttributeStability.LABELS; 469 } 470 }; 471 472 /** Attribute mapper for the {@code LocalVariableTypeTable} attribute */ 473 public static final AttributeMapper<LocalVariableTypeTableAttribute> 474 LOCAL_VARIABLE_TYPE_TABLE = new AbstractAttributeMapper<>(NAME_LOCAL_VARIABLE_TYPE_TABLE, true) { 475 @Override 476 public LocalVariableTypeTableAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 477 return new BoundAttribute.BoundLocalVariableTypeTableAttribute(e, cf, this, p); 478 } 479 480 @Override 481 protected void writeBody(BufWriter buf, LocalVariableTypeTableAttribute attr) { 482 List<LocalVariableTypeInfo> infos = attr.localVariableTypes(); 483 buf.writeU2(infos.size()); 484 for (LocalVariableTypeInfo info : infos) { 485 buf.writeU2(info.startPc()); 486 buf.writeU2(info.length()); 487 buf.writeIndex(info.name()); 488 buf.writeIndex(info.signature()); 489 buf.writeU2(info.slot()); 490 } 491 } 492 493 @Override 494 public AttributeMapper.AttributeStability stability() { 495 return AttributeStability.LABELS; 496 } 497 }; 498 499 /** Attribute mapper for the {@code MethodParameters} attribute */ 500 public static final AttributeMapper<MethodParametersAttribute> 501 METHOD_PARAMETERS = new AbstractAttributeMapper<>(NAME_METHOD_PARAMETERS) { 502 @Override 503 public MethodParametersAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 504 return new BoundAttribute.BoundMethodParametersAttribute(cf, this, p); 505 } 506 507 @Override 508 protected void writeBody(BufWriter buf, MethodParametersAttribute attr) { 509 List<MethodParameterInfo> parameters = attr.parameters(); 510 buf.writeU1(parameters.size()); 511 for (MethodParameterInfo info : parameters) { 512 buf.writeIndexOrZero(info.name().orElse(null)); 513 buf.writeU2(info.flagsMask()); 514 } 515 } 516 517 @Override 518 public AttributeMapper.AttributeStability stability() { 519 return AttributeStability.CP_REFS; 520 } 521 }; 522 523 /** Attribute mapper for the {@code Module} attribute */ 524 public static final AttributeMapper<ModuleAttribute> 525 MODULE = new AbstractAttributeMapper<>(NAME_MODULE) { 526 @Override 527 public ModuleAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 528 return new BoundAttribute.BoundModuleAttribute(cf, this, p); 529 } 530 531 @Override 532 protected void writeBody(BufWriter buf, ModuleAttribute attr) { 533 buf.writeIndex(attr.moduleName()); 534 buf.writeU2(attr.moduleFlagsMask()); 535 buf.writeIndexOrZero(attr.moduleVersion().orElse(null)); 536 buf.writeU2(attr.requires().size()); 537 for (ModuleRequireInfo require : attr.requires()) { 538 buf.writeIndex(require.requires()); 539 buf.writeU2(require.requiresFlagsMask()); 540 buf.writeIndexOrZero(require.requiresVersion().orElse(null)); 541 } 542 buf.writeU2(attr.exports().size()); 543 for (ModuleExportInfo export : attr.exports()) { 544 buf.writeIndex(export.exportedPackage()); 545 buf.writeU2(export.exportsFlagsMask()); 546 buf.writeListIndices(export.exportsTo()); 547 } 548 buf.writeU2(attr.opens().size()); 549 for (ModuleOpenInfo open : attr.opens()) { 550 buf.writeIndex(open.openedPackage()); 551 buf.writeU2(open.opensFlagsMask()); 552 buf.writeListIndices(open.opensTo()); 553 } 554 buf.writeListIndices(attr.uses()); 555 buf.writeU2(attr.provides().size()); 556 for (ModuleProvideInfo provide : attr.provides()) { 557 buf.writeIndex(provide.provides()); 558 buf.writeListIndices(provide.providesWith()); 559 } 560 } 561 562 @Override 563 public AttributeMapper.AttributeStability stability() { 564 return AttributeStability.CP_REFS; 565 } 566 }; 567 568 /** Attribute mapper for the {@code ModuleHashes} attribute */ 569 public static final AttributeMapper<ModuleHashesAttribute> 570 MODULE_HASHES = new AbstractAttributeMapper<>(NAME_MODULE_HASHES) { 571 @Override 572 public ModuleHashesAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 573 return new BoundAttribute.BoundModuleHashesAttribute(cf, this, p); 574 } 575 576 @Override 577 protected void writeBody(BufWriter buf, ModuleHashesAttribute attr) { 578 buf.writeIndex(attr.algorithm()); 579 List<ModuleHashInfo> hashes = attr.hashes(); 580 buf.writeU2(hashes.size()); 581 for (ModuleHashInfo hash : hashes) { 582 buf.writeIndex(hash.moduleName()); 583 buf.writeU2(hash.hash().length); 584 buf.writeBytes(hash.hash()); 585 } 586 } 587 588 @Override 589 public AttributeMapper.AttributeStability stability() { 590 return AttributeStability.CP_REFS; 591 } 592 }; 593 594 /** Attribute mapper for the {@code ModuleMainClass} attribute */ 595 public static final AttributeMapper<ModuleMainClassAttribute> 596 MODULE_MAIN_CLASS = new AbstractAttributeMapper<>(NAME_MODULE_MAIN_CLASS) { 597 @Override 598 public ModuleMainClassAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 599 return new BoundAttribute.BoundModuleMainClassAttribute(cf, this, p); 600 } 601 602 @Override 603 protected void writeBody(BufWriter buf, ModuleMainClassAttribute attr) { 604 buf.writeIndex(attr.mainClass()); 605 } 606 607 @Override 608 public AttributeMapper.AttributeStability stability() { 609 return AttributeStability.CP_REFS; 610 } 611 }; 612 613 /** Attribute mapper for the {@code ModulePackages} attribute */ 614 public static final AttributeMapper<ModulePackagesAttribute> 615 MODULE_PACKAGES = new AbstractAttributeMapper<>(NAME_MODULE_PACKAGES) { 616 @Override 617 public ModulePackagesAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 618 return new BoundAttribute.BoundModulePackagesAttribute(cf, this, p); 619 } 620 621 @Override 622 protected void writeBody(BufWriter buf, ModulePackagesAttribute attr) { 623 buf.writeListIndices(attr.packages()); 624 } 625 626 @Override 627 public AttributeMapper.AttributeStability stability() { 628 return AttributeStability.CP_REFS; 629 } 630 }; 631 632 /** Attribute mapper for the {@code ModuleResolution} attribute */ 633 public static final AttributeMapper<ModuleResolutionAttribute> 634 MODULE_RESOLUTION = new AbstractAttributeMapper<>(NAME_MODULE_RESOLUTION) { 635 @Override 636 public ModuleResolutionAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 637 return new BoundAttribute.BoundModuleResolutionAttribute(cf, this, p); 638 } 639 640 @Override 641 protected void writeBody(BufWriter buf, ModuleResolutionAttribute attr) { 642 buf.writeU2(attr.resolutionFlags()); 643 } 644 645 @Override 646 public AttributeMapper.AttributeStability stability() { 647 return AttributeStability.STATELESS; 648 } 649 }; 650 651 /** Attribute mapper for the {@code ModuleTarget} attribute */ 652 public static final AttributeMapper<ModuleTargetAttribute> 653 MODULE_TARGET = new AbstractAttributeMapper<>(NAME_MODULE_TARGET) { 654 @Override 655 public ModuleTargetAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 656 return new BoundAttribute.BoundModuleTargetAttribute(cf, this, p); 657 } 658 659 @Override 660 protected void writeBody(BufWriter buf, ModuleTargetAttribute attr) { 661 buf.writeIndex(attr.targetPlatform()); 662 } 663 664 @Override 665 public AttributeMapper.AttributeStability stability() { 666 return AttributeStability.CP_REFS; 667 } 668 }; 669 670 /** Attribute mapper for the {@code NestHost} attribute */ 671 public static final AttributeMapper<NestHostAttribute> 672 NEST_HOST = new AbstractAttributeMapper<>(NAME_NEST_HOST) { 673 @Override 674 public NestHostAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 675 return new BoundAttribute.BoundNestHostAttribute(cf, this, p); 676 } 677 678 @Override 679 protected void writeBody(BufWriter buf, NestHostAttribute attr) { 680 buf.writeIndex(attr.nestHost()); 681 } 682 683 @Override 684 public AttributeMapper.AttributeStability stability() { 685 return AttributeStability.CP_REFS; 686 } 687 }; 688 689 /** Attribute mapper for the {@code NestMembers} attribute */ 690 public static final AttributeMapper<NestMembersAttribute> 691 NEST_MEMBERS = new AbstractAttributeMapper<>(NAME_NEST_MEMBERS) { 692 @Override 693 public NestMembersAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 694 return new BoundAttribute.BoundNestMembersAttribute(cf, this, p); 695 } 696 697 @Override 698 protected void writeBody(BufWriter buf, NestMembersAttribute attr) { 699 buf.writeListIndices(attr.nestMembers()); 700 } 701 702 @Override 703 public AttributeMapper.AttributeStability stability() { 704 return AttributeStability.CP_REFS; 705 } 706 }; 707 708 /** Attribute mapper for the {@code PermittedSubclasses} attribute */ 709 public static final AttributeMapper<PermittedSubclassesAttribute> 710 PERMITTED_SUBCLASSES = new AbstractAttributeMapper<>(NAME_PERMITTED_SUBCLASSES) { 711 @Override 712 public PermittedSubclassesAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 713 return new BoundAttribute.BoundPermittedSubclassesAttribute(cf, this, p); 714 } 715 716 @Override 717 protected void writeBody(BufWriter buf, PermittedSubclassesAttribute attr) { 718 buf.writeListIndices(attr.permittedSubclasses()); 719 } 720 721 @Override 722 public AttributeMapper.AttributeStability stability() { 723 return AttributeStability.CP_REFS; 724 } 725 }; 726 727 /** Attribute mapper for the {@code Preload} attribute */ 728 public static final AttributeMapper<PreloadAttribute> 729 PRELOAD = new AbstractAttributeMapper<>(NAME_PRELOAD) { 730 @Override 731 public PreloadAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 732 return new BoundAttribute.BoundPreloadAttribute(cf, this, p); 733 } 734 735 @Override 736 protected void writeBody(BufWriter buf, PreloadAttribute attr) { 737 buf.writeListIndices(attr.preloads()); 738 } 739 740 @Override 741 public AttributeMapper.AttributeStability stability() { 742 return AttributeStability.CP_REFS; 743 } 744 }; 745 746 /** Attribute mapper for the {@code Record} attribute */ 747 public static final AttributeMapper<RecordAttribute> 748 RECORD = new AbstractAttributeMapper<>(NAME_RECORD) { 749 @Override 750 public RecordAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 751 return new BoundAttribute.BoundRecordAttribute(cf, this, p); 752 } 753 754 @Override 755 protected void writeBody(BufWriter buf, RecordAttribute attr) { 756 List<RecordComponentInfo> components = attr.components(); 757 buf.writeU2(components.size()); 758 for (RecordComponentInfo info : components) { 759 buf.writeIndex(info.name()); 760 buf.writeIndex(info.descriptor()); 761 buf.writeList(info.attributes()); 762 } 763 } 764 765 @Override 766 public AttributeMapper.AttributeStability stability() { 767 return AttributeStability.CP_REFS; 768 } 769 }; 770 771 /** Attribute mapper for the {@code RuntimeInvisibleAnnotations} attribute */ 772 public static final AttributeMapper<RuntimeInvisibleAnnotationsAttribute> 773 RUNTIME_INVISIBLE_ANNOTATIONS = new AbstractAttributeMapper<>(NAME_RUNTIME_INVISIBLE_ANNOTATIONS) { 774 @Override 775 public RuntimeInvisibleAnnotationsAttribute readAttribute(AttributedElement enclosing, ClassReader cf, int pos) { 776 return new BoundAttribute.BoundRuntimeInvisibleAnnotationsAttribute(cf, pos); 777 } 778 779 @Override 780 protected void writeBody(BufWriter buf, RuntimeInvisibleAnnotationsAttribute attr) { 781 buf.writeList(attr.annotations()); 782 } 783 784 @Override 785 public AttributeMapper.AttributeStability stability() { 786 return AttributeStability.CP_REFS; 787 } 788 }; 789 790 /** Attribute mapper for the {@code RuntimeInvisibleParameterAnnotations} attribute */ 791 public static final AttributeMapper<RuntimeInvisibleParameterAnnotationsAttribute> 792 RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS = new AbstractAttributeMapper<>(NAME_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS) { 793 @Override 794 public RuntimeInvisibleParameterAnnotationsAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 795 return new BoundAttribute.BoundRuntimeInvisibleParameterAnnotationsAttribute(cf, this, p); 796 } 797 798 @Override 799 protected void writeBody(BufWriter buf, RuntimeInvisibleParameterAnnotationsAttribute attr) { 800 List<List<Annotation>> lists = attr.parameterAnnotations(); 801 buf.writeU1(lists.size()); 802 for (List<Annotation> list : lists) 803 buf.writeList(list); 804 } 805 806 @Override 807 public AttributeMapper.AttributeStability stability() { 808 return AttributeStability.CP_REFS; 809 } 810 }; 811 812 /** Attribute mapper for the {@code RuntimeInvisibleTypeAnnotations} attribute */ 813 public static final AttributeMapper<RuntimeInvisibleTypeAnnotationsAttribute> 814 RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = new AbstractAttributeMapper<>(NAME_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS) { 815 @Override 816 public RuntimeInvisibleTypeAnnotationsAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 817 return new BoundAttribute.BoundRuntimeInvisibleTypeAnnotationsAttribute(e, cf, this, p); 818 } 819 820 @Override 821 protected void writeBody(BufWriter buf, RuntimeInvisibleTypeAnnotationsAttribute attr) { 822 buf.writeList(attr.annotations()); 823 } 824 825 @Override 826 public AttributeMapper.AttributeStability stability() { 827 return AttributeStability.UNSTABLE; 828 } 829 }; 830 831 /** Attribute mapper for the {@code RuntimeVisibleAnnotations} attribute */ 832 public static final AttributeMapper<RuntimeVisibleAnnotationsAttribute> 833 RUNTIME_VISIBLE_ANNOTATIONS = new AbstractAttributeMapper<>(NAME_RUNTIME_VISIBLE_ANNOTATIONS) { 834 @Override 835 public RuntimeVisibleAnnotationsAttribute readAttribute(AttributedElement enclosing, ClassReader cf, int pos) { 836 return new BoundAttribute.BoundRuntimeVisibleAnnotationsAttribute(cf, pos); 837 } 838 839 @Override 840 protected void writeBody(BufWriter buf, RuntimeVisibleAnnotationsAttribute attr) { 841 buf.writeList(attr.annotations()); 842 } 843 844 @Override 845 public AttributeMapper.AttributeStability stability() { 846 return AttributeStability.CP_REFS; 847 } 848 }; 849 850 /** Attribute mapper for the {@code RuntimeVisibleParameterAnnotations} attribute */ 851 public static final AttributeMapper<RuntimeVisibleParameterAnnotationsAttribute> 852 RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS = new AbstractAttributeMapper<>(NAME_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS) { 853 @Override 854 public RuntimeVisibleParameterAnnotationsAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 855 return new BoundAttribute.BoundRuntimeVisibleParameterAnnotationsAttribute(cf, this, p); 856 } 857 858 @Override 859 protected void writeBody(BufWriter buf, RuntimeVisibleParameterAnnotationsAttribute attr) { 860 List<List<Annotation>> lists = attr.parameterAnnotations(); 861 buf.writeU1(lists.size()); 862 for (List<Annotation> list : lists) 863 buf.writeList(list); 864 } 865 866 @Override 867 public AttributeMapper.AttributeStability stability() { 868 return AttributeStability.CP_REFS; 869 } 870 }; 871 872 /** Attribute mapper for the {@code RuntimeVisibleTypeAnnotations} attribute */ 873 public static final AttributeMapper<RuntimeVisibleTypeAnnotationsAttribute> 874 RUNTIME_VISIBLE_TYPE_ANNOTATIONS = new AbstractAttributeMapper<>(NAME_RUNTIME_VISIBLE_TYPE_ANNOTATIONS) { 875 @Override 876 public RuntimeVisibleTypeAnnotationsAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 877 return new BoundAttribute.BoundRuntimeVisibleTypeAnnotationsAttribute(e, cf, this, p); 878 } 879 880 @Override 881 protected void writeBody(BufWriter buf, RuntimeVisibleTypeAnnotationsAttribute attr) { 882 buf.writeList(attr.annotations()); 883 } 884 885 @Override 886 public AttributeMapper.AttributeStability stability() { 887 return AttributeStability.UNSTABLE; 888 } 889 }; 890 891 /** Attribute mapper for the {@code Signature} attribute */ 892 public static final AttributeMapper<SignatureAttribute> 893 SIGNATURE = new AbstractAttributeMapper<>(NAME_SIGNATURE) { 894 @Override 895 public SignatureAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 896 return new BoundAttribute.BoundSignatureAttribute(cf, this, p); 897 } 898 899 @Override 900 protected void writeBody(BufWriter buf, SignatureAttribute attr) { 901 buf.writeIndex(attr.signature()); 902 } 903 904 @Override 905 public AttributeMapper.AttributeStability stability() { 906 return AttributeStability.CP_REFS; 907 } 908 }; 909 910 /** Attribute mapper for the {@code SourceDebugExtension} attribute */ 911 public static final AttributeMapper<SourceDebugExtensionAttribute> 912 SOURCE_DEBUG_EXTENSION = new AbstractAttributeMapper<>(NAME_SOURCE_DEBUG_EXTENSION) { 913 @Override 914 public SourceDebugExtensionAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 915 return new BoundAttribute.BoundSourceDebugExtensionAttribute(cf, this, p); 916 } 917 918 @Override 919 protected void writeBody(BufWriter buf, SourceDebugExtensionAttribute attr) { 920 buf.writeBytes(attr.contents()); 921 } 922 923 @Override 924 public AttributeMapper.AttributeStability stability() { 925 return AttributeStability.STATELESS; 926 } 927 }; 928 929 /** Attribute mapper for the {@code SourceFile} attribute */ 930 public static final AttributeMapper<SourceFileAttribute> 931 SOURCE_FILE = new AbstractAttributeMapper<>(NAME_SOURCE_FILE) { 932 @Override 933 public SourceFileAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 934 return new BoundAttribute.BoundSourceFileAttribute(cf, this, p); 935 } 936 937 @Override 938 protected void writeBody(BufWriter buf, SourceFileAttribute attr) { 939 buf.writeIndex(attr.sourceFile()); 940 } 941 942 @Override 943 public AttributeMapper.AttributeStability stability() { 944 return AttributeStability.CP_REFS; 945 } 946 }; 947 948 /** Attribute mapper for the {@code SourceID} attribute */ 949 public static final AttributeMapper<SourceIDAttribute> 950 SOURCE_ID = new AbstractAttributeMapper<>(NAME_SOURCE_ID) { 951 @Override 952 public SourceIDAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 953 return new BoundAttribute.BoundSourceIDAttribute(cf, this, p); 954 } 955 956 @Override 957 protected void writeBody(BufWriter buf, SourceIDAttribute attr) { 958 buf.writeIndex(attr.sourceId()); 959 } 960 961 @Override 962 public AttributeMapper.AttributeStability stability() { 963 return AttributeStability.CP_REFS; 964 } 965 }; 966 967 /** Attribute mapper for the {@code StackMapTable} attribute */ 968 public static final AttributeMapper<StackMapTableAttribute> 969 STACK_MAP_TABLE = new AbstractAttributeMapper<>(NAME_STACK_MAP_TABLE) { 970 @Override 971 public StackMapTableAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 972 return new BoundAttribute.BoundStackMapTableAttribute((CodeImpl)e, cf, this, p); 973 } 974 975 @Override 976 protected void writeBody(BufWriter b, StackMapTableAttribute attr) { 977 StackMapDecoder.writeFrames(b, attr.entries()); 978 } 979 980 @Override 981 public AttributeMapper.AttributeStability stability() { 982 return AttributeStability.LABELS; 983 } 984 }; 985 986 987 /** Attribute mapper for the {@code Synthetic} attribute */ 988 public static final AttributeMapper<SyntheticAttribute> 989 SYNTHETIC = new AbstractAttributeMapper<>(NAME_SYNTHETIC, true) { 990 @Override 991 public SyntheticAttribute readAttribute(AttributedElement e, ClassReader cf, int p) { 992 return new BoundAttribute.BoundSyntheticAttribute(cf, this, p); 993 } 994 995 @Override 996 protected void writeBody(BufWriter buf, SyntheticAttribute attr) { 997 // empty 998 } 999 1000 @Override 1001 public AttributeMapper.AttributeStability stability() { 1002 return AttributeStability.STATELESS; 1003 } 1004 }; 1005 1006 /** 1007 * {@return the attribute mapper for a standard attribute} 1008 * 1009 * @param name the name of the attribute to find 1010 */ 1011 public static AttributeMapper<?> standardAttribute(Utf8Entry name) { 1012 return _ATTR_MAP.get(name); 1013 } 1014 1015 /** 1016 * All standard attribute mappers. 1017 */ 1018 public static final Set<AttributeMapper<?>> PREDEFINED_ATTRIBUTES = Set.of( 1019 ANNOTATION_DEFAULT, 1020 BOOTSTRAP_METHODS, 1021 CHARACTER_RANGE_TABLE, 1022 CODE, 1023 COMPILATION_ID, 1024 CONSTANT_VALUE, 1025 DEPRECATED, 1026 ENCLOSING_METHOD, 1027 EXCEPTIONS, 1028 INNER_CLASSES, 1029 LINE_NUMBER_TABLE, 1030 LOCAL_VARIABLE_TABLE, 1031 LOCAL_VARIABLE_TYPE_TABLE, 1032 METHOD_PARAMETERS, 1033 MODULE, 1034 MODULE_HASHES, 1035 MODULE_MAIN_CLASS, 1036 MODULE_PACKAGES, 1037 MODULE_RESOLUTION, 1038 MODULE_TARGET, 1039 NEST_HOST, 1040 NEST_MEMBERS, 1041 PERMITTED_SUBCLASSES, 1042 PRELOAD, 1043 RECORD, 1044 RUNTIME_INVISIBLE_ANNOTATIONS, 1045 RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS, 1046 RUNTIME_INVISIBLE_TYPE_ANNOTATIONS, 1047 RUNTIME_VISIBLE_ANNOTATIONS, 1048 RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS, 1049 RUNTIME_VISIBLE_TYPE_ANNOTATIONS, 1050 SIGNATURE, 1051 SOURCE_DEBUG_EXTENSION, 1052 SOURCE_FILE, 1053 SOURCE_ID, 1054 STACK_MAP_TABLE, 1055 SYNTHETIC); 1056 1057 private static final Map<Utf8Entry, AttributeMapper<?>> _ATTR_MAP; 1058 //no lambdas here as this is on critical JDK boostrap path 1059 static { 1060 var map = new HashMap<Utf8Entry, AttributeMapper<?>>(64); 1061 for (var am : PREDEFINED_ATTRIBUTES) { 1062 map.put(AbstractPoolEntry.rawUtf8EntryFromStandardAttributeName(am.name()), am); 1063 } 1064 _ATTR_MAP = Collections.unmodifiableMap(map); 1065 } 1066 }