|
|
@ -7,15 +7,15 @@ import javax.tools.StandardJavaFileManager;
|
|
|
|
import javax.tools.ToolProvider;
|
|
|
|
import javax.tools.ToolProvider;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileFilter;
|
|
|
|
import java.io.FileFilter;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
|
|
|
import java.net.URLClassLoader;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Stack;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -127,47 +127,58 @@ public class ClassUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param javaFile java源文件
|
|
|
|
* @param sourcePath 源文件路径
|
|
|
|
* @param classPath 类编译文件存放路径
|
|
|
|
* @param classPath 编译文件路径
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean compile(File javaFile, String classPath) throws IOException {
|
|
|
|
public static boolean compile(String sourcePath, String classPath) {
|
|
|
|
return compile(FileUtil.readFileToString(javaFile), javaFile.getName().replaceAll("\\.java$", ""), classPath);
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
|
|
|
|
|
|
|
Iterable<String> options = Arrays.asList("-d", classPath);
|
|
|
|
|
|
|
|
List<File> fileList = listJavaFile(sourcePath);
|
|
|
|
|
|
|
|
File[] files = new File[fileList.size()];
|
|
|
|
|
|
|
|
fileList.toArray(files);
|
|
|
|
|
|
|
|
Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(files);
|
|
|
|
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, javaFileObjects);
|
|
|
|
|
|
|
|
return task.call();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param source 源文件内容
|
|
|
|
* @param className 类名
|
|
|
|
* @param className
|
|
|
|
* @param classContent 类内容
|
|
|
|
* @param classPath
|
|
|
|
* @param classPath 编译路径
|
|
|
|
* @return
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static boolean compile(String source, String className, String classPath) {
|
|
|
|
public static boolean compileStringJava(String className, String classContent, String classPath) {
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
|
|
|
try {
|
|
|
|
|
|
|
|
Iterable<String> options = Arrays.asList("-d", classPath);
|
|
|
|
Iterable<String> options = Arrays.asList("-d", classPath);
|
|
|
|
SimpleJavaFileObject sourceObject = new StringSourceJavaObject(className, source);
|
|
|
|
SimpleJavaFileObject simpleJavaFileObject = new SimpleJavaFileObject(
|
|
|
|
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
|
|
|
|
URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects);
|
|
|
|
JavaFileObject.Kind.SOURCE) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Iterable<JavaFileObject> simpleJavaFileObjects = Arrays.asList(simpleJavaFileObject);
|
|
|
|
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, simpleJavaFileObjects);
|
|
|
|
return task.call();
|
|
|
|
return task.call();
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class StringSourceJavaObject extends SimpleJavaFileObject {
|
|
|
|
private static List<File> listJavaFile(String sourcePath) {
|
|
|
|
private String content = null;
|
|
|
|
ArrayList<File> result = new ArrayList<>();
|
|
|
|
|
|
|
|
File file = new File(sourcePath);
|
|
|
|
|
|
|
|
if (!file.exists()) file.mkdirs();
|
|
|
|
|
|
|
|
|
|
|
|
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
|
|
|
|
for (File item : file.listFiles()) {
|
|
|
|
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
|
|
|
|
if (item.isFile() && item.getName().endsWith(".java")) {
|
|
|
|
this.content = content;
|
|
|
|
result.add(item);
|
|
|
|
|
|
|
|
} else if (item.isDirectory()) {
|
|
|
|
|
|
|
|
result.addAll(listJavaFile(item.getAbsolutePath()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void loadClass(String classPath) throws Exception {
|
|
|
|
|
|
|
|
loadClass(new File(classPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -242,7 +253,7 @@ public class ClassUtil {
|
|
|
|
* @throws Exception 获取类异常
|
|
|
|
* @throws Exception 获取类异常
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static Class<?> getClass(String className) throws Exception {
|
|
|
|
public static Class<?> getClass(String className) throws Exception {
|
|
|
|
ClassLoader loader = xyz.wbsite.frame.utils.ClassUtil.class.getClassLoader();
|
|
|
|
ClassLoader loader = ClassUtil.class.getClassLoader();
|
|
|
|
return loader.loadClass(className);
|
|
|
|
return loader.loadClass(className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|