feat: 修改classloader
This commit is contained in:
parent
18c3d327d7
commit
ee334b4cd5
|
@ -0,0 +1,15 @@
|
||||||
|
package com.flyfish.framework.compiler.core;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 静态访问器
|
||||||
|
*
|
||||||
|
* @author wangyu
|
||||||
|
*/
|
||||||
|
public interface ClassLoaders {
|
||||||
|
|
||||||
|
MemoryClassLoader CLASS_LOADER = new MemoryClassLoader();
|
||||||
|
|
||||||
|
static MemoryClassLoader memory() {
|
||||||
|
return CLASS_LOADER;
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,67 +16,58 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class JavaStringCompiler implements Closeable {
|
public class JavaStringCompiler implements Closeable {
|
||||||
|
|
||||||
private final JavaCompiler compiler;
|
private final JavaCompiler compiler;
|
||||||
private final MemoryJavaFileManager manager;
|
private final MemoryJavaFileManager manager;
|
||||||
private final MemoryClassLoader classLoader;
|
private final MemoryClassLoader classLoader;
|
||||||
|
|
||||||
public JavaStringCompiler() {
|
public JavaStringCompiler(MemoryClassLoader classLoader) {
|
||||||
this.compiler = ToolProvider.getSystemJavaCompiler();
|
this.compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
StandardJavaFileManager stdManager = compiler.getStandardFileManager(null, null, null);
|
StandardJavaFileManager stdManager = compiler.getStandardFileManager(null, null, null);
|
||||||
this.manager = new MemoryJavaFileManager(stdManager);
|
this.manager = new MemoryJavaFileManager(stdManager, classLoader);
|
||||||
this.classLoader = new MemoryClassLoader(manager.getClassBytes());
|
this.classLoader = classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a Java source file in memory.
|
* Compile a Java source file in memory.
|
||||||
*
|
*
|
||||||
* @param fileName Java file name, e.g. "Test.java"
|
* @param fileName Java file name, e.g. "Test.java"
|
||||||
* @param source The source code as String.
|
* @param source The source code as String.
|
||||||
* @return The compiled results as Map that contains class name as key,
|
* @return The compiled results as Map that contains class name as key,
|
||||||
* class binary as value.
|
* class binary as value.
|
||||||
* @throws IOException If compile error.
|
* @throws IOException If compile error.
|
||||||
*/
|
*/
|
||||||
public Map<String, byte[]> compile(String fileName, String source) {
|
public Map<String, byte[]> compile(String fileName, String source) {
|
||||||
JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
|
JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
|
||||||
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, null, null, Collections.singletonList(javaFileObject));
|
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, null, null, Collections.singletonList(javaFileObject));
|
||||||
Boolean result = task.call();
|
Boolean result = task.call();
|
||||||
if (result == null || !result) {
|
if (result == null || !result) {
|
||||||
throw new RuntimeException("Compilation failed.");
|
throw new RuntimeException("Compilation failed.");
|
||||||
}
|
}
|
||||||
return manager.getClassBytes();
|
return manager.getClassBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load class from compiled classes.
|
* Load class from compiled classes.
|
||||||
*
|
*
|
||||||
* @param name Full class name.
|
* @param name Full class name.
|
||||||
* @return The Class instance.
|
* @return The Class instance.
|
||||||
* @throws ClassNotFoundException If class not found.
|
* @throws ClassNotFoundException If class not found.
|
||||||
*/
|
*/
|
||||||
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||||
return classLoader.loadClass(name);
|
return classLoader.loadClass(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* get the classLoader
|
public void close() {
|
||||||
*
|
try {
|
||||||
* @return 结果
|
if (null != manager) {
|
||||||
*/
|
manager.close();
|
||||||
public ClassLoader getClassLoader() {
|
}
|
||||||
return classLoader;
|
if (null != classLoader) {
|
||||||
}
|
classLoader.close();
|
||||||
|
}
|
||||||
@Override
|
} catch (IOException e) {
|
||||||
public void close() {
|
e.printStackTrace();
|
||||||
try {
|
}
|
||||||
if (null != manager) {
|
}
|
||||||
manager.close();
|
|
||||||
}
|
|
||||||
if (null != classLoader) {
|
|
||||||
classLoader.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.flyfish.framework.compiler.core;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load class from byte[] which is compiled in memory.
|
* Load class from byte[] which is compiled in memory.
|
||||||
|
@ -11,22 +12,24 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
class MemoryClassLoader extends URLClassLoader {
|
class MemoryClassLoader extends URLClassLoader {
|
||||||
|
|
||||||
// class name to class bytes:
|
// class name to class bytes:
|
||||||
private final Map<String, byte[]> classBytes;
|
private final Map<String, byte[]> classBytes = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public MemoryClassLoader(Map<String, byte[]> classBytes) {
|
public MemoryClassLoader() {
|
||||||
super(new URL[0], MemoryClassLoader.class.getClassLoader());
|
super(new URL[0], MemoryClassLoader.class.getClassLoader());
|
||||||
this.classBytes = classBytes;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||||
byte[] buf = classBytes.get(name);
|
byte[] buf = classBytes.get(name);
|
||||||
if (buf == null) {
|
if (buf == null) {
|
||||||
return super.findClass(name);
|
return super.findClass(name);
|
||||||
}
|
}
|
||||||
classBytes.remove(name);
|
classBytes.remove(name);
|
||||||
return defineClass(name, buf, 0, buf.length);
|
return defineClass(name, buf, 0, buf.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map<String, byte[]> getClassBytes() {
|
||||||
|
return classBytes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
class MemoryJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
class MemoryJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
||||||
|
|
||||||
// compiled classes in bytes:
|
// compiled classes in bytes:
|
||||||
final Map<String, byte[]> classBytes = new ConcurrentHashMap<>();
|
private final Map<String, byte[]> classBytes;
|
||||||
|
|
||||||
MemoryJavaFileManager(JavaFileManager fileManager) {
|
MemoryJavaFileManager(JavaFileManager fileManager, MemoryClassLoader classLoader) {
|
||||||
super(fileManager);
|
super(fileManager);
|
||||||
|
classBytes = classLoader.getClassBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, byte[]> getClassBytes() {
|
public Map<String, byte[]> getClassBytes() {
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
package com.flyfish.framework.compiler.support;
|
package com.flyfish.framework.compiler.support;
|
||||||
|
|
||||||
import com.flyfish.framework.compiler.DynamicJavaCompiler;
|
import com.flyfish.framework.compiler.DynamicJavaCompiler;
|
||||||
|
import com.flyfish.framework.compiler.core.ClassLoaders;
|
||||||
import com.flyfish.framework.compiler.core.JavaStringCompiler;
|
import com.flyfish.framework.compiler.core.JavaStringCompiler;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.util.StreamUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,7 +15,7 @@ import java.util.Map;
|
||||||
public class DelegateJavaCompiler implements DynamicJavaCompiler {
|
public class DelegateJavaCompiler implements DynamicJavaCompiler {
|
||||||
|
|
||||||
// 第三方编译器
|
// 第三方编译器
|
||||||
private final JavaStringCompiler compiler = new JavaStringCompiler();
|
private final JavaStringCompiler compiler = new JavaStringCompiler(ClassLoaders.memory());
|
||||||
|
|
||||||
private DelegateJavaCompiler() {
|
private DelegateJavaCompiler() {
|
||||||
}
|
}
|
||||||
|
@ -27,7 +25,7 @@ public class DelegateJavaCompiler implements DynamicJavaCompiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ClassLoader classLoader() {
|
public static ClassLoader classLoader() {
|
||||||
return getInstance().compiler.getClassLoader();
|
return ClassLoaders.memory();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,17 +52,16 @@ public class DelegateJavaCompiler implements DynamicJavaCompiler {
|
||||||
compiler.close();
|
compiler.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class SingletonHolder {
|
private static class SingletonHolder {
|
||||||
|
|
||||||
private static final DelegateJavaCompiler INSTANCE = new DelegateJavaCompiler();
|
private static final DelegateJavaCompiler INSTANCE = new DelegateJavaCompiler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws ClassNotFoundException, IOException {
|
// public static void main(String[] args) throws ClassNotFoundException, IOException {
|
||||||
long time = System.currentTimeMillis();
|
// long time = System.currentTimeMillis();
|
||||||
System.out.println(StreamUtils.copyToString(DelegateJavaCompiler.class.getResourceAsStream("./"), StandardCharsets.UTF_8));
|
// System.out.println(StreamUtils.copyToString(DelegateJavaCompiler.class.getResourceAsStream("./"), StandardCharsets.UTF_8));
|
||||||
Class<?> clazz = getInstance().compile("Fuck.java", "package com.flyfish.project; public class Fuck {}");
|
// Class<?> clazz = getInstance().compile("Fuck.java", "package com.flyfish.project; public class Fuck {}");
|
||||||
System.out.println(clazz + "耗时:" + (System.currentTimeMillis() - time) / 1000.0 + "秒");
|
// System.out.println(clazz + "耗时:" + (System.currentTimeMillis() - time) / 1000.0 + "秒");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.flyfish.framework.boot;
|
package com.flyfish.framework.boot;
|
||||||
|
|
||||||
|
|
||||||
|
import com.flyfish.framework.compiler.core.ClassLoaders;
|
||||||
import com.flyfish.framework.compiler.support.DelegateJavaCompiler;
|
import com.flyfish.framework.compiler.support.DelegateJavaCompiler;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
@ -14,7 +15,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||||
public class FlyfishAppRunner {
|
public class FlyfishAppRunner {
|
||||||
|
|
||||||
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
|
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
|
||||||
Thread.currentThread().setContextClassLoader(DelegateJavaCompiler.classLoader());
|
Thread.currentThread().setContextClassLoader(ClassLoaders.memory());
|
||||||
return SpringApplication.run(new Class[]{primarySource}, args);
|
return SpringApplication.run(new Class[]{primarySource}, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue