因为一些蛋疼的需求,需要其他开发商将相应java代码来实现效果,这里就需要用到动态编译。
这里网上查了一下资料,忘记来源,自己整理并优化了一番,实现了js的eval效果
/**
* @author onedear
*
*/
public class Compiler {
private static boolean hasFirstInit = false;
public static String getClassCode (String initParam) {
StringBuffer sb = new StringBuffer();
sb.append("public class ETEvaler {")
.append("public Object eval() {")
.append(initParam)
.append("}")
.append("}");
return sb.toString() ;
}
public static Object eval(String sourceCode)
throws SecurityException, NoSuchMethodException, IOException,
ClassNotFoundException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException, InstantiationException {
URLClassLoader classLoader = new URLClassLoader(
new URL[] { new File(System.getProperty("java.io.tmpdir")).toURI().toURL() });
Class clazz = null ;
boolean hasInit = true ;
try{
clazz = classLoader.loadClass("ETEvaler");
} catch (ClassNotFoundException e) {
//说明类未初始化过,需要初始化
hasInit = false ;
}
if(!hasInit || !hasFirstInit) {
hasFirstInit = true;
String classCode = getClassCode(sourceCode);
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if(compiler == null)
throw new IllegalArgumentException("系统java编译器无法找到,请确认类路径中已经包含tools.jar(注:JDK 6中默认自带,JRE 6中默认不带)");
DiagnosticCollector diagnostics = new DiagnosticCollector();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
String fileName = "ETEvaler.java";
File file = new File(System.getProperty("java.io.tmpdir"), fileName);
PrintWriter pw = new PrintWriter(file);
pw.println(classCode);
pw.close();
Iterable compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(file
.getAbsolutePath()));
JavaCompiler.CompilationTask task = compiler.getTask(null,
fileManager, diagnostics, null, null, compilationUnits);
boolean success = task.call();
fileManager.close();
}
//必须重新加载,否则当重新eval时无法立刻生效
classLoader = new URLClassLoader(
new URL[] { new File(System.getProperty("java.io.tmpdir")).toURI().toURL() });
clazz = classLoader.loadClass("ETEvaler");
Method method = clazz.getDeclaredMethod("eval");
Object value = method.invoke(clazz.newInstance() );
return value ;
}
}
调用方式相当简单,如
Compiler.eval("System.out.println(\"e1111112222222\");");
String returnValue = Compiler.eval("System.out.println(\"e1111112222222\");return \"returnValue\";");