Index: src/org/netbeans/api/java/source/CompilationController.java =================================================================== RCS file: /cvs/java/source/src/org/netbeans/api/java/source/CompilationController.java,v --- src/org/netbeans/api/java/source/CompilationController.java 18 Mar 2007 09:04:06 -0000 1.6 +++ src/org/netbeans/api/java/source/CompilationController.java 4 Jun 2007 16:41:16 -0000 @@ -24,6 +24,7 @@ import com.sun.tools.javac.api.JavacTaskImpl; import java.io.IOException; import java.util.List; +import javax.lang.model.element.TypeElement; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.swing.text.Document; @@ -104,6 +105,17 @@ @Override public CompilationUnitTree getCompilationUnit() { return this.delegate.getCompilationUnit(); + } + + + /** + * Returns all top level elements defined in file for which this {@link CompilationController} + * was created. The {@link CompilationInfo} has to be in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED}. + * @return list of top level elements, it may return null when this {@link CompilationController} is not + * in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED} or higher. + */ + public List getTopLevelElements () { + return this.delegate.getTopLevelElements(); } /** Index: src/org/netbeans/api/java/source/CompilationInfo.java =================================================================== RCS file: /cvs/java/source/src/org/netbeans/api/java/source/CompilationInfo.java,v --- src/org/netbeans/api/java/source/CompilationInfo.java 24 Apr 2007 06:26:05 -0000 1.10 +++ src/org/netbeans/api/java/source/CompilationInfo.java 4 Jun 2007 16:41:16 -0000 @@ -20,23 +20,36 @@ package org.netbeans.api.java.source; import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.TreePath; import com.sun.source.util.Trees; import com.sun.tools.javac.api.JavacTaskImpl; +import com.sun.tools.javac.code.Symbol; +import com.sun.tools.javac.model.JavacElements; import java.io.IOException; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.TypeElement; +import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; import javax.swing.text.Document; import javax.tools.DiagnosticListener; import javax.tools.Diagnostic; +import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; +import javax.tools.StandardLocation; import org.netbeans.api.lexer.TokenHierarchy; +import org.netbeans.modules.java.source.parsing.FileObjects; import org.netbeans.modules.java.source.parsing.SourceFileObject; import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation; import org.openide.ErrorManager; import org.openide.cookies.EditorCookie; import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; import org.openide.loaders.DataObject; @@ -144,6 +157,66 @@ localErrors.add(m); } return localErrors; + } + + /** + * Returns all top level elements defined in file for which the {@link CompilationInfo} + * was created. The {@link CompilationInfo} has to be in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED}. + * @return list of top level elements, it may return null when this {@link CompilationInfo} is not + * in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED} or higher. + */ + public List getTopLevelElements () { + if (this.jfo == null) { + throw new IllegalStateException (); + } + List result = new ArrayList(); + if (this.javaSource.isClassFile()) { + Elements elements = getElements(); + assert elements != null; + assert this.javaSource.rootFo != null; + String name = FileObjects.convertFolder2Package(FileObjects.stripExtension(FileUtil.getRelativePath(javaSource.rootFo, fo))); + TypeElement e = ((JavacElements)elements).getTypeElementByBinaryName(name); + if (e != null) { + if (!isLocal(e)) { + result.add (e); + } + } + } + else { + CompilationUnitTree cu = getCompilationUnit(); + if (cu == null) { + return null; + } + else { + final Trees trees = getTrees(); + assert trees != null; + List typeDecls = cu.getTypeDecls(); + TreePath cuPath = new TreePath(cu); + for( Tree t : typeDecls ) { + TreePath p = new TreePath(cuPath,t); + Element e = trees.getElement(p); + if (e instanceof TypeElement) { + result.add((TypeElement)e); + } + } + } + } + return result; + } + + //todo: remove when Abort from javac is fixed + private static boolean isLocal (TypeElement sym) { + if (sym.getQualifiedName().contentEquals("")) { //NOI18N + return true; + } + Element enclosing = sym.getEnclosingElement(); + while (enclosing != null && enclosing.getKind() != ElementKind.PACKAGE) { + if (!enclosing.getKind().isClass() && !enclosing.getKind().isInterface()) { + return true; + } + enclosing = enclosing.getEnclosingElement(); + } + return false; } public Trees getTrees() {