Search code examples
javaclassloaderjsr199

Malformed Java package class name while loading runtime-compile source


I have a Java runtime compiler as follows:

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();
    }
}

And an accompanying example simple Classloader:

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);
    }
}

When compiling and loading a class, the resulting class's package name is concatenated into the class 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"

The output of this is unexpectedly the following:

Package: null
Name: TestClass

Where am I going wrong here?


Solution

  • The example.test package was undefined and was needed to be initalized by calling definePackage() within the compile method:

    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();
    }