Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 925
Nathan Avatar asked Aug 13 '20 16:08

Nathan


People also ask

Which opcodes are not defined in the JVM interface?

The JVM opcodes, access flags and array type codes. This interface does not define all the JVM opcodes because some opcodes are automatically handled. For example, the xLOAD and xSTORE opcodes are automatically replaced by xLOAD_n and xSTORE_n opcodes when possible. The xLOAD_n and xSTORE_n opcodes are therefore not defined in this interface.

What is ASM API in Java?

The ASM API provides two styles of interacting with Java classes for transformation and generation: event-based and tree-based. 3.1. Event-based API This API is heavily based on the Visitor pattern and is similar in feel to the SAX parsing model of processing XML documents.

Where can I get the latest version of ASM and ASM-util?

We can get the latest versions of asm and asm-util from Maven Central. 3. ASM API Basics The ASM API provides two styles of interacting with Java classes for transformation and generation: event-based and tree-based.

What is the most common versioning strategy for an API?

URI path versioning is the most common. This strategy involves putting the version number in the path of the URI, and is often done with the prefix "v". More often than not, API designers use it to refer to their application version (i.e. "trunk") rather than the endpoint version (i.e. "leaf" or "branch"), but that's not always a safe assumption.


2 Answers

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.

like image 67
Holger Avatar answered Oct 18 '22 22:10

Holger


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.

like image 33
rzwitserloot Avatar answered Oct 18 '22 22:10

rzwitserloot