Skip to content

Commit

Permalink
8343881: java.lang.classfile.Attribute attributeName() method should …
Browse files Browse the repository at this point in the history
…return Utf8Entry

Reviewed-by: liach
  • Loading branch information
asotona committed Nov 15, 2024
1 parent 75c651f commit ba39321
Show file tree
Hide file tree
Showing 29 changed files with 415 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package java.lang.classfile;

import java.lang.classfile.attribute.*;
import java.lang.classfile.constantpool.Utf8Entry;

import jdk.internal.classfile.impl.BoundAttribute;
import jdk.internal.classfile.impl.UnboundAttribute;
Expand Down Expand Up @@ -65,7 +66,7 @@ public sealed interface Attribute<A extends Attribute<A>>
/**
* {@return the name of the attribute}
*/
String attributeName();
Utf8Entry attributeName();

/**
* {@return the {@link AttributeMapper} associated with this attribute}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package java.lang.classfile;

import java.lang.classfile.constantpool.Utf8Entry;
import jdk.internal.classfile.impl.TemporaryConstantPool;
import jdk.internal.javac.PreviewFeature;

/**
Expand Down Expand Up @@ -55,8 +57,8 @@ public final AttributeMapper<T> attributeMapper() {
}

@Override
public final String attributeName() {
return mapper.name();
public Utf8Entry attributeName() {
return TemporaryConstantPool.INSTANCE.utf8Entry(mapper.name());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public final String name() {
@Override
public final void writeAttribute(BufWriter writer, T attr) {
BufWriterImpl buf = (BufWriterImpl) writer;
buf.writeIndex(buf.constantPool().utf8Entry(name));
buf.writeIndex(attr.attributeName());
int lengthIndex = buf.skip(4);
writeBody(buf, attr);
int written = buf.size() - lengthIndex - 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract sealed class BoundAttribute<T extends Attribute<T>>
private final AttributeMapper<T> mapper;
final ClassReaderImpl classReader;
final int payloadStart;
Utf8Entry name;

BoundAttribute(ClassReader classReader, AttributeMapper<T> mapper, int payloadStart) {
this.mapper = mapper;
Expand All @@ -59,8 +60,11 @@ public int payloadLen() {
}

@Override
public String attributeName() {
return mapper.name();
public Utf8Entry attributeName() {
if (name == null) {
name = classReader.readEntry(payloadStart - 6, Utf8Entry.class);
}
return name;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ private static MapNode classToTree(ClassModel clm, Verbosity verbosity) {
list("flags", "flag", clm.flags().flags().stream().map(AccessFlag::name)),
leaf("superclass", clm.superclass().map(ClassEntry::asInternalName).orElse("")),
list("interfaces", "interface", clm.interfaces().stream().map(ClassEntry::asInternalName)),
list("attributes", "attribute", clm.attributes().stream().map(Attribute::attributeName)))
list("attributes", "attribute", clm.attributes().stream().map(Attribute::attributeName).map(Utf8Entry::stringValue)))
.with(constantPoolToTree(clm.constantPool(), verbosity))
.with(attributesToTree(clm.attributes(), verbosity))
.with(new ListNodeImpl(BLOCK, "fields", clm.fields().stream().map(f ->
Expand Down Expand Up @@ -672,7 +672,7 @@ private static MapNode fieldToTree(FieldModel f, Verbosity verbosity) {
"flag", f.flags().flags().stream().map(AccessFlag::name)),
leaf("field type", f.fieldType().stringValue()),
list("attributes",
"attribute", f.attributes().stream().map(Attribute::attributeName)))
"attribute", f.attributes().stream().map(Attribute::attributeName).map(Utf8Entry::stringValue)))
.with(attributesToTree(f.attributes(), verbosity));
}

Expand All @@ -683,7 +683,7 @@ public static MapNode methodToTree(MethodModel m, Verbosity verbosity) {
"flag", m.flags().flags().stream().map(AccessFlag::name)),
leaf("method type", m.methodType().stringValue()),
list("attributes",
"attribute", m.attributes().stream().map(Attribute::attributeName)))
"attribute", m.attributes().stream().map(Attribute::attributeName).map(Utf8Entry::stringValue)))
.with(attributesToTree(m.attributes(), verbosity))
.with(codeToTree((CodeAttribute)m.code().orElse(null), verbosity));
}
Expand All @@ -694,7 +694,7 @@ private static MapNode codeToTree(CodeAttribute com, Verbosity verbosity) {
codeNode.with(leaf("max stack", com.maxStack()));
codeNode.with(leaf("max locals", com.maxLocals()));
codeNode.with(list("attributes",
"attribute", com.attributes().stream().map(Attribute::attributeName)));
"attribute", com.attributes().stream().map(Attribute::attributeName).map(Utf8Entry::stringValue)));
var stackMap = new MapNodeImpl(BLOCK, "stack map frames");
var visibleTypeAnnos = new LinkedHashMap<Integer, List<TypeAnnotation>>();
var invisibleTypeAnnos = new LinkedHashMap<Integer, List<TypeAnnotation>>();
Expand Down Expand Up @@ -996,7 +996,7 @@ private static Node[] attributesToTree(List<Attribute<?>> attributes, Verbosity
"name", rc.name().stringValue(),
"type", rc.descriptor().stringValue()))
.with(list("attributes", "attribute", rc.attributes().stream()
.map(Attribute::attributeName)))
.map(Attribute::attributeName).map(Utf8Entry::stringValue)))
.with(attributesToTree(rc.attributes(), verbosity)))));
case AnnotationDefaultAttribute ada ->
nodes.add(new MapNodeImpl(FLOW, "annotation default").with(elementValueToTree(ada.defaultValue())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ public void writeBody(BufWriterImpl b) {
if (crSize < characterRangesCount)
b.patchU2(pos, crSize);
}

@Override
public Utf8Entry attributeName() {
return constantPool.utf8Entry(Attributes.NAME_CHARACTER_RANGE_TABLE);
}
};
attributes.withAttribute(a);
}
Expand All @@ -265,6 +270,11 @@ public void writeBody(BufWriterImpl b) {
if (lvSize < localVariablesCount)
b.patchU2(pos, lvSize);
}

@Override
public Utf8Entry attributeName() {
return constantPool.utf8Entry(Attributes.NAME_LOCAL_VARIABLE_TABLE);
}
};
attributes.withAttribute(a);
}
Expand All @@ -289,6 +299,11 @@ public void writeBody(BufWriterImpl b) {
if (lvtSize < localVariableTypesCount)
b.patchU2(pos, lvtSize);
}

@Override
public Utf8Entry attributeName() {
return constantPool.utf8Entry(Attributes.NAME_LOCAL_VARIABLE_TYPE_TABLE);
}
};
attributes.withAttribute(a);
}
Expand Down Expand Up @@ -371,6 +386,11 @@ public void writeBody(BufWriterImpl buf) {
dcb.attributes.writeTo(buf);
buf.setLabelContext(null);
}

@Override
public Utf8Entry attributeName() {
return constantPool.utf8Entry(Attributes.NAME_CODE);
}
};
}

Expand Down Expand Up @@ -416,6 +436,11 @@ public void writeTo(BufWriterImpl b) {
b.writeU2(buf.size() / 4);
b.writeBytes(buf);
}

@Override
public Utf8Entry attributeName() {
return buf.constantPool().utf8Entry(Attributes.NAME_LINE_NUMBER_TABLE);
}
}

private boolean codeAndExceptionsMatch(int codeLength) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void writeBody(BufWriterImpl b) {
for (int i = 0; i < bsmSize; i++)
bootstrapMethodEntry(i).writeTo(buf);
}

@Override
public Utf8Entry attributeName() {
return utf8Entry(Attributes.NAME_BOOTSTRAP_METHODS);
}
};
a.writeTo(buf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.lang.classfile.constantpool.ConstantPoolBuilder;
import java.lang.classfile.constantpool.InvokeDynamicEntry;
import java.lang.classfile.constantpool.MemberRefEntry;
import java.lang.classfile.constantpool.Utf8Entry;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import java.util.ArrayList;
Expand Down Expand Up @@ -401,6 +402,11 @@ public void writeBody(BufWriterImpl b) {
prevFrame = fr;
}
}

@Override
public Utf8Entry attributeName() {
return cp.utf8Entry(Attributes.NAME_STACK_MAP_TABLE);
}
};
}

Expand Down
Loading

0 comments on commit ba39321

Please sign in to comment.