1

我的服务器中有一个导出的 Eclipse Java 项目,我希望能够编译该项目并ASTParser与 JDT 一起使用。

我可以使用 编译项目BatchCompiler,但是它在控制台上运行并给我PrintWriters 而不是一系列问题和错误。此外,我希望能够在 Eclipse 中使用提案,并且BatchCompiler不是为此目的而构建的。

因此我尝试使用ASTParser,它可以与char[]或一起使用ICompilationUnitCompletionProposalCollector并且org.eclipse.jdt.internal.compiler.Compiler.Compiler需要ICompilationUnit,所以我必须创建一个ICompilationUnit只能由 IJavaProject ( https://dl.dropboxusercontent.com/u/10773282/2012/eclipse_workspace.pdf ) 创建的才能使用这些功能。

似乎创建的唯一方法IJavaProject是使用ResourcesPlugin.getWorkspace(),但是它返回java.lang.IllegalStateException: Workspace is closed.到我的计算机上,似乎原因是我编写的程序不是 Eclipse 插件。

有没有办法在没有 Eclipse 环境的情况下创建 IJavaProject?

4

2 回答 2

3

From the comments, it looks like you are trying to do more than just parsing, you actually want to get some form of content assist.

I'm afraid that you're asking for too much. There is no simple way to get the power and flexibility of JDT outside of a running Eclipse instance (believe me, I've tried). There's no simple way, but if you are brave and strong willed, you can one of try following:

  1. Run a headless Eclipse on your server that works on top of an actual workspace. This would be the easiest to implement, but would be the most resource intensive and least flexible way of doing things.
  2. Use the jdt core jar, and create alternate implementations of the IResource hierarchy, and the parts of JFace that are used by the the parser and the CompletionEngine. This would likely be the most feature-rich way to go, but also the most brittle. I can't guarantee that this would work as you may need to create some very complex stubs for internal Eclipse non-API classes.
  3. Avoid the CompletionEngine and the ASTParser entirely and just use the batch compiler. You would then need to provide an alternate implementation of org.eclipse.jdt.internal.compiler.env.INameEnvironment. This implementation would be able to find types, files, and compilation units in your actual project structure. You'd need to reimplement support for content assist, but this would most likely work reasonably well.

I am actually fairly interested in doing something like this (but I lack the time to do it). If you are seriously considering creating a headless JDT that can run on a server, feel free to ask for more information. I am quite familiar with JDT internals.

于 2013-05-22T16:45:15.887 回答
2

我有一个类似的问题。以下是如何在ASTParser没有 Eclipse 的情况下使用(它只需要类路径上的核心 JDT JAR):http ://blog.pdark.de/2010/11/05/using-eclipse-to-parse-java-code/

于 2013-05-22T12:47:05.373 回答