8339198: Remove tag field from AbstractPoolEntry

Reviewed-by: asotona, redestad
This commit is contained in:
Chen Liang 2024-09-20 16:11:39 +00:00
parent 90d3a64b0a
commit ab81197d0d

View File

@ -57,6 +57,8 @@ import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ArraysSupport; import jdk.internal.util.ArraysSupport;
import jdk.internal.vm.annotation.Stable; import jdk.internal.vm.annotation.Stable;
import static java.lang.classfile.ClassFile.*;
public abstract sealed class AbstractPoolEntry { public abstract sealed class AbstractPoolEntry {
/* /*
Invariant: a {CP,BSM} entry for pool P refer only to {CP,BSM} entries Invariant: a {CP,BSM} entry for pool P refer only to {CP,BSM} entries
@ -96,12 +98,10 @@ public abstract sealed class AbstractPoolEntry {
} }
final ConstantPool constantPool; final ConstantPool constantPool;
public final byte tag;
private final int index; private final int index;
private final int hash; private final int hash;
private AbstractPoolEntry(ConstantPool constantPool, int tag, int index, int hash) { private AbstractPoolEntry(ConstantPool constantPool, int index, int hash) {
this.tag = (byte) tag;
this.index = index; this.index = index;
this.hash = hash; this.hash = hash;
this.constantPool = constantPool; this.constantPool = constantPool;
@ -116,12 +116,10 @@ public abstract sealed class AbstractPoolEntry {
return hash; return hash;
} }
public byte tag() { public abstract byte tag();
return tag;
}
public int width() { public int width() {
return (tag == ClassFile.TAG_LONG || tag == ClassFile.TAG_DOUBLE) ? 2 : 1; return 1;
} }
abstract void writeTo(BufWriterImpl buf); abstract void writeTo(BufWriterImpl buf);
@ -159,7 +157,7 @@ public abstract sealed class AbstractPoolEntry {
Utf8EntryImpl(ConstantPool cpm, int index, Utf8EntryImpl(ConstantPool cpm, int index,
byte[] rawBytes, int offset, int rawLen) { byte[] rawBytes, int offset, int rawLen) {
super(cpm, ClassFile.TAG_UTF8, index, 0); super(cpm, index, 0);
this.rawBytes = rawBytes; this.rawBytes = rawBytes;
this.offset = offset; this.offset = offset;
this.rawLen = rawLen; this.rawLen = rawLen;
@ -171,7 +169,7 @@ public abstract sealed class AbstractPoolEntry {
} }
Utf8EntryImpl(ConstantPool cpm, int index, String s, int hash) { Utf8EntryImpl(ConstantPool cpm, int index, String s, int hash) {
super(cpm, ClassFile.TAG_UTF8, index, 0); super(cpm, index, 0);
this.rawBytes = null; this.rawBytes = null;
this.offset = 0; this.offset = 0;
this.rawLen = 0; this.rawLen = 0;
@ -182,7 +180,7 @@ public abstract sealed class AbstractPoolEntry {
} }
Utf8EntryImpl(ConstantPool cpm, int index, Utf8EntryImpl u) { Utf8EntryImpl(ConstantPool cpm, int index, Utf8EntryImpl u) {
super(cpm, ClassFile.TAG_UTF8, index, 0); super(cpm, index, 0);
this.rawBytes = u.rawBytes; this.rawBytes = u.rawBytes;
this.offset = u.offset; this.offset = u.offset;
this.rawLen = u.rawLen; this.rawLen = u.rawLen;
@ -194,6 +192,11 @@ public abstract sealed class AbstractPoolEntry {
this.typeSym = u.typeSym; this.typeSym = u.typeSym;
} }
@Override
public byte tag() {
return TAG_UTF8;
}
/** /**
* {@jvms 4.4.7} String content is encoded in modified UTF-8. * {@jvms 4.4.7} String content is encoded in modified UTF-8.
* *
@ -417,7 +420,7 @@ public abstract sealed class AbstractPoolEntry {
@Override @Override
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(TAG_UTF8);
if (rawBytes != null) { if (rawBytes != null) {
pool.writeU2(rawLen); pool.writeU2(rawLen);
pool.writeBytes(rawBytes, offset, rawLen); pool.writeBytes(rawBytes, offset, rawLen);
@ -449,7 +452,7 @@ public abstract sealed class AbstractPoolEntry {
protected final T ref1; protected final T ref1;
public AbstractRefEntry(ConstantPool constantPool, int tag, int index, T ref1) { public AbstractRefEntry(ConstantPool constantPool, int tag, int index, T ref1) {
super(constantPool, tag, index, hash1(tag, ref1.index())); super(constantPool, index, hash1(tag, ref1.index()));
this.ref1 = ref1; this.ref1 = ref1;
} }
@ -458,7 +461,7 @@ public abstract sealed class AbstractPoolEntry {
} }
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(tag());
pool.writeU2(ref1.index()); pool.writeU2(ref1.index());
} }
@ -474,7 +477,7 @@ public abstract sealed class AbstractPoolEntry {
protected final U ref2; protected final U ref2;
public AbstractRefsEntry(ConstantPool constantPool, int tag, int index, T ref1, U ref2) { public AbstractRefsEntry(ConstantPool constantPool, int tag, int index, T ref1, U ref2) {
super(constantPool, tag, index, hash2(tag, ref1.index(), ref2.index())); super(constantPool, index, hash2(tag, ref1.index(), ref2.index()));
this.ref1 = ref1; this.ref1 = ref1;
this.ref2 = ref2; this.ref2 = ref2;
} }
@ -488,7 +491,7 @@ public abstract sealed class AbstractPoolEntry {
} }
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(tag());
pool.writeU2(ref1.index()); pool.writeU2(ref1.index());
pool.writeU2(ref2.index()); pool.writeU2(ref2.index());
} }
@ -519,7 +522,12 @@ public abstract sealed class AbstractPoolEntry {
public ClassDesc sym = null; public ClassDesc sym = null;
ClassEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) { ClassEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) {
super(cpm, ClassFile.TAG_CLASS, index, name); super(cpm, TAG_CLASS, index, name);
}
@Override
public byte tag() {
return TAG_CLASS;
} }
@Override @Override
@ -557,7 +565,12 @@ public abstract sealed class AbstractPoolEntry {
public static final class PackageEntryImpl extends AbstractNamedEntry implements PackageEntry { public static final class PackageEntryImpl extends AbstractNamedEntry implements PackageEntry {
PackageEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) { PackageEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) {
super(cpm, ClassFile.TAG_PACKAGE, index, name); super(cpm, TAG_PACKAGE, index, name);
}
@Override
public byte tag() {
return TAG_PACKAGE;
} }
@Override @Override
@ -583,7 +596,12 @@ public abstract sealed class AbstractPoolEntry {
public static final class ModuleEntryImpl extends AbstractNamedEntry implements ModuleEntry { public static final class ModuleEntryImpl extends AbstractNamedEntry implements ModuleEntry {
ModuleEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) { ModuleEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name) {
super(cpm, ClassFile.TAG_MODULE, index, name); super(cpm, TAG_MODULE, index, name);
}
@Override
public byte tag() {
return TAG_MODULE;
} }
@Override @Override
@ -610,7 +628,12 @@ public abstract sealed class AbstractPoolEntry {
implements NameAndTypeEntry { implements NameAndTypeEntry {
NameAndTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name, Utf8EntryImpl type) { NameAndTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl name, Utf8EntryImpl type) {
super(cpm, ClassFile.TAG_NAMEANDTYPE, index, name, type); super(cpm, TAG_NAMEANDTYPE, index, name, type);
}
@Override
public byte tag() {
return TAG_NAMEANDTYPE;
} }
@Override @Override
@ -670,7 +693,7 @@ public abstract sealed class AbstractPoolEntry {
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o instanceof AbstractMemberRefEntry m) { if (o instanceof AbstractMemberRefEntry m) {
return tag == m.tag() return tag() == m.tag()
&& owner().equals(m.owner()) && owner().equals(m.owner())
&& nameAndType().equals(m.nameAndType()); && nameAndType().equals(m.nameAndType());
} }
@ -682,7 +705,12 @@ public abstract sealed class AbstractPoolEntry {
FieldRefEntryImpl(ConstantPool cpm, int index, FieldRefEntryImpl(ConstantPool cpm, int index,
ClassEntryImpl owner, NameAndTypeEntryImpl nameAndType) { ClassEntryImpl owner, NameAndTypeEntryImpl nameAndType) {
super(cpm, ClassFile.TAG_FIELDREF, index, owner, nameAndType); super(cpm, TAG_FIELDREF, index, owner, nameAndType);
}
@Override
public byte tag() {
return TAG_FIELDREF;
} }
@Override @Override
@ -698,6 +726,11 @@ public abstract sealed class AbstractPoolEntry {
super(cpm, ClassFile.TAG_METHODREF, index, owner, nameAndType); super(cpm, ClassFile.TAG_METHODREF, index, owner, nameAndType);
} }
@Override
public byte tag() {
return TAG_METHODREF;
}
@Override @Override
public MethodRefEntry clone(ConstantPoolBuilder cp) { public MethodRefEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.methodRefEntry(ref1, ref2); return cp.canWriteDirect(constantPool) ? this : cp.methodRefEntry(ref1, ref2);
@ -711,6 +744,11 @@ public abstract sealed class AbstractPoolEntry {
super(cpm, ClassFile.TAG_INTERFACEMETHODREF, index, owner, nameAndType); super(cpm, ClassFile.TAG_INTERFACEMETHODREF, index, owner, nameAndType);
} }
@Override
public byte tag() {
return TAG_INTERFACEMETHODREF;
}
@Override @Override
public InterfaceMethodRefEntry clone(ConstantPoolBuilder cp) { public InterfaceMethodRefEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.interfaceMethodRefEntry(ref1, ref2); return cp.canWriteDirect(constantPool) ? this : cp.interfaceMethodRefEntry(ref1, ref2);
@ -723,17 +761,17 @@ public abstract sealed class AbstractPoolEntry {
private BootstrapMethodEntryImpl bootstrapMethod; private BootstrapMethodEntryImpl bootstrapMethod;
private final NameAndTypeEntryImpl nameAndType; private final NameAndTypeEntryImpl nameAndType;
AbstractDynamicConstantPoolEntry(ConstantPool cpm, int tag, int index, int hash, BootstrapMethodEntryImpl bootstrapMethod, AbstractDynamicConstantPoolEntry(ConstantPool cpm, int index, int hash, BootstrapMethodEntryImpl bootstrapMethod,
NameAndTypeEntryImpl nameAndType) { NameAndTypeEntryImpl nameAndType) {
super(cpm, tag, index, hash); super(cpm, index, hash);
this.bsmIndex = bootstrapMethod.bsmIndex(); this.bsmIndex = bootstrapMethod.bsmIndex();
this.bootstrapMethod = bootstrapMethod; this.bootstrapMethod = bootstrapMethod;
this.nameAndType = nameAndType; this.nameAndType = nameAndType;
} }
AbstractDynamicConstantPoolEntry(ConstantPool cpm, int tag, int index, int hash, int bsmIndex, AbstractDynamicConstantPoolEntry(ConstantPool cpm, int index, int hash, int bsmIndex,
NameAndTypeEntryImpl nameAndType) { NameAndTypeEntryImpl nameAndType) {
super(cpm, tag, index, hash); super(cpm, index, hash);
this.bsmIndex = bsmIndex; this.bsmIndex = bsmIndex;
this.bootstrapMethod = null; this.bootstrapMethod = null;
this.nameAndType = nameAndType; this.nameAndType = nameAndType;
@ -764,7 +802,7 @@ public abstract sealed class AbstractPoolEntry {
} }
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(tag());
pool.writeU2(bsmIndex); pool.writeU2(bsmIndex);
pool.writeU2(nameAndType.index()); pool.writeU2(nameAndType.index());
} }
@ -793,15 +831,20 @@ public abstract sealed class AbstractPoolEntry {
InvokeDynamicEntryImpl(ConstantPool cpm, int index, int hash, BootstrapMethodEntryImpl bootstrapMethod, InvokeDynamicEntryImpl(ConstantPool cpm, int index, int hash, BootstrapMethodEntryImpl bootstrapMethod,
NameAndTypeEntryImpl nameAndType) { NameAndTypeEntryImpl nameAndType) {
super(cpm, ClassFile.TAG_INVOKEDYNAMIC, index, hash, bootstrapMethod, nameAndType); super(cpm, index, hash, bootstrapMethod, nameAndType);
} }
InvokeDynamicEntryImpl(ConstantPool cpm, int index, int bsmIndex, InvokeDynamicEntryImpl(ConstantPool cpm, int index, int bsmIndex,
NameAndTypeEntryImpl nameAndType) { NameAndTypeEntryImpl nameAndType) {
super(cpm, ClassFile.TAG_INVOKEDYNAMIC, index, hash2(ClassFile.TAG_INVOKEDYNAMIC, bsmIndex, nameAndType.index()), super(cpm, index, hash2(TAG_INVOKEDYNAMIC, bsmIndex, nameAndType.index()),
bsmIndex, nameAndType); bsmIndex, nameAndType);
} }
@Override
public byte tag() {
return TAG_INVOKEDYNAMIC;
}
@Override @Override
public InvokeDynamicEntry clone(ConstantPoolBuilder cp) { public InvokeDynamicEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.invokeDynamicEntry(bootstrap(), nameAndType()); return cp.canWriteDirect(constantPool) ? this : cp.invokeDynamicEntry(bootstrap(), nameAndType());
@ -813,15 +856,20 @@ public abstract sealed class AbstractPoolEntry {
ConstantDynamicEntryImpl(ConstantPool cpm, int index, int hash, BootstrapMethodEntryImpl bootstrapMethod, ConstantDynamicEntryImpl(ConstantPool cpm, int index, int hash, BootstrapMethodEntryImpl bootstrapMethod,
NameAndTypeEntryImpl nameAndType) { NameAndTypeEntryImpl nameAndType) {
super(cpm, ClassFile.TAG_CONSTANTDYNAMIC, index, hash, bootstrapMethod, nameAndType); super(cpm, index, hash, bootstrapMethod, nameAndType);
} }
ConstantDynamicEntryImpl(ConstantPool cpm, int index, int bsmIndex, ConstantDynamicEntryImpl(ConstantPool cpm, int index, int bsmIndex,
NameAndTypeEntryImpl nameAndType) { NameAndTypeEntryImpl nameAndType) {
super(cpm, ClassFile.TAG_CONSTANTDYNAMIC, index, hash2(ClassFile.TAG_CONSTANTDYNAMIC, bsmIndex, nameAndType.index()), super(cpm, index, hash2(TAG_CONSTANTDYNAMIC, bsmIndex, nameAndType.index()),
bsmIndex, nameAndType); bsmIndex, nameAndType);
} }
@Override
public byte tag() {
return TAG_CONSTANTDYNAMIC;
}
@Override @Override
public ConstantDynamicEntry clone(ConstantPoolBuilder cp) { public ConstantDynamicEntry clone(ConstantPoolBuilder cp) {
return cp.canWriteDirect(constantPool) ? this : cp.constantDynamicEntry(bootstrap(), nameAndType()); return cp.canWriteDirect(constantPool) ? this : cp.constantDynamicEntry(bootstrap(), nameAndType());
@ -836,18 +884,23 @@ public abstract sealed class AbstractPoolEntry {
MethodHandleEntryImpl(ConstantPool cpm, int index, int hash, int refKind, AbstractPoolEntry.AbstractMemberRefEntry MethodHandleEntryImpl(ConstantPool cpm, int index, int hash, int refKind, AbstractPoolEntry.AbstractMemberRefEntry
reference) { reference) {
super(cpm, ClassFile.TAG_METHODHANDLE, index, hash); super(cpm, index, hash);
this.refKind = refKind; this.refKind = refKind;
this.reference = reference; this.reference = reference;
} }
MethodHandleEntryImpl(ConstantPool cpm, int index, int refKind, AbstractPoolEntry.AbstractMemberRefEntry MethodHandleEntryImpl(ConstantPool cpm, int index, int refKind, AbstractPoolEntry.AbstractMemberRefEntry
reference) { reference) {
super(cpm, ClassFile.TAG_METHODHANDLE, index, hash2(ClassFile.TAG_METHODHANDLE, refKind, reference.index())); super(cpm, index, hash2(ClassFile.TAG_METHODHANDLE, refKind, reference.index()));
this.refKind = refKind; this.refKind = refKind;
this.reference = reference; this.reference = reference;
} }
@Override
public byte tag() {
return TAG_METHODHANDLE;
}
@Override @Override
public int kind() { public int kind() {
return refKind; return refKind;
@ -869,7 +922,7 @@ public abstract sealed class AbstractPoolEntry {
@Override @Override
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(TAG_METHODHANDLE);
pool.writeU1(refKind); pool.writeU1(refKind);
pool.writeU2(reference.index()); pool.writeU2(reference.index());
} }
@ -901,7 +954,12 @@ public abstract sealed class AbstractPoolEntry {
implements MethodTypeEntry { implements MethodTypeEntry {
MethodTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl descriptor) { MethodTypeEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl descriptor) {
super(cpm, ClassFile.TAG_METHODTYPE, index, descriptor); super(cpm, TAG_METHODTYPE, index, descriptor);
}
@Override
public byte tag() {
return TAG_METHODTYPE;
} }
@Override @Override
@ -937,7 +995,12 @@ public abstract sealed class AbstractPoolEntry {
implements StringEntry { implements StringEntry {
StringEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl utf8) { StringEntryImpl(ConstantPool cpm, int index, Utf8EntryImpl utf8) {
super(cpm, ClassFile.TAG_STRING, index, utf8); super(cpm, TAG_STRING, index, utf8);
}
@Override
public byte tag() {
return TAG_STRING;
} }
@Override @Override
@ -983,13 +1046,18 @@ public abstract sealed class AbstractPoolEntry {
private final int val; private final int val;
IntegerEntryImpl(ConstantPool cpm, int index, int i) { IntegerEntryImpl(ConstantPool cpm, int index, int i) {
super(cpm, ClassFile.TAG_INTEGER, index, hash1(ClassFile.TAG_INTEGER, Integer.hashCode(i))); super(cpm, index, hash1(ClassFile.TAG_INTEGER, Integer.hashCode(i)));
val = i; val = i;
} }
@Override
public byte tag() {
return TAG_INTEGER;
}
@Override @Override
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(TAG_INTEGER);
pool.writeInt(val); pool.writeInt(val);
} }
@ -1024,13 +1092,18 @@ public abstract sealed class AbstractPoolEntry {
private final float val; private final float val;
FloatEntryImpl(ConstantPool cpm, int index, float f) { FloatEntryImpl(ConstantPool cpm, int index, float f) {
super(cpm, ClassFile.TAG_FLOAT, index, hash1(ClassFile.TAG_FLOAT, Float.hashCode(f))); super(cpm, index, hash1(ClassFile.TAG_FLOAT, Float.hashCode(f)));
val = f; val = f;
} }
@Override
public byte tag() {
return TAG_FLOAT;
}
@Override @Override
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(TAG_FLOAT);
pool.writeFloat(val); pool.writeFloat(val);
} }
@ -1064,13 +1137,23 @@ public abstract sealed class AbstractPoolEntry {
private final long val; private final long val;
LongEntryImpl(ConstantPool cpm, int index, long l) { LongEntryImpl(ConstantPool cpm, int index, long l) {
super(cpm, ClassFile.TAG_LONG, index, hash1(ClassFile.TAG_LONG, Long.hashCode(l))); super(cpm, index, hash1(ClassFile.TAG_LONG, Long.hashCode(l)));
val = l; val = l;
} }
@Override
public byte tag() {
return TAG_LONG;
}
@Override
public int width() {
return 2;
}
@Override @Override
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(TAG_LONG);
pool.writeLong(val); pool.writeLong(val);
} }
@ -1104,13 +1187,23 @@ public abstract sealed class AbstractPoolEntry {
private final double val; private final double val;
DoubleEntryImpl(ConstantPool cpm, int index, double d) { DoubleEntryImpl(ConstantPool cpm, int index, double d) {
super(cpm, ClassFile.TAG_DOUBLE, index, hash1(ClassFile.TAG_DOUBLE, Double.hashCode(d))); super(cpm, index, hash1(ClassFile.TAG_DOUBLE, Double.hashCode(d)));
val = d; val = d;
} }
@Override
public byte tag() {
return TAG_DOUBLE;
}
@Override
public int width() {
return 2;
}
@Override @Override
void writeTo(BufWriterImpl pool) { void writeTo(BufWriterImpl pool) {
pool.writeU1(tag); pool.writeU1(TAG_DOUBLE);
pool.writeDouble(val); pool.writeDouble(val);
} }