从自定义类加载器中使用 javax.tools.ToolProvider?
似乎无法按照 Ant 或 Webstart 的要求使用自定义类加载器中的 javax.tools.ToolProvider
:http://bugs.sun.com/view_bug.do?bug_id=6548428
It seems to be impossible to use javax.tools.ToolProvider
from a custom classloader as required by Ant or Webstart: http://bugs.sun.com/view_bug.do?bug_id=6548428
javax.tools.ToolProvider.getSystemJavaCompiler()
将 javax.tools.JavaCompiler
加载到 URLClassLoader 中,其父级是系统类加载器.API 似乎不允许用户指定父类加载器.
javax.tools.ToolProvider.getSystemJavaCompiler()
loads javax.tools.JavaCompiler
into a URLClassLoader whose parent is the system classloader. The API does not seem to allow users to specify a parent classloader.
如何使用自定义类加载器中的 javax.tools.JavaCompiler
?
例如:
- Ant 加载
MyParserTask
MyParserTask
解析 Java 源代码MyParserTask
由委托给系统类加载器的AntClassLoader
加载javax.tools.JavaCompiler
由URLClassLoader
加载,委托给系统类加载器
- Ant loads
MyParserTask
MyParserTask
parses Java source-codeMyParserTask
is loaded byAntClassLoader
that delegates to the system classloaderjavax.tools.JavaCompiler
is loaded byURLClassLoader
thast delegates to the system classloader
稍后,MyParserTask
调用:
javax.tools.CompilationTask task = compiler.getTask(...);
com.sun.source.util.JavacTask javacTask = (com.sun.source.util.JavacTask) task;
javacTask.parse().next().accept(visitor, unused); // parsing happens here
- 看到这两个类如何驻留在不同的类加载器上,
MyParserTask
似乎没有办法与JavacTask
交互而不会得到ClassCastException代码>错误.
- Seeing how the two classes reside on separate classloaders, there doesn't seem to be a way for
MyParserTask
to interact withJavacTask
without gettingClassCastException
errors.
有什么想法吗?
推荐答案
我遇到了完全相同的问题.我正在使用自定义 ant 任务来扫描 AST 以查找某些类型的方法调用.我的解决方案可能不适合您,而是自己实例化编译器,而不是使用 ToolProvider.
I had exactly the same problem. I'm using a custom ant task to scan the AST for certain kinds of method invocations. My solution, which may not be appropriate for you, was to instantiate the compiler myself instead of using the ToolProvider.
我换了
JavaCompiler 编译器 = ToolProvider.getSystemJavaCompiler();
与
JavaCompiler 编译器 = (JavaCompiler)Class.forName("com.sun.tools.javac.api.JavacTool").newInstance();
这当然不是面向未来或在所有环境中都是安全的,但它是一个取决于您的需求的选项.如果其他人在自定义 Ant 任务中使用 ToolProvider 有更好的方法,请分享.
This certainly isn't future proof or safe in all environments, but it is an option depending on your needs. If someone else has a better way around using ToolProvider in custom Ant tasks please share.
相关文章