Unexpected behaviour for ClassReader#readUTF8
Given the index to a
CONSTANT_Utf8_info struct within the constant pool, and a
ClassReader instance, I'm unable to read the
CONSTANT_Utf8_info struct without using reflection to call
ClassReader#readUTF. This is because the
readUTF8 actually reads a
CONSTANT_String_info struct. Is it possible to expose a method which allows reading a
CONSTANT_Utf8_info struct, or make
Can't you call readUTF8 with the offset in the class file where the index of the CONSTANT_Utf8_info is stored? Also note that the ClassReader.read* methods are designed for Attribute subclasses only, normally you should not need access to the constant pool and the API hides access to it on purpose (the read* methods are an exception, but this is needed to be able to implement custom class attributes).
Unfortunately, all I'm given is the index to the
CONSTANT_Utf8_info. I understand that the ASM project doesn't like to expose this level of access, but at this point my project is so dependent on ASM that the only alternative is to maintain a downstream fork forever. It would be much more convenient for myself and others in the future if ASM, as a bytecode manipulation framework, provided access to this information directly.
So you can use ClassReader.getItem() to get the start of the UTF8 string in ClassReader.b, and then use DataInputStream.readUTF + ByteArrayInputStream to read the string. Indeed, readUTF uses the same modified UTF-8 format as the one used in the class file (compare http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html and https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html#jvms-4.4.7).
Feels kind of hacky, but I suppose it's better than reflection. Thanks!
closedToggle commit list