Using javax Tool for cascade memory compilation
Eclipse's JDT compiler provides an interface inameenvironment, which defines the method findtype (...), enabling you to perform cascading compilation Curiously, I wonder if there is a way to use the standard JDK compiler toolkit?
Please note that this scheme is a template engine, which is used for in memory compilation and classes generated by interdependent template files, and it cannot predict the order of template files you encounter. Therefore, foo may need to be compiled before its parent column can be compiled. Therefore, you need a mechanism to perform cascading compilation, which means that during the compilation of foo, You need to generate another source code column and compile it first to continue the compilation of foo: some code is as follows:
private NameEnvironmentAnswer findType(final String name) { try { if (!name.contains(TemplateClass.CN_SUFFIX)) { return findStandType(name); } char[] fileName = name.tocharArray(); TemplateClass templateClass = classCache.getByClassName(name); // TemplateClass exists if (templateClass != null) { if (templateClass.javaByteCode != null) { ClassFileReader classFileReader = new ClassFileReader(templateClass.javaByteCode,fileName,true); return new NameEnvironmentAnswer(classFileReader,null); } // Cascade compilation ICompilationUnit compilationUnit = new CompilationUnit(name); return new NameEnvironmentAnswer(compilationUnit,null); } // So it's a standard class return findStandType(name); } catch (ClassFormatException e) { // Something very very bad throw new RuntimeException(e); } }
Solution
Based on our comment dialogue, I think the answer is clear: No, you can't do it with the JDK compiler When it requests a package, it gives you a hook instead of a specific class dependency
What I know about getting as close as possible is:
Here's a nice article and code, although it needs to be adapted to deal with memory classes Specifically, the problem you described is handled by the Java FileManager List (...) method processing You must return here the javafileobjects that you have cached in memory You will most likely need to create a subclass of forwardingjavafilemanager, as described in this article - although modify to handle the cache class you are using
You can use it to write something If it returns an error, use a regular expression to find the missing content After generating / compiling code for what is missing, try compiling the original code again
Note: it sometimes requires the FQN of the dependent class as forwardingfilemanager Packagename parameter in list (...) I didn't try to return to class at that time It may not work because the packages will not match, but it may