The following unit test will fail to compile. If the return value is changed to simply "SelfRef.class" everything works fine.
public void testAccessingCompilingClass() throws Exception {
SimpleCompiler sc = new SimpleCompiler();
sc.cook("package test.simple;" +
"public class SelfRef {" +
" public Class getSelf() {" +
" return test.simple.SelfRef.class;" +
" }" +
"}"
);
Class self = sc.getClassLoader().loadClass("test.simple.SelfRef");
Method m = self.getDeclaredMethod("getSelf", null);
Object inst = self.newInstance();
Class res = (Class)m.invoke(inst, null);
assertEquals(self, res);
}
It also factors some of the duplicated code in UnitCompiler.java into helper methods, which I did while coming to understand the code and the problem better.