Introduction to bytecode project library

xiaoxiao2021-03-06  63

Simply put, the bytecode project library is a class library for processing Java bytecodes. Use the bytecode project library, developers can easily analyze, create and operate Java class files (files ending with .class, or the first four bytes are files of 0xcafebab). The bytecode project will occur after compiling or before loading classes, some technologies are optimized or enhanced in the performance of existing Class, and another technique is used to make existing Class more easily or used. To avoid bulky code generation. The class represents a class by some objects including functions such as functions, members, and bytecode instructions.

Here I use some of the APIs provided by Javassit to display the characteristics of the field code project library.

1. ClassPool pool = classpool.getDefault ();

2. CTClass NodeClass = pool.get ("Test.sql.dmlStatementNode");

3. System.out.println (NodeClass.getsuperclass (). Getname ());

4. NodeClass.SetsuperClass (Pool.Get ("Test.sql.Node");

5. nodeclass.writefile ();

The ClassPool object is a factory factory. It searches for each class file in the specified classpath and establishes a single-order CTClass object for a search class file. The GET method returns CTClass for describing the class according to the specified class name. Object. In the third line, print the parent class of DMLSTATEMENTNODE, change its parent class to Node in the fourth line, and then write the classified result after the change. If you want to use the changed class directly at runtime, you don't write it into a class file. Simply change the fifth line to

5. Class class = nodeclass.toclass ();

6. Node Node = (DMLStatementNode) nodeclass.newinstance ();

Similarly, we can also redefine a new class:

1. ClassPool pool = classpool.getDefault ();

2. CTClass cc = pool.makeclass ("Test.sql.ddlStatementNode");

3. Ctmethod M = CtnewMethod.make ("Public void printsubtree () {system.out.println (/" print subtree / ");}"

4. cc.addmethod (m);

5. Class class = cc.toClass ();

6. DdlStatementNode Node = (DDLStatement) Class.newInstance ();

7. node.printsubtree ();

Below is a few more famous bytecode project libraries, and each byte code engineering is not the same, see the respective documents.

Javassit (http://www.csg.is.titech.ac.jp/~chiba/javassist/)

BCEL (http://jakarta.apache.org/bcel/)

ObjectWeb asm (http://asm.objectweb.org/)

You can get information about the open source-by-step project library from http://java-source.net/open-source/bytecode-libraries

转载请注明原文地址:https://www.9cbs.com/read-85304.html

New Post(0)