如何将 JDK6 ToolProvider 和 JavaCompiler 与上下文类加载器一起使用? [英] How do I use JDK6 ToolProvider and JavaCompiler with the context classloader?
问题描述
我的用例是使用 JDK 6 中提供的 ToolProvider 和 JavaCompiler 类从 Java 程序编译生成的源文件.源文件包含对上下文类加载器(它在 J2EE 容器中运行)中的类的引用,但不在系统类加载器.我的理解是默认情况下 ToolProvider 将使用系统类加载器创建 JavaCompiler 实例.
My usage case is compiling generated source files from a java program using the ToolProvider and JavaCompiler classes provided in JDK 6. The source files contain references to classes in the context classloader (it runs in a J2EE container), but not in the system classloader. My understanding is that by default the ToolProvider will create the JavaCompiler instance with the system classloader.
有没有办法指定 JavaCompiler 使用的类加载器?
Is there a way to specify a classloader for JavaCompiler to use?
我尝试了这种方法,修改自 IBM DeveloperWorks 上的一些内容:
I tried this approach, modified from something on IBM DeveloperWorks:
FileManagerImpl fm =
new FileManagerImpl(compiler.getStandardFileManager(null, null, null););
FileManagerImpl 定义为:
with FileManagerImpl defined as:
static final class FileManagerImpl
extends ForwardingJavaFileManager<JavaFileManager> {
public FileManagerImpl(JavaFileManager fileManager) {
super(fileManager);
}
@Override
public ClassLoader getClassLoader(JavaFileManager.Location location) {
new Exception().printStackTrace();
return Thread.currentThread().getContextClassLoader();
}
}
堆栈跟踪表明它在注释处理期间只被调用一次.我验证了要编译的源文件中引用的类不在系统类路径中,但可以从上下文类加载器中获得.
The stacktrace indicates it's only called once during annotation processing. I verified the class referenced in the source file to be compiled is not on the system classpath but is available from the context classloader.
推荐答案
如果您知道 contextclassloader 已知的文件的类路径,您可以将它们传递给编译器:
If you know the classpath to the files that are known to the contextclassloader you can pass them to the compiler:
StandardJavaFileManager fileManager = compiler.getStandardFileManager(this /* diagnosticlistener */, null, null);
// get compilationunits from somewhere, for instance via fileManager.getJavaFileObjectsFromFiles(List<file> files)
List<String> options = new ArrayList<String>();
options.add("-classpath");
StringBuilder sb = new StringBuilder();
URLClassLoader urlClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
for (URL url : urlClassLoader.getURLs())
sb.append(url.getFile()).append(File.pathSeparator);
options.add(sb.toString());
CompilationTask task = compiler.getTask(null, fileManager, this /* diagnosticlistener */, options, null, compilationUnits);
task.call();
此示例假设您使用的是 URLClassloader(它允许您检索类路径),但您可以根据需要插入自己的类路径.
This example assumes you're using a URLClassloader (which allows you to retrieve the classpath) but you could insert your own classpath if you wanted to.
这篇关于如何将 JDK6 ToolProvider 和 JavaCompiler 与上下文类加载器一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!