我有一个 Java 运行时编译器,如下所示:
public class Compiler {
private final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
private final Map<String, String> source = new HashMap<String, String>();
private final MemoryFileManager manager = new MemoryFileManager(this.compiler);
public void add(String classname, String fileContent) {
add(Collections.singletonMap(classname, fileContent));
}
public void add(Map<String, String> map) {
source.putAll(map);
}
public void compile() {
List<Source> list = new ArrayList<Source>();
for (Map.Entry<String, String> entry : source.entrySet()) {
list.add(new Source(entry.getKey(), JavaFileObject.Kind.SOURCE, entry.getValue()));
}
this.compiler.getTask(null, this.manager, null, null, null, list).call();
}
public byte[] getByteCode(String name) {
return this.manager.map.get(name).toByteArray();
}
}
以及一个随附的简单类加载器示例:
public class SimpleClassLoader extends ClassLoader {
private String classname;
private byte[] byteCode;
public SimpleClassLoader(String classname, byte[] byteCode) {
super(SimpleClassLoader.class.getClassLoader());
this.classname = classname;
this.byteCode = byteCode;
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if(name.equals(classname)){
return defineClass(name, byteCode, 0, byteCode.length);
}
return super.findClass(name);
}
}
当编译和加载一个类时,生成的类的包名被连接到类名中:
Compiler compiler = new Compiler();
String className = "example.test.TestClass";
String source = "package example.test; public class TestClass{}";
compiler.add(className, source);
compiler.compile();
byte[] byteCode = compiler.getByteCode(className);
Class<?> aClass = Class.forName(className, true, new SimpleClassLoader(className, byteCode));
System.out.println("Package: " + aClass.getPackage()); // Should be "example.text"
System.out.println("Name: " + aClass.getSimpleName()); // Should be "TestClass"
出乎意料的输出如下:
Package: null
Name: TestClass
我在哪里错了?