Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[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:

Back to the top