|
|
@ -1,8 +1,22 @@
|
|
|
|
package xyz.wbsite.dbtool.web.frame.utils;
|
|
|
|
package xyz.wbsite.dbtool.web.frame.utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.tools.JavaCompiler;
|
|
|
|
|
|
|
|
import javax.tools.JavaFileObject;
|
|
|
|
|
|
|
|
import javax.tools.SimpleJavaFileObject;
|
|
|
|
|
|
|
|
import javax.tools.StandardJavaFileManager;
|
|
|
|
|
|
|
|
import javax.tools.ToolProvider;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
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.URISyntaxException;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import java.net.URLClassLoader;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* ClassUtil
|
|
|
|
* ClassUtil
|
|
|
@ -47,7 +61,7 @@ public class ClassUtil {
|
|
|
|
* @param pojoClass POJO对象
|
|
|
|
* @param pojoClass POJO对象
|
|
|
|
* @return 方法
|
|
|
|
* @return 方法
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static Method getMethod(String name, Class<?> pojoClass) throws RuntimeException {
|
|
|
|
public static Method getMethod(String name, Class<?> pojoClass) {
|
|
|
|
String getMethodName = "get" + StringUtil.upperFirstWord(name);
|
|
|
|
String getMethodName = "get" + StringUtil.upperFirstWord(name);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return pojoClass.getMethod(getMethodName);
|
|
|
|
return pojoClass.getMethod(getMethodName);
|
|
|
@ -73,7 +87,7 @@ public class ClassUtil {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return pojoClass.getMethod(setMethodName, type);
|
|
|
|
return pojoClass.getMethod(setMethodName, type);
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
throw new RuntimeException("can not find[" + name + "]method");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -111,4 +125,124 @@ public class ClassUtil {
|
|
|
|
System.arraycopy(f2, 0, fields, f1.length, f2.length);
|
|
|
|
System.arraycopy(f2, 0, fields, f1.length, f2.length);
|
|
|
|
return fields;
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param javaFile java源文件
|
|
|
|
|
|
|
|
* @param classPath 类编译文件存放路径
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean compile(File javaFile, String classPath) throws IOException {
|
|
|
|
|
|
|
|
return compile(FileUtil.readFileToString(javaFile), javaFile.getName().replaceAll("\\.java$", ""), classPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @param source 源文件内容
|
|
|
|
|
|
|
|
* @param className
|
|
|
|
|
|
|
|
* @param classPath
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean compile(String source, String className, String classPath) {
|
|
|
|
|
|
|
|
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
|
|
|
|
|
|
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Iterable<String> options = Arrays.asList("-d", classPath);
|
|
|
|
|
|
|
|
SimpleJavaFileObject sourceObject = new StringSourceJavaObject(className, source);
|
|
|
|
|
|
|
|
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
|
|
|
|
|
|
|
|
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects);
|
|
|
|
|
|
|
|
return task.call();
|
|
|
|
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 加载指定clazzPath
|
|
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
|
|
* 例如clazzPath = /classes class文件的实际位置是/classes/text/test.class
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param classPath
|
|
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static void loadClass(File classPath) throws Exception {
|
|
|
|
|
|
|
|
// 记录加载.class文件的数量
|
|
|
|
|
|
|
|
int clazzCount = 0;
|
|
|
|
|
|
|
|
//only handle the folder
|
|
|
|
|
|
|
|
if (classPath.isFile()) {
|
|
|
|
|
|
|
|
classPath = classPath.getParentFile();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (classPath.exists() && classPath.isDirectory()) {
|
|
|
|
|
|
|
|
// 获取路径长度
|
|
|
|
|
|
|
|
int classPathLen = classPath.getAbsolutePath().length() + 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Stack<File> stack = new Stack<>();
|
|
|
|
|
|
|
|
stack.push(classPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 遍历类路径
|
|
|
|
|
|
|
|
while (stack.isEmpty() == false) {
|
|
|
|
|
|
|
|
File path = stack.pop();
|
|
|
|
|
|
|
|
File[] classFiles = path.listFiles(new FileFilter() {
|
|
|
|
|
|
|
|
public boolean accept(File pathname) {
|
|
|
|
|
|
|
|
return pathname.isDirectory() || pathname.getName().endsWith(".class");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
for (File subFile : classFiles) {
|
|
|
|
|
|
|
|
if (subFile.isDirectory()) {
|
|
|
|
|
|
|
|
stack.push(subFile);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (clazzCount++ == 0) {
|
|
|
|
|
|
|
|
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
|
|
|
|
|
|
|
boolean accessible = method.isAccessible();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (accessible == false) {
|
|
|
|
|
|
|
|
method.setAccessible(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 设置类加载器
|
|
|
|
|
|
|
|
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
|
|
|
|
|
|
|
|
// 将当前类路径加入到类加载器中
|
|
|
|
|
|
|
|
method.invoke(classLoader, classPath.toURI().toURL());
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
method.setAccessible(accessible);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 文件名称
|
|
|
|
|
|
|
|
String className = subFile.getAbsolutePath();
|
|
|
|
|
|
|
|
className = className.substring(classPathLen, className.length() - 6);
|
|
|
|
|
|
|
|
className = className.replace(File.separatorChar, '.');
|
|
|
|
|
|
|
|
// 加载Class类
|
|
|
|
|
|
|
|
Class.forName(className);
|
|
|
|
|
|
|
|
System.out.println(String.format("读取应用程序类文件[class=%s]", className));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取指定类
|
|
|
|
|
|
|
|
* 获取之前请确认是否加载 {@link #loadClass(File)}
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param className 类名(包含package,不含后缀)
|
|
|
|
|
|
|
|
* @return 类
|
|
|
|
|
|
|
|
* @throws Exception 获取类异常
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static Class<?> getClass(String className) throws Exception {
|
|
|
|
|
|
|
|
ClassLoader loader = ClassUtil.class.getClassLoader();
|
|
|
|
|
|
|
|
return loader.loadClass(className);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|