这两天新接到一个需求,是这样。从页面上文本写一个拦截器,然后上传这个拦截器的源码,生成对象并调用对象的方法。
我当时的反应就是很懵逼的 。。。这个操作也太骚了吧
年前写了个用groovy来执行,但是会出现一些问题,不能满足需求。
年后开始重新思考这个问题,然后在网上找到了一篇资料
http://blog.sina.com.cn/s/blog_70279be20101dk0j.html
重点是找到了一个东西
jdk提供一个动态编译的类。JavaCompiler javac;
这样首先是解决了,将源码编译成为对应的class文件,接下来是动态加载class文件到项目中生成。这一步就通过类加载器来完成。
理清除思路后,我就开始做一个最简单的通过main方法执行生成一个动态类 ,然后将代码移植到springboot项目中。
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author: Bobby
* @create: 2020-02-11 15:29
* @description:动态执行
**/
public class DynamicLoader {
/**
* auto fill in the java-name with code, return null if cannot find the public class
*
* @param javaSrc source code string
* @return return the Map, the KEY means ClassName, the VALUE means bytecode.
*/
public static Map<String, byte[]> compile(String javaSrc) {
Pattern pattern = Pattern.compile("public\\s+class\\s+(\\w+)");
Matcher matcher = pattern.matcher(javaSrc);
if (matcher.find()) {
return compile(matcher.group(1) + ".java", javaSrc);
}
return null;
}
public static Map<String, byte[]> compile(String javaName, String javaSrc) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager stdManager = compiler.getStandardFileManager(null, null, null);
try (MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager)) {
JavaFileObject javaFileObject = manage