Lines 48-53
Link Here
|
48 |
import java.beans.PropertyChangeListener; |
48 |
import java.beans.PropertyChangeListener; |
49 |
import java.io.IOException; |
49 |
import java.io.IOException; |
50 |
import java.net.URL; |
50 |
import java.net.URL; |
|
|
51 |
import java.util.Arrays; |
51 |
import java.util.Collection; |
52 |
import java.util.Collection; |
52 |
import java.util.Collections; |
53 |
import java.util.Collections; |
53 |
import java.util.EnumSet; |
54 |
import java.util.EnumSet; |
Lines 63-68
Link Here
|
63 |
import javax.lang.model.element.ElementKind; |
64 |
import javax.lang.model.element.ElementKind; |
64 |
import javax.lang.model.element.TypeElement; |
65 |
import javax.lang.model.element.TypeElement; |
65 |
import org.apache.lucene.document.Document; |
66 |
import org.apache.lucene.document.Document; |
|
|
67 |
import org.netbeans.api.annotations.common.CheckForNull; |
66 |
import org.netbeans.api.annotations.common.NonNull; |
68 |
import org.netbeans.api.annotations.common.NonNull; |
67 |
import org.netbeans.api.annotations.common.NullUnknown; |
69 |
import org.netbeans.api.annotations.common.NullUnknown; |
68 |
import org.netbeans.api.java.classpath.ClassPath; |
70 |
import org.netbeans.api.java.classpath.ClassPath; |
Lines 212-230
Link Here
|
212 |
}; |
214 |
}; |
213 |
|
215 |
|
214 |
/** |
216 |
/** |
215 |
* Scope used by {@link ClassIndex} to search in |
217 |
* Default predefined {@link SearchScopeType}s |
216 |
*/ |
218 |
*/ |
217 |
public enum SearchScope { |
219 |
public enum SearchScope implements SearchScopeType { |
218 |
/** |
220 |
/** |
219 |
* Search is done in source path |
221 |
* Search is done in source path |
220 |
*/ |
222 |
*/ |
221 |
SOURCE, |
223 |
SOURCE { |
|
|
224 |
@Override |
225 |
public boolean isSources() {return true;} |
226 |
@Override |
227 |
public boolean isDependencies() {return false;} |
228 |
}, |
222 |
/** |
229 |
/** |
223 |
* Search is done in compile and boot path |
230 |
* Search is done in compile and boot path |
224 |
*/ |
231 |
*/ |
225 |
DEPENDENCIES |
232 |
DEPENDENCIES { |
|
|
233 |
@Override |
234 |
public boolean isSources() {return false;} |
235 |
@Override |
236 |
public boolean isDependencies() {return true;} |
237 |
}; |
238 |
|
239 |
@Override |
240 |
@CheckForNull |
241 |
public Set<? extends String> getPackages() { |
242 |
return null; |
243 |
} |
226 |
}; |
244 |
}; |
227 |
|
245 |
|
|
|
246 |
/** |
247 |
* Scope used by {@link ClassIndex} to search in |
248 |
* @since 0.82.0 |
249 |
*/ |
250 |
public static interface SearchScopeType { |
251 |
/** |
252 |
* Limits search only into given packages. |
253 |
* @return set of packages to search in or null which |
254 |
* means all packages |
255 |
*/ |
256 |
@CheckForNull |
257 |
Set<? extends String> getPackages(); |
258 |
|
259 |
/** |
260 |
* Search in source path. |
261 |
* @return if true search in done in sources |
262 |
*/ |
263 |
boolean isSources(); |
264 |
|
265 |
/** |
266 |
* Search in dependent libraries bootpath, compilepath. |
267 |
* @return if true search in done in dependent libraries |
268 |
*/ |
269 |
boolean isDependencies(); |
270 |
} |
271 |
|
228 |
static { |
272 |
static { |
229 |
ClassIndexImpl.FACTORY = new ClassIndexFactoryImpl(); |
273 |
ClassIndexImpl.FACTORY = new ClassIndexFactoryImpl(); |
230 |
} |
274 |
} |
Lines 288-294
Link Here
|
288 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
332 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
289 |
* inside call of this method. |
333 |
* inside call of this method. |
290 |
*/ |
334 |
*/ |
291 |
public @NullUnknown Set<ElementHandle<TypeElement>> getElements (final @NonNull ElementHandle<TypeElement> element, final @NonNull Set<SearchKind> searchKind, final @NonNull Set<SearchScope> scope) { |
335 |
public @NullUnknown Set<ElementHandle<TypeElement>> getElements ( |
|
|
336 |
final @NonNull ElementHandle<TypeElement> element, |
337 |
final @NonNull Set<SearchKind> searchKind, |
338 |
final @NonNull Set<? extends SearchScopeType> scope) { |
292 |
assert element != null; |
339 |
assert element != null; |
293 |
assert element.getSignature()[0] != null; |
340 |
assert element.getSignature()[0] != null; |
294 |
assert searchKind != null; |
341 |
assert searchKind != null; |
Lines 301-307
Link Here
|
301 |
if (!ut.isEmpty()) { |
348 |
if (!ut.isEmpty()) { |
302 |
for (ClassIndexImpl query : queries) { |
349 |
for (ClassIndexImpl query : queries) { |
303 |
try { |
350 |
try { |
304 |
query.search(binaryName, ut, thConvertor, result); |
351 |
query.search( |
|
|
352 |
binaryName, |
353 |
ut, |
354 |
scope, |
355 |
thConvertor, |
356 |
result); |
305 |
} catch (Index.IndexClosedException e) { |
357 |
} catch (Index.IndexClosedException e) { |
306 |
logClosedIndex (query); |
358 |
logClosedIndex (query); |
307 |
} catch (IOException e) { |
359 |
} catch (IOException e) { |
Lines 324-343
Link Here
|
324 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
376 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
325 |
* inside call of this method. |
377 |
* inside call of this method. |
326 |
*/ |
378 |
*/ |
327 |
public @NullUnknown Set<FileObject> getResources (final @NonNull ElementHandle<TypeElement> element, final @NonNull Set<SearchKind> searchKind, final @NonNull Set<SearchScope> scope) { |
379 |
public @NullUnknown Set<FileObject> getResources ( |
|
|
380 |
final @NonNull ElementHandle<TypeElement> element, |
381 |
final @NonNull Set<SearchKind> searchKind, |
382 |
final @NonNull Set<? extends SearchScopeType> scope) { |
328 |
assert element != null; |
383 |
assert element != null; |
329 |
assert element.getSignature()[0] != null; |
384 |
assert element.getSignature()[0] != null; |
330 |
assert searchKind != null; |
385 |
assert searchKind != null; |
331 |
final Set<FileObject> result = new HashSet<FileObject> (); |
386 |
final Set<FileObject> result = new HashSet<FileObject> (); |
332 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
387 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
333 |
final Set<ClassIndexImpl.UsageType> ut = encodeSearchKind(element.getKind(),searchKind); |
388 |
final Set<ClassIndexImpl.UsageType> ut = encodeSearchKind(element.getKind(),searchKind); |
334 |
final String binaryName = element.getSignature()[0]; |
389 |
final String binaryName = element.getSignature()[0]; |
335 |
try { |
390 |
try { |
336 |
if (!ut.isEmpty()) { |
391 |
if (!ut.isEmpty()) { |
337 |
for (ClassIndexImpl query : queries) { |
392 |
for (ClassIndexImpl query : queries) { |
338 |
final Convertor<? super Document, FileObject> foConvertor = DocumentUtil.fileObjectConvertor (query.getSourceRoots()); |
393 |
final Convertor<? super Document, FileObject> foConvertor = DocumentUtil.fileObjectConvertor (query.getSourceRoots()); |
339 |
try { |
394 |
try { |
340 |
query.search (binaryName, ut, foConvertor, result); |
395 |
query.search( |
|
|
396 |
binaryName, |
397 |
ut, |
398 |
scope, |
399 |
foConvertor, |
400 |
result); |
341 |
} catch (Index.IndexClosedException e) { |
401 |
} catch (Index.IndexClosedException e) { |
342 |
logClosedIndex (query); |
402 |
logClosedIndex (query); |
343 |
} catch (IOException e) { |
403 |
} catch (IOException e) { |
Lines 349-355
Link Here
|
349 |
} catch (InterruptedException e) { |
409 |
} catch (InterruptedException e) { |
350 |
return null; |
410 |
return null; |
351 |
} |
411 |
} |
352 |
} |
412 |
} |
353 |
|
413 |
|
354 |
|
414 |
|
355 |
/** |
415 |
/** |
Lines 362-368
Link Here
|
362 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
422 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
363 |
* inside call of this method. |
423 |
* inside call of this method. |
364 |
*/ |
424 |
*/ |
365 |
public @NullUnknown Set<ElementHandle<TypeElement>> getDeclaredTypes (final @NonNull String name, final @NonNull NameKind kind, final @NonNull Set<SearchScope> scope) { |
425 |
public @NullUnknown Set<ElementHandle<TypeElement>> getDeclaredTypes ( |
|
|
426 |
final @NonNull String name, |
427 |
final @NonNull NameKind kind, |
428 |
final @NonNull Set<? extends SearchScopeType> scope) { |
366 |
assert name != null; |
429 |
assert name != null; |
367 |
assert kind != null; |
430 |
assert kind != null; |
368 |
final Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>>(); |
431 |
final Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>>(); |
Lines 400-406
Link Here
|
400 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
463 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
401 |
* inside call of this method. |
464 |
* inside call of this method. |
402 |
*/ |
465 |
*/ |
403 |
public @NullUnknown Set<String> getPackageNames (final @NonNull String prefix, boolean directOnly, final @NonNull Set<SearchScope> scope) { |
466 |
public @NullUnknown Set<String> getPackageNames ( |
|
|
467 |
final @NonNull String prefix, |
468 |
boolean directOnly, |
469 |
final @NonNull Set<? extends SearchScopeType> scope) { |
404 |
assert prefix != null; |
470 |
assert prefix != null; |
405 |
final Set<String> result = new HashSet<String> (); |
471 |
final Set<String> result = new HashSet<String> (); |
406 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
472 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
Lines 419-427
Link Here
|
419 |
return null; |
485 |
return null; |
420 |
} |
486 |
} |
421 |
} |
487 |
} |
422 |
|
488 |
|
423 |
// Private innerclasses ---------------------------------------------------- |
489 |
/** |
424 |
|
490 |
* Creates a search scope limited to list of packages. |
|
|
491 |
* @param base the base search scope to restrict |
492 |
* @param pkgs a list of packages in which the search should be performed |
493 |
* @return a newly created search scope |
494 |
* @since 0.82.0 |
495 |
*/ |
496 |
@NonNull |
497 |
public static SearchScopeType createPackageSearchScope( |
498 |
@NonNull final SearchScopeType base, |
499 |
@NonNull final String... pkgs) { |
500 |
final Set<String> pkgSet = new HashSet<String>(Arrays.asList(pkgs)); |
501 |
final Set<? extends String> basePkgs = base.getPackages(); |
502 |
if (basePkgs != null) { |
503 |
pkgSet.addAll(basePkgs); |
504 |
} |
505 |
final Set<String> newPkgs = Collections.unmodifiableSet(pkgSet); |
506 |
return new SearchScopeType() { |
507 |
@Override |
508 |
public Set<? extends String> getPackages() { |
509 |
return newPkgs; |
510 |
} |
511 |
|
512 |
@Override |
513 |
public boolean isSources() { |
514 |
return base.isSources(); |
515 |
} |
516 |
|
517 |
@Override |
518 |
public boolean isDependencies() { |
519 |
return base.isDependencies(); |
520 |
} |
521 |
}; |
522 |
} |
523 |
|
524 |
// <editor-fold defaultstate="collapsed" desc="Private implementation"> |
425 |
private static class ClassIndexFactoryImpl implements ClassIndexFactory { |
525 |
private static class ClassIndexFactoryImpl implements ClassIndexFactory { |
426 |
|
526 |
|
427 |
public ClassIndex create(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) { |
527 |
public ClassIndex create(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) { |
Lines 465-479
Link Here
|
465 |
} |
565 |
} |
466 |
}); |
566 |
}); |
467 |
} |
567 |
} |
468 |
|
568 |
|
469 |
private Iterable<? extends ClassIndexImpl> getQueries (final Set<SearchScope> scope) { |
569 |
private Iterable<? extends ClassIndexImpl> getQueries (final Set<? extends SearchScopeType> scope) { |
470 |
final Set<ClassIndexImpl> result = new HashSet<ClassIndexImpl> (); |
570 |
final Set<ClassIndexImpl> result = new HashSet<ClassIndexImpl> (); |
471 |
synchronized (this) { |
571 |
synchronized (this) { |
472 |
if (scope.contains(SearchScope.SOURCE)) { |
572 |
for (SearchScopeType s : scope) { |
473 |
result.addAll(this.sourceIndeces); |
573 |
if (s.isSources()) { |
474 |
} |
574 |
result.addAll(this.sourceIndeces); |
475 |
if (scope.contains(SearchScope.DEPENDENCIES)) { |
575 |
} |
476 |
result.addAll(this.depsIndeces); |
576 |
if (s.isDependencies()) { |
|
|
577 |
result.addAll(this.depsIndeces); |
578 |
} |
477 |
} |
579 |
} |
478 |
} |
580 |
} |
479 |
LOGGER.log( |
581 |
LOGGER.log( |
Lines 487-494
Link Here
|
487 |
result |
589 |
result |
488 |
}); |
590 |
}); |
489 |
return result; |
591 |
return result; |
490 |
} |
592 |
} |
491 |
|
593 |
|
492 |
private void createQueriesForRoots (final ClassPath cp, final boolean sources, final Set<? super ClassIndexImpl> queries, final Set<? super URL> oldState) { |
594 |
private void createQueriesForRoots (final ClassPath cp, final boolean sources, final Set<? super ClassIndexImpl> queries, final Set<? super URL> oldState) { |
493 |
final PathRegistry preg = PathRegistry.getDefault(); |
595 |
final PathRegistry preg = PathRegistry.getDefault(); |
494 |
List<ClassPath.Entry> entries = cp.entries(); |
596 |
List<ClassPath.Entry> entries = cp.entries(); |
Lines 820-823
Link Here
|
820 |
private static void assertParserEventThread() { |
922 |
private static void assertParserEventThread() { |
821 |
assert Utilities.isTaskProcessorThread(Thread.currentThread()); |
923 |
assert Utilities.isTaskProcessorThread(Thread.currentThread()); |
822 |
} |
924 |
} |
|
|
925 |
//</editor-fold> |
823 |
} |
926 |
} |