如何将 Opcodes 中 ASM 的 API 版本映射到 Java 版本?

Nat*_*han 4 java version java-bytecode-asm

ASMClassVisitor构造函数需要传递一个OpcodesASM4ASM5ASM6ASM7ASM8ASM9

我如何知道ASM#每个版本的 Java 使用哪个?我ASM#将在 Java 8 中使用什么?我ASM#将在 Java 11 中使用什么?

Hol*_*ger 6

这些ASM…常量描述了您的软件所需的最低ASM 库版本。这对于兼容性至关重要,例如在访问者 API 中,因为当您覆盖旧版本中不存在的方法时,您在链接旧版本时不会注意到。该方法永远不会被调用。

So, using the ASM… constant allows to spot such issue earlier. That’s why some implementation classes offer a constructors not requiring the version number, not allowed for subclasses, whereas their constructor for subclasses does require it. As only subclasses can override methods, thus, are affected by this issue.

If you are not planning to use your software with an older version of the ASM library, just use the number corresponding to your current ASM library version, i.e. the highest without the EXPERIMENTAL suffix. Otherwise, I suggest using the older version during development and testing, which again allows to just use the highest ASM… number existing in that version.

You can use the newest ASM library to generate classes targeting all versions. It depends on the version you’re passing to the visit method. Which is V1_8 for Java 8 and V11 for Java 11. The actual values of these constants are identical to the versions of the JVM specification.