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.