ASM introduction:修订间差异

来自WHY42
Riguz留言 | 贡献
Riguz留言 | 贡献
 
(未显示同一用户的2个中间版本)
第5行: 第5行:
= API structure =
= API structure =


* <syntaxhighlight lang="java" inline>org.objectweb.asm</syntaxhighlight> and <syntaxhighlight lang="java" inline>org.objectweb.asm.signature</syntaxhighlight>: packages define the event based API and provide the class parser and writer com- ponents. They are contained in the asm.jar archive.
* <syntaxhighlight lang="java" inline>org.objectweb.asm</syntaxhighlight>
* <syntaxhighlight lang="java" inline>org.objectweb.asm.signature</syntaxhighlight>: packages define the event based API and provide the class parser and writer com- ponents. They are contained in the asm.jar archive.
* <syntaxhighlight lang="java" inline>org.objectweb.asm.util</syntaxhighlight>:provides various tools based on the core API that can be used during the development and debuging of ASM applications.
* <syntaxhighlight lang="java" inline>org.objectweb.asm.commons</syntaxhighlight>:provides several useful pre- defined class transformers, mostly based on the core API. It is contained in the asm-commons.jar archive.
* <syntaxhighlight lang="java" inline>org.objectweb.asm.tree</syntaxhighlight>:defines the object based API, and provides tools to convert between the event based and the object based representations.
* <syntaxhighlight lang="java" inline>org.objectweb.asm.tree.analysis</syntaxhighlight>:provides a class anal- ysis framework and several predefined class analyzers, based on the tree API. It is contained in the asm-analysis.jar archive.


= Core API=
<syntaxhighlight lang="java">
public static ClassWriter createClassWriter(String name) {
    ClassWriter classWriter = new ClassWriter(0);
    classWriter.visit(V1_8,
            ACC_PUBLIC + ACC_FINAL,
            name,
            null,
            "java/lang/Object",
            null);
    return classWriter;
}


byte[] b = classWriter.toByteArray();
</syntaxhighlight>
= Tree API=
<syntaxhighlight lang="java">
public static ClassNode createClass(String name) {
    ClassNode classNode = new ClassNode();
    classNode.version = V1_8;
    classNode.access = ACC_PUBLIC + ACC_FINAL;
    classNode.name = name;
    classNode.superName = "java/lang/Object";
    return classNode;
}
</syntaxhighlight>


[[Category:JVM]]
[[Category:JVM]]
[[Category:ASM]]
[[Category:ASM]]

2023年12月19日 (二) 08:48的最新版本

Event based API vs Tree API

  • The event based API is faster and requires less memory than the object based API, since there is no need to create and store in memory a tree of objects representing the class (the same difference also exists between SAX and DOM).
  • However implementing class transformations can be more difficult with the event based API, since only one element of the class is available at any given time (the element that corresponds to the current event), while the whole class is available in memory with the object based API.

API structure

  • org.objectweb.asm
  • org.objectweb.asm.signature: packages define the event based API and provide the class parser and writer com- ponents. They are contained in the asm.jar archive.
  • org.objectweb.asm.util:provides various tools based on the core API that can be used during the development and debuging of ASM applications.
  • org.objectweb.asm.commons:provides several useful pre- defined class transformers, mostly based on the core API. It is contained in the asm-commons.jar archive.
  • org.objectweb.asm.tree:defines the object based API, and provides tools to convert between the event based and the object based representations.
  • org.objectweb.asm.tree.analysis:provides a class anal- ysis framework and several predefined class analyzers, based on the tree API. It is contained in the asm-analysis.jar archive.

Core API

public static ClassWriter createClassWriter(String name) {
    ClassWriter classWriter = new ClassWriter(0);
    classWriter.visit(V1_8,
            ACC_PUBLIC + ACC_FINAL,
            name,
            null,
            "java/lang/Object",
            null);
    return classWriter;
}

byte[] b = classWriter.toByteArray();

Tree API

public static ClassNode createClass(String name) {
    ClassNode classNode = new ClassNode();
    classNode.version = V1_8;
    classNode.access = ACC_PUBLIC + ACC_FINAL;
    classNode.name = name;
    classNode.superName = "java/lang/Object";
    return classNode;
}