从 java class 文件中的十六进制值解释访问标志

Interpreting access flags from hex value in java class file

这是一个 java class 文件的十六进制转储,幻数、minor_version、major_version 和常量池已被排除。

我想找到 class 的访问标志。如果我理解正确,访问标志将是此十六进制转储中的第一个值,因为前面的值已被排除。

这给了我值 04 和 21 作为访问标志。我的问题是这些与访问标志值不对应(没有 21)。我如何解释这些值以从下面访问标志 table?

ACC_PUBLIC  0x0001  Declared public; may be accessed from outside its package.
ACC_FINAL   0x0010  Declared final; no subclasses allowed.
ACC_SUPER   0x0020  Treat superclass methods specially when invoked by the invokespecial instruction.
ACC_INTERFACE   0x0200  Is an interface, not a class.
ACC_ABSTRACT    0x0400  Declared abstract; must not be instantiated.
ACC_SYNTHETIC   0x1000  Declared synthetic; not present in the source code.
ACC_ANNOTATION  0x2000  Declared as an annotation type.
ACC_ENUM    0x4000  Declared as an enum type.

您似乎对访问标志是 bitmask 这一事实感到困惑。也就是说,将相关标志“或”在一起以获得位掩码。在 0x0421 的情况下是 ACC_ABSTRACT | ACC_SUPER | ACC_PUBLIC == 0x0400 | 0x0020 | 0x0001 == 0x0421.

你可以测试一个标志,例如ACC_ABSTRACT,通过将标志常量与位掩码进行“与”运算来设置。如果它非零,则设置标志。例如,myFlags & ACC_ABSTRACT != 0

您也可以使用 Apache Common 的 ClassParser。它已经为您实现了 class 文件解析。

因为所有的Class access and property modifiers of specific attribute value(True or False) will attend the or operation都建立了整面旗帜。假设 class 是:

  • Public 0x0001
  • 摘要0x0400

最后的标志将是

 0x0001|0x0400=0x0401
 # expand them to do the or operation:
                   Public
                   |
                   v
 0000 0000 0000 0001  <-- this is 0x0001
 OR
 0000 0100 0000 0000  <-- this is 0x0400
       ^
       |
      Abstract
 =
 0000 0100 0000 0001 <-- flag is 0x0401

所以根据你的class的标志(0x0421),属性应该是:

  • 摘要0x0400
  • ACC_SUPER 0x0020
  • Public 0x0001

这将构建 0x0421 的标志。