在Java类文件规范中,在关于长和双表示的部分中,文档说:
CONSTANT_Long_info和CONSTANT_Double_info表示8字节数字(长和双)常量:
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}所有8字节常量都占用类文件的constant_pool表中的两个条目。如果CONSTANT_Long_info或CONSTANT_Double_info结构是索引n处的constant_pool表中的项,则池中的下一个可用项位于索引n+2。constant_pool索引n+1必须有效,但被认为不可用。
我怀疑的是,tag of entry in constant_pool in Indexn+1。
发布于 2015-12-19 01:50:58
索引n+1没有标记,因为索引n+1没有常量池条目。就像索引0。没有它的数据,它直接跳到下一个条目。
发布于 2015-12-18 19:03:49
通过尝试和错误,在塔索斯的帮助下,我得到了一个解决方案。当在索引n+1 n处发现常量长或双倍时,的索引不是美元,只有。
我将用代码显示:
List<Constant> constants = new ArrayList<>();
byte[] bytecode = ... // reads the class byte code
int constantPoolCount = getConstantPoolCount(bytecode);
int offset = getConstantPoolStartOffset(bytecode);
for(int i = 1; i < constantPoolCount; i++){
int tag = bytecode[offset];
offset++;
if(tag == LONG_CONSTANT_TAG) {
byte[] highBytes = Arrays.copyOfRange(bytecode, offset, offset+4);
offset += 4;
byte[] lowBytes = Arrays.copyOfRange(bytecode, offset, offset+4);
offset += 4;
ConstantLong c = new ConstantLong(highBytes, lowBytes);
constants.add(c);
// because we are at CONSTANT LONG, the entry at n+1 is not used
constants.add(null);
i++;
} else if(tag == DOUBLE_CONSTANT_TAG) {
... // performs the same, like LONG_CONSTANT_TAG
}
// read more entries without skip indexes.
}注意,constants列表从零开始索引,而constant_pool字节码中的条目从1开始索引。
https://stackoverflow.com/questions/34361378
复制相似问题