0

我有一个 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

我在哪里错了?

4

2 回答 2

0

的结果aClass.getName()是正确的。如果您希望程序返回“TestClass”,您应该使用aClass.getSimpleName().

我检查的源代码java.lang.Class.getPackage()。它将在您的情况下调用Package.getPackage(this)which 调用class.getClassLoader().getPackage("example.test")。因为SimpleClassLoader有 override 方法protected Class findClass(String name),您还应该重写该方法protected Package getPackage(String name)并返回您期望的对象。

于 2016-03-26T15:25:06.923 回答
0

example.test包未定义,需要通过definePackage()在 compile 方法中调用来初始化:

public void compile() {
    List<Source> list = new ArrayList<Source>();
    for (Map.Entry<PackageClass, String> entry : source.entrySet()) {
        list.add(new Source(entry.getKey().getCanonicalName(), JavaFileObject.Kind.SOURCE, entry.getValue()));
        if(getPackage(entry.getKey().getPackage()) == null) {
            definePackage(entry.getKey().getPackage(), null, null, null, null, null, null, null);
        }
    }
    this.compiler.getTask(null, this.manager, null, null, null, list).call();
}
于 2016-03-27T22:15:37.570 回答