无法获取ITranslationUnit在Eclipse插件中创建C文件的AST [英] Can't get ITranslationUnit for creating an AST of an C-File within a Eclipse Plugin

查看:289
本文介绍了无法获取ITranslationUnit在Eclipse插件中创建C文件的AST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过工作区中生成的项目的C-File生成一个具有CDT Eclipse Framework帮助的AST。但是每次我尝试通过



$ pre> ITranslationUnit tu =(ITranslationUnit)ice;

ice and tu为null,我收到以下错误

  java.lang.NullPointerException 
在org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.expandLocation(TeamPDOMImportOperation.java:135)
在org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.getImportLocation(TeamPDOMImportOperation.java:126)
在org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.run(TeamPDOMImportOperation.java:92)
在org.eclipse.cdt.internal.core.pdom.PDOMManager.createIndexer(PDOMManager.java:600)
在org.eclipse.cdt.internal.core.pdom.PDOMSetupJob.run(PDOMSetupJob。 java:58)
在org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

我有一个Eclipse插件,它实现了IApplication扩展。在运行方法我有以下代码。

  IProgressMonitor progressMonitor = new NullProgressMonitor(); 
IWorkspaceRoot root = ResourcesPlugin.getWorkspace()。getRoot();
IProject project = root.getProject(NewProject);
try {
if(project.exists()){
if(project.isOpen())
project.close(null);
project.delete(false,false,progressMonitor);
project.create(progressMonitor);
project.open(progressMonitor);
}
else {
project.create(progressMonitor);
project.open(progressMonitor);
}

if(!project.isNatureEnabled(org.eclipse.cdt.core.CProjectNature.C_NATURE_ID)){
CProjectNature.addCNature(project,null);
}

project.close(null);
project.open(null);

} catch(CoreException e1){
e1.printStackTrace();
}

IFile file = project.getFile(stub.c);
try {
IPath workspacerawlocation = ResourcesPlugin.getWorkspace()。getRoot()。getRawLocation();
FileInputStream fileStream = new FileInputStream(workspacerawlocation.append(stub.h)。toString());
if(!file.exists())
file.create(fileStream,false,null);

ICElement ice = CoreModel.getDefault()。create(file);
if(ice == null)
System.out.println(ice is null);
ITranslationUnit tu =(ITranslationUnit)ice;
if(tu == null){
System.out.println(tu is null);
}
else {
IASTTranslationUnit ast = tu.getAST();
}

} catch(CoreException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(FileNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

有人可以帮忙吗?我是否想念一些插件依赖或初始化?

解决方案

跟随我的评论:为了做一些代码操作,你必须使CDT索引器按照C Model / AST所要求的工作。我建议创建C项目配置的两种可能方法:



1]查看 org.eclipse.cdt.core.tests 中的测试列表,请参见#How_can_I_programmatically_create_a_new_CDT_project.3Frel =nofollow>这里。这些项目包含非常简单的.cproject配置,但可能需要对您的需求进行某些更改。



2]将C程序从工作C项目保存到某处,并在创建项目,只需将其复制。您可能需要刷新项目,或关闭/重新打开它,以便CDT重新加载配置。


I'm trying to generate an AST with help of the CDT Eclipse Framework from an C-File of a generated project in my workspace. But every time i try to get an TranslationUnit through

ICElement ice = CoreModel.getDefault().create(file);
ITranslationUnit tu= (ITranslationUnit) ice;

ice and tu are null and i get the following error

java.lang.NullPointerException
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.expandLocation(TeamPDOMImportOperation.java:135)
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.getImportLocation(TeamPDOMImportOperation.java:126)
at org.eclipse.cdt.internal.core.pdom.TeamPDOMImportOperation.run(TeamPDOMImportOperation.java:92)
at org.eclipse.cdt.internal.core.pdom.PDOMManager.createIndexer(PDOMManager.java:600)
at org.eclipse.cdt.internal.core.pdom.PDOMSetupJob.run(PDOMSetupJob.java:58)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

I have an Eclipse Plugin which implements an IApplication extension. In the run Method i have following code.

    IProgressMonitor progressMonitor = new NullProgressMonitor();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject("NewProject");
    try {
        if(project.exists()){
            if(project.isOpen())
                project.close(null);
            project.delete(false, false, progressMonitor);
            project.create(progressMonitor);
            project.open(progressMonitor);
        }
        else{
            project.create(progressMonitor);
            project.open(progressMonitor);
        }           

        if(!project.isNatureEnabled(org.eclipse.cdt.core.CProjectNature.C_NATURE_ID)){
            CProjectNature.addCNature(project, null);
        }

        project.close(null);
        project.open(null);

    } catch (CoreException e1) {
        e1.printStackTrace();
    }

    IFile file = project.getFile("stub.c");           
    try {
        IPath workspacerawlocation = ResourcesPlugin.getWorkspace().getRoot().getRawLocation();
        FileInputStream fileStream = new FileInputStream(workspacerawlocation.append("stub.h").toString());
        if(!file.exists())
            file.create(fileStream, false, null);

        ICElement ice = CoreModel.getDefault().create(file);
        if(ice == null)
            System.out.println("ice is null");
        ITranslationUnit tu= (ITranslationUnit) ice;
        if(tu == null){
            System.out.println("tu is null");
        }
        else{
            IASTTranslationUnit ast =  tu.getAST();
        }

    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

Can anyone help? Did i miss some Plugin-dependencies or initializations?

解决方案

Following up to my comment: in order to do some code manipulation, you have to have the CDT indexer working as it is required by C Model/AST. I suggest two possible ways of creating a C project configuration:

1] Look here for list of tests in org.eclipse.cdt.core.tests which create CDT projects. These projects contain very simple .cproject configuration, but some alterations to your needs might be required.

2] Save a .cproject from a working C project somewhere and when creating your projects, just copy it into. You may need to refresh the project somehow or close/reopen it so that CDT reloads the configuration.

这篇关于无法获取ITranslationUnit在Eclipse插件中创建C文件的AST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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