1 11 package org.eclipse.jdt.internal.core.search.indexing; 12 13 import java.io.IOException ; 14 import java.net.URI ; 15 import java.util.HashSet ; 16 17 import org.eclipse.core.filesystem.EFS; 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.resources.IResourceProxy; 22 import org.eclipse.core.resources.IResourceProxyVisitor; 23 import org.eclipse.core.resources.IWorkspaceRoot; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.jdt.core.IClasspathEntry; 28 import org.eclipse.jdt.core.JavaCore; 29 import org.eclipse.jdt.internal.compiler.SourceElementParser; 30 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable; 31 import org.eclipse.jdt.internal.core.ClasspathEntry; 32 import org.eclipse.jdt.internal.core.JavaProject; 33 import org.eclipse.jdt.internal.core.index.Index; 34 import org.eclipse.jdt.internal.core.search.processing.JobManager; 35 import org.eclipse.jdt.internal.core.util.Util; 36 37 public class IndexAllProject extends IndexRequest { 38 IProject project; 39 40 public IndexAllProject(IProject project, IndexManager manager) { 41 super(project.getFullPath(), manager); 42 this.project = project; 43 } 44 public boolean equals(Object o) { 45 if (o instanceof IndexAllProject) 46 return this.project.equals(((IndexAllProject) o).project); 47 return false; 48 } 49 54 public boolean execute(IProgressMonitor progressMonitor) { 55 56 if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) return true; 57 if (!project.isAccessible()) return true; 59 ReadWriteMonitor monitor = null; 60 try { 61 JavaProject javaProject = (JavaProject)JavaCore.create(this.project); 63 IClasspathEntry[] entries = javaProject.getRawClasspath(); 65 int length = entries.length; 66 IClasspathEntry[] sourceEntries = new IClasspathEntry[length]; 67 int sourceEntriesNumber = 0; 68 for (int i = 0; i < length; i++) { 69 IClasspathEntry entry = entries[i]; 70 if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) 71 sourceEntries[sourceEntriesNumber++] = entry; 72 } 73 if (sourceEntriesNumber == 0) { 74 IPath projectPath = javaProject.getPath(); 75 for (int i = 0; i < length; i++) { 76 IClasspathEntry entry = entries[i]; 77 if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(projectPath)) { 78 this.manager.indexLibrary(projectPath, this.project); 81 return true; 82 } 83 } 84 85 Index index = this.manager.getIndexForUpdate(this.containerPath, true, true ); 87 if (index != null) 88 this.manager.saveIndex(index); 89 return true; 90 } 91 if (sourceEntriesNumber != length) 92 System.arraycopy(sourceEntries, 0, sourceEntries = new IClasspathEntry[sourceEntriesNumber], 0, sourceEntriesNumber); 93 94 Index index = this.manager.getIndexForUpdate(this.containerPath, true, true ); 95 if (index == null) return true; 96 monitor = index.monitor; 97 if (monitor == null) return true; 99 monitor.enterRead(); 101 String [] paths = index.queryDocumentNames(""); int max = paths == null ? 0 : paths.length; 103 final SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11); 104 final String OK = "OK"; final String DELETED = "DELETED"; if (paths != null) { 107 for (int i = 0; i < max; i++) 108 indexedFileNames.put(paths[i], DELETED); 109 } 110 final long indexLastModified = max == 0 ? 0L : index.getIndexFile().lastModified(); 111 112 IWorkspaceRoot root = this.project.getWorkspace().getRoot(); 113 for (int i = 0; i < sourceEntriesNumber; i++) { 114 if (this.isCancelled) return false; 115 116 IClasspathEntry entry = sourceEntries[i]; 117 IResource sourceFolder = root.findMember(entry.getPath()); 118 if (sourceFolder != null) { 119 120 final HashSet outputs = new HashSet (); 122 if (sourceFolder.getType() == IResource.PROJECT) { 123 outputs.add(javaProject.getOutputLocation()); 125 for (int j = 0; j < sourceEntriesNumber; j++) { 126 IPath output = sourceEntries[j].getOutputLocation(); 127 if (output != null) { 128 outputs.add(output); 129 } 130 } 131 } 132 final boolean hasOutputs = !outputs.isEmpty(); 133 134 final char[][] inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars(); 135 final char[][] exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars(); 136 if (max == 0) { 137 sourceFolder.accept( 138 new IResourceProxyVisitor() { 139 public boolean visit(IResourceProxy proxy) { 140 if (isCancelled) return false; 141 switch(proxy.getType()) { 142 case IResource.FILE : 143 if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) { 144 IFile file = (IFile) proxy.requestResource(); 145 if (exclusionPatterns != null || inclusionPatterns != null) 146 if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns)) 147 return false; 148 indexedFileNames.put(Util.relativePath(file.getFullPath(), 1), file); 149 } 150 return false; 151 case IResource.FOLDER : 152 if (exclusionPatterns != null && inclusionPatterns == null) { 153 if (Util.isExcluded(proxy.requestFullPath(), inclusionPatterns, exclusionPatterns, true)) 155 return false; 156 } 157 if (hasOutputs && outputs.contains(proxy.requestFullPath())) 158 return false; 159 } 160 return true; 161 } 162 }, 163 IResource.NONE 164 ); 165 } else { 166 sourceFolder.accept( 167 new IResourceProxyVisitor() { 168 public boolean visit(IResourceProxy proxy) throws CoreException { 169 if (isCancelled) return false; 170 switch(proxy.getType()) { 171 case IResource.FILE : 172 if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) { 173 IFile file = (IFile) proxy.requestResource(); 174 URI location = file.getLocationURI(); 175 if (location == null) return false; 176 if (exclusionPatterns != null || inclusionPatterns != null) 177 if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns)) 178 return false; 179 String relativePathString = Util.relativePath(file.getFullPath(), 1); 180 indexedFileNames.put(relativePathString, 181 indexedFileNames.get(relativePathString) == null 182 || indexLastModified < EFS.getStore(location).fetchInfo().getLastModified() 183 ? (Object ) file 184 : (Object ) OK); 185 } 186 return false; 187 case IResource.FOLDER : 188 if (exclusionPatterns != null || inclusionPatterns != null) 189 if (Util.isExcluded(proxy.requestResource(), inclusionPatterns, exclusionPatterns)) 190 return false; 191 if (hasOutputs && outputs.contains(proxy.requestFullPath())) 192 return false; 193 } 194 return true; 195 } 196 }, 197 IResource.NONE 198 ); 199 } 200 } 201 } 202 203 SourceElementParser parser = this.manager.getSourceElementParser(javaProject, null); 204 Object [] names = indexedFileNames.keyTable; 205 Object [] values = indexedFileNames.valueTable; 206 for (int i = 0, namesLength = names.length; i < namesLength; i++) { 207 String name = (String ) names[i]; 208 if (name != null) { 209 if (this.isCancelled) return false; 210 211 Object value = values[i]; 212 if (value != OK) { 213 if (value == DELETED) 214 this.manager.remove(name, this.containerPath); 215 else 216 this.manager.addSource((IFile) value, this.containerPath, parser); 217 } 218 } 219 } 220 221 this.manager.request(new SaveIndex(this.containerPath, this.manager)); 223 } catch (CoreException e) { 224 if (JobManager.VERBOSE) { 225 Util.verbose("-> failed to index " + this.project + " because of the following exception:", System.err); e.printStackTrace(); 227 } 228 this.manager.removeIndex(this.containerPath); 229 return false; 230 } catch (IOException e) { 231 if (JobManager.VERBOSE) { 232 Util.verbose("-> failed to index " + this.project + " because of the following exception:", System.err); e.printStackTrace(); 234 } 235 this.manager.removeIndex(this.containerPath); 236 return false; 237 } finally { 238 if (monitor != null) 239 monitor.exitRead(); } 241 return true; 242 } 243 public int hashCode() { 244 return this.project.hashCode(); 245 } 246 protected Integer updatedIndexState() { 247 return IndexManager.REBUILDING_STATE; 248 } 249 public String toString() { 250 return "indexing project " + this.project.getFullPath(); } 252 } 253 | Popular Tags |