Former-commit-id: f2d78c8c8015645270d8a19d3063126510d820ca
master
wangbing 4 years ago
parent c3f2c07f22
commit e1026c8002

@ -7,15 +7,15 @@ 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.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Stack;
/**
@ -127,47 +127,58 @@ public class ClassUtil {
}
/**
* @param javaFile java
* @param classPath
* @param sourcePath
* @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);
public static boolean compile(String sourcePath, String 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 classPath
* @param className
* @param classContent
* @param classPath
* @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();
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;
Iterable<String> options = Arrays.asList("-d", classPath);
SimpleJavaFileObject simpleJavaFileObject = new SimpleJavaFileObject(
URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
JavaFileObject.Kind.SOURCE) {
};
Iterable<JavaFileObject> simpleJavaFileObjects = Arrays.asList(simpleJavaFileObject);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, simpleJavaFileObjects);
return task.call();
}
private static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
private static List<File> listJavaFile(String sourcePath) {
ArrayList<File> result = new ArrayList<>();
File file = new File(sourcePath);
if (!file.exists()) file.mkdirs();
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
for (File item : file.listFiles()) {
if (item.isFile() && item.getName().endsWith(".java")) {
result.add(item);
} else if (item.isDirectory()) {
result.addAll(listJavaFile(item.getAbsolutePath()));
}
}
return result;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
public static void loadClass(String classPath) throws Exception {
loadClass(new File(classPath));
}
/**

@ -0,0 +1,20 @@
package xyz.wbsite.dbtool.web.frame.utils;
public class Formatter {
public static String formatJava(String content) {
// 聚合
content = content.replaceAll("\\s+|\\n", " ");
content = content.replaceAll(";\\s", ";");
content = content.replaceAll("\\s?\\{\\s?", "{");
content = content.replaceAll("\\s?}\\s?", "}");
content = content.replaceAll("\\*/\\s?", "\\*/");
//分散
content = content.replaceAll("\\*/", "*/\n");
content = content.replaceAll(";", ";\n");
content = content.replaceAll("\\{", "\n{\n");
content = content.replaceAll("}", "}\n");
return content;
}
}

@ -7,15 +7,15 @@ 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.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Stack;
/**
@ -127,47 +127,58 @@ public class ClassUtil {
}
/**
* @param javaFile java
* @param classPath
* @param sourcePath
* @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);
public static boolean compile(String sourcePath, String 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 classPath
* @param className
* @param classContent
* @param classPath
* @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();
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;
Iterable<String> options = Arrays.asList("-d", classPath);
SimpleJavaFileObject simpleJavaFileObject = new SimpleJavaFileObject(
URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
JavaFileObject.Kind.SOURCE) {
};
Iterable<JavaFileObject> simpleJavaFileObjects = Arrays.asList(simpleJavaFileObject);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, simpleJavaFileObjects);
return task.call();
}
private static class StringSourceJavaObject extends SimpleJavaFileObject {
private String content = null;
private static List<File> listJavaFile(String sourcePath) {
ArrayList<File> result = new ArrayList<>();
File file = new File(sourcePath);
if (!file.exists()) file.mkdirs();
public StringSourceJavaObject(String name, String content) throws URISyntaxException {
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
for (File item : file.listFiles()) {
if (item.isFile() && item.getName().endsWith(".java")) {
result.add(item);
} else if (item.isDirectory()) {
result.addAll(listJavaFile(item.getAbsolutePath()));
}
}
return result;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
return content;
}
public static void loadClass(String classPath) throws Exception {
loadClass(new File(classPath));
}
/**
@ -242,7 +253,7 @@ public class ClassUtil {
* @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);
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.