首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java类文件规范-双长表示

Java类文件规范-双长表示
EN

Stack Overflow用户
提问于 2015-12-18 17:58:00
回答 2查看 130关注 0票数 1

在Java类文件规范中,在关于长和双表示的部分中,文档说:

CONSTANT_Long_info和CONSTANT_Double_info表示8字节数字(长和双)常量:

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2015-12-19 01:50:58

索引n+1没有标记,因为索引n+1没有常量池条目。就像索引0。没有它的数据,它直接跳到下一个条目。

票数 1
EN

Stack Overflow用户

发布于 2015-12-18 19:03:49

通过尝试和错误,在塔索斯的帮助下,我得到了一个解决方案。当在索引n+1 n处发现常量长或双倍时,的索引不是美元,只有

我将用代码显示:

代码语言:javascript
复制
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开始索引。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34361378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档