我在 Eclipse 工作区中有类 A 和继承 A 的类 B。
我遇到的问题是,当我尝试使用 eclipse JDT API 获取类型 B 的超级类型时,我什么也没得到。这是代码(我从 -列出具有完全限定名称的所有子类 https://stackoverflow.com/questions/1426173/list-all-subclasses-with-fully-qualified-names):
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
java.io.File workspaceDirectory = root.getLocation().toFile();
// 1. The name of the project in the workspace
IProgressMonitor pm = new NullProgressMonitor();
IProject orig = root.getProject(this.projectName);
orig.open(pm);
this.javaProject = JavaCore.create(orig);
orig.refreshLocal(IResource.DEPTH_INFINITE, pm);
// 2. Find the type
IType type = this.javaProject.findType("p.B"); <-- returns correct type info
ITypeHierarchy hier = type.newSupertypeHierarchy(new NullProgressMonitor());
IType[] types = hier.getAllSuperclasses(type);
System.out.println(types); <-- Returns []
我还添加了刷新/更新包中资源的代码。
IPackageFragmentRoot[] packageFragmentRoots = this.javaProject.getPackageFragmentRoots();
for (IPackageFragmentRoot proot: packageFragmentRoots)
{
proot.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);
}
除了获取分层类型信息之外,一切正常。
可能出了什么问题?在执行 API 之前我是否错过了任何设置?
我的是一个无头 RCP 应用程序。
这可能是一个临时解决方案,但它对我有用。
简答
创建一个lib目录,并将其复制rtstubs.jar https://dl.dropbox.com/u/10773282/2013/rtstubs.jar进入目录。
您可能需要刷新 (F5) eclipse IDE 才能看到 jar 文件是否包含在项目中。
然后,在“Java Build Path”中,您需要添加这个jar文件。
将 jar 文件包含在包片段中后,您将获得类层次结构。
长答案(为什么这可以解决问题)
CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast) 和层次结构解析器 (org.eclipse.jdt.internal.core.hierarchy)
它有一个字段ignoreFurtherInvestigation
,和一个方法hasErrors()
返回该字段。
org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver#resolve() 方法调用 hasError() 将类型信息添加到缓存。但是,如果不包含 jar 文件,hasError() 方法将始终返回 false 以防止存储任何类层次结构信息。
org.eclipse.jdt.internal.core.JavaProjectElementInfo
该类具有缓存初始化方法,例如initializePackageNames
and getProjectCache
. In getProjectCache()
方法,包片段元素根被加载并添加到缓存中。
通过包片段中的 rtstubs.jar,缓存现在包含所有 Java 类层次结构。如果没有这个设置,在缓存构建过程中,ignoreFurtherInvestigation字段会打开,并且hasError()方法返回true,不包含类层次结构信息,什么也不返回。
ADDED
另一种解决方案是使用 IRegion。
如何在 JDT TypeHierarchy 中设置区域(= java 元素集)参数? https://stackoverflow.com/questions/14419171/how-can-i-set-the-region-set-of-java-elements-parameter-in-jdt-typehierarchy/14421553#14421553
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)