我能够这样做:
GlobalSearchScope scope = GlobalSearchScope.allScope(project);
PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass("org.myPackage.MyClass", scope);
if ( psiClass != null ) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
fileEditorManager.openFile(psiClass.getContainingFile().getVirtualFile(), true, true);
} else {
//handle the class not found
}
在这里找到答案:https ://code.google.com/p/ide-examples/wiki/IntelliJIdeaPsiCookbook#Find_a_Class
编辑后的答案
我终于做了类似的事情:
GlobalSearchScope scope = GlobalSearchScope.allScope(project);
PsiClass psiClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
if (psiClass != null) {
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
//Open the file containing the class
VirtualFile vf = psiClass.getContainingFile().getVirtualFile();
//Jump there
new OpenFileDescriptor(project, vf, 1, 0).navigateInEditor(project, false);
} else {
//Handle file not found here....
return;
}