1 19 20 package org.netbeans.api.retouche.source; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.HashSet ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.logging.Logger ; 29 import org.netbeans.api.gsf.Index; 30 import org.netbeans.api.java.classpath.ClassPath; 31 import org.netbeans.modules.retouche.source.GlobalSourcePath; 32 import org.netbeans.modules.retouche.source.usages.ClassIndexFactory; 33 import org.netbeans.modules.retouche.source.usages.ClassIndexImpl; 34 import org.netbeans.modules.retouche.source.usages.ClassIndexManager; 35 import org.openide.util.Exceptions; 36 37 50 public final class ClassIndex extends Index { 51 52 private static final Logger LOGGER = Logger.getLogger(ClassIndex.class.getName()); 53 54 private final ClassPath bootPath; 55 private final ClassPath classPath; 56 private final ClassPath sourcePath; 57 58 private Set <ClassIndexImpl> sourceIndeces; private Set <ClassIndexImpl> depsIndeces; 60 61 63 static { 64 ClassIndexImpl.FACTORY = new ClassIndexFactoryImpl(); 65 } 66 67 ClassIndex(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) { 68 this.bootPath = bootPath; 72 this.classPath = classPath; 73 this.sourcePath = sourcePath; 74 } 75 76 public void gsfSearch(final String primaryField, final String name, final NameKind kind, 77 final Set <SearchScope> scope, 78 final Set <SearchResult> result) throws IOException { 79 assert primaryField != null; 80 assert name != null; 81 assert kind != null; 82 final Iterable <? extends ClassIndexImpl> queries = this.getQueries(scope); 83 for (ClassIndexImpl query : queries) { 85 query.gsfSearch(primaryField, name, kind, scope, result); 86 } 87 LOGGER.fine(String.format("ClassIndex.gsfSearch returned %d elements\n", result.size())); 88 } 89 90 public void gsfStore(Set <Map <String ,String >> fieldToData, Set <Map <String ,String >> noIndexFields, Map <String ,String > toDelete) throws IOException { 91 throw new RuntimeException ("Not yet implemented"); 93 } 94 95 private static class ClassIndexFactoryImpl implements ClassIndexFactory { 188 189 public ClassIndex create(final ClassPath bootPath, final ClassPath classPath, final ClassPath sourcePath) { 190 return new ClassIndex(bootPath, classPath, sourcePath); 191 } 192 193 } 194 195 197 198 private synchronized Iterable <? extends ClassIndexImpl> getQueries (final Set <SearchScope> scope) { 199 Set <ClassIndexImpl> result = new HashSet <ClassIndexImpl> (); 200 201 if (scope.contains(SearchScope.SOURCE)) { 202 if (this.sourceIndeces == null) { 203 Set <ClassIndexImpl> indeces = new HashSet <ClassIndexImpl>(); 204 createQueriesForRoots (this.sourcePath, true, indeces); 205 this.sourceIndeces = indeces; 206 } 207 result.addAll(this.sourceIndeces); 208 } 209 210 if (scope.contains(SearchScope.DEPENDENCIES)) { 211 if (this.depsIndeces == null) { 212 Set <ClassIndexImpl> indeces = new HashSet <ClassIndexImpl>(); 213 Set <URL > bootRoots = ClassIndexManager.getDefault().getBootRoots(); 225 for (URL u : bootRoots) { 226 try { 227 ClassIndexImpl ci = ClassIndexManager.getDefault().getUsagesQuery(u); 228 if (ci != null) { 229 indeces.add(ci); 230 } 231 } catch (IOException ioe) { 232 Exceptions.printStackTrace(ioe); 233 } 234 } 235 createQueriesForRoots (this.classPath, false, indeces); 237 this.depsIndeces = indeces; 238 } 239 result.addAll(this.depsIndeces); 240 } 241 LOGGER.fine(String.format("ClassIndex.queries[Scope=%s, sourcePath=%s, bootPath=%s, classPath=%s] => %s\n",scope,sourcePath,bootPath,classPath,result)); 242 return result; 243 } 244 245 246 public static 248 void createQueriesForRoots (final ClassPath cp, final boolean sources, final Set <? super ClassIndexImpl> queries) { 249 final GlobalSourcePath gsp = GlobalSourcePath.getDefault(); 250 List <ClassPath.Entry> entries = cp.entries(); 251 for (ClassPath.Entry entry : entries) { 252 try { 253 boolean indexNow = false; 254 URL [] srcRoots; 255 if (!sources) { 256 URL srcRoot = org.netbeans.modules.retouche.source.usages.Index.getSourceRootForClassFolder (entry.getURL()); 257 if (srcRoot != null) { 258 srcRoots = new URL [] {srcRoot}; 259 } 260 else { 261 srcRoots = gsp.getSourceRootForBinaryRoot (entry.getURL(), cp, true); 262 if (srcRoots == null) { 263 indexNow = true; 264 srcRoots = new URL [] {entry.getURL()}; 265 } 266 } 267 } 269 else { 270 srcRoots = new URL [] {entry.getURL()}; 271 } 272 for (URL srcRoot : srcRoots) { 273 ClassIndexImpl ci = ClassIndexManager.getDefault().getUsagesQuery(srcRoot); 274 if (ci != null) { 275 queries.add (ci); 276 } 277 } 278 } catch (IOException ioe) { 279 Exceptions.printStackTrace(ioe); 280 } 281 } 282 } 283 284 } 326 | Popular Tags |