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

@ -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 simpleJavaFileObject = new SimpleJavaFileObject(
SimpleJavaFileObject sourceObject = new StringSourceJavaObject(className, source); URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject); JavaFileObject.Kind.SOURCE) {
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects); };
return task.call(); Iterable<JavaFileObject> simpleJavaFileObjects = Arrays.asList(simpleJavaFileObject);
} catch (URISyntaxException e) { JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, simpleJavaFileObjects);
e.printStackTrace(); return task.call();
}
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()));
}
} }
return result;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { public static void loadClass(String classPath) throws Exception {
return content; 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 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 simpleJavaFileObject = new SimpleJavaFileObject(
SimpleJavaFileObject sourceObject = new StringSourceJavaObject(className, source); URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
Iterable<? extends JavaFileObject> fileObjects = Arrays.asList(sourceObject); JavaFileObject.Kind.SOURCE) {
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, fileObjects); };
return task.call(); Iterable<JavaFileObject> simpleJavaFileObjects = Arrays.asList(simpleJavaFileObject);
} catch (URISyntaxException e) { JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, options, null, simpleJavaFileObjects);
e.printStackTrace(); return task.call();
}
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()));
}
} }
return result;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { public static void loadClass(String classPath) throws Exception {
return content; 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);
} }
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.