Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] [ajdt core] !!!


Okay, here is some quick and dirty (but complete) code to walk the structure of a compilation unit:

package org.eclipse.ajdt.ui.snippets;

import org.eclipse.ajdt.ui.AspectJUIPlugin;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IParent;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.ui.IWorkingCopyManager;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

public class ShowCUStructure implements IWorkbenchWindowActionDelegate {
        public void run(IAction action) {
                IWorkbenchPart part = AspectJUIPlugin.getDefault()
                                .getActiveWorkbenchWindow().getActivePage().getActivePart();
                if ((part != null) && (part instanceof JavaEditor)) {
                        JavaEditor editor = (JavaEditor) part;
                        IWorkingCopyManager manager = JavaPlugin.getDefault()
                                        .getWorkingCopyManager();
                        ICompilationUnit unit = manager.getWorkingCopy(editor
                                        .getEditorInput());
                        System.out.println("unit: "+unit.getElementName());
                        System.out.println("unit class: "+unit.getClass());
                        showChildren(unit,"");
                }
        }

        private void showChildren(IParent parent, String indent) {
                try {
                        IJavaElement[] child = parent.getChildren();
                        for (int i = 0; i < child.length; i++) {
                                System.out.println(indent + child[i].getElementName() + " ("
                                                + child[i].getClass().getName() + ")");
                                if (child[i] instanceof IParent) {
                                        showChildren((IParent) child[i], indent + "  ");
                                }
                        }
                } catch (JavaModelException e) {
                        e.printStackTrace();
                }
        }
       
        public void init(IWorkbenchWindow window) {}
        public void selectionChanged(IAction action, ISelection selection) {}
        public void dispose() {}
}

And here is some plugin.xml stuff to activate it:

   <extension
                point="org.eclipse.ui.actionSets">
             <actionSet
            label="Snippet: Show CU structure"
            description="Snippet: Show CU structure"
            visible="true"
            id="org.eclipse.ajdt.ui.snippets.ShowCU">
         <action
               definitionId="org.eclipse.ajdt.ui.snippets.ShowCU"
               label="Snippet: Show CU structure"
               class="org.eclipse.ajdt.ui.snippets.ShowCUStructure"
               menubarPath="navigate/open.ext"
               id="showCU">
         </action>
      </actionSet>
   </extension>            
   <extension
         point="org.eclipse.ui.commands">
      <command
            name="Snippet: Show CU structure command"
            description="Snippet: Show CU structure"
            category="org.eclipse.jdt.ui.category.refactoring"
            id="org.eclipse.ajdt.ui.snippets.ShowCU">
      </command>
      <keyBinding
            string="F7"
            scope="org.eclipse.jdt.ui.javaEditorScope"
            command="org.eclipse.ajdt.ui.snippets.ShowCU"
            configuration="org.eclipse.ui.defaultAcceleratorConfiguration">
      </keyBinding>
   </extension>

Now pressing F7 (or using the navigate menu entry) from an editor, will print out the structure of the compilation unit for that editor (works with regular CUs and AJCompilationUnits). Here's some example output for GetInfo.aj in the TJP example:

unit: GetInfo.aj
unit class: class org.eclipse.ajdt.core.javaelements.AJCompilationUnit
tjp (org.eclipse.jdt.internal.core.PackageDeclaration)
 (org.eclipse.jdt.internal.core.ImportContainer)
  org.aspectj.lang.JoinPoint (org.eclipse.jdt.internal.core.ImportDeclaration)
  org.aspectj.lang.reflect.CodeSignature (org.eclipse.jdt.internal.core.ImportDeclaration)
GetInfo (org.eclipse.ajdt.core.javaelements.AspectElement)
  println (org.eclipse.jdt.internal.core.SourceMethod)
  goCut (org.eclipse.ajdt.core.javaelements.PointcutElement)
  demoExecs (org.eclipse.ajdt.core.javaelements.PointcutElement)
  around (org.eclipse.ajdt.core.javaelements.AdviceElement)
  printParameters (org.eclipse.jdt.internal.core.SourceMethod)

As you can see it prints out the class too, so you can do instanceof tests to look for PointcutElements etc.

This is exploring the known structure (as used to populate the Outline view etc) - it may or may not contain enough detail, depending on what you're trying to do.

Regards,

Matt.

--
Matt Chapman, mchapman@xxxxxxxxxx
AJDT Development, http://www.eclipse.org/ajdt


adrian_colyer@xxxxxxxxxx wrote on 30/05/2005 21:02:11:
> AspectDeclaration (in org.aspectj.ajdt.core) extends (org.aspectj.)
> org.eclipse.jdt.internal.compiler.*ast*.TypeDeclaration. The JDT
> compiler has two mirrored structures - one for the AST tree, and one
> for the DOM tree -  and it also has the related IJavaElement (model)
> tree. In the AspectJ project, we extend the AST tree with AspectJ
> elements to support compilation (these are the types you see in the
> org.aspectj.ajdt.compiler.internal.ast package), but we do not yet
> extend the DOM tree to support AspectJ elements. This latter
> capability is not needed for compilation, although it will be needed
> to support more advanced IDE features. AJDT itself builds some read-
> only structure from AspectJ files in the model tree, that contain
> sufficient information to tell you what you need here. *Matt* could
> you post a snippet showing how you walk the structure of a source
> file for e.g. the package explorer view please?


Alex Vasc <altvex@xxxxxxxxx> wrote on 27/05/2005 16:43:06:
> I did it because the AspectDeclaration extends the TypeDeclaration
> from jdt.core.dom. So, I supposed that I could get this element from a
> .java file. Is this correct? Any thoughts?! Please, tell me if I´m
> doing something stupid! My only itention is to get aspectj elements
> from an open file editor.
>
> Best regards!

Back to the top