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 
  26 package jdk.internal.classfile.impl;
  27 
  28 import java.lang.classfile.*;
  29 import java.lang.classfile.attribute.*;
  30 import java.lang.classfile.constantpool.*;
  31 import java.util.ArrayList;
  32 import java.util.Collections;
  33 import java.util.List;
  34 import java.util.Objects;
  35 import java.util.Optional;
  36 import java.util.function.Function;
  37 
  38 import jdk.internal.access.SharedSecrets;
  39 
  40 import static java.lang.classfile.Attributes.*;
  41 
  42 public abstract sealed class BoundAttribute<T extends Attribute<T>>
  43         extends AbstractElement
  44         implements Attribute<T>, Util.Writable {
  45 
  46     static final int NAME_AND_LENGTH_PREFIX = 6;
  47     private final AttributeMapper<T> mapper;
  48     final ClassReaderImpl classReader;
  49     final int payloadStart;
  50     Utf8Entry name;
  51 
  52     BoundAttribute(ClassReader classReader, AttributeMapper<T> mapper, int payloadStart) {
  53         this.mapper = mapper;
  54         this.classReader = (ClassReaderImpl)classReader;
  55         this.payloadStart = payloadStart;
  56     }
  57 
  58     public int payloadLen() {
  59         return classReader.readInt(payloadStart - 4);
  60     }
  61 
  62     @Override
  63     public Utf8Entry attributeName() {
  64         if (name == null) {
  65             name = classReader.readEntry(payloadStart - 6, Utf8Entry.class);
  66         }
  67         return name;
  68     }
  69 
  70     @Override
  71     public AttributeMapper<T> attributeMapper() {
  72         return mapper;
  73     }
  74 
  75     public byte[] contents() {
  76         return classReader.readBytes(payloadStart, payloadLen());
  77     }
  78 
  79     @Override
  80     public void writeTo(DirectClassBuilder builder) {
  81         builder.writeAttribute(this);
  82     }
  83 
  84     @Override
  85     public void writeTo(DirectCodeBuilder builder) {
  86         builder.writeAttribute(this);
  87     }
  88 
  89     @Override
  90     public void writeTo(DirectMethodBuilder builder) {
  91         builder.writeAttribute(this);
  92     }
  93 
  94     @Override
  95     public void writeTo(DirectFieldBuilder builder) {
  96         builder.writeAttribute(this);
  97     }
  98 
  99     @Override
 100     @SuppressWarnings("unchecked")
 101     public void writeTo(BufWriterImpl buf) {
 102         if (!buf.canWriteDirect(classReader))
 103             attributeMapper().writeAttribute(buf, (T) this);
 104         else
 105             classReader.copyBytesTo(buf, payloadStart - NAME_AND_LENGTH_PREFIX, payloadLen() + NAME_AND_LENGTH_PREFIX);
 106     }
 107 
 108     public ConstantPool constantPool() {
 109         return classReader;
 110     }
 111 
 112     @Override
 113     public String toString() {
 114         return String.format("Attribute[name=%s]", mapper.name());
 115     }
 116 
 117     <E extends PoolEntry> List<E> readEntryList(int p, Class<E> type) {
 118         int cnt = classReader.readU2(p);
 119         p += 2;
 120         var entries = new Object[cnt];
 121         int end = p + (cnt * 2);
 122         for (int i = 0; p < end; i++, p += 2) {
 123             entries[i] = classReader.readEntry(p, type);
 124         }
 125         return SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(entries);
 126     }
 127 
 128     public static List<Attribute<?>> readAttributes(AttributedElement enclosing, ClassReader reader, int pos,
 129                                                                   Function<Utf8Entry, AttributeMapper<?>> customAttributes) {
 130         int size = reader.readU2(pos);
 131         var filled = new ArrayList<Attribute<?>>(size);
 132         int p = pos + 2;
 133         int cfLen = reader.classfileLength();
 134         for (int i = 0; i < size; ++i) {
 135             Utf8Entry name = reader.readEntry(p, Utf8Entry.class);
 136             int len = reader.readInt(p + 2);
 137             p += 6;
 138             if (len < 0 || len > cfLen - p) {
 139                 throw new IllegalArgumentException("attribute " + name.stringValue() + " too big to handle");
 140             }
 141 
 142             var mapper = standardAttribute(name);
 143             if (mapper == null) {
 144                 mapper = customAttributes.apply(name);
 145             }
 146             if (mapper != null) {
 147                 filled.add(Objects.requireNonNull(mapper.readAttribute(enclosing, reader, p)));
 148             } else {
 149                 AttributeMapper<UnknownAttribute> fakeMapper = new AttributeMapper<>() {
 150                     @Override
 151                     public String name() {
 152                         return name.stringValue();
 153                     }
 154 
 155                     @Override
 156                     public UnknownAttribute readAttribute(AttributedElement enclosing, ClassReader cf, int pos) {
 157                         // Will never get called
 158                         throw new UnsupportedOperationException();
 159                     }
 160 
 161                     @Override
 162                     public void writeAttribute(BufWriter buf, UnknownAttribute attr) {
 163                         buf.writeIndex(name);
 164                         var cont = attr.contents();
 165                         buf.writeInt(cont.length);
 166                         buf.writeBytes(cont);
 167                     }
 168 
 169                     @Override
 170                     public boolean allowMultiple() {
 171                         return true;
 172                     }
 173 
 174                     @Override
 175                     public AttributeMapper.AttributeStability stability() {
 176                         return AttributeStability.UNKNOWN;
 177                     }
 178                 };
 179                 filled.add(new BoundUnknownAttribute(reader, fakeMapper, p));
 180             }
 181             p += len;
 182         }
 183         return Collections.unmodifiableList(filled);
 184     }
 185 
 186     public static final class BoundUnknownAttribute extends BoundAttribute<UnknownAttribute>
 187             implements UnknownAttribute {
 188         public BoundUnknownAttribute(ClassReader cf, AttributeMapper<UnknownAttribute> mapper, int pos) {
 189             super(cf, mapper, pos);
 190         }
 191     }
 192 
 193     public static final class BoundStackMapTableAttribute
 194             extends BoundAttribute<StackMapTableAttribute>
 195             implements StackMapTableAttribute {
 196         final MethodModel method;
 197         final LabelContext ctx;
 198         List<StackMapFrameInfo> entries = null;
 199 
 200         public BoundStackMapTableAttribute(CodeImpl code, ClassReader cf, AttributeMapper<StackMapTableAttribute> mapper, int pos) {
 201             super(cf, mapper, pos);
 202             method = code.parent().orElseThrow();
 203             ctx = code;
 204         }
 205 
 206         @Override
 207         public List<StackMapFrameInfo> entries() {
 208             if (entries == null) {
 209                 entries = new StackMapDecoder(classReader, payloadStart, ctx, StackMapDecoder.initFrameLocals(method)).entries();
 210             }
 211             return entries;
 212         }
 213     }
 214 
 215     public static final class BoundSyntheticAttribute extends BoundAttribute<SyntheticAttribute>
 216             implements SyntheticAttribute {
 217         public BoundSyntheticAttribute(ClassReader cf, AttributeMapper<SyntheticAttribute> mapper, int pos) {
 218             super(cf, mapper, pos);
 219         }
 220     }
 221 
 222     public static final class BoundLineNumberTableAttribute
 223             extends BoundAttribute<LineNumberTableAttribute>
 224             implements LineNumberTableAttribute {
 225         private List<LineNumberInfo> lineNumbers = null;
 226 
 227         public BoundLineNumberTableAttribute(ClassReader cf, AttributeMapper<LineNumberTableAttribute> mapper, int pos) {
 228             super(cf, mapper, pos);
 229         }
 230 
 231         @Override
 232         public List<LineNumberInfo> lineNumbers() {
 233             if (lineNumbers == null) {
 234                 int nLn = classReader.readU2(payloadStart);
 235                 LineNumberInfo[] elements = new LineNumberInfo[nLn];
 236                 int p = payloadStart + 2;
 237                 int pEnd = p + (nLn * 4);
 238                 for (int i = 0; p < pEnd; p += 4, i++) {
 239                     int startPc = classReader.readU2(p);
 240                     int lineNumber = classReader.readU2(p + 2);
 241                     elements[i] = LineNumberInfo.of(startPc, lineNumber);
 242                 }
 243                 lineNumbers = List.of(elements);
 244             }
 245             return lineNumbers;
 246         }
 247     }
 248 
 249     public static final class BoundCharacterRangeTableAttribute extends BoundAttribute<CharacterRangeTableAttribute> implements CharacterRangeTableAttribute {
 250         private List<CharacterRangeInfo> characterRangeTable = null;
 251 
 252         public BoundCharacterRangeTableAttribute(ClassReader cf, AttributeMapper<CharacterRangeTableAttribute> mapper, int pos) {
 253             super(cf, mapper, pos);
 254         }
 255 
 256         @Override
 257         public List<CharacterRangeInfo> characterRangeTable() {
 258             if (characterRangeTable == null) {
 259                 int nLn = classReader.readU2(payloadStart);
 260                 CharacterRangeInfo[] elements = new CharacterRangeInfo[nLn];
 261                 int p = payloadStart + 2;
 262                 int pEnd = p + (nLn * 14);
 263                 for (int i = 0; p < pEnd; p += 14, i++) {
 264                     int startPc = classReader.readU2(p);
 265                     int endPc = classReader.readU2(p + 2);
 266                     int characterRangeStart = classReader.readInt(p + 4);
 267                     int characterRangeEnd = classReader.readInt(p + 8);
 268                     int flags = classReader.readU2(p + 12);
 269                     elements[i] = CharacterRangeInfo.of(startPc, endPc, characterRangeStart, characterRangeEnd, flags);
 270                 }
 271                 characterRangeTable = List.of(elements);
 272             }
 273             return characterRangeTable;
 274         }
 275     }
 276 
 277     public static final class BoundLocalVariableTableAttribute
 278             extends BoundAttribute<LocalVariableTableAttribute>
 279             implements LocalVariableTableAttribute {
 280         private final CodeImpl codeAttribute;
 281         private List<LocalVariableInfo> localVars = null;
 282 
 283         public BoundLocalVariableTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<LocalVariableTableAttribute> mapper, int pos) {
 284             super(cf, mapper, pos);
 285             if (enclosing instanceof CodeImpl ci) {
 286                 this.codeAttribute = ci;
 287             } else {
 288                 throw new IllegalArgumentException("Invalid LocalVariableTable attribute location");
 289             }
 290         }
 291 
 292         @Override
 293         public List<LocalVariableInfo> localVariables() {
 294             if (localVars == null) {
 295                 int cnt = classReader.readU2(payloadStart);
 296                 BoundLocalVariable[] elements = new BoundLocalVariable[cnt];
 297                 int p = payloadStart + 2;
 298                 int pEnd = p + (cnt * 10);
 299                 for (int i = 0; p < pEnd; p += 10, i++) {
 300                     elements[i] = new BoundLocalVariable(codeAttribute, p);
 301                 }
 302                 localVars = List.of(elements);
 303             }
 304             return localVars;
 305         }
 306     }
 307 
 308     public static final class BoundLocalVariableTypeTableAttribute
 309             extends BoundAttribute<LocalVariableTypeTableAttribute>
 310             implements LocalVariableTypeTableAttribute {
 311         private final CodeImpl codeAttribute;
 312         private List<LocalVariableTypeInfo> localVars = null;
 313 
 314         public BoundLocalVariableTypeTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<LocalVariableTypeTableAttribute> mapper, int pos) {
 315             super(cf, mapper, pos);
 316             if (enclosing instanceof CodeImpl ci) {
 317                 this.codeAttribute = ci;
 318             } else {
 319                 throw new IllegalArgumentException("Invalid LocalVariableTypeTable attribute location");
 320             }
 321         }
 322 
 323         @Override
 324         public List<LocalVariableTypeInfo> localVariableTypes() {
 325             if (localVars == null) {
 326                 final int cnt = classReader.readU2(payloadStart);
 327                 BoundLocalVariableType[] elements = new BoundLocalVariableType[cnt];
 328                 int p = payloadStart + 2;
 329                 int pEnd = p + (cnt * 10);
 330                 for (int i = 0; p < pEnd; p += 10, i++) {
 331                     elements[i] = new BoundLocalVariableType(codeAttribute, p);
 332                 }
 333                 localVars = List.of(elements);
 334             }
 335             return localVars;
 336         }
 337     }
 338 
 339     public static final class BoundMethodParametersAttribute extends BoundAttribute<MethodParametersAttribute>
 340             implements MethodParametersAttribute {
 341         private List<MethodParameterInfo> parameters = null;
 342 
 343         public BoundMethodParametersAttribute(ClassReader cf, AttributeMapper<MethodParametersAttribute> mapper, int pos) {
 344             super(cf, mapper, pos);
 345         }
 346 
 347         @Override
 348         public List<MethodParameterInfo> parameters() {
 349             if (parameters == null) {
 350                 final int cnt = classReader.readU1(payloadStart);
 351                 MethodParameterInfo[] elements = new MethodParameterInfo[cnt];
 352                 int p = payloadStart + 1;
 353                 int pEnd = p + (cnt * 4);
 354                 for (int i = 0; p < pEnd; p += 4, i++) {
 355                     Utf8Entry name = classReader.readEntryOrNull(p, Utf8Entry.class);
 356                     int accessFlags = classReader.readU2(p + 2);
 357                     elements[i] = MethodParameterInfo.of(Optional.ofNullable(name), accessFlags);
 358                 }
 359                 parameters = List.of(elements);
 360             }
 361             return parameters;
 362         }
 363     }
 364 
 365     public static final class BoundModuleHashesAttribute extends BoundAttribute<ModuleHashesAttribute>
 366             implements ModuleHashesAttribute {
 367         private List<ModuleHashInfo> hashes = null;
 368 
 369         public BoundModuleHashesAttribute(ClassReader cf, AttributeMapper<ModuleHashesAttribute> mapper, int pos) {
 370             super(cf, mapper, pos);
 371         }
 372 
 373         @Override
 374         public Utf8Entry algorithm() {
 375             return classReader.readEntry(payloadStart, Utf8Entry.class);
 376         }
 377 
 378         @Override
 379         public List<ModuleHashInfo> hashes() {
 380             if (hashes == null) {
 381                 final int cnt = classReader.readU2(payloadStart + 2);
 382                 ModuleHashInfo[] elements = new ModuleHashInfo[cnt];
 383                 int p = payloadStart + 4;
 384                 //System.err.printf("%5d: ModuleHashesAttr alg = %s, cnt = %d%n", pos, algorithm(), cnt);
 385                 for (int i = 0; i < cnt; ++i) {
 386                     ModuleEntry module = classReader.readEntry(p, ModuleEntry.class);
 387                     int hashLength = classReader.readU2(p + 2);
 388                     //System.err.printf("%5d:     [%d] module = %s, hashLength = %d%n", p, i, module, hashLength);
 389                     p += 4;
 390                     elements[i] = ModuleHashInfo.of(module, classReader.readBytes(p, hashLength));
 391                     p += hashLength;
 392                 }
 393                 hashes = List.of(elements);
 394             }
 395             return hashes;
 396         }
 397     }
 398 
 399     public static final class BoundRecordAttribute extends BoundAttribute<RecordAttribute>
 400             implements RecordAttribute {
 401         private List<RecordComponentInfo> components = null;
 402 
 403         public BoundRecordAttribute(ClassReader cf, AttributeMapper<RecordAttribute> mapper, int pos) {
 404             super(cf, mapper, pos);
 405         }
 406 
 407         @Override
 408         public List<RecordComponentInfo> components() {
 409             if (components == null) {
 410                 final int cnt = classReader.readU2(payloadStart);
 411                 RecordComponentInfo[] elements = new RecordComponentInfo[cnt];
 412                 int p = payloadStart + 2;
 413                 for (int i = 0; i < cnt; i++) {
 414                     elements[i] = new BoundRecordComponentInfo(classReader, p);
 415                     p = classReader.skipAttributeHolder(p + 4);
 416                 }
 417                 components = List.of(elements);
 418             }
 419             return components;
 420         }
 421     }
 422 
 423     public static final class BoundDeprecatedAttribute extends BoundAttribute<DeprecatedAttribute>
 424             implements DeprecatedAttribute {
 425         public BoundDeprecatedAttribute(ClassReader cf, AttributeMapper<DeprecatedAttribute> mapper, int pos) {
 426             super(cf, mapper, pos);
 427         }
 428     }
 429 
 430     public static final class BoundSignatureAttribute extends BoundAttribute<SignatureAttribute>
 431             implements SignatureAttribute {
 432         public BoundSignatureAttribute(ClassReader cf, AttributeMapper<SignatureAttribute> mapper, int pos) {
 433             super(cf, mapper, pos);
 434         }
 435 
 436         @Override
 437         public Utf8Entry signature() {
 438             return classReader.readEntry(payloadStart, Utf8Entry.class);
 439         }
 440     }
 441 
 442     public static final class BoundSourceFileAttribute extends BoundAttribute<SourceFileAttribute>
 443             implements SourceFileAttribute {
 444         public BoundSourceFileAttribute(ClassReader cf, AttributeMapper<SourceFileAttribute> mapper, int pos) {
 445             super(cf, mapper, pos);
 446         }
 447 
 448         @Override
 449         public Utf8Entry sourceFile() {
 450             return classReader.readEntry(payloadStart, Utf8Entry.class);
 451         }
 452 
 453     }
 454 
 455     public static final class BoundModuleMainClassAttribute extends BoundAttribute<ModuleMainClassAttribute> implements ModuleMainClassAttribute {
 456         public BoundModuleMainClassAttribute(ClassReader cf, AttributeMapper<ModuleMainClassAttribute> mapper, int pos) {
 457             super(cf, mapper, pos);
 458         }
 459 
 460         @Override
 461         public ClassEntry mainClass() {
 462             return classReader.readEntry(payloadStart, ClassEntry.class);
 463         }
 464     }
 465 
 466     public static final class BoundNestHostAttribute extends BoundAttribute<NestHostAttribute>
 467             implements NestHostAttribute {
 468         public BoundNestHostAttribute(ClassReader cf, AttributeMapper<NestHostAttribute> mapper, int pos) {
 469             super(cf, mapper, pos);
 470         }
 471 
 472         @Override
 473         public ClassEntry nestHost() {
 474             return classReader.readEntry(payloadStart, ClassEntry.class);
 475         }
 476     }
 477 
 478     public static final class BoundSourceDebugExtensionAttribute extends BoundAttribute<SourceDebugExtensionAttribute>
 479             implements SourceDebugExtensionAttribute {
 480         public BoundSourceDebugExtensionAttribute(ClassReader cf, AttributeMapper<SourceDebugExtensionAttribute> mapper, int pos) {
 481             super(cf, mapper, pos);
 482         }
 483     }
 484 
 485     public static final class BoundConstantValueAttribute extends BoundAttribute<ConstantValueAttribute>
 486             implements ConstantValueAttribute {
 487         public BoundConstantValueAttribute(ClassReader cf, AttributeMapper<ConstantValueAttribute> mapper, int pos) {
 488             super(cf, mapper, pos);
 489         }
 490 
 491         @Override
 492         public ConstantValueEntry constant() {
 493             return classReader.readEntry(payloadStart, ConstantValueEntry.class);
 494         }
 495 
 496     }
 497 
 498     public static final class BoundModuleTargetAttribute extends BoundAttribute<ModuleTargetAttribute>
 499             implements ModuleTargetAttribute {
 500         public BoundModuleTargetAttribute(ClassReader cf, AttributeMapper<ModuleTargetAttribute> mapper, int pos) {
 501             super(cf, mapper, pos);
 502         }
 503 
 504         @Override
 505         public Utf8Entry targetPlatform() {
 506             return classReader.readEntry(payloadStart, Utf8Entry.class);
 507         }
 508     }
 509 
 510     public static final class BoundCompilationIDAttribute extends BoundAttribute<CompilationIDAttribute>
 511             implements CompilationIDAttribute {
 512         public BoundCompilationIDAttribute(ClassReader cf, AttributeMapper<CompilationIDAttribute> mapper, int pos) {
 513             super(cf, mapper, pos);
 514         }
 515 
 516         @Override
 517         public Utf8Entry compilationId() {
 518             return classReader.readEntry(payloadStart, Utf8Entry.class);
 519         }
 520     }
 521 
 522     public static final class BoundSourceIDAttribute extends BoundAttribute<SourceIDAttribute>
 523             implements SourceIDAttribute {
 524         public BoundSourceIDAttribute(ClassReader cf, AttributeMapper<SourceIDAttribute> mapper, int pos) {
 525             super(cf, mapper, pos);
 526         }
 527 
 528         @Override
 529         public Utf8Entry sourceId() {
 530             return classReader.readEntry(payloadStart, Utf8Entry.class);
 531         }
 532     }
 533 
 534     public static final class BoundModuleResolutionAttribute extends BoundAttribute<ModuleResolutionAttribute>
 535             implements ModuleResolutionAttribute {
 536         public BoundModuleResolutionAttribute(ClassReader cf, AttributeMapper<ModuleResolutionAttribute> mapper, int pos) {
 537             super(cf, mapper, pos);
 538         }
 539 
 540         @Override
 541         public int resolutionFlags() {
 542             return classReader.readU2(payloadStart);
 543         }
 544     }
 545 
 546     public static final class BoundExceptionsAttribute extends BoundAttribute<ExceptionsAttribute>
 547             implements ExceptionsAttribute {
 548         private List<ClassEntry> exceptions = null;
 549 
 550         public BoundExceptionsAttribute(ClassReader cf, AttributeMapper<ExceptionsAttribute> mapper, int pos) {
 551             super(cf, mapper, pos);
 552         }
 553 
 554         @Override
 555         public List<ClassEntry> exceptions() {
 556             if (exceptions == null) {
 557                 exceptions = readEntryList(payloadStart, ClassEntry.class);
 558             }
 559             return exceptions;
 560         }
 561     }
 562 
 563     public static final class BoundModuleAttribute extends BoundAttribute<ModuleAttribute>
 564             implements ModuleAttribute {
 565         private List<ModuleRequireInfo> requires = null;
 566         private List<ModuleExportInfo> exports = null;
 567         private List<ModuleOpenInfo> opens = null;
 568         private List<ClassEntry> uses = null;
 569         private List<ModuleProvideInfo> provides = null;
 570 
 571         public BoundModuleAttribute(ClassReader cf, AttributeMapper<ModuleAttribute> mapper, int pos) {
 572             super(cf, mapper, pos);
 573         }
 574 
 575         @Override
 576         public ModuleEntry moduleName() {
 577             return classReader.readEntry(payloadStart, ModuleEntry.class);
 578         }
 579 
 580         @Override
 581         public int moduleFlagsMask() {
 582             return classReader.readU2(payloadStart + 2);
 583         }
 584 
 585         @Override
 586         public Optional<Utf8Entry> moduleVersion() {
 587             return Optional.ofNullable(classReader.readEntryOrNull(payloadStart + 4, Utf8Entry.class));
 588         }
 589 
 590         @Override
 591         public List<ModuleRequireInfo> requires() {
 592             if (requires == null) {
 593                 structure();
 594             }
 595             return requires;
 596         }
 597 
 598         @Override
 599         public List<ModuleExportInfo> exports() {
 600             if (exports == null) {
 601                 structure();
 602             }
 603             return exports;
 604         }
 605 
 606         @Override
 607         public List<ModuleOpenInfo> opens() {
 608             if (opens == null) {
 609                 structure();
 610             }
 611             return opens;
 612         }
 613 
 614         @Override
 615         public List<ClassEntry> uses() {
 616             if (uses == null) {
 617                 structure();
 618             }
 619             return uses;
 620         }
 621 
 622         @Override
 623         public List<ModuleProvideInfo> provides() {
 624             if (provides == null) {
 625                 structure();
 626             }
 627             return provides;
 628         }
 629 
 630         private void structure() {
 631             int p = payloadStart + 8;
 632 
 633             {
 634                 int cnt = classReader.readU2(payloadStart + 6);
 635                 ModuleRequireInfo[] elements = new ModuleRequireInfo[cnt];
 636                 int end = p + (cnt * 6);
 637                 for (int i = 0; p < end; p += 6, i++) {
 638                     elements[i] = ModuleRequireInfo.of(classReader.readEntry(p, ModuleEntry.class),
 639                             classReader.readU2(p + 2),
 640                             classReader.readEntryOrNull(p + 4, Utf8Entry.class));
 641                 }
 642                 requires = List.of(elements);
 643             }
 644 
 645             {
 646                 int cnt = classReader.readU2(p);
 647                 p += 2;
 648                 ModuleExportInfo[] elements = new ModuleExportInfo[cnt];
 649                 for (int i = 0; i < cnt; i++) {
 650                     PackageEntry pe = classReader.readEntry(p, PackageEntry.class);
 651                     int exportFlags = classReader.readU2(p + 2);
 652                     p += 4;
 653                     List<ModuleEntry> exportsTo = readEntryList(p, ModuleEntry.class);
 654                     p += 2 + exportsTo.size() * 2;
 655                     elements[i] = ModuleExportInfo.of(pe, exportFlags, exportsTo);
 656                 }
 657                 exports = List.of(elements);
 658             }
 659 
 660             {
 661                 int cnt = classReader.readU2(p);
 662                 p += 2;
 663                 ModuleOpenInfo[] elements = new ModuleOpenInfo[cnt];
 664                 for (int i = 0; i < cnt; i++) {
 665                     PackageEntry po = classReader.readEntry(p, PackageEntry.class);
 666                     int opensFlags = classReader.readU2(p + 2);
 667                     p += 4;
 668                     List<ModuleEntry> opensTo = readEntryList(p, ModuleEntry.class);
 669                     p += 2 + opensTo.size() * 2;
 670                     elements[i] = ModuleOpenInfo.of(po, opensFlags, opensTo);
 671                 }
 672                 opens = List.of(elements);
 673             }
 674 
 675             {
 676                 uses = readEntryList(p, ClassEntry.class);
 677                 p += 2 + uses.size() * 2;
 678                 int cnt = classReader.readU2(p);
 679                 p += 2;
 680                 ModuleProvideInfo[] elements = new ModuleProvideInfo[cnt];
 681                 provides = new ArrayList<>(cnt);
 682                 for (int i = 0; i < cnt; i++) {
 683                     ClassEntry c = classReader.readEntry(p, ClassEntry.class);
 684                     p += 2;
 685                     List<ClassEntry> providesWith = readEntryList(p, ClassEntry.class);
 686                     p += 2 + providesWith.size() * 2;
 687                     elements[i] = ModuleProvideInfo.of(c, providesWith);
 688                 }
 689                 provides = List.of(elements);
 690             }
 691         }
 692     }
 693 
 694     public static final class BoundModulePackagesAttribute extends BoundAttribute<ModulePackagesAttribute>
 695             implements ModulePackagesAttribute {
 696         private List<PackageEntry> packages = null;
 697 
 698         public BoundModulePackagesAttribute(ClassReader cf, AttributeMapper<ModulePackagesAttribute> mapper, int pos) {
 699             super(cf, mapper, pos);
 700         }
 701 
 702         @Override
 703         public List<PackageEntry> packages() {
 704             if (packages == null) {
 705                 packages = readEntryList(payloadStart, PackageEntry.class);
 706             }
 707             return packages;
 708         }
 709     }
 710 
 711     public static final class BoundNestMembersAttribute extends BoundAttribute<NestMembersAttribute>
 712             implements NestMembersAttribute {
 713 
 714         private List<ClassEntry> members = null;
 715 
 716         public BoundNestMembersAttribute(ClassReader cf, AttributeMapper<NestMembersAttribute> mapper, int pos) {
 717             super(cf, mapper, pos);
 718         }
 719 
 720         @Override
 721         public List<ClassEntry> nestMembers() {
 722             if (members == null) {
 723                 members = readEntryList(payloadStart, ClassEntry.class);
 724             }
 725             return members;
 726         }
 727     }
 728 
 729     public static final class BoundBootstrapMethodsAttribute extends BoundAttribute<BootstrapMethodsAttribute>
 730             implements BootstrapMethodsAttribute {
 731 
 732         private List<BootstrapMethodEntry> bootstraps = null;
 733         private final int size;
 734 
 735         public BoundBootstrapMethodsAttribute(ClassReader reader, AttributeMapper<BootstrapMethodsAttribute> mapper, int pos) {
 736             super(reader, mapper, pos);
 737             size = classReader.readU2(pos);
 738         }
 739 
 740         @Override
 741         public int bootstrapMethodsSize() {
 742             return size;
 743         }
 744 
 745         @Override
 746         public List<BootstrapMethodEntry> bootstrapMethods() {
 747             if (bootstraps == null) {
 748                 BootstrapMethodEntry[] bs = new BootstrapMethodEntry[size];
 749                 int p = payloadStart + 2;
 750                 for (int i = 0; i < size; ++i) {
 751                     final var handle = classReader.readEntry(p, AbstractPoolEntry.MethodHandleEntryImpl.class);
 752                     final List<LoadableConstantEntry> args = readEntryList(p + 2, LoadableConstantEntry.class);
 753                     p += 4 + args.size() * 2;
 754                     int hash = BootstrapMethodEntryImpl.computeHashCode(handle, args);
 755                     bs[i] = new BootstrapMethodEntryImpl(classReader, i, hash, handle, args);
 756                 }
 757                 bootstraps = List.of(bs);
 758             }
 759             return bootstraps;
 760         }
 761     }
 762 
 763     public static final class BoundInnerClassesAttribute extends BoundAttribute<InnerClassesAttribute>
 764             implements InnerClassesAttribute {
 765         private List<InnerClassInfo> classes;
 766 
 767         public BoundInnerClassesAttribute(ClassReader cf, AttributeMapper<InnerClassesAttribute> mapper, int pos) {
 768             super(cf, mapper, pos);
 769         }
 770 
 771         @Override
 772         public List<InnerClassInfo> classes() {
 773             if (classes == null) {
 774                 final int cnt = classReader.readU2(payloadStart);
 775                 int p = payloadStart + 2;
 776                 InnerClassInfo[] elements = new InnerClassInfo[cnt];
 777                 for (int i = 0; i < cnt; i++) {
 778                     ClassEntry innerClass = classReader.readEntry(p, ClassEntry.class);
 779                     var outerClass = classReader.readEntryOrNull(p + 2, ClassEntry.class);
 780                     var innerName = classReader.readEntryOrNull(p + 4, Utf8Entry.class);
 781                     int flags = classReader.readU2(p + 6);
 782                     p += 8;
 783                     elements[i] = InnerClassInfo.of(innerClass, Optional.ofNullable(outerClass), Optional.ofNullable(innerName), flags);
 784                 }
 785                 classes = List.of(elements);
 786             }
 787             return classes;
 788         }
 789     }
 790 
 791     public static final class BoundEnclosingMethodAttribute extends BoundAttribute<EnclosingMethodAttribute>
 792             implements EnclosingMethodAttribute {
 793         public BoundEnclosingMethodAttribute(ClassReader cf, AttributeMapper<EnclosingMethodAttribute> mapper, int pos) {
 794             super(cf, mapper, pos);
 795         }
 796 
 797         @Override
 798         public ClassEntry enclosingClass() {
 799             return classReader.readEntry(payloadStart, ClassEntry.class);
 800         }
 801 
 802         @Override
 803         public Optional<NameAndTypeEntry> enclosingMethod() {
 804             return Optional.ofNullable(classReader.readEntryOrNull(payloadStart + 2, NameAndTypeEntry.class));
 805         }
 806     }
 807 
 808     public static final class BoundAnnotationDefaultAttr
 809             extends BoundAttribute<AnnotationDefaultAttribute>
 810             implements AnnotationDefaultAttribute {
 811         private AnnotationValue annotationValue;
 812 
 813         public BoundAnnotationDefaultAttr(ClassReader cf, AttributeMapper<AnnotationDefaultAttribute> mapper, int pos) {
 814             super(cf, mapper, pos);
 815         }
 816 
 817         @Override
 818         public AnnotationValue defaultValue() {
 819             if (annotationValue == null)
 820                 annotationValue = AnnotationReader.readElementValue(classReader, payloadStart);
 821             return annotationValue;
 822         }
 823     }
 824 
 825     public static final class BoundRuntimeVisibleTypeAnnotationsAttribute extends BoundAttribute<RuntimeVisibleTypeAnnotationsAttribute>
 826             implements RuntimeVisibleTypeAnnotationsAttribute {
 827 
 828         private final LabelContext labelContext;
 829 
 830         public BoundRuntimeVisibleTypeAnnotationsAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<RuntimeVisibleTypeAnnotationsAttribute> mapper, int pos) {
 831             super(cf, mapper, pos);
 832             this.labelContext = (enclosing instanceof LabelContext lc) ? lc : null;
 833         }
 834 
 835         @Override
 836         public List<TypeAnnotation> annotations() {
 837             return AnnotationReader.readTypeAnnotations(classReader, payloadStart, labelContext);
 838         }
 839     }
 840 
 841     public static final class BoundRuntimeInvisibleTypeAnnotationsAttribute
 842             extends BoundAttribute<RuntimeInvisibleTypeAnnotationsAttribute>
 843             implements RuntimeInvisibleTypeAnnotationsAttribute {
 844         public BoundRuntimeInvisibleTypeAnnotationsAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper<RuntimeInvisibleTypeAnnotationsAttribute> mapper, int pos) {
 845             super(cf, mapper, pos);
 846             this.labelContext = (enclosing instanceof LabelContext lc) ? lc : null;
 847         }
 848 
 849         private final LabelContext labelContext;
 850 
 851         @Override
 852         public List<TypeAnnotation> annotations() {
 853             return AnnotationReader.readTypeAnnotations(classReader, payloadStart, labelContext);
 854         }
 855     }
 856 
 857     public static final class BoundRuntimeVisibleParameterAnnotationsAttribute
 858             extends BoundAttribute<RuntimeVisibleParameterAnnotationsAttribute>
 859             implements RuntimeVisibleParameterAnnotationsAttribute {
 860 
 861         public BoundRuntimeVisibleParameterAnnotationsAttribute(ClassReader cf, AttributeMapper<RuntimeVisibleParameterAnnotationsAttribute> mapper, int pos) {
 862             super(cf, mapper, pos);
 863         }
 864 
 865         @Override
 866         public List<List<Annotation>> parameterAnnotations() {
 867             return AnnotationReader.readParameterAnnotations(classReader, payloadStart);
 868         }
 869     }
 870 
 871     public static final class BoundRuntimeInvisibleParameterAnnotationsAttribute
 872             extends BoundAttribute<RuntimeInvisibleParameterAnnotationsAttribute>
 873             implements RuntimeInvisibleParameterAnnotationsAttribute {
 874 
 875         public BoundRuntimeInvisibleParameterAnnotationsAttribute(ClassReader cf, AttributeMapper<RuntimeInvisibleParameterAnnotationsAttribute> mapper, int pos) {
 876             super(cf, mapper, pos);
 877         }
 878 
 879         @Override
 880         public List<List<Annotation>> parameterAnnotations() {
 881             return AnnotationReader.readParameterAnnotations(classReader, payloadStart);
 882         }
 883     }
 884 
 885     public static final class BoundRuntimeInvisibleAnnotationsAttribute
 886             extends BoundAttribute<RuntimeInvisibleAnnotationsAttribute>
 887             implements RuntimeInvisibleAnnotationsAttribute {
 888         private List<Annotation> inflated;
 889 
 890         public BoundRuntimeInvisibleAnnotationsAttribute(ClassReader cf,
 891                                                          int payloadStart) {
 892             super(cf, Attributes.runtimeInvisibleAnnotations(), payloadStart);
 893         }
 894 
 895         @Override
 896         public List<Annotation> annotations() {
 897             if (inflated == null)
 898                 inflated = AnnotationReader.readAnnotations(classReader, payloadStart);
 899             return inflated;
 900         }
 901     }
 902 
 903     public static final class BoundRuntimeVisibleAnnotationsAttribute
 904             extends BoundAttribute<RuntimeVisibleAnnotationsAttribute>
 905             implements RuntimeVisibleAnnotationsAttribute {
 906         private List<Annotation> inflated;
 907 
 908         public BoundRuntimeVisibleAnnotationsAttribute(ClassReader cf,
 909                                                        int payloadStart) {
 910             super(cf, Attributes.runtimeVisibleAnnotations(), payloadStart);
 911         }
 912 
 913         @Override
 914         public List<Annotation> annotations() {
 915             if (inflated == null)
 916                 inflated = AnnotationReader.readAnnotations(classReader, payloadStart);
 917             return inflated;
 918         }
 919     }
 920 
 921     public static final class BoundPermittedSubclassesAttribute extends BoundAttribute<PermittedSubclassesAttribute>
 922             implements PermittedSubclassesAttribute {
 923         private List<ClassEntry> permittedSubclasses = null;
 924 
 925         public BoundPermittedSubclassesAttribute(ClassReader cf, AttributeMapper<PermittedSubclassesAttribute> mapper, int pos) {
 926             super(cf, mapper, pos);
 927         }
 928 
 929         @Override
 930         public List<ClassEntry> permittedSubclasses() {
 931             if (permittedSubclasses == null) {
 932                 permittedSubclasses = readEntryList(payloadStart, ClassEntry.class);
 933             }
 934             return permittedSubclasses;
 935         }
 936     }
 937 
 938     public static final class BoundLoadableDescriptorsAttribute extends BoundAttribute<LoadableDescriptorsAttribute>
 939             implements LoadableDescriptorsAttribute {
 940         private List<Utf8Entry> loadableDescriptors = null;
 941 
 942         public BoundLoadableDescriptorsAttribute(ClassReader cf, AttributeMapper<LoadableDescriptorsAttribute> mapper, int pos) {
 943             super(cf, mapper, pos);
 944         }
 945 
 946         @Override
 947         public List<Utf8Entry> loadableDescriptors() {
 948             if (loadableDescriptors == null) {
 949                 loadableDescriptors = readEntryList(payloadStart, Utf8Entry.class);
 950             }
 951             return loadableDescriptors;
 952         }
 953     }
 954 
 955     public abstract static sealed class BoundCodeAttribute
 956             extends BoundAttribute<CodeAttribute>
 957             implements CodeAttribute
 958             permits CodeImpl {
 959         protected final int codeStart;
 960         protected final int codeLength;
 961         protected final int codeEnd;
 962         protected final int attributePos;
 963         protected final int exceptionHandlerPos;
 964         protected final int exceptionHandlerCnt;
 965         protected final MethodModel enclosingMethod;
 966 
 967         public BoundCodeAttribute(AttributedElement enclosing,
 968                                   ClassReader reader,
 969                                   AttributeMapper<CodeAttribute> mapper,
 970                                   int payloadStart) {
 971             super(reader, mapper, payloadStart);
 972             this.codeLength = classReader.readInt(payloadStart + 4);
 973             this.enclosingMethod = (MethodModel) enclosing;
 974             this.codeStart = payloadStart + 8;
 975             this.codeEnd = codeStart + codeLength;
 976             this.exceptionHandlerPos = codeEnd;
 977             this.exceptionHandlerCnt = classReader.readU2(exceptionHandlerPos);
 978             this.attributePos = exceptionHandlerPos + 2 + exceptionHandlerCnt * 8;
 979         }
 980 
 981         // CodeAttribute
 982 
 983         @Override
 984         public int maxStack() {
 985             return classReader.readU2(payloadStart);
 986         }
 987 
 988         @Override
 989         public int maxLocals() {
 990             return classReader.readU2(payloadStart + 2);
 991         }
 992 
 993         @Override
 994         public int codeLength() {
 995             return codeLength;
 996         }
 997 
 998         @Override
 999         public byte[] codeArray() {
1000             return classReader.readBytes(payloadStart + 8, codeLength());
1001         }
1002     }
1003 
1004     /**
1005      * {@return the attribute mapper for a standard attribute}
1006      *
1007      * @param name the name of the attribute to find
1008      */
1009     public static AttributeMapper<?> standardAttribute(Utf8Entry name) {
1010         // critical bootstrap path, so no lambdas nor method handles here
1011         return switch (name.hashCode()) {
1012             case 0x46699ff2 ->
1013                 name.equalsString(NAME_ANNOTATION_DEFAULT) ? annotationDefault() : null;
1014             case 0x5208e184 ->
1015                 name.equalsString(NAME_BOOTSTRAP_METHODS) ? bootstrapMethods() : null;
1016             case 0xcb60907a ->
1017                 name.equalsString(NAME_CHARACTER_RANGE_TABLE) ? characterRangeTable() : null;
1018             case 0x4020220d ->
1019                 name.equalsString(NAME_CODE) ? code() : null;
1020             case 0xc20dd1fe ->
1021                 name.equalsString(NAME_COMPILATION_ID) ? compilationId() : null;
1022             case 0xcab1940d ->
1023                 name.equalsString(NAME_CONSTANT_VALUE) ? constantValue() : null;
1024             case 0x558641d3 ->
1025                 name.equalsString(NAME_DEPRECATED) ? deprecated() : null;
1026             case 0x51d443cd ->
1027                 name.equalsString(NAME_ENCLOSING_METHOD) ? enclosingMethod() : null;
1028             case 0x687c1624 ->
1029                 name.equalsString(NAME_EXCEPTIONS) ? exceptions() : null;
1030             case 0x7adb2910 ->
1031                 name.equalsString(NAME_INNER_CLASSES) ? innerClasses() : null;
1032             case 0x653f0551 ->
1033                 name.equalsString(NAME_LINE_NUMBER_TABLE) ? lineNumberTable() : null;
1034            case 0x5f348b64 ->
1035                 name.equalsString(NAME_LOADABLE_DESCRIPTORS) ? loadableDescriptors() : null;
1036             case 0x64c75927 ->
1037                 name.equalsString(NAME_LOCAL_VARIABLE_TABLE) ? localVariableTable() : null;
1038             case 0x6697f98d ->
1039                 name.equalsString(NAME_LOCAL_VARIABLE_TYPE_TABLE) ? localVariableTypeTable() : null;
1040             case 0xdbb0cdcb ->
1041                 name.equalsString(NAME_METHOD_PARAMETERS) ? methodParameters() : null;
1042             case 0xc9b0928c ->
1043                 name.equalsString(NAME_MODULE) ? module() : null;
1044             case 0x41cd27e8 ->
1045                 name.equalsString(NAME_MODULE_HASHES) ? moduleHashes() : null;
1046             case 0x7deb0a13 ->
1047                 name.equalsString(NAME_MODULE_MAIN_CLASS) ? moduleMainClass() : null;
1048             case 0x6706ff99 ->
1049                 name.equalsString(NAME_MODULE_PACKAGES) ? modulePackages() : null;
1050             case 0x60272858 ->
1051                 name.equalsString(NAME_MODULE_RESOLUTION) ? moduleResolution() : null;
1052             case 0x5646d73d ->
1053                 name.equalsString(NAME_MODULE_TARGET) ? moduleTarget() : null;
1054             case 0x50336c40 ->
1055                 name.equalsString(NAME_NEST_HOST) ? nestHost() : null;
1056             case 0x4735ab81 ->
1057                 name.equalsString(NAME_NEST_MEMBERS) ? nestMembers() : null;
1058             case 0x7100d9fe ->
1059                 name.equalsString(NAME_PERMITTED_SUBCLASSES) ? permittedSubclasses() : null;
1060             case 0xd1ab5871 ->
1061                 name.equalsString(NAME_RECORD) ? record() : null;
1062             case 0x7588550f ->
1063                 name.equalsString(NAME_RUNTIME_INVISIBLE_ANNOTATIONS) ? runtimeInvisibleAnnotations() : null;
1064             case 0xcc74da30 ->
1065                 name.equalsString(NAME_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS) ? runtimeInvisibleParameterAnnotations() : null;
1066             case 0xf67697f5 ->
1067                 name.equalsString(NAME_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS) ? runtimeInvisibleTypeAnnotations() : null;
1068             case 0xe0837d2a ->
1069                 name.equalsString(NAME_RUNTIME_VISIBLE_ANNOTATIONS) ? runtimeVisibleAnnotations() : null;
1070             case 0xc945a075 ->
1071                 name.equalsString(NAME_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS) ? runtimeVisibleParameterAnnotations() : null;
1072             case 0x611a3a90 ->
1073                 name.equalsString(NAME_RUNTIME_VISIBLE_TYPE_ANNOTATIONS) ? runtimeVisibleTypeAnnotations() : null;
1074             case 0xf76fb898 ->
1075                 name.equalsString(NAME_SIGNATURE) ? signature() : null;
1076             case 0x6b41b047 ->
1077                 name.equalsString(NAME_SOURCE_DEBUG_EXTENSION) ? sourceDebugExtension() : null;
1078             case 0x748c2857 ->
1079                 name.equalsString(NAME_SOURCE_FILE) ? sourceFile() : null;
1080             case 0x6bf13a96 ->
1081                 name.equalsString(NAME_SOURCE_ID) ? sourceId() : null;
1082             case 0xfa85ee5a ->
1083                 name.equalsString(NAME_STACK_MAP_TABLE) ? stackMapTable() : null;
1084             case 0xf2670725 ->
1085                 name.equalsString(NAME_SYNTHETIC) ? synthetic() : null;
1086             default -> null;
1087         };
1088     }
1089 }