How do I map ASM's API version in Opcodes to Java version?

huangapple go评论88阅读模式
英文:

How do I map ASM's API version in Opcodes to Java version?

问题

ClassVisitor 构造函数需要传递 Opcodes 的其中一个版本:ASM4ASM5ASM6ASM7ASM8ASM9

如何确定每个 Java 版本应使用哪个 ASM#?Java 8 应使用哪个 ASM#?Java 11 应使用哪个 ASM#

英文:

ASM's ClassVisitor constructor requires passing one of Opcodes's ASM4, ASM5, ASM6, ASM7, ASM8 or ASM9.

How do I know which ASM# to use for each version of Java? What ASM# would I use for Java 8? What ASM# would I use for Java 11?

答案1

得分: 9

以下是已翻译的内容:

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

因此,使用 ASM… 常量可以更早地发现这种问题。这就是为什么一些实现类提供了不需要版本号的构造函数,不允许子类使用,而它们的用于子类的构造函数需要版本号。因为只有子类可以重写方法,因此受到这个问题的影响。

如果您不计划将软件与较旧版本的ASM库一起使用,只需使用与当前ASM库版本对应的数字,即不带 EXPERIMENTAL 后缀的最高版本。否则,我建议在开发和测试期间使用旧版本,这将允许您使用该版本中存在的最高 ASM… 数字。

您可以使用最新的ASM库生成针对所有版本的类。这取决于您传递给 visit 方法的版本。对于Java 8,它是 V1_8,对于Java 11,它是 V11。这些常量的实际值与Java虚拟机规范的版本相同。

英文:

The ASM… constants describe the minimum ASM library version required by your software. This is crucial for compatibility, e.g. in the Visitor API, as when you’re overriding a method that does not exist in an older version, you wouldn’t notice when linking against an older version. The method would just never get called.

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.

答案2

得分: 1

根据我所知,没有简单的方法,但是ASM的变更日志非常有帮助。请注意,这些版本指的是ASM的版本,而不是类文件格式的版本。

英文:

As far as I know, no easy way, but the changelog of ASM helps a lot. Note that these versions refer to the ASM version, NOT the class file format version.

答案3

得分: 0

以下是翻译好的代码部分:

"你需要指定支持你所拥有的类文件的ASM API版本,因此,如果你处于一个使用未知最新ASM的上下文中,你可以通过反射字段搜索来指定它。

private static int ASM_VERSION = Opcodes.ASM5; // 默认为Java 8的第一个版本

static {
try {
for (int i = 6; true; i++) { // 循环,直到没有更多版本可用
Field asmField = org.objectweb.asm.Opcodes.class.getField("ASM" + i);
ASM_VERSION = asmField.getInt(null);
}
} catch (Throwable exception) {
}
}

然后使用它:

ClassVisitor cv = new ClassVisitor(ASM_VERSION)"

英文:

It appears that you need to specify a version of the ASM API that supports whatever class files you have, so if you are in a context that uses an unknown latest ASM, you can specify it by a reflective field search.

private static int ASM_VERSION = Opcodes.ASM5;		// Default to first Java 8 version

static {
	try {
		for (int i = 6; true; i++) {				// Loop for as many versions are available
			Field asmField = org.objectweb.asm.Opcodes.class.getField("ASM" + i);
			ASM_VERSION = asmField.getInt(null);
		}
	} catch (Throwable exception) {
	}
}

and then use it as:

ClassVisitor cv = new ClassVisitor(ASM_VERSION)

huangapple
  • 本文由 发表于 2020年8月14日 00:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63399682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定