This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 105618
Collapse All | Expand All

(-)src/org/netbeans/api/java/source/CompilationController.java (+12 lines)
Lines 24-29 Link Here
24
import com.sun.tools.javac.api.JavacTaskImpl;
24
import com.sun.tools.javac.api.JavacTaskImpl;
25
import java.io.IOException;
25
import java.io.IOException;
26
import java.util.List;
26
import java.util.List;
27
import javax.lang.model.element.TypeElement;
27
import javax.lang.model.util.Elements;
28
import javax.lang.model.util.Elements;
28
import javax.lang.model.util.Types;
29
import javax.lang.model.util.Types;
29
import javax.swing.text.Document;
30
import javax.swing.text.Document;
Lines 104-109 Link Here
104
    @Override
105
    @Override
105
    public CompilationUnitTree getCompilationUnit() {
106
    public CompilationUnitTree getCompilationUnit() {
106
        return this.delegate.getCompilationUnit();
107
        return this.delegate.getCompilationUnit();
108
    }
109
    
110
    
111
    /**
112
     * Returns all top level elements defined in file for which this {@link CompilationController}
113
     * was created. The {@link CompilationInfo} has to be in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED}.
114
     * @return list of top level elements, it may return null when this {@link CompilationController} is not
115
     * in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED} or higher.
116
     */
117
    public List<? extends TypeElement> getTopLevelElements () {
118
        return this.delegate.getTopLevelElements();
107
    }
119
    }
108
120
109
    /**
121
    /**
(-)src/org/netbeans/api/java/source/CompilationInfo.java (+73 lines)
Lines 20-42 Link Here
20
package org.netbeans.api.java.source;
20
package org.netbeans.api.java.source;
21
21
22
import com.sun.source.tree.CompilationUnitTree;
22
import com.sun.source.tree.CompilationUnitTree;
23
import com.sun.source.tree.Tree;
24
import com.sun.source.util.TreePath;
23
import com.sun.source.util.Trees;
25
import com.sun.source.util.Trees;
24
import com.sun.tools.javac.api.JavacTaskImpl;
26
import com.sun.tools.javac.api.JavacTaskImpl;
27
import com.sun.tools.javac.code.Symbol;
28
import com.sun.tools.javac.model.JavacElements;
25
import java.io.IOException;
29
import java.io.IOException;
26
import java.util.ArrayList;
30
import java.util.ArrayList;
31
import java.util.LinkedList;
27
import java.util.List;
32
import java.util.List;
33
import javax.lang.model.element.Element;
34
import javax.lang.model.element.ElementKind;
35
import javax.lang.model.element.TypeElement;
36
import javax.lang.model.util.ElementFilter;
28
import javax.lang.model.util.Elements;
37
import javax.lang.model.util.Elements;
29
import javax.lang.model.util.Types;
38
import javax.lang.model.util.Types;
30
import javax.swing.text.Document;
39
import javax.swing.text.Document;
31
import javax.tools.DiagnosticListener;
40
import javax.tools.DiagnosticListener;
32
import javax.tools.Diagnostic;
41
import javax.tools.Diagnostic;
42
import javax.tools.JavaFileManager;
33
import javax.tools.JavaFileObject;
43
import javax.tools.JavaFileObject;
44
import javax.tools.StandardLocation;
34
import org.netbeans.api.lexer.TokenHierarchy;
45
import org.netbeans.api.lexer.TokenHierarchy;
46
import org.netbeans.modules.java.source.parsing.FileObjects;
35
import org.netbeans.modules.java.source.parsing.SourceFileObject;
47
import org.netbeans.modules.java.source.parsing.SourceFileObject;
36
import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
48
import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
37
import org.openide.ErrorManager;
49
import org.openide.ErrorManager;
38
import org.openide.cookies.EditorCookie;
50
import org.openide.cookies.EditorCookie;
39
import org.openide.filesystems.FileObject;
51
import org.openide.filesystems.FileObject;
52
import org.openide.filesystems.FileUtil;
40
import org.openide.loaders.DataObject;
53
import org.openide.loaders.DataObject;
41
54
42
55
Lines 144-149 Link Here
144
                localErrors.add(m);
157
                localErrors.add(m);
145
        }
158
        }
146
        return localErrors;
159
        return localErrors;
160
    }
161
    
162
    /**
163
     * Returns all top level elements defined in file for which the {@link CompilationInfo}
164
     * was created. The {@link CompilationInfo} has to be in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED}.
165
     * @return list of top level elements, it may return null when this {@link CompilationInfo} is not
166
     * in phase {@link JavaSource#Phase#ELEMENTS_RESOLVED} or higher.
167
     */
168
    public List<? extends TypeElement> getTopLevelElements () {
169
        if (this.jfo == null) {
170
            throw new IllegalStateException ();
171
        }
172
        List<TypeElement> result = new ArrayList<TypeElement>();
173
        if (this.javaSource.isClassFile()) {
174
            Elements elements = getElements();
175
            assert elements != null;
176
            assert this.javaSource.rootFo != null;
177
            String name = FileObjects.convertFolder2Package(FileObjects.stripExtension(FileUtil.getRelativePath(javaSource.rootFo, fo)));
178
            TypeElement e = ((JavacElements)elements).getTypeElementByBinaryName(name);
179
            if (e != null) {                
180
                if (!isLocal(e)) {
181
                    result.add (e);
182
                }
183
            }
184
        }
185
        else {
186
            CompilationUnitTree cu = getCompilationUnit();
187
            if (cu == null) {
188
                return null;
189
            }
190
            else {
191
                final Trees trees = getTrees();
192
                assert trees != null;
193
                List<? extends Tree> typeDecls = cu.getTypeDecls();
194
                TreePath cuPath = new TreePath(cu);
195
                for( Tree t : typeDecls ) {
196
                    TreePath p = new TreePath(cuPath,t);
197
                    Element e = trees.getElement(p);
198
                    if (e instanceof TypeElement) {
199
                        result.add((TypeElement)e);
200
                    }
201
                }
202
            }
203
        }
204
        return result;
205
    }
206
    
207
    //todo: remove when Abort from javac is fixed
208
    private static boolean isLocal (TypeElement sym) {
209
        if  (sym.getQualifiedName().contentEquals("")) {    //NOI18N
210
            return true;
211
        }        
212
        Element enclosing = sym.getEnclosingElement();
213
        while (enclosing != null && enclosing.getKind() != ElementKind.PACKAGE) {
214
            if (!enclosing.getKind().isClass() && !enclosing.getKind().isInterface()) {
215
                return true;
216
            }
217
            enclosing = enclosing.getEnclosingElement();
218
        }
219
        return false;
147
    }
220
    }
148
    
221
    
149
    public Trees getTrees() {
222
    public Trees getTrees() {
150
    
223
    

Return to bug 105618