In the general Java application development process, it is relatively simple for developers to use Java. Open the usual IDE, write the Java source code, and use the functions provided by the IDE to directly run the Java program. The process behind this development mode is: developers write Java source code files (. java), The IDE will call the Java compiler to compile the Java source code into byte code unrelated to the platform, and save it on the disk as a class file (. class ). The Java Virtual Machine (JVM) is responsible for loading and executing JAVA byte code. Java uses this method to achieve its goal of "writing once, running everywhere (write once, run anywhere. The Byte Code contained in java files can be used by JVM on different platforms. Java Byte Code not only exists on the disk as a file, but also can be downloaded over the network, and can only exist in the memory. The class loader in JVM defines the Java class from the byte array (byte []) that contains the byte code. In some cases, you may need to dynamically generate JAVA byte code or modify the existing Java byte code. At this time, we need to use the relevant technologies described in this article. First, we will introduce how to dynamically compile Java source files.
Dynamic compilation of Java source files
In general, developers write all the Java source code and compile it successfully before running the program. For some applications, the content of Java source code can be determined at runtime. At this time, you need to dynamically compile the source code to generate the java byte code, and then load and execute it by JVM. A typical scenario is an online evaluation system (such as PKU judgeonline) that allows users to upload Java code, which is compiled, run, and determined by the system in the background. When dynamically compiling Java source files, you can call the Java compiler directly in the program.
JSR 199 introduces Java compiler APIs. If JDK 6 is used, you can use this API to dynamically compile Java code. For example, the following code is used to dynamically compile the simplest Hello world class. The Java class code is saved in a string.
Public class compilertest {public static void main (string [] ARGs) throws exception {string source = "public class main {public static void main (string [] ARGs) {system. out. println (/"Hello world! /") ;}}"; Javacompiler compiler = toolprovider. getsystemjavacompiler (); standardjavafilemanager filemanager = compiler. getstandardfilemanager (null, null, null); stringsourcejavaobject sourceobject = new compilertest. stringsourcejavaobject ("Main", source); iterable <extends javafileobject> fileobjects = arrays. aslist (sourceobject); compilationtask task = compiler. gettask (null, filemanager, null, NUL L, null, fileobjects); boolean result = task. Call (); If (result) {system. Out. println ("compiled successfully. ") ;}} Static class stringsourcejavaobject extends simplejavafileobject {private string content = NULL; Public stringsourcejavaobject (string name, string content )?? Throws urisyntaxexception {super (URI. create ("string: //" + name. replace ('. ','/') + kind. source. extension), kind. source); this. content = content;} public charsequence getcharcontent (Boolean ignoreencodingerrors )?? Throws ioexception {return content ;}}}
If you cannot use the Java compiler API provided by JDK 6, you can use the tool class COM. sun. tools. javac. main, but this tool class can only compile files stored on the disk, similar to directly using javac commands.
Another available tool is the compiler provided by ECLIPSE jdt core. This is an incremental Java compiler used in the eclipse Java development environment. It supports running and debugging of wrong code. The compiler can also be used independently. The play framework uses the jdt compiler internally to dynamically compile Java source code. In development mode, the play framework regularly scans the Java source code files in the project. Once any modifications are found, the Java source code is automatically compiled. Therefore, after modifying the code, refresh the page to see the changes. When using these dynamic compilation methods, make sure that the tools. jar in JDK is in the classpath of the application.
The following is an example of how to perform four arithmetic operations in Java, such as obtaining the value (3 + 4) * 7-10. The general practice is to analyze the input Calculation Expression and simulate the calculation process by yourself. Considering the existence of parentheses and the priority of operators, the calculation process is complicated and error-prone. Another method is to use the script language introduced by JSR 223 to directly execute the input expression as JavaScript or javafx scripts and get the result. The following code dynamically generates and compiles the Java source code, and then loads the Java class to execute and obtain the result. This method is fully implemented using Java.
Private Static double calculate (string expr) throws calculationexception {string classname = "calculatormain"; string methodname = "Calculate "; string source = "public class" + classname + "{public static double" + methodname + "() {return" + expr + ";}}"; // omitting the Code related to the dynamic compilation of Java source code, see the previous section boolean result = task. call (); If (result) {classloader loader = calculator. class. getclassloader (); try {Cl Ass <?> Clazz = loader. loadclass (classname); Method method = clazz. getmethod (methodname, new class <?> [] {}); Object value = method. invoke (null, new object [] {}); Return (double) value;} catch (exception e) {Throw new calculationexception ("internal error. ") ;}} Else {Throw new calculationexception (" incorrect expression. ");}}
The above Code provides the basic mode for dynamically generated JAVA byte code, that is, to load byte code through the class loader and create an instance of Java class objects, then, call the methods in the object through the Java reflection API.
Java Byte Code Enhancement
Java Byte Code enhancement refers to modifying and enhancing JAVA byte code after it is generated. This method is equivalent to modifying the binary file of the application. This implementation method can be seen in many Java frameworks. Java Byte Code enhancement is usually used with annotation in Java source files. Annotations in the Java source code declare the behavior to be enhanced and the relevant metadata, the Framework completes the enhancement of the byte code at runtime. Java Byte Code enhancement is applicable in many scenarios, generally focusing on reducing redundant code and shielding developers from the underlying implementation details. Those who have used JavaBeans may feel complicated and difficult to maintain the getter/setter methods that must be added. With the byte code enhancement, developers only need to declare the attributes in the bean. The getter/setter method can be automatically added by modifying the byte code. People who have used JPA will find some additional fields and methods added to the object class during program debugging. These domains and methods are dynamically added by the JPA implementation at runtime. Byte Code enhancement is also used in some implementations of Aspect-oriented programming (AOP.
Before discussing how to enhance the byte code, we will first introduce the organization of the byte code that represents a Java class or interface.
Class file {0 xcafebabe, minor version number, major version number, constant pool size, constant pool array, access control tag, current class information, parent class information, number of Implemented interfaces, array of implemented interface information, number of domains, array of domain information, number of methods, array of method information, number of attributes, and array of attribute information}
As shown above, the byte code of a class or interface uses a loose organizational structure, and the content contained in it is arranged in sequence. Content that may contain multiple entries, such as Implemented interfaces, domains, methods, and attributes, is represented in arrays. Before the array, the number of entries in the array is displayed. Different content types have different internal structures. For developers, if they directly manipulate byte arrays containing byte code, the development efficiency is relatively low and error-prone. There are already many open-source libraries that can modify the byte code or create the byte code content of the new Java class from the beginning. These class libraries include ASM, cglib, serp, and bcel. Using these class libraries can reduce the complexity of enhanced byte code to a certain extent. For example, consider the following simple requirement and output the corresponding logs before all methods of a Java class are executed. Anyone familiar with AOP knows that we can use a prior enhancement (before advice) to solve this problem. If ASM is used, the related code is as follows:
ClassReader cr = new ClassReader(is);ClassNode cn = new ClassNode();cr.accept(cn, 0);for (Object object : cn.methods) { MethodNode mn = (MethodNode) object; if ("<init>".equals(mn.name) || "<clinit>".equals(mn.name)) { continue; } InsnList insns = mn.instructions; InsnList il = new InsnList(); il.add(new FieldInsnNode(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")); il.add(new LdcInsnNode("Enter method -> " + mn.name)); il.add(new MethodInsnNode(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V")); insns.insert(il); mn.maxStack += 3;}ClassWriter cw = new ClassWriter(0);cn.accept(cw);byte[] b = cw.toByteArray();
From classwriter, you can obtain the byte array containing the enhanced byte code. You can write the byte code back to the disk or use it directly by the class loader. In the above example, the logic of the enhancement section is relatively simple. It just traverses all the methods in the Java class and adds a call to the system. Out. println method. In byte code, the Java method body is composed of a series of commands. What we need to do is to generate commands that call the system. Out. println method and insert these commands to the beginning of the command set. ASM abstracts these commands, but it is difficult to get familiar with all the commands. ASM provides a tool class asmifierclassvisitor, which can print the structure information of the byte code of the Java class. When you need to enhance a class, you can first modify the source code, and then use this tool class to compare the differences between the byte code before and after the modification, so as to determine how to write the enhanced code.
The time to enhance class files is after Java source code compilation and before JVM execution. Common practices include:
- The IDE performs the compilation operation. For example, Google App Engine's Eclipse plug-in will run datanucleus after compilation to enhance the object class.
- Complete the build process, for example, using ant or Maven to perform relevant operations.
- Implement your own Java class loader. After obtaining the byte code of the Java class, perform enhanced processing first, and then define the Java class from the modified byte code.
- Use the java. Lang. instrument package introduced by JDK 5.
Java. Lang. Instrument
Due to the large number of requirements for modifying the java byte code, JDK 5 introduces the java. Lang. instrument package and is further enhanced in JDK 6. The basic idea is to add some agents at JVM startup ). Each proxy is a jar package, and a proxy class is specified in the manifest file. This class will contain a premain method. At startup, JVM will first execute the premain method of the proxy class, and then execute the main method of the Java program itself. You can modify the byte code of the program in the premain method. JDK 6 also allows dynamic addition of proxies after JVM startup. Java. lang. the instrument package supports two types of modification scenarios: one is to redefine a Java class, that is, to completely replace the byte code of a Java class; the other is to convert an existing Java class, it is equivalent to the Class byte code enhancement mentioned above. As an example, we need to implement the java. Lang. instrument. classfiletransformer interface to convert existing Java classes.
Static class methodentrytransformer implements classfiletransformer {public byte [] transform (classloader loader, string classname, class <?> Classbeingredefined ,? Protectiondomain, byte [] classfilebuffer) throws illegalclassformatexception {try {classreader Cr = new classreader (classfilebuffer); classnode Cn = new classnode (); // The Code classwriter CW = new classwriter (0); CN. accept (CW); Return CW. tobytearray () ;}catch (exception e) {return NULL ;}}}
With this conversion class, you can use it in the premain method of the proxy.
public static void premain(String args, Instrumentation inst) { inst.addTransformer(new MethodEntryTransformer());}
Compress the proxy class into a jar package and declare the name of the proxy class through premain-class in the jar package list file. When running the Java program, add the JVM startup parameter-javaagent: myagent. jar. In this way, the JVM completes the conversion operation before loading the byte code of the Java class.
Summary
It is interesting to manipulate JAVA byte code. It makes it easy to modify binary distribution Java programs and is suitable for performance analysis, debugging tracking, logging, and other tasks. Another important role is to free developers from the tedious Java syntax. Developers only need to write important Code related to business logic. You can dynamically generate byte code for codes that are added only because of syntax requirements or fixed patterns. Bytecode enhancement is different from source code generation. After the source code is generated, it has become a part of the program. developers need to maintain it: either manually modify the generated source code or regenerate it. The Byte Code enhancement process is completely transparent to developers. The proper use of the Java Byte Code Manipulation Technology can better solve a certain type of development problems.
Transferred from: http://www.infoq.com/cn/articles/cf-java-byte-code authorCheng Fu