为什么我在JDT API中没有使用getAllSuperclasses()获得超类? [英] Why I got no super classes with getAllSuperclasses() in JDT API?

查看:91
本文介绍了为什么我在JDT API中没有使用getAllSuperclasses()获得超类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有A类,并且B类在Eclipse工作区中继承了A。






我遇到的问题是,当我尝试使用Eclipse JDT API获得B型的超类型时,我什么也没得到。这是代码(我从-


I have class A, and class B that inherits A in Eclipse workspace.

The issue that I have is that I got nothing when I tried to get the super types of type B using eclipse JDT API. This is the code (I got the code from - 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 []

I also added the code to refresh/update the resources in package.

IPackageFragmentRoot[] packageFragmentRoots = this.javaProject.getPackageFragmentRoots();
for (IPackageFragmentRoot proot: packageFragmentRoots)
{
    proot.getResource().refreshLocal(IResource.DEPTH_INFINITE, null);
}

Everything works fine except getting the hierarchical type information. What might be wrong? Did I miss any setup before executing the API?

Mine is a headless RCP application.

解决方案

This may be a temporary solution, but it worked for me.

Short Answer

Make a lib directory, and copy this rtstubs.jar into the directory. You may need to refresh(F5) the eclipse IDE to see the jar file is included in the project.

Then, in "Java Build Path", you need to add this jar file.

After the inclusion of the jar file in package fragment, you'll get the class hierarchy.

Long Answer (why does this solve the issue)

CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast) and Hierarchy Resolver (org.eclipse.jdt.internal.core.hierarchy)

It has a field ignoreFurtherInvestigation, and a method hasErrors() returns this field.

org.eclipse.jdt.internal.core.hierarchy.HierarchyResolver#resolve() method invokes hasError() to add type information to cache. However, without the inclusion of the jar file, the hasError() method always returns false to prevent any class hierarchical information is stored.

org.eclipse.jdt.internal.core.JavaProjectElementInfo

This class has cache initialization methods such as initializePackageNames and getProjectCache. In getProjectCache() method, package fragment element roots are loaded and added to the cache.

With the rtstubs.jar in the package fragment, the cache now contains all the Java class hierarchy. Without this setup, in the course of cache build up, the ignoreFurtherInvestigation filed is on, and hasError() method returns true not to contain the class hierarchical information to return just nothing.

ADDED

The other solution can be using IRegion.

How can I set the region (=set of java Elements) parameter in JDT TypeHierarchy?

这篇关于为什么我在JDT API中没有使用getAllSuperclasses()获得超类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆