KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > ElementScanningTask


1 /*
2  * ElementScanningTask.java
3  *
4  * Created on November 9, 2006, 6:04 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.java.navigation;
11 import com.sun.source.tree.CompilationUnitTree;
12 import com.sun.source.tree.Tree;
13 import com.sun.source.util.SourcePositions;
14 import com.sun.source.util.SourcePositions;
15 import com.sun.source.util.TreePath;
16 import com.sun.source.util.Trees;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import javax.lang.model.element.Element;
21 import javax.lang.model.element.Element;
22 import javax.lang.model.element.ElementKind;
23 import javax.lang.model.element.ExecutableElement;
24 import javax.lang.model.element.PackageElement;
25 import javax.lang.model.element.TypeElement;
26 import javax.lang.model.element.TypeParameterElement;
27 import javax.lang.model.element.VariableElement;
28 import javax.lang.model.type.ArrayType;
29 import javax.lang.model.type.DeclaredType;
30 import javax.lang.model.type.DeclaredType;
31 import javax.lang.model.type.TypeKind;
32 import javax.lang.model.type.TypeMirror;
33 import javax.lang.model.type.TypeVariable;
34 import javax.lang.model.type.TypeVariable;
35 import javax.lang.model.type.WildcardType;
36 import javax.lang.model.util.ElementScanner6;
37 import org.netbeans.api.java.source.CancellableTask;
38 import org.netbeans.api.java.source.CompilationInfo;
39 import org.netbeans.api.java.source.ElementHandle;
40 import org.netbeans.modules.java.navigation.ElementNode.Description;
41
42 /** XXX Remove the ElementScanner class from here it should be wenough to
43  * consult the Elements class. It should also permit for showing inherited members.
44  *
45  * @author phrebejk
46  */

47 public class ElementScanningTask implements CancellableTask<CompilationInfo>{
48     
49     private ClassMemberPanelUI ui;
50     private FindChildrenElementVisitor scanner;
51     private volatile boolean canceled;
52     
53     public ElementScanningTask( ClassMemberPanelUI ui ) {
54         this.ui = ui;
55     }
56     
57     public void cancel() {
58         //System.out.println("Element task canceled");
59
canceled = true;
60         if ( scanner != null ) {
61             scanner.cancel();
62         }
63     }
64
65     public void run(CompilationInfo info) throws Exception JavaDoc {
66         
67         canceled = false; // Task shared for one file needs reset first
68

69         //System.out.println("The task is running" + info.getFileObject().getNameExt() + "=====================================" ) ;
70

71         Description rootDescription = new Description( ui );
72         rootDescription.fileObject = info.getFileObject();
73         rootDescription.subs = new ArrayList JavaDoc<Description>();
74         
75         // Get all outerclasses in the Compilation unit
76
CompilationUnitTree cuTree = info.getCompilationUnit();
77         List JavaDoc<? extends Tree> typeDecls = cuTree.getTypeDecls();
78         List JavaDoc<Element> elements = new ArrayList JavaDoc<Element>( typeDecls.size() );
79         TreePath cuPath = new TreePath( cuTree );
80         for( Tree t : typeDecls ) {
81             TreePath p = new TreePath( cuPath, t );
82             Element e = info.getTrees().getElement( p );
83             if ( e != null ) {
84                 elements.add( e );
85             }
86         }
87         
88         if ( !canceled ) {
89             scanner = new FindChildrenElementVisitor(info);
90             for (Element element : elements) {
91                 scanner.scan(element, rootDescription);
92             }
93         }
94         
95         if ( !canceled ) {
96             ui.refresh( rootDescription );
97             
98         }
99     }
100      
101     private static class FindChildrenElementVisitor extends ElementScanner6<Void JavaDoc, ElementNode.Description> {
102         
103         private CompilationInfo info;
104         
105         private volatile boolean canceled = false;
106         
107         public FindChildrenElementVisitor(CompilationInfo info) {
108             this.info = info;
109         }
110         
111         void cancel() {
112             canceled = true;
113         }
114         
115         public Void JavaDoc visitPackage(PackageElement e, Description p) {
116             return null; // No interest in packages here
117
}
118         
119         public Void JavaDoc visitType(TypeElement e, Description p) {
120             if ( !canceled && !info.getElementUtilities().isSynthetic(e) ) {
121                 
122                 Description d = new Description(p.ui, e.getSimpleName().toString(), ElementHandle.create(e), e.getKind());
123                 d.modifiers = e.getModifiers();
124                 d.subs = new ArrayList JavaDoc<Description>();
125                 d.pos = getPosition( e );
126                 d.htmlHeader = createHtmlHeader( e, info.getElements().isDeprecated(e) );
127                 if ( d.pos == -1 ) {
128                     return null;
129                 }
130                 super.visitType(e, d);
131                 p.subs.add(d);
132             }
133             return null;
134         }
135         
136         public Void JavaDoc visitVariable(VariableElement e, Description p) {
137             if ( !canceled && !info.getElementUtilities().isSynthetic(e) &&
138                 ( e.getKind() == ElementKind.FIELD || e.getKind() == ElementKind.ENUM_CONSTANT ) ) {
139                 Description d = new Description(p.ui, e.getSimpleName().toString(), ElementHandle.create(e), e.getKind());
140                 d.modifiers = e.getModifiers();
141                 d.pos = getPosition( e );
142                 d.htmlHeader = createHtmlHeader( e, info.getElements().isDeprecated(e) );
143                 if ( d.pos == -1 ) {
144                     return null;
145                 }
146                 super.visitVariable(e,d);
147                 p.subs.add(d);
148             }
149             return null;
150         }
151         
152         public Void JavaDoc visitExecutable(ExecutableElement e, Description p) {
153             if ( !canceled && !info.getElementUtilities().isSynthetic(e) ) {
154                 Description d = new Description(p.ui, e.getSimpleName().toString(), ElementHandle.create(e), e.getKind());
155                 d.modifiers = e.getModifiers();
156                 d.pos = getPosition( e );
157                 if ( d.pos == -1 ) {
158                     return null;
159                 }
160                 d.htmlHeader = createHtmlHeader(e, info.getElements().isDeprecated(e));
161                 super.visitExecutable(e, d);
162                 p.subs.add(d);
163             }
164             return null;
165         }
166         
167         
168         public Void JavaDoc visitTypeParameter(TypeParameterElement e, Description p) {
169             return null;
170         }
171         
172         private long getPosition( Element e ) {
173              Trees trees = info.getTrees();
174              CompilationUnitTree cut = info.getCompilationUnit();
175              Tree t = trees.getTree(e);
176              
177              if ( t == null ) {
178                  // Methods like values in Enums - should noty be shown in navigator
179
return -1;
180              }
181              
182              SourcePositions sourcePositions = trees.getSourcePositions();
183              
184              return sourcePositions.getStartPosition(cut, t );
185         }
186         
187                 
188         /** Creates HTML display name of the Executable element */
189         private String JavaDoc createHtmlHeader( ExecutableElement e, boolean isDeprecated ) {
190             
191             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
192             if ( isDeprecated ) {
193                 sb.append("<s>"); // NOI18N
194
}
195             if ( e.getKind() == ElementKind.CONSTRUCTOR ) {
196                 sb.append(e.getEnclosingElement().getSimpleName());
197             }
198             else {
199                 sb.append(e.getSimpleName());
200             }
201             if ( isDeprecated ) {
202                 sb.append("</s>"); // NOI18N
203
}
204             
205             sb.append("("); // NOI18N
206

207             List JavaDoc<? extends VariableElement> params = e.getParameters();
208             for( Iterator JavaDoc<? extends VariableElement> it = params.iterator(); it.hasNext(); ) {
209                 VariableElement param = it.next();
210                 sb.append(print( param.asType()));
211                 sb.append(" "); // NOI18N
212
sb.append(param.getSimpleName());
213                 if ( it.hasNext() ) {
214                     sb.append(", "); // NOI18N
215
}
216             }
217             
218             
219             sb.append(")"); // NOI18N
220

221             if ( e.getKind() != ElementKind.CONSTRUCTOR ) {
222                 TypeMirror rt = e.getReturnType();
223                 if ( rt.getKind() != TypeKind.VOID ) {
224                     sb.append(" : "); // NOI18N
225
sb.append(print(e.getReturnType()));
226                 }
227             }
228                 
229             return sb.toString();
230         }
231         
232         private String JavaDoc createHtmlHeader( VariableElement e, boolean isDeprecated ) {
233             
234             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
235             
236             if ( isDeprecated ) {
237                 sb.append("<s>"); // NOI18N
238
}
239             sb.append(e.getSimpleName());
240             if ( isDeprecated ) {
241                 sb.append("</s>"); // NOI18N
242
}
243             
244             if ( e.getKind() != ElementKind.ENUM_CONSTANT ) {
245                 sb.append( " : " ); // NOI18N
246
sb.append(print(e.asType()));
247             }
248                         
249             return sb.toString();
250         }
251         
252         private String JavaDoc createHtmlHeader( TypeElement e, boolean isDeprecated ) {
253             
254             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
255             if ( isDeprecated ) {
256                 sb.append("<s>"); // NOI18N
257
}
258             sb.append(e.getSimpleName());
259             if ( isDeprecated ) {
260                 sb.append("</s>"); // NOI18N
261
}
262             // sb.append(print(e.asType()));
263
List JavaDoc<? extends TypeParameterElement> typeParams = e.getTypeParameters();
264             
265             //System.out.println("Element " + e + "type params" + typeParams.size() );
266

267             if ( typeParams != null && !typeParams.isEmpty() ) {
268                 sb.append("&lt;"); // NOI18N
269

270                 for( Iterator JavaDoc<? extends TypeParameterElement> it = typeParams.iterator(); it.hasNext(); ) {
271                     TypeParameterElement tp = it.next();
272                     sb.append( tp.getSimpleName() );
273                     try { // XXX Verry ugly -> file a bug against Javac?
274
List JavaDoc<? extends TypeMirror> bounds = tp.getBounds();
275                         //System.out.println( tp.getSimpleName() + " bounds size " + bounds.size() );
276
if ( !bounds.isEmpty() ) {
277                             sb.append(printBounds(bounds));
278                         }
279                     }
280                     catch ( NullPointerException JavaDoc npe ) {
281                         System.err.println("El " + e );
282                         npe.printStackTrace();
283                     }
284                     if ( it.hasNext() ) {
285                         sb.append(", "); // NOI18N
286
}
287                 }
288                 
289                 sb.append("&gt;"); // NOI18N
290
}
291             return sb.toString();
292         }
293         
294         private String JavaDoc printBounds( List JavaDoc<? extends TypeMirror> bounds ) {
295             if ( bounds.size() == 1 && "java.lang.Object".equals( bounds.get(0).toString() ) ) {
296                 return "";
297             }
298             
299             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
300             
301             sb.append( " extends " ); // NOI18N
302

303             for (Iterator JavaDoc<? extends TypeMirror> it = bounds.iterator(); it.hasNext();) {
304                 TypeMirror bound = it.next();
305                 sb.append(print(bound));
306                 if ( it.hasNext() ) {
307                     sb.append(" & " ); // NOI18N
308
}
309                
310             }
311
312             return sb.toString();
313         }
314         
315         private String JavaDoc print( TypeMirror tm ) {
316             StringBuilder JavaDoc sb;
317             
318             switch ( tm.getKind() ) {
319                 case DECLARED:
320                     DeclaredType dt = (DeclaredType)tm;
321                     sb = new StringBuilder JavaDoc( dt.asElement().getSimpleName().toString() );
322                     List JavaDoc<? extends TypeMirror> typeArgs = dt.getTypeArguments();
323                     if ( !typeArgs.isEmpty() ) {
324                         sb.append("&lt;");
325                         
326                         for (Iterator JavaDoc<? extends TypeMirror> it = typeArgs.iterator(); it.hasNext();) {
327                             TypeMirror ta = it.next();
328                             sb.append(print(ta));
329                             if ( it.hasNext() ) {
330                                 sb.append(", ");
331                             }
332                         }
333                         sb.append("&gt;");
334                     }
335                     
336                     return sb.toString();
337                 case TYPEVAR:
338                     TypeVariable tv = (TypeVariable)tm;
339                     sb = new StringBuilder JavaDoc( tv.asElement().getSimpleName().toString() );
340                     return sb.toString();
341                 case ARRAY:
342                     ArrayType at = (ArrayType)tm;
343                     sb = new StringBuilder JavaDoc( print(at.getComponentType()) );
344                     sb.append("[]");
345                     return sb.toString();
346                 case WILDCARD:
347                     WildcardType wt = (WildcardType)tm;
348                     sb = new StringBuilder JavaDoc("?");
349                     if ( wt.getExtendsBound() != null ) {
350                         sb.append(" extends "); // NOI18N
351
sb.append(print(wt.getExtendsBound()));
352                     }
353                     if ( wt.getSuperBound() != null ) {
354                         sb.append(" super "); // NOI18N
355
sb.append(print(wt.getSuperBound()));
356                     }
357                     return sb.toString();
358                 default:
359                     return tm.toString();
360             }
361         }
362         
363         
364     }
365     
366 }
367
Popular Tags