Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] eclipseCon 2008: Static Analysis in PTP with CDT (now complete, first mail may be deleted)

First, make sure you are using CDT 4.0.3, this code hasn't been tested on 5.0 but may work, not sure.

>IAdaptable iad = (IAdaptable) this.openedFile;
>//test end
>ITranslationUnit tu = (ITranslationUnit) iad.getAdapter(ITranslationUnit.class);

You say you are getting nullPointerException on that last line.
Where on that line?
What is the class of this.openedFile?
Perhaps it is not adaptable to an ITranslationUnit and thus iad.getAdapter is returning null?

In the code I think you took that from, the 'obj' it's starting with is assumed to be a (single) selection from the Project Explorer view in the CDT perspective.
a file, which, as one of the members of a Structured Selection, is an ITranslationUnit.
(from the example, ASTWalkerAction, runSelectionExample)
But from the last line of your post, i think you have already figured this out!


Beth Tibbitts (859) 243-4981 (TL 545-4981)
High Productivity Tools / Parallel Tools
IBM T.J.Watson Research Center
Mailing Address: IBM Corp., 745 West New Circle Road, Lexington, KY 40511
Inactive hide details for "Florian Kaltner" <metr0n@xxxxxx>"Florian Kaltner" <metr0n@xxxxxx>

          "Florian Kaltner" <metr0n@xxxxxx>
          Sent by: cdt-dev-bounces@xxxxxxxxxxx

          06/30/08 08:47 AM

          Please respond to
          "CDT General developers list." <cdt-dev@xxxxxxxxxxx>





[cdt-dev] eclipseCon 2008: Static Analysis in PTP with CDT (now complete, first mail may be deleted)


I tried to use the code from page 10: Creating an AST (1): get ITranslationUnit in the following context:

I registered a listener on a File open action:

PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
public void run() {
IWorkbenchWindow dwindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = dwindow.getActivePage();
if (page != null) {
page.addPartListener(new FileOpenedListener());

which implements the IPartListener2 interface:

public void partActivated(IWorkbenchPartReference partRef) {

public void partBroughtToTop(IWorkbenchPartReference partRef) {

public void partClosed(IWorkbenchPartReference partRef) {

public void partDeactivated(IWorkbenchPartReference partRef) {

public void partHidden(IWorkbenchPartReference partRef) {

public void partInputChanged(IWorkbenchPartReference partRef) {


public void partOpened(IWorkbenchPartReference partRef) {


public void partVisible(IWorkbenchPartReference partRef) {
IEditorPart editor = partRef.getPage().getActiveEditor();
if (editor != null) {

IResource resource = extractResource(editor);

if (resource != null) {
this.openedFile = resource;


The extracted resource is a file containing source code:

private IResource extractResource(IEditorPart editor) {
IEditorInput input = editor.getEditorInput();
if (!(input instanceof IFileEditorInput))
return null;
return ((IFileEditorInput) input).getFile();

in the method getUnresolvedIncludesFromAST() i am trying to use the code from the presentation:

private void getUnresolvedIncludesFromAST() {

IAdaptable iad = (IAdaptable) this.openedFile;

//test end
ITranslationUnit tu = (ITranslationUnit) iad.getAdapter(ITranslationUnit.class);

IASTTranslationUnit ast = null;
ast = tu.getAST();

IASTPreprocessorIncludeStatement[] includeStatements = ast
for (IASTPreprocessorIncludeStatement includeStatement : includeStatements) {
if (!includeStatement.isResolved()) {
System.out.println("Include path of unresolved Include: "
+ includeStatement.getName());

but i am getting a NullPointerException on ITranslationUnit tu = (ITranslationUnit)iad.getAdapter(ITranslationUnit.class);

So i downloaded the sample plug-in from CVS which didn't run first throwing this error message:

Plug-in 'org.eclipse.ptp.pldt.sampleCDTstaticAnalysis' contributed an invalid Menu Extension (Path: 'sampleMenu' is invalid): cdtast2.actions.SampleAction

I replaced the line:




Allowing me to see that the code from the presentation works in that case.

To get my plug-in working i used
ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create( this.openedFile.getFullPath());
instead of the one suggested in the presentation.

But i am still wondering why the code didn't work.

Maybe its because of the different object which is cast to IAdaptable? (Using the debugger i saw that the object which was cast to IAdaptable in the code from the presentation was classified as ITranslationUnit from the beginning, whereas casting the IResource to IAdaptable still shows an actual IResource Type).


Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen:
cdt-dev mailing list

GIF image

GIF image

GIF image

Back to the top