<div dir="ltr">Hi ClassFile API list,<div>I call for addition of a type-checked entryByIndex in ConstantPool, with a signature:</div><div><T extends PoolEntry> T entryByIndex(int index, Class<T> cls)<br></div><div>and a type-checked readEntryOrNull in ClassReader, with a signature:</div><div><T extends PoolEntry> T readEntryOrNull(int offset, Class<T> cls)</div><div>Which will throw ConstantPoolException if the entry is of a mismatched type, much like the type-checked readClassEntry in ClassBuilder.</div><div><br></div><div>A search for existing generic ConstantPool::entryByIndex and ClassReader::readEntryOrNull in JDK reveals that most of their usages within the jdk.internal.classfile.impl and its subpackages involve a direct cast right after retrieving the result. These casts are susceptible to malformed classfiles putting entries of wrong type, such as a Utf8 at the cursor of superclass entry, throwing ClassCastException, which is out of spec with the Classfile API.</div><div><br></div><div>I recommend adding these 2 methods for user convenience, and migrating all existing entryByIndex/readEntryOrNull with casts to these 2 new methods, to enhance the robustness of the ClassFile API. (On a side note, we can promote ClassReader::utf8EntryByIndex to ConstantPool too)</div><div><br></div><div>Please feel free to comment or critique this proposal.</div><div><br></div><div>Chen Liang</div></div>