如何从 Eclipse JDT 中的类名获取 IType
我正在实现 JUnit 新测试套件向导的一个变体,我需要从另一个源获取测试类,而不是从当前项目获取测试类。它们以完全限定的类名字符串的形式出现在我面前。
其中一些可能尚不存在于该用户的工作空间中,更不用说存在于当前项目的类路径中了。用户稍后需要导入这些项目,但我还不想在向导中搞乱它。我需要将所有类添加到新套件中,无论它们是否存在。
对于那些已经在此项目的类路径中的类,我可以使用 IJavaProject.findType(String fullQualifiedName) 。是否有类似的方法来获取(尚)不可见的类的 IType?
我很乐意凭空构建一个 IType,但 IType 似乎不喜欢被构建。
I'm implementing a variant of the JUnit New Test Suite Wizard, and instead of getting test classes from the current project, I need to get them from another source. They come to me as strings of fully-qualified class names.
Some of them may not yet exist in this user's workspace, let alone in the classpath of the current project. The user will need to import the projects for these later, but I don't want to mess with that in my wizard yet. I need to just add all classes to the new suite whether they exist yet or not.
For those classes that are already in this project's classpath, I can use IJavaProject.findType(String fullyQualifiedName) . Is there an analogous way to get ITypes for classes that are not (yet) visible?
I would be happy to construct an IType out of thin air, but ITypes don't seem to like being constructed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是不可能的:Java 文档模型接口是基于类路径创建的。
更糟糕的是,如果该项目不存在于用户工作区中,则生成的代码将无法编译,这是不允许任意创建此类构造的另一个原因。
如果我是你,我会尝试帮助用户导入不存在的项目,以防类型不可用,从而避免使用 Java 文档模型进行处理。
I don't think that is possible: the Java Document Model interfaces are created based on the classpath.
Even worse, if the project do not exist in the users workspace, the resulting code would not compile, and that is another reason for not allowing the arbitrary creation of such constructs.
If I were you, I would try to help the user to import the non-existing projects in case of types are not available, thus avoiding the tackling with the Java Document Model.
出于我的目的,创建一个
HypotheticalType
和一个HypotheticalMethod
就完成了工作。我附上概述,以防其他人需要遵循此路径。首先,我创建了一个
HypotheticalType
并让它实现IType
接口。我在修改后的向导中的适当位置实例化了其中之一。使用 Eclipse 的 Outline 视图,我在新类中的所有方法上创建了一个方法断点。这让我可以检测在执行向导期间实际调用了哪些方法。我还修改了构造函数,以采用String
形式获取我需要向导处理的类的名称。本练习中几乎忽略了所有新方法。我发现我可以保留所有方法的默认实现(大多数情况下返回 null 或返回 false),除了以下方法:
exists()
- 无需修改getAncestor(int)< /code> - 不需要修改,但返回我的假设类的包可能很有用,例如,如果我的类是
java.lang.Object.class
,则返回java.lang< /代码>。
getDeclaringType()
- 无需修改getElementName()
- 修改为返回类名,例如,如果我的类是java.lang.Object.class
>,返回对象
。getElementType()
- 修改为返回IJavaElement.TYPE
getFlags()
- 尚未修改,但可能是getMethod(String, String [])
- 修改为返回新的HypotheticalMethod(name)
getMethods()
- 修改为返回新的IMethod[] { new HypotheticalMethod("dudMethod") }
在这个过程中,我发现我需要能够返回一个
HypotheticalMethod
,因此我也创建了该类型,继承自IMethod,并使用相同的技术来确定必须实现哪些方法。这些是在此向导运行时调用的唯一方法:
String
参数以引入方法的名称exists()
- 无需修改这涵盖了我原来问题的解决方案。 Zoltán,我将按照您在即将到来的迭代中建议的方式进行操作,并尝试在所需的类尚未在此项目的类路径中以及所需的类不在某个项目中的情况下帮助用户但在工作空间中。
For my purposes, creating a
HypotheticalType
and aHypotheticalMethod
got the job done. I'm attaching an overview in case anyone else needs to follow this path.First I created a
HypotheticalType
and had it implement theIType
interface. I instantiated one of these at the proper spot in my modified wizard. Using Eclipse's Outline view I created a method breakpoint on all methods in my new class. This let me detect which methods were actually getting called during execution of my wizard. I also modified the constructor to take, as aString
, the name of the class I needed the wizard to handle.Almost all of the new methods are ignored in this exercise. I found that I could keep the default implementation (return null or return false in most cases) for all methods except the following:
exists()
- no modification neededgetAncestor(int)
- no modification needed, but it might be useful to return the package of my hypothetical class, e.g. if my class isjava.lang.Object.class
, returnjava.lang
.getDeclaringType()
- no modification neededgetElementName()
- modified to return the class name, e.g. if my class isjava.lang.Object.class
, returnObject
.getElementType()
- modified to returnIJavaElement.TYPE
getFlags()
- not modified yet, but might begetMethod(String, String[])
- modified to return a newHypotheticalMethod(name)
getMethods()
- modified to return newIMethod[] { new HypotheticalMethod("dudMethod") }
In the process I discovered that I need to be able to return a
HypotheticalMethod
, so I created that type as well, inheriting fromIMethod
, and used the same techniques to determine which methods had to be implemented. These are the only methods that get called while this wizard runs:String
parameter to bring in the name of the methodexists()
- no modification neededisMainMethod()
- no modification neededThat covers the solution to my original question. Zoltán, I'll be doing as you suggested in an upcoming iteration and trying to assist the user in both the case in which the desired class is not yet in this project's classpath, and the case in which the desired class is in some project not yet in the workspace.