黑马程序员技术交流社区

标题: 【上海校区】JAVA 的动态编译 [打印本页]

作者: 梦缠绕的时候    时间: 2018-8-10 09:29
标题: 【上海校区】JAVA 的动态编译
JAVA 6.0引入了动态编译机制,也就是说,你利用java的io流把完整的程序代码写入.java后缀的文件,可以实现载入动态的实现载入,动态代理就是利用了此机制完成的。


下面假设在D盘根目录下有一个hello.java文件(实际上此文件多数时候应该由java创建):

编译步骤:

1、调用 ToolProvider的getSystemJavaCompiler()方法,返回JavaCompiler

2、使用JavaCompiler的getStandardFileManager([url=]DiagnosticListener[/url]<? super[url=]JavaFileObject[/url]> diagnosticListener, [url=]Locale[/url]locale, [url=]Charset[/url] charset)方法,获取一个[url=]StandardJavaFileManager[/url]

3.把获取的[url=]StandardJavaFileManager[/url]传入 getTask([url=]Writer[/url] out, [url=]JavaFileManager[/url] fileManager, [url=]DiagnosticListener[/url]<? super [url=]JavaFileObject[/url]> diagnosticListener, [url=]Iterable[/url]<[url=]String[/url]> options,[url=]Iterable[/url]<[url=]String[/url]> classes,[url=]Iterable[/url]<? extends [url=]JavaFileObject[/url]> compilationUnits) 方法获得 [url=]JavaCompiler.CompilationTask[/url]

4、激活task 即可 task.call();这样文件编译成功了,稍显麻烦。



output:

Hello world!


API解释:


Interface JavaCompiler


JavaCompiler.CompilationTask getTask(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) Creates a future for a compilation task with the given components and arguments. The compilation might not have completed as described in the CompilationTask interface.

out - a Writer for additional output from the compiler; use System.err if null定义Writer类型输出流 ,null使用System.err输出流

fileManager - a file manager; if null use the compiler's standard filemanager定义fileManager,null使用编译器标准

fileManagerdiagnosticListener - a diagnostic listener; if null use the compiler's default method for reporting diagnostics定义diagnosticListener(诊断监听器)

options - compiler options, null means no options

classes - names of classes to be processed by annotation processing, null means no class names注解类

compilationUnits - the compilation units to compile, null means no compilation units

Returns:

          an object representing the compilation

Throws:

          RuntimeException - if an unrecoverable error occurred in a user supplied component. The cause will be the error in user code. IllegalArgumentException - if any of the given compilation units are of other kind than source







getStandardFileManager
[url=]StandardJavaFileManager[/url] 创建一个实例

The standard file manager will be automatically reopened if it is accessed after calls toflush or close. The standard file manager must be usable with other tools.


Parameters:

      diagnosticListener - a diagnostic listener for non-fatal diagnostics; ifnull use the compiler's default method for reporting diagnostic

locale - the locale to apply when formatting diagnostics; null means the [url=]default locale[/url].charset - the character set used for decoding bytes; if null use the platform defaultReturns:the standard file manager

作者: 不二晨    时间: 2018-8-16 16:55
奈斯




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2