Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JDK6.0 uses Compiler API

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how JDK6.0 uses Compiler API. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Now we can use JDK6's Compiler API (JSR 199) to dynamically compile Java source files, Compiler API combined with reflection function can dynamically generate Java code and compile and execute the code, which has the characteristics of dynamic language. This feature is very useful for some applications that need dynamic compilation, such as JSP Web Server. When we manually modify JSP, we do not want to restart Web Server to see the effect. At this time, we can use Compiler API to dynamically compile JSP files. Of course, the current JSP Web Server also supports JSP hot deployment. Today's JSP Web Server compiles the code by calling javac through Runtime.exec or ProcessBuilder at run time, which requires us to generate another process to do the compilation work, which is not elegant enough and easy to make the code dependent on a particular operating system Compiler API provides a richer way to do dynamic compilation through a set of easy-to-use standard API, and it is cross-platform. The following code demonstrates the use of Compiler API:

Public class CompilerAPITester {

Private static String JAVA_SOURCE_FILE = "DynamicObject.java"

Private static String JAVA_CLASS_FILE = "DynamicObject.class"

Private static String JAVA_CLASS_NAME = "DynamicObject"

Public static void main (String [] args) {

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler ()

StandardJavaFileManager fileManager = compiler.getStandardFileManager (null, null, null)

GenerateJavaClass ()

Try {

/ / copy the generated class file to the ClassPath of the program. The following line of code is specific to the Windows+IntelliJ IDEA 6.0project and is not portable.

Runtime.getRuntime () .exec (cmd / c copy + JAVA_CLASS_FILE+ "classesproductionJDK6Features")

Iterable sourcefiles = fileManager.getJavaFileObjects (JAVA_SOURCE_FILE)

Compiler.getTask (null, fileManager, null, sourcefiles). Call ()

FileManager.close ()

Class.forName (JAVA_CLASS_NAME). NewInstance (); / / create an instance of the dynamically compiled DynamicObject class

} catch (Exception ex) {

Ex.printStackTrace ()

}

}

Public static void generateJavaClass () {

Try {

FileWriter fw = new FileWriter (JAVA_SOURCE_FILE)

BufferedWriter bw = new BufferedWriter (fw)

Bw.write ("public class" + JAVA_CLASS_NAME+ "{")

Bw.newLine ()

Bw.write ("public" + JAVA_CLASS_NAME+ "() {System.out.println (" In the constructor of DynamicObject ");}}")

Bw.flush ()

Bw.close ()

} catch (IOException ex) {

Ex.printStackTrace ()

}

}

}

After the program runs, two files, DynamicObject.java and DynamicObject.class, are generated and output on the console:

In the constructor of DynamicObject

This is the end of the article on "how JDK6.0 uses Compiler API". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report