From ca38cd477adb5441b2464c8b45509befbad24475 Mon Sep 17 00:00:00 2001 From: Adam Sotona Date: Fri, 8 Dec 2023 16:34:07 +0100 Subject: [PATCH] conversion to constant methods --- .../impl/AbstractBoundLocalVariable.java | 14 +- .../classfile/impl/AbstractInstruction.java | 43 +-- .../classfile/impl/AbstractUnboundModel.java | 13 +- .../classfile/impl/BoundAttribute.java | 296 +++++++----------- .../impl/BoundRecordComponentInfo.java | 8 +- .../classfile/impl/BufferedMethodBuilder.java | 23 +- .../internal/classfile/impl/ClassImpl.java | 28 +- .../classfile/impl/ClassReaderImpl.java | 54 ++-- .../jdk/internal/classfile/impl/CodeImpl.java | 39 +-- .../classfile/impl/DirectMethodBuilder.java | 23 +- .../internal/classfile/impl/FieldImpl.java | 8 +- .../internal/classfile/impl/MethodImpl.java | 25 +- .../classfile/impl/SplitConstantPool.java | 73 ++--- 13 files changed, 246 insertions(+), 401 deletions(-) diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractBoundLocalVariable.java b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractBoundLocalVariable.java index b6b6554ff3c3b..96d6b3bfcf426 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractBoundLocalVariable.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractBoundLocalVariable.java @@ -32,8 +32,6 @@ public class AbstractBoundLocalVariable extends AbstractElement { protected final CodeImpl code; protected final int offset; - private Utf8Entry nameEntry; - private Utf8Entry secondaryEntry; public AbstractBoundLocalVariable(CodeImpl code, int offset) { this.code = code; @@ -44,20 +42,16 @@ protected int nameIndex() { return code.classReader.readU2(offset + 4); } - public Utf8Entry name() { - if (nameEntry == null) - nameEntry = (Utf8Entry) code.constantPool().entryByIndex(nameIndex()); - return nameEntry; + public const Utf8Entry name() { + return (Utf8Entry) code.constantPool().entryByIndex(nameIndex()); } protected int secondaryIndex() { return code.classReader.readU2(offset + 6); } - protected Utf8Entry secondaryEntry() { - if (secondaryEntry == null) - secondaryEntry = (Utf8Entry) code.constantPool().entryByIndex(secondaryIndex()); - return secondaryEntry; + protected const Utf8Entry secondaryEntry() { + return (Utf8Entry) code.constantPool().entryByIndex(secondaryIndex()); } public Label startScope() { diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractInstruction.java b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractInstruction.java index b77dbef8d2987..bd23827aae6d6 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractInstruction.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractInstruction.java @@ -374,17 +374,13 @@ public String toString() { public static final class BoundFieldInstruction extends BoundInstruction implements FieldInstruction { - private FieldRefEntry fieldEntry; - public BoundFieldInstruction(Opcode op, CodeImpl code, int pos) { super(op, op.sizeIfFixed(), code, pos); } @Override - public FieldRefEntry field() { - if (fieldEntry == null) - fieldEntry = code.classReader.readEntry(pos + 1, FieldRefEntry.class); - return fieldEntry; + public const FieldRefEntry field() { + return code.classReader.readEntry(pos + 1, FieldRefEntry.class); } @Override @@ -404,17 +400,14 @@ public String toString() { public static final class BoundInvokeInstruction extends BoundInstruction implements InvokeInstruction { - MemberRefEntry methodEntry; public BoundInvokeInstruction(Opcode op, CodeImpl code, int pos) { super(op, op.sizeIfFixed(), code, pos); } @Override - public MemberRefEntry method() { - if (methodEntry == null) - methodEntry = code.classReader.readEntry(pos + 1, MemberRefEntry.class); - return methodEntry; + public const MemberRefEntry method() { + return code.classReader.readEntry(pos + 1, MemberRefEntry.class); } @Override @@ -444,17 +437,14 @@ public String toString() { public static final class BoundInvokeInterfaceInstruction extends BoundInstruction implements InvokeInstruction { - InterfaceMethodRefEntry methodEntry; public BoundInvokeInterfaceInstruction(Opcode op, CodeImpl code, int pos) { super(op, op.sizeIfFixed(), code, pos); } @Override - public MemberRefEntry method() { - if (methodEntry == null) - methodEntry = code.classReader.readEntry(pos + 1, InterfaceMethodRefEntry.class); - return methodEntry; + public const MemberRefEntry method() { + return code.classReader.readEntry(pos + 1, InterfaceMethodRefEntry.class); } @Override @@ -484,17 +474,14 @@ public String toString() { public static final class BoundInvokeDynamicInstruction extends BoundInstruction implements InvokeDynamicInstruction { - InvokeDynamicEntry indyEntry; BoundInvokeDynamicInstruction(Opcode op, CodeImpl code, int pos) { super(op, op.sizeIfFixed(), code, pos); } @Override - public InvokeDynamicEntry invokedynamic() { - if (indyEntry == null) - indyEntry = code.classReader.readEntry(pos + 1, InvokeDynamicEntry.class); - return indyEntry; + public const InvokeDynamicEntry invokedynamic() { + return code.classReader.readEntry(pos + 1, InvokeDynamicEntry.class); } @Override @@ -514,17 +501,14 @@ public String toString() { public static final class BoundNewObjectInstruction extends BoundInstruction implements NewObjectInstruction { - ClassEntry classEntry; BoundNewObjectInstruction(CodeImpl code, int pos) { super(Opcode.NEW, Opcode.NEW.sizeIfFixed(), code, pos); } @Override - public ClassEntry className() { - if (classEntry == null) - classEntry = code.classReader.readClassEntry(pos + 1); - return classEntry; + public const ClassEntry className() { + return code.classReader.readClassEntry(pos + 1); } @Override @@ -621,17 +605,14 @@ public String toString() { public static final class BoundTypeCheckInstruction extends BoundInstruction implements TypeCheckInstruction { - ClassEntry typeEntry; public BoundTypeCheckInstruction(Opcode op, CodeImpl code, int pos) { super(op, op.sizeIfFixed(), code, pos); } @Override - public ClassEntry type() { - if (typeEntry == null) - typeEntry = code.classReader.readClassEntry(pos + 1); - return typeEntry; + public const ClassEntry type() { + return code.classReader.readClassEntry(pos + 1); } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractUnboundModel.java b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractUnboundModel.java index 6aa046eb06dd8..12bf250a507c0 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractUnboundModel.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/AbstractUnboundModel.java @@ -38,7 +38,6 @@ public abstract sealed class AbstractUnboundModel implements CompoundElement, AttributedElement permits BufferedCodeBuilder.Model, BufferedFieldBuilder.Model, BufferedMethodBuilder.Model { private final List elements; - private List> attributes; public AbstractUnboundModel(List elements) { this.elements = elements; @@ -60,12 +59,10 @@ public List elementList() { } @Override - public List> attributes() { - if (attributes == null) - attributes = elements.stream() - .filter(e -> e instanceof Attribute) - .>map(e -> (Attribute) e) - .toList(); - return attributes; + public const List> attributes() { + return elements.stream() + .filter(e -> e instanceof Attribute) + .>map(e -> (Attribute) e) + .toList(); } } diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java b/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java index 0d740b2af2415..f9c13c7010559 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java @@ -196,7 +196,6 @@ public static final class BoundStackMapTableAttribute implements StackMapTableAttribute { final MethodModel method; final LabelContext ctx; - List entries = null; public BoundStackMapTableAttribute(CodeImpl code, ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); @@ -205,11 +204,8 @@ public BoundStackMapTableAttribute(CodeImpl code, ClassReader cf, AttributeMappe } @Override - public List entries() { - if (entries == null) { - entries = new StackMapDecoder(classReader, payloadStart, ctx, StackMapDecoder.initFrameLocals(method)).entries(); - } - return entries; + public const List entries() { + return new StackMapDecoder(classReader, payloadStart, ctx, StackMapDecoder.initFrameLocals(method)).entries(); } } @@ -223,55 +219,47 @@ public BoundSyntheticAttribute(ClassReader cf, AttributeMapper implements LineNumberTableAttribute { - private List lineNumbers = null; public BoundLineNumberTableAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List lineNumbers() { - if (lineNumbers == null) { - int nLn = classReader.readU2(payloadStart); - LineNumberInfo[] elements = new LineNumberInfo[nLn]; - int p = payloadStart + 2; - int pEnd = p + (nLn * 4); - for (int i = 0; p < pEnd; p += 4, i++) { - int startPc = classReader.readU2(p); - int lineNumber = classReader.readU2(p + 2); - elements[i] = LineNumberInfo.of(startPc, lineNumber); - } - lineNumbers = List.of(elements); + public const List lineNumbers() { + int nLn = classReader.readU2(payloadStart); + LineNumberInfo[] elements = new LineNumberInfo[nLn]; + int p = payloadStart + 2; + int pEnd = p + (nLn * 4); + for (int i = 0; p < pEnd; p += 4, i++) { + int startPc = classReader.readU2(p); + int lineNumber = classReader.readU2(p + 2); + elements[i] = LineNumberInfo.of(startPc, lineNumber); } - return lineNumbers; + return List.of(elements); } } public static final class BoundCharacterRangeTableAttribute extends BoundAttribute implements CharacterRangeTableAttribute { - private List characterRangeTable = null; public BoundCharacterRangeTableAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List characterRangeTable() { - if (characterRangeTable == null) { - int nLn = classReader.readU2(payloadStart); - CharacterRangeInfo[] elements = new CharacterRangeInfo[nLn]; - int p = payloadStart + 2; - int pEnd = p + (nLn * 14); - for (int i = 0; p < pEnd; p += 14, i++) { - int startPc = classReader.readU2(p); - int endPc = classReader.readU2(p + 2); - int characterRangeStart = classReader.readInt(p + 4); - int characterRangeEnd = classReader.readInt(p + 8); - int flags = classReader.readU2(p + 12); - elements[i] = CharacterRangeInfo.of(startPc, endPc, characterRangeStart, characterRangeEnd, flags); - } - characterRangeTable = List.of(elements); + public const List characterRangeTable() { + int nLn = classReader.readU2(payloadStart); + CharacterRangeInfo[] elements = new CharacterRangeInfo[nLn]; + int p = payloadStart + 2; + int pEnd = p + (nLn * 14); + for (int i = 0; p < pEnd; p += 14, i++) { + int startPc = classReader.readU2(p); + int endPc = classReader.readU2(p + 2); + int characterRangeStart = classReader.readInt(p + 4); + int characterRangeEnd = classReader.readInt(p + 8); + int flags = classReader.readU2(p + 12); + elements[i] = CharacterRangeInfo.of(startPc, endPc, characterRangeStart, characterRangeEnd, flags); } - return characterRangeTable; + return List.of(elements); } } @@ -279,7 +267,6 @@ public static final class BoundLocalVariableTableAttribute extends BoundAttribute implements LocalVariableTableAttribute { private final CodeImpl codeAttribute; - private List localVars = null; public BoundLocalVariableTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); @@ -287,18 +274,15 @@ public BoundLocalVariableTableAttribute(AttributedElement enclosing, ClassReader } @Override - public List localVariables() { - if (localVars == null) { - int cnt = classReader.readU2(payloadStart); - BoundLocalVariable[] elements = new BoundLocalVariable[cnt]; - int p = payloadStart + 2; - int pEnd = p + (cnt * 10); - for (int i = 0; p < pEnd; p += 10, i++) { - elements[i] = new BoundLocalVariable(codeAttribute, p); - } - localVars = List.of(elements); + public const List localVariables() { + int cnt = classReader.readU2(payloadStart); + BoundLocalVariable[] elements = new BoundLocalVariable[cnt]; + int p = payloadStart + 2; + int pEnd = p + (cnt * 10); + for (int i = 0; p < pEnd; p += 10, i++) { + elements[i] = new BoundLocalVariable(codeAttribute, p); } - return localVars; + return List.of(elements); } } @@ -306,7 +290,6 @@ public static final class BoundLocalVariableTypeTableAttribute extends BoundAttribute implements LocalVariableTypeTableAttribute { private final CodeImpl codeAttribute; - private List localVars = null; public BoundLocalVariableTypeTableAttribute(AttributedElement enclosing, ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); @@ -314,50 +297,42 @@ public BoundLocalVariableTypeTableAttribute(AttributedElement enclosing, ClassRe } @Override - public List localVariableTypes() { - if (localVars == null) { - final int cnt = classReader.readU2(payloadStart); - BoundLocalVariableType[] elements = new BoundLocalVariableType[cnt]; - int p = payloadStart + 2; - int pEnd = p + (cnt * 10); - for (int i = 0; p < pEnd; p += 10, i++) { - elements[i] = new BoundLocalVariableType(codeAttribute, p); - } - localVars = List.of(elements); + public const List localVariableTypes() { + final int cnt = classReader.readU2(payloadStart); + BoundLocalVariableType[] elements = new BoundLocalVariableType[cnt]; + int p = payloadStart + 2; + int pEnd = p + (cnt * 10); + for (int i = 0; p < pEnd; p += 10, i++) { + elements[i] = new BoundLocalVariableType(codeAttribute, p); } - return localVars; + return List.of(elements); } } public static final class BoundMethodParametersAttribute extends BoundAttribute implements MethodParametersAttribute { - private List parameters = null; public BoundMethodParametersAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List parameters() { - if (parameters == null) { - final int cnt = classReader.readU1(payloadStart); - MethodParameterInfo[] elements = new MethodParameterInfo[cnt]; - int p = payloadStart + 1; - int pEnd = p + (cnt * 4); - for (int i = 0; p < pEnd; p += 4, i++) { - Utf8Entry name = classReader.readUtf8EntryOrNull(p); - int accessFlags = classReader.readU2(p + 2); - elements[i] = MethodParameterInfo.of(Optional.ofNullable(name), accessFlags); - } - parameters = List.of(elements); + public const List parameters() { + final int cnt = classReader.readU1(payloadStart); + MethodParameterInfo[] elements = new MethodParameterInfo[cnt]; + int p = payloadStart + 1; + int pEnd = p + (cnt * 4); + for (int i = 0; p < pEnd; p += 4, i++) { + Utf8Entry name = classReader.readUtf8EntryOrNull(p); + int accessFlags = classReader.readU2(p + 2); + elements[i] = MethodParameterInfo.of(Optional.ofNullable(name), accessFlags); } - return parameters; + return List.of(elements); } } public static final class BoundModuleHashesAttribute extends BoundAttribute implements ModuleHashesAttribute { - private List hashes = null; public BoundModuleHashesAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); @@ -369,47 +344,40 @@ public Utf8Entry algorithm() { } @Override - public List hashes() { - if (hashes == null) { - final int cnt = classReader.readU2(payloadStart + 2); - ModuleHashInfo[] elements = new ModuleHashInfo[cnt]; - int p = payloadStart + 4; - //System.err.printf("%5d: ModuleHashesAttr alg = %s, cnt = %d%n", pos, algorithm(), cnt); - for (int i = 0; i < cnt; ++i) { - ModuleEntry module = classReader.readModuleEntry(p); - int hashLength = classReader.readU2(p + 2); - //System.err.printf("%5d: [%d] module = %s, hashLength = %d%n", p, i, module, hashLength); - p += 4; - elements[i] = ModuleHashInfo.of(module, classReader.readBytes(p, hashLength)); - p += hashLength; - } - hashes = List.of(elements); + public const List hashes() { + final int cnt = classReader.readU2(payloadStart + 2); + ModuleHashInfo[] elements = new ModuleHashInfo[cnt]; + int p = payloadStart + 4; + //System.err.printf("%5d: ModuleHashesAttr alg = %s, cnt = %d%n", pos, algorithm(), cnt); + for (int i = 0; i < cnt; ++i) { + ModuleEntry module = classReader.readModuleEntry(p); + int hashLength = classReader.readU2(p + 2); + //System.err.printf("%5d: [%d] module = %s, hashLength = %d%n", p, i, module, hashLength); + p += 4; + elements[i] = ModuleHashInfo.of(module, classReader.readBytes(p, hashLength)); + p += hashLength; } - return hashes; + return List.of(elements); } } public static final class BoundRecordAttribute extends BoundAttribute implements RecordAttribute { - private List components = null; public BoundRecordAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List components() { - if (components == null) { - final int cnt = classReader.readU2(payloadStart); - RecordComponentInfo[] elements = new RecordComponentInfo[cnt]; - int p = payloadStart + 2; - for (int i = 0; i < cnt; i++) { - elements[i] = new BoundRecordComponentInfo(classReader, p); - p = classReader.skipAttributeHolder(p + 4); - } - components = List.of(elements); + public const List components() { + final int cnt = classReader.readU2(payloadStart); + RecordComponentInfo[] elements = new RecordComponentInfo[cnt]; + int p = payloadStart + 2; + for (int i = 0; i < cnt; i++) { + elements[i] = new BoundRecordComponentInfo(classReader, p); + p = classReader.skipAttributeHolder(p + 4); } - return components; + return List.of(elements); } } @@ -538,18 +506,14 @@ public int resolutionFlags() { public static final class BoundExceptionsAttribute extends BoundAttribute implements ExceptionsAttribute { - private List exceptions = null; public BoundExceptionsAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List exceptions() { - if (exceptions == null) { - exceptions = readEntryList(payloadStart); - } - return exceptions; + public const List exceptions() { + return readEntryList(payloadStart); } } @@ -686,43 +650,33 @@ private void structure() { public static final class BoundModulePackagesAttribute extends BoundAttribute implements ModulePackagesAttribute { - private List packages = null; public BoundModulePackagesAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List packages() { - if (packages == null) { - packages = readEntryList(payloadStart); - } - return packages; + public const List packages() { + return readEntryList(payloadStart); } } public static final class BoundNestMembersAttribute extends BoundAttribute implements NestMembersAttribute { - private List members = null; - public BoundNestMembersAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List nestMembers() { - if (members == null) { - members = readEntryList(payloadStart); - } - return members; + public const List nestMembers() { + return readEntryList(payloadStart); } } public static final class BoundBootstrapMethodsAttribute extends BoundAttribute implements BootstrapMethodsAttribute { - private List bootstraps = null; private final int size; public BoundBootstrapMethodsAttribute(ClassReader reader, AttributeMapper mapper, int pos) { @@ -736,55 +690,48 @@ public int bootstrapMethodsSize() { } @Override - public List bootstrapMethods() { - if (bootstraps == null) { - BootstrapMethodEntry[] bs = new BootstrapMethodEntry[size]; - int p = payloadStart + 2; - for (int i = 0; i < size; ++i) { - final AbstractPoolEntry.MethodHandleEntryImpl handle - = (AbstractPoolEntry.MethodHandleEntryImpl) classReader.readMethodHandleEntry(p); - final List args = readEntryList(p + 2); - p += 4 + args.size() * 2; - int hash = BootstrapMethodEntryImpl.computeHashCode(handle, args); - bs[i] = new BootstrapMethodEntryImpl(classReader, i, hash, handle, args); - } - bootstraps = List.of(bs); + public const List bootstrapMethods() { + BootstrapMethodEntry[] bs = new BootstrapMethodEntry[size]; + int p = payloadStart + 2; + for (int i = 0; i < size; ++i) { + final AbstractPoolEntry.MethodHandleEntryImpl handle + = (AbstractPoolEntry.MethodHandleEntryImpl) classReader.readMethodHandleEntry(p); + final List args = readEntryList(p + 2); + p += 4 + args.size() * 2; + int hash = BootstrapMethodEntryImpl.computeHashCode(handle, args); + bs[i] = new BootstrapMethodEntryImpl(classReader, i, hash, handle, args); } - return bootstraps; + return List.of(bs); } } public static final class BoundInnerClassesAttribute extends BoundAttribute implements InnerClassesAttribute { - private List classes; public BoundInnerClassesAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List classes() { - if (classes == null) { - final int cnt = classReader.readU2(payloadStart); - int p = payloadStart + 2; - InnerClassInfo[] elements = new InnerClassInfo[cnt]; - for (int i = 0; i < cnt; i++) { - ClassEntry innerClass = classReader.readClassEntry(p); // TODO FIXME - int outerClassIndex = classReader.readU2(p + 2); - ClassEntry outerClass = outerClassIndex == 0 - ? null - : (ClassEntry) classReader.entryByIndex(outerClassIndex); - int innerNameIndex = classReader.readU2(p + 4); - Utf8Entry innerName = innerNameIndex == 0 - ? null - : (Utf8Entry) classReader.entryByIndex(innerNameIndex); - int flags = classReader.readU2(p + 6); - p += 8; - elements[i] = InnerClassInfo.of(innerClass, Optional.ofNullable(outerClass), Optional.ofNullable(innerName), flags); - } - classes = List.of(elements); + public const List classes() { + final int cnt = classReader.readU2(payloadStart); + int p = payloadStart + 2; + InnerClassInfo[] elements = new InnerClassInfo[cnt]; + for (int i = 0; i < cnt; i++) { + ClassEntry innerClass = classReader.readClassEntry(p); // TODO FIXME + int outerClassIndex = classReader.readU2(p + 2); + ClassEntry outerClass = outerClassIndex == 0 + ? null + : (ClassEntry) classReader.entryByIndex(outerClassIndex); + int innerNameIndex = classReader.readU2(p + 4); + Utf8Entry innerName = innerNameIndex == 0 + ? null + : (Utf8Entry) classReader.entryByIndex(innerNameIndex); + int flags = classReader.readU2(p + 6); + p += 8; + elements[i] = InnerClassInfo.of(innerClass, Optional.ofNullable(outerClass), Optional.ofNullable(innerName), flags); } - return classes; + return List.of(elements); } } @@ -808,17 +755,14 @@ public Optional enclosingMethod() { public static final class BoundAnnotationDefaultAttr extends BoundAttribute implements AnnotationDefaultAttribute { - private AnnotationValue annotationValue; public BoundAnnotationDefaultAttr(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public AnnotationValue defaultValue() { - if (annotationValue == null) - annotationValue = AnnotationReader.readElementValue(classReader, payloadStart); - return annotationValue; + public const AnnotationValue defaultValue() { + return AnnotationReader.readElementValue(classReader, payloadStart); } } @@ -885,7 +829,6 @@ public List> parameterAnnotations() { public static final class BoundRuntimeInvisibleAnnotationsAttribute extends BoundAttribute implements RuntimeInvisibleAnnotationsAttribute { - private List inflated; public BoundRuntimeInvisibleAnnotationsAttribute(ClassReader cf, int payloadStart) { @@ -893,17 +836,14 @@ public BoundRuntimeInvisibleAnnotationsAttribute(ClassReader cf, } @Override - public List annotations() { - if (inflated == null) - inflated = AnnotationReader.readAnnotations(classReader, payloadStart); - return inflated; + public const List annotations() { + return AnnotationReader.readAnnotations(classReader, payloadStart); } } public static final class BoundRuntimeVisibleAnnotationsAttribute extends BoundAttribute implements RuntimeVisibleAnnotationsAttribute { - private List inflated; public BoundRuntimeVisibleAnnotationsAttribute(ClassReader cf, int payloadStart) { @@ -911,27 +851,21 @@ public BoundRuntimeVisibleAnnotationsAttribute(ClassReader cf, } @Override - public List annotations() { - if (inflated == null) - inflated = AnnotationReader.readAnnotations(classReader, payloadStart); - return inflated; + public const List annotations() { + return AnnotationReader.readAnnotations(classReader, payloadStart); } } public static final class BoundPermittedSubclassesAttribute extends BoundAttribute implements PermittedSubclassesAttribute { - private List permittedSubclasses = null; public BoundPermittedSubclassesAttribute(ClassReader cf, AttributeMapper mapper, int pos) { super(cf, mapper, pos); } @Override - public List permittedSubclasses() { - if (permittedSubclasses == null) { - permittedSubclasses = readEntryList(payloadStart); - } - return permittedSubclasses; + public const List permittedSubclasses() { + return readEntryList(payloadStart); } } diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/BoundRecordComponentInfo.java b/src/java.base/share/classes/jdk/internal/classfile/impl/BoundRecordComponentInfo.java index d2e0dc07378a6..78866c86582ce 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/BoundRecordComponentInfo.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/BoundRecordComponentInfo.java @@ -36,7 +36,6 @@ public final class BoundRecordComponentInfo private final ClassReader reader; private final int startPos, attributesPos; - private List> attributes; public BoundRecordComponentInfo(ClassReader reader, int startPos) { this.reader = reader; @@ -55,10 +54,7 @@ public Utf8Entry descriptor() { } @Override - public List> attributes() { - if (attributes == null) { - attributes = BoundAttribute.readAttributes(null, reader, attributesPos, reader.customAttributes()); - } - return attributes; + public const List> attributes() { + return BoundAttribute.readAttributes(null, reader, attributesPos, reader.customAttributes()); } } diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java index c6cf9d8ea5353..a2d0ee8c2def6 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/BufferedMethodBuilder.java @@ -52,8 +52,6 @@ public final class BufferedMethodBuilder private final Utf8Entry desc; private AccessFlags flags; private final MethodModel original; - private int[] parameterSlots; - MethodTypeDesc mDesc; public BufferedMethodBuilder(SplitConstantPool constantPool, ClassFileImpl context, @@ -97,15 +95,12 @@ public Utf8Entry methodType() { } @Override - public MethodTypeDesc methodTypeSymbol() { - if (mDesc == null) { - if (original instanceof MethodInfo mi) { - mDesc = mi.methodTypeSymbol(); - } else { - mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue()); - } + public const MethodTypeDesc methodTypeSymbol() { + if (original instanceof MethodInfo mi) { + return mi.methodTypeSymbol(); + } else { + return MethodTypeDesc.ofDescriptor(methodType().stringValue()); } - return mDesc; } @Override @@ -113,11 +108,13 @@ public int methodFlags() { return flags.flagsMask(); } + private const int[] parameterSlots() { + return Util.parseParameterSlots(methodFlags(), methodTypeSymbol()); + } + @Override public int parameterSlot(int paramNo) { - if (parameterSlots == null) - parameterSlots = Util.parseParameterSlots(methodFlags(), methodTypeSymbol()); - return parameterSlots[paramNo]; + return parameterSlots()[paramNo]; } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java index 8604ec5e59711..733ff8a03eaa1 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassImpl.java @@ -60,8 +60,6 @@ public final class ClassImpl private final int attributesPos; private final List methods; private final List fields; - private List> attributes; - private List interfaces; public ClassImpl(byte[] cfbytes, ClassFileImpl context) { this.reader = new ClassReaderImpl(cfbytes, context); @@ -128,27 +126,21 @@ public Optional superclass() { } @Override - public List interfaces() { - if (interfaces == null) { - int pos = reader.thisClassPos() + 4; - int cnt = reader.readU2(pos); + public const List interfaces() { + int pos = reader.thisClassPos() + 4; + int cnt = reader.readU2(pos); + pos += 2; + var arr = new Object[cnt]; + for (int i = 0; i < cnt; ++i) { + arr[i] = reader.readClassEntry(pos); pos += 2; - var arr = new Object[cnt]; - for (int i = 0; i < cnt; ++i) { - arr[i] = reader.readClassEntry(pos); - pos += 2; - } - this.interfaces = SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(arr); } - return interfaces; + return SharedSecrets.getJavaUtilCollectionAccess().listFromTrustedArray(arr); } @Override - public List> attributes() { - if (attributes == null) { - attributes = BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); - } - return attributes; + public const List> attributes() { + return BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); } // ClassModel diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java index 67bdaa481d372..82dd668aa8b51 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java @@ -63,8 +63,6 @@ public final class ClassReaderImpl private final Function> attributeMapper; private final int flags; private final int thisClassPos; - private ClassEntry thisClass; - private Optional superclass; private final int constantPoolCount; private final int[] cpOffset; @@ -73,8 +71,6 @@ public final class ClassReaderImpl final PoolEntry[] cp; private ClassModel containedClass; - private List bsmEntries; - private BootstrapMethodsAttribute bootstrapMethodsAttribute; ClassReaderImpl(byte[] classfileBytes, ClassFileImpl context) { @@ -148,20 +144,14 @@ public int flags() { } @Override - public ClassEntry thisClassEntry() { - if (thisClass == null) { - thisClass = readEntry(thisClassPos, ClassEntry.class); - } - return thisClass; + public const ClassEntry thisClassEntry() { + return readEntry(thisClassPos, ClassEntry.class); } @Override - public Optional superclassEntry() { - if (superclass == null) { - int scIndex = readU2(thisClassPos + 2); - superclass = Optional.ofNullable(scIndex == 0 ? null : (ClassEntry) entryByIndex(scIndex)); - } - return superclass; + public const Optional superclassEntry() { + int scIndex = readU2(thisClassPos + 2); + return Optional.ofNullable(scIndex == 0 ? null : (ClassEntry) entryByIndex(scIndex)); } @Override @@ -250,29 +240,21 @@ public void copyBytesTo(BufWriter buf, int p, int len) { buf.writeBytes(buffer, p, len); } - BootstrapMethodsAttribute bootstrapMethodsAttribute() { - - if (bootstrapMethodsAttribute == null) { - bootstrapMethodsAttribute - = containedClass.findAttribute(Attributes.BOOTSTRAP_METHODS) - .orElse(new UnboundAttribute.EmptyBootstrapAttribute()); - } - - return bootstrapMethodsAttribute; + const BootstrapMethodsAttribute bootstrapMethodsAttribute() { + return containedClass.findAttribute(Attributes.BOOTSTRAP_METHODS) + .orElse(new UnboundAttribute.EmptyBootstrapAttribute()); } - List bsmEntries() { - if (bsmEntries == null) { - bsmEntries = new ArrayList<>(); - BootstrapMethodsAttribute attr = bootstrapMethodsAttribute(); - List list = attr.bootstrapMethods(); - if (!list.isEmpty()) { - for (BootstrapMethodEntry bm : list) { - AbstractPoolEntry.MethodHandleEntryImpl handle = (AbstractPoolEntry.MethodHandleEntryImpl) bm.bootstrapMethod(); - List args = bm.arguments(); - int hash = BootstrapMethodEntryImpl.computeHashCode(handle, args); - bsmEntries.add(new BootstrapMethodEntryImpl(this, bsmEntries.size(), hash, handle, args)); - } + const List bsmEntries() { + List bsmEntries = new ArrayList<>(); + BootstrapMethodsAttribute attr = bootstrapMethodsAttribute(); + List list = attr.bootstrapMethods(); + if (!list.isEmpty()) { + for (BootstrapMethodEntry bm : list) { + AbstractPoolEntry.MethodHandleEntryImpl handle = (AbstractPoolEntry.MethodHandleEntryImpl) bm.bootstrapMethod(); + List args = bm.arguments(); + int hash = BootstrapMethodEntryImpl.computeHashCode(handle, args); + bsmEntries.add(new BootstrapMethodEntryImpl(this, bsmEntries.size(), hash, handle, args)); } } return bsmEntries; diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java index 394f7cc807aa4..a6a9ac17a45ca 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/CodeImpl.java @@ -68,9 +68,6 @@ public final class CodeImpl } } - List exceptionTable; - List> attributes; - // Inflated for iteration LabelImpl[] labels; int[] lineNumbers; @@ -132,11 +129,8 @@ private void inflateMetadata() { // CodeAttribute @Override - public List> attributes() { - if (attributes == null) { - attributes = BoundAttribute.readAttributes(this, classReader, attributePos, classReader.customAttributes()); - } - return attributes; + public const List> attributes() { + return BoundAttribute.readAttributes(this, classReader, attributePos, classReader.customAttributes()); } @Override @@ -190,22 +184,19 @@ public void forEachElement(Consumer consumer) { } @Override - public List exceptionHandlers() { - if (exceptionTable == null) { - inflateMetadata(); - exceptionTable = new ArrayList<>(exceptionHandlerCnt); - iterateExceptionHandlers(new ExceptionHandlerAction() { - @Override - public void accept(int s, int e, int h, int c) { - ClassEntry catchTypeEntry = c == 0 - ? null - : (ClassEntry) constantPool().entryByIndex(c); - exceptionTable.add(new AbstractPseudoInstruction.ExceptionCatchImpl(getLabel(h), getLabel(s), getLabel(e), catchTypeEntry)); - } - }); - exceptionTable = Collections.unmodifiableList(exceptionTable); - } - return exceptionTable; + public const List exceptionHandlers() { + inflateMetadata(); + List exceptionTable = new ArrayList<>(exceptionHandlerCnt); + iterateExceptionHandlers(new ExceptionHandlerAction() { + @Override + public void accept(int s, int e, int h, int c) { + ClassEntry catchTypeEntry = c == 0 + ? null + : (ClassEntry) constantPool().entryByIndex(c); + exceptionTable.add(new AbstractPseudoInstruction.ExceptionCatchImpl(getLabel(h), getLabel(s), getLabel(e), catchTypeEntry)); + } + }); + return Collections.unmodifiableList(exceptionTable); } public boolean compareCodeBytes(BufWriter buf, int offset, int len) { diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/DirectMethodBuilder.java b/src/java.base/share/classes/jdk/internal/classfile/impl/DirectMethodBuilder.java index 50daf24305666..068b63b9a6e9e 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/DirectMethodBuilder.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/DirectMethodBuilder.java @@ -47,8 +47,6 @@ public final class DirectMethodBuilder final Utf8Entry name; final Utf8Entry desc; int flags; - int[] parameterSlots; - MethodTypeDesc mDesc; public DirectMethodBuilder(SplitConstantPool constantPool, ClassFileImpl context, @@ -82,15 +80,12 @@ public Utf8Entry methodType() { } @Override - public MethodTypeDesc methodTypeSymbol() { - if (mDesc == null) { - if (original instanceof MethodInfo mi) { - mDesc = mi.methodTypeSymbol(); - } else { - mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue()); - } + public const MethodTypeDesc methodTypeSymbol() { + if (original instanceof MethodInfo mi) { + return mi.methodTypeSymbol(); + } else { + return MethodTypeDesc.ofDescriptor(methodType().stringValue()); } - return mDesc; } @Override @@ -98,11 +93,13 @@ public int methodFlags() { return flags; } + private const int[] parameterSlots() { + return Util.parseParameterSlots(methodFlags(), methodTypeSymbol()); + } + @Override public int parameterSlot(int paramNo) { - if (parameterSlots == null) - parameterSlots = Util.parseParameterSlots(methodFlags(), methodTypeSymbol()); - return parameterSlots[paramNo]; + return parameterSlots()[paramNo]; } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java index 6645ddb9396ab..2ecfd76dd7974 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/FieldImpl.java @@ -37,7 +37,6 @@ public final class FieldImpl private final ClassReader reader; private final int startPos, endPos, attributesPos; - private List> attributes; public FieldImpl(ClassReader reader, int startPos, int endPos, int attributesPos) { this.reader = reader; @@ -70,11 +69,8 @@ public Utf8Entry fieldType() { } @Override - public List> attributes() { - if (attributes == null) { - attributes = BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); - } - return attributes; + public const List> attributes() { + return BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java index 0baaa5865aec8..436eb55ba06d6 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/MethodImpl.java @@ -38,9 +38,6 @@ public final class MethodImpl private final ClassReader reader; private final int startPos, endPos, attributesPos; - private List> attributes; - private int[] parameterSlots; - private MethodTypeDesc mDesc; public MethodImpl(ClassReader reader, int startPos, int endPos, int attrStart) { this.reader = reader; @@ -73,11 +70,8 @@ public Utf8Entry methodType() { } @Override - public MethodTypeDesc methodTypeSymbol() { - if (mDesc == null) { - mDesc = MethodTypeDesc.ofDescriptor(methodType().stringValue()); - } - return mDesc; + public const MethodTypeDesc methodTypeSymbol() { + return MethodTypeDesc.ofDescriptor(methodType().stringValue()); } @Override @@ -85,19 +79,18 @@ public int methodFlags() { return reader.readU2(startPos); } + private const int[] parameterSlots() { + return Util.parseParameterSlots(methodFlags(), methodTypeSymbol()); + } + @Override public int parameterSlot(int paramNo) { - if (parameterSlots == null) - parameterSlots = Util.parseParameterSlots(methodFlags(), methodTypeSymbol()); - return parameterSlots[paramNo]; + return parameterSlots()[paramNo]; } @Override - public List> attributes() { - if (attributes == null) { - attributes = BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); - } - return attributes; + public const List> attributes() { + return BoundAttribute.readAttributes(this, reader, attributesPos, reader.customAttributes()); } @Override diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/SplitConstantPool.java b/src/java.base/share/classes/jdk/internal/classfile/impl/SplitConstantPool.java index eb5aab90bb016..a23ba59d55b8e 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/SplitConstantPool.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/SplitConstantPool.java @@ -65,8 +65,6 @@ public final class SplitConstantPool implements ConstantPoolBuilder { private PoolEntry[] myEntries; private BootstrapMethodEntryImpl[] myBsmEntries; private boolean doneFullScan; - private EntryMap map; - private EntryMap bsmMap; public SplitConstantPool() { this.size = 1; @@ -175,35 +173,34 @@ public void writeTo(BufWriter buf) { } } - private EntryMap map() { - if (map == null) { - map = new EntryMap<>(Math.max(size, 1024), .75f) { - @Override - protected PoolEntry fetchElement(int index) { - return entryByIndex(index); - } - }; - // Doing a full scan here yields fall-off-the-cliff performance results, - // especially if we only need a few entries that are already - // inflated (such as attribute names). - // So we inflate the map with whatever we've got from the parent, and - // later, if we miss, we do a one-time full inflation before creating - // a new entry. - for (int i=1; i map() { + EntryMap map = new EntryMap<>(Math.max(size, 1024), .75f) { + @Override + protected PoolEntry fetchElement(int index) { + return entryByIndex(index); } - for (int i = Math.max(parentSize, 1); i < size; ) { - PoolEntry cpi = myEntries[i - parentSize]; + }; + // Doing a full scan here yields fall-off-the-cliff performance results, + // especially if we only need a few entries that are already + // inflated (such as attribute names). + // So we inflate the map with whatever we've got from the parent, and + // later, if we miss, we do a one-time full inflation before creating + // a new entry. + for (int i=1; i bsmMap() { - if (bsmMap == null) { - bsmMap = new EntryMap<>(Math.max(bsmSize, 16), .75f) { - @Override - protected BootstrapMethodEntryImpl fetchElement(int index) { - return bootstrapMethodEntry(index); - } - }; - for (int i=0; i bsmMap() { + EntryMap bsmMap = new EntryMap<>(Math.max(bsmSize, 16), .75f) { + @Override + protected BootstrapMethodEntryImpl fetchElement(int index) { + return bootstrapMethodEntry(index); } + }; + for (int i=0; i