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 87-92
Link Here
|
87 |
import org.openide.filesystems.FileObject; |
89 |
import org.openide.filesystems.FileObject; |
88 |
import org.openide.util.Exceptions; |
90 |
import org.openide.util.Exceptions; |
89 |
import org.openide.util.Mutex; |
91 |
import org.openide.util.Mutex; |
|
|
92 |
import org.openide.util.Parameters; |
90 |
import org.openide.util.WeakListeners; |
93 |
import org.openide.util.WeakListeners; |
91 |
|
94 |
|
92 |
/** |
95 |
/** |
Lines 212-230
Link Here
|
212 |
}; |
215 |
}; |
213 |
|
216 |
|
214 |
/** |
217 |
/** |
215 |
* Scope used by {@link ClassIndex} to search in |
218 |
* Default predefined {@link SearchScopeType}s |
216 |
*/ |
219 |
*/ |
217 |
public enum SearchScope { |
220 |
public enum SearchScope implements SearchScopeType { |
218 |
/** |
221 |
/** |
219 |
* Search is done in source path |
222 |
* Search is done in source path |
220 |
*/ |
223 |
*/ |
221 |
SOURCE, |
224 |
SOURCE { |
|
|
225 |
@Override |
226 |
public boolean isSources() {return true;} |
227 |
@Override |
228 |
public boolean isDependencies() {return false;} |
229 |
}, |
222 |
/** |
230 |
/** |
223 |
* Search is done in compile and boot path |
231 |
* Search is done in compile and boot path |
224 |
*/ |
232 |
*/ |
225 |
DEPENDENCIES |
233 |
DEPENDENCIES { |
|
|
234 |
@Override |
235 |
public boolean isSources() {return false;} |
236 |
@Override |
237 |
public boolean isDependencies() {return true;} |
238 |
}; |
239 |
|
240 |
@Override |
241 |
@CheckForNull |
242 |
public Set<? extends String> getPackages() { |
243 |
return null; |
244 |
} |
226 |
}; |
245 |
}; |
227 |
|
246 |
|
|
|
247 |
/** |
248 |
* Scope used by {@link ClassIndex} to search in |
249 |
* @since 0.82.0 |
250 |
*/ |
251 |
public static interface SearchScopeType { |
252 |
/** |
253 |
* Limits search only into given packages. |
254 |
* @return set of packages to search in or null which |
255 |
* means all packages |
256 |
*/ |
257 |
@CheckForNull |
258 |
Set<? extends String> getPackages(); |
259 |
|
260 |
/** |
261 |
* Search in source path. |
262 |
* @return if true search in done in sources |
263 |
*/ |
264 |
boolean isSources(); |
265 |
|
266 |
/** |
267 |
* Search in dependent libraries bootpath, compilepath. |
268 |
* @return if true search in done in dependent libraries |
269 |
*/ |
270 |
boolean isDependencies(); |
271 |
} |
272 |
|
228 |
static { |
273 |
static { |
229 |
ClassIndexImpl.FACTORY = new ClassIndexFactoryImpl(); |
274 |
ClassIndexImpl.FACTORY = new ClassIndexFactoryImpl(); |
230 |
} |
275 |
} |
Lines 288-294
Link Here
|
288 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
333 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
289 |
* inside call of this method. |
334 |
* inside call of this method. |
290 |
*/ |
335 |
*/ |
291 |
public @NullUnknown Set<ElementHandle<TypeElement>> getElements (final @NonNull ElementHandle<TypeElement> element, final @NonNull Set<SearchKind> searchKind, final @NonNull Set<SearchScope> scope) { |
336 |
public @NullUnknown Set<ElementHandle<TypeElement>> getElements ( |
|
|
337 |
final @NonNull ElementHandle<TypeElement> element, |
338 |
final @NonNull Set<SearchKind> searchKind, |
339 |
final @NonNull Set<? extends SearchScopeType> scope) { |
292 |
assert element != null; |
340 |
assert element != null; |
293 |
assert element.getSignature()[0] != null; |
341 |
assert element.getSignature()[0] != null; |
294 |
assert searchKind != null; |
342 |
assert searchKind != null; |
Lines 301-307
Link Here
|
301 |
if (!ut.isEmpty()) { |
349 |
if (!ut.isEmpty()) { |
302 |
for (ClassIndexImpl query : queries) { |
350 |
for (ClassIndexImpl query : queries) { |
303 |
try { |
351 |
try { |
304 |
query.search(binaryName, ut, thConvertor, result); |
352 |
query.search( |
|
|
353 |
binaryName, |
354 |
ut, |
355 |
scope, |
356 |
thConvertor, |
357 |
result); |
305 |
} catch (Index.IndexClosedException e) { |
358 |
} catch (Index.IndexClosedException e) { |
306 |
logClosedIndex (query); |
359 |
logClosedIndex (query); |
307 |
} catch (IOException e) { |
360 |
} catch (IOException e) { |
Lines 324-343
Link Here
|
324 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
377 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
325 |
* inside call of this method. |
378 |
* inside call of this method. |
326 |
*/ |
379 |
*/ |
327 |
public @NullUnknown Set<FileObject> getResources (final @NonNull ElementHandle<TypeElement> element, final @NonNull Set<SearchKind> searchKind, final @NonNull Set<SearchScope> scope) { |
380 |
public @NullUnknown Set<FileObject> getResources ( |
|
|
381 |
final @NonNull ElementHandle<TypeElement> element, |
382 |
final @NonNull Set<SearchKind> searchKind, |
383 |
final @NonNull Set<? extends SearchScopeType> scope) { |
328 |
assert element != null; |
384 |
assert element != null; |
329 |
assert element.getSignature()[0] != null; |
385 |
assert element.getSignature()[0] != null; |
330 |
assert searchKind != null; |
386 |
assert searchKind != null; |
331 |
final Set<FileObject> result = new HashSet<FileObject> (); |
387 |
final Set<FileObject> result = new HashSet<FileObject> (); |
332 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
388 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
333 |
final Set<ClassIndexImpl.UsageType> ut = encodeSearchKind(element.getKind(),searchKind); |
389 |
final Set<ClassIndexImpl.UsageType> ut = encodeSearchKind(element.getKind(),searchKind); |
334 |
final String binaryName = element.getSignature()[0]; |
390 |
final String binaryName = element.getSignature()[0]; |
335 |
try { |
391 |
try { |
336 |
if (!ut.isEmpty()) { |
392 |
if (!ut.isEmpty()) { |
337 |
for (ClassIndexImpl query : queries) { |
393 |
for (ClassIndexImpl query : queries) { |
338 |
final Convertor<? super Document, FileObject> foConvertor = DocumentUtil.fileObjectConvertor (query.getSourceRoots()); |
394 |
final Convertor<? super Document, FileObject> foConvertor = DocumentUtil.fileObjectConvertor (query.getSourceRoots()); |
339 |
try { |
395 |
try { |
340 |
query.search (binaryName, ut, foConvertor, result); |
396 |
query.search( |
|
|
397 |
binaryName, |
398 |
ut, |
399 |
scope, |
400 |
foConvertor, |
401 |
result); |
341 |
} catch (Index.IndexClosedException e) { |
402 |
} catch (Index.IndexClosedException e) { |
342 |
logClosedIndex (query); |
403 |
logClosedIndex (query); |
343 |
} catch (IOException e) { |
404 |
} catch (IOException e) { |
Lines 349-355
Link Here
|
349 |
} catch (InterruptedException e) { |
410 |
} catch (InterruptedException e) { |
350 |
return null; |
411 |
return null; |
351 |
} |
412 |
} |
352 |
} |
413 |
} |
353 |
|
414 |
|
354 |
|
415 |
|
355 |
/** |
416 |
/** |
Lines 362-368
Link Here
|
362 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
423 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
363 |
* inside call of this method. |
424 |
* inside call of this method. |
364 |
*/ |
425 |
*/ |
365 |
public @NullUnknown Set<ElementHandle<TypeElement>> getDeclaredTypes (final @NonNull String name, final @NonNull NameKind kind, final @NonNull Set<SearchScope> scope) { |
426 |
public @NullUnknown Set<ElementHandle<TypeElement>> getDeclaredTypes ( |
|
|
427 |
final @NonNull String name, |
428 |
final @NonNull NameKind kind, |
429 |
final @NonNull Set<? extends SearchScopeType> scope) { |
366 |
assert name != null; |
430 |
assert name != null; |
367 |
assert kind != null; |
431 |
assert kind != null; |
368 |
final Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>>(); |
432 |
final Set<ElementHandle<TypeElement>> result = new HashSet<ElementHandle<TypeElement>>(); |
Lines 371-377
Link Here
|
371 |
try { |
435 |
try { |
372 |
for (ClassIndexImpl query : queries) { |
436 |
for (ClassIndexImpl query : queries) { |
373 |
try { |
437 |
try { |
374 |
query.getDeclaredTypes (name, kind, thConvertor, result); |
438 |
query.getDeclaredTypes ( |
|
|
439 |
name, |
440 |
kind, |
441 |
scope, |
442 |
thConvertor, |
443 |
result); |
375 |
} catch (Index.IndexClosedException e) { |
444 |
} catch (Index.IndexClosedException e) { |
376 |
logClosedIndex (query); |
445 |
logClosedIndex (query); |
377 |
} catch (IOException e) { |
446 |
} catch (IOException e) { |
Lines 400-406
Link Here
|
400 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
469 |
* It may return null when the caller is a CancellableTask<CompilationInfo> and is cancelled |
401 |
* inside call of this method. |
470 |
* inside call of this method. |
402 |
*/ |
471 |
*/ |
403 |
public @NullUnknown Set<String> getPackageNames (final @NonNull String prefix, boolean directOnly, final @NonNull Set<SearchScope> scope) { |
472 |
public @NullUnknown Set<String> getPackageNames ( |
|
|
473 |
final @NonNull String prefix, |
474 |
boolean directOnly, |
475 |
final @NonNull Set<? extends SearchScopeType> scope) { |
404 |
assert prefix != null; |
476 |
assert prefix != null; |
405 |
final Set<String> result = new HashSet<String> (); |
477 |
final Set<String> result = new HashSet<String> (); |
406 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
478 |
final Iterable<? extends ClassIndexImpl> queries = this.getQueries (scope); |
Lines 419-427
Link Here
|
419 |
return null; |
491 |
return null; |
420 |
} |
492 |
} |
421 |
} |
493 |
} |
422 |
|
494 |
|
423 |
// Private innerclasses ---------------------------------------------------- |
495 |
/** |
424 |
|
496 |
* Creates a search scope limited to list of packages. |
|
|
497 |
* @param base the base search scope to restrict |
498 |
* @param pkgs a list of packages in which the search should be performed |
499 |
* @return a newly created search scope |
500 |
* @since 0.82.0 |
501 |
*/ |
502 |
@NonNull |
503 |
public static SearchScopeType createPackageSearchScope( |
504 |
@NonNull final SearchScopeType base, |
505 |
@NonNull final String... pkgs) { |
506 |
Parameters.notNull("base", base); //NOI18N |
507 |
Parameters.notNull("pkgs", pkgs); //NOI18N |
508 |
final Set<String> pkgSet = new HashSet<String>(Arrays.asList(pkgs)); |
509 |
final Set<? extends String> basePkgs = base.getPackages(); |
510 |
if (basePkgs != null) { |
511 |
pkgSet.addAll(basePkgs); |
512 |
} |
513 |
final Set<String> newPkgs = Collections.unmodifiableSet(pkgSet); |
514 |
return new SearchScopeType() { |
515 |
@Override |
516 |
public Set<? extends String> getPackages() { |
517 |
return newPkgs; |
518 |
} |
519 |
|
520 |
@Override |
521 |
public boolean isSources() { |
522 |
return base.isSources(); |
523 |
} |
524 |
|
525 |
@Override |
526 |
public boolean isDependencies() { |
527 |
return base.isDependencies(); |
528 |
} |
529 |
}; |
530 |
} |
531 |
|
532 |
// <editor-fold defaultstate="collapsed" desc="Private implementation"> |
425 |
private static class ClassIndexFactoryImpl implements ClassIndexFactory { |
533 |
private static class ClassIndexFactoryImpl implements ClassIndexFactory { |
426 |
|
534 |
|
427 |
public ClassIndex create(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) { |
535 |
public ClassIndex create(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) { |
Lines 465-479
Link Here
|
465 |
} |
573 |
} |
466 |
}); |
574 |
}); |
467 |
} |
575 |
} |
468 |
|
576 |
|
469 |
private Iterable<? extends ClassIndexImpl> getQueries (final Set<SearchScope> scope) { |
577 |
private Iterable<? extends ClassIndexImpl> getQueries (final Set<? extends SearchScopeType> scope) { |
470 |
final Set<ClassIndexImpl> result = new HashSet<ClassIndexImpl> (); |
578 |
final Set<ClassIndexImpl> result = new HashSet<ClassIndexImpl> (); |
471 |
synchronized (this) { |
579 |
synchronized (this) { |
472 |
if (scope.contains(SearchScope.SOURCE)) { |
580 |
for (SearchScopeType s : scope) { |
473 |
result.addAll(this.sourceIndeces); |
581 |
if (s.isSources()) { |
474 |
} |
582 |
result.addAll(this.sourceIndeces); |
475 |
if (scope.contains(SearchScope.DEPENDENCIES)) { |
583 |
} |
476 |
result.addAll(this.depsIndeces); |
584 |
if (s.isDependencies()) { |
|
|
585 |
result.addAll(this.depsIndeces); |
586 |
} |
477 |
} |
587 |
} |
478 |
} |
588 |
} |
479 |
LOGGER.log( |
589 |
LOGGER.log( |
Lines 487-494
Link Here
|
487 |
result |
597 |
result |
488 |
}); |
598 |
}); |
489 |
return result; |
599 |
return result; |
490 |
} |
600 |
} |
491 |
|
601 |
|
492 |
private void createQueriesForRoots (final ClassPath cp, final boolean sources, final Set<? super ClassIndexImpl> queries, final Set<? super URL> oldState) { |
602 |
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(); |
603 |
final PathRegistry preg = PathRegistry.getDefault(); |
494 |
List<ClassPath.Entry> entries = cp.entries(); |
604 |
List<ClassPath.Entry> entries = cp.entries(); |
Lines 820-823
Link Here
|
820 |
private static void assertParserEventThread() { |
930 |
private static void assertParserEventThread() { |
821 |
assert Utilities.isTaskProcessorThread(Thread.currentThread()); |
931 |
assert Utilities.isTaskProcessorThread(Thread.currentThread()); |
822 |
} |
932 |
} |
|
|
933 |
//</editor-fold> |
823 |
} |
934 |
} |