Java Compiler API 学习

package demo;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import com.xiuye.util.log.LogUtil;

public class Demo8 {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, URISyntaxException {

		//简单编译型
		JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
		//仅仅相当于编译,但不执行
		int i = jc.run(null, null, null, "-d",".","C:\\Users\\admin\\Desktop\\java\\Demo1.java");
		LogUtil.log(i);
		//复杂编译型
		File[] f1 = { new File("C:\\Users\\admin\\Desktop\\java\\Demo1.java") };
//		File[] f2 = { new File("C:\\Users\\admin\\Desktop\\java\\Demo1.java") };
		//diagnostics 
		DiagnosticCollector diagnostics = new DiagnosticCollector();
		StandardJavaFileManager sjfm = jc.getStandardFileManager(diagnostics, null, null);
		Iterable compilationUnits1 = sjfm.getJavaFileObjectsFromFiles(Arrays.asList(f1));
		// 设置 -d . 非常重要
		List options = Arrays.asList("-d", ".","-verbose");
		//编译不执行
		LogUtil.log(jc.getTask(null, sjfm, null, options, null, compilationUnits1).call());
		LogUtil.log("enter");
		LogUtil.log(diagnostics.getDiagnostics());
		for (Diagnostic diagnostic : diagnostics.getDiagnostics())
			LogUtil.log("Error on line", diagnostic.getLineNumber(), "in", diagnostic.getSource().toUri());

		//java string code
		//直接编译java code
		JavaSourceFromString source = new JavaSourceFromString("A"/*类名*/, "import com.xiuye.util.log.LogUtil;public class A{public A(){LogUtil.log(\"A, OK\");}"
				+ "public String toString(){return \"这是一个字符串写的类哈哈哈哈哈\";}}")  ;
		LogUtil.log(jc.getTask(null, sjfm, diagnostics, options, null, Arrays.asList(source)).call());
		LogUtil.log(diagnostics.getDiagnostics());
		for (Diagnostic diagnostic : diagnostics.getDiagnostics())
			LogUtil.log("Error on line", diagnostic.getLineNumber(), "in", diagnostic.getSource().toUri());

		sjfm.flush();  
		sjfm.close();
		//"." <=> ""
//		URL u = Paths.get("").toUri().toURL();	
		URL u = Paths.get(".").toUri().toURL();	
		URL []urls = {u};
		URLClassLoader cl = new URLClassLoader(urls);
		Class a = cl.loadClass("A");
		LogUtil.log(a);
		LogUtil.log(a.newInstance());
	}

	/**
     * A file object used to represent source coming from a string.
     */
    public static class JavaSourceFromString extends SimpleJavaFileObject {
        /**
         * The source code of this "file".
         */
        final String code;

        /**
         * Constructs a new JavaSourceFromString.
         * @param name the name of the compilation unit represented by this file object
         * @param code the source code for the compilation unit represented by this file object
         */
        JavaSourceFromString(String name, String code) {
            super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension),
                  Kind.SOURCE);
            this.code = code;
        }

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) {
            return code;
        }
    }
	
}



0
[解析开始时间 RegularFileObject[C:\Users\admin\Desktop\java\Demo1.java]]
[解析已完成, 用时 5 毫秒]
[源文件的搜索路径: E:\code\Utils\build\classes,E:\servers\apache-tomcat-7.0.94\lib\annotations-api.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ant.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ha.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-tribes.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina.jar,E:\servers\apache-tomcat-7.0.94\lib\ecj-4.4.2.jar,E:\servers\apache-tomcat-7.0.94\lib\el-api.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper-el.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper.jar,E:\servers\apache-tomcat-7.0.94\lib\jsp-api.jar,E:\servers\apache-tomcat-7.0.94\lib\servlet-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-coyote.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-dbcp.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-es.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-fr.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ja.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ru.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-jdbc.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-util.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat7-websocket.jar,E:\servers\apache-tomcat-7.0.94\lib\websocket-api.jar,E:\code\Util\bin]
[类文件的搜索路径: C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\sunrsasign.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar,C:\Program Files\Java\jdk1.8.0_161\jre\classes,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar,E:\code\Utils\build\classes,E:\servers\apache-tomcat-7.0.94\lib\annotations-api.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ant.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ha.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-tribes.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina.jar,E:\servers\apache-tomcat-7.0.94\lib\ecj-4.4.2.jar,E:\servers\apache-tomcat-7.0.94\lib\el-api.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper-el.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper.jar,E:\servers\apache-tomcat-7.0.94\lib\jsp-api.jar,E:\servers\apache-tomcat-7.0.94\lib\servlet-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-coyote.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-dbcp.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-es.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-fr.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ja.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ru.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-jdbc.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-util.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat7-websocket.jar,E:\servers\apache-tomcat-7.0.94\lib\websocket-api.jar,E:\code\Util\bin]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Enum.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Comparable.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)]]
[正在检查demo.Demo1]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/CloneNotSupportedException.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Class.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Override.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/Annotation.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/Target.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/ElementType.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/Retention.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/annotation/RetentionPolicy.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/System.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/PrintStream.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Appendable.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/Closeable.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/FilterOutputStream.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/OutputStream.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/Flushable.class)]]
[已写入RegularFileObject[.\demo\Demo1$Test1$1.class]]
[已写入RegularFileObject[.\demo\Demo1$Test1.class]]
[已写入RegularFileObject[.\demo\Demo1$1.class]]
[已写入RegularFileObject[.\demo\Demo1.class]]
[共 145 毫秒]
true
enter
[]
[解析开始时间 WrappedJavaFileObject[demo.Demo8$JavaSourceFromString[string:///A.java]]]
[解析已完成, 用时 2 毫秒]
[源文件的搜索路径: E:\code\Utils\build\classes,E:\servers\apache-tomcat-7.0.94\lib\annotations-api.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ant.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ha.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-tribes.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina.jar,E:\servers\apache-tomcat-7.0.94\lib\ecj-4.4.2.jar,E:\servers\apache-tomcat-7.0.94\lib\el-api.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper-el.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper.jar,E:\servers\apache-tomcat-7.0.94\lib\jsp-api.jar,E:\servers\apache-tomcat-7.0.94\lib\servlet-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-coyote.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-dbcp.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-es.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-fr.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ja.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ru.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-jdbc.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-util.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat7-websocket.jar,E:\servers\apache-tomcat-7.0.94\lib\websocket-api.jar,E:\code\Util\bin]
[类文件的搜索路径: C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\sunrsasign.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar,C:\Program Files\Java\jdk1.8.0_161\jre\classes,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar,C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar,E:\code\Utils\build\classes,E:\servers\apache-tomcat-7.0.94\lib\annotations-api.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ant.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-ha.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina-tribes.jar,E:\servers\apache-tomcat-7.0.94\lib\catalina.jar,E:\servers\apache-tomcat-7.0.94\lib\ecj-4.4.2.jar,E:\servers\apache-tomcat-7.0.94\lib\el-api.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper-el.jar,E:\servers\apache-tomcat-7.0.94\lib\jasper.jar,E:\servers\apache-tomcat-7.0.94\lib\jsp-api.jar,E:\servers\apache-tomcat-7.0.94\lib\servlet-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-api.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-coyote.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-dbcp.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-es.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-fr.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ja.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-i18n-ru.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-jdbc.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat-util.jar,E:\servers\apache-tomcat-7.0.94\lib\tomcat7-websocket.jar,E:\servers\apache-tomcat-7.0.94\lib\websocket-api.jar,E:\code\Util\bin]
[正在加载RegularFileObject[E:\code\Util\bin\com\xiuye\util\log\LogUtil.class]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/String.class)]]
[正在检查A]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/io/Serializable.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Byte.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Character.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Short.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Long.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Float.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Integer.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Double.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Boolean.class)]]
[正在加载ZipFileIndexFileObject[C:\Program Files\Java\jdk1.8.0_161\lib\ct.sym(META-INF/sym/rt.jar/java/lang/Void.class)]]
[已写入RegularFileObject[.\A.class]]
[共 47 毫秒]
true
[]
class A
A, OK
这是一个字符串写的类哈哈哈哈哈

 

你可能感兴趣的:(Java)