1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.jdt.core.*; 21 import org.eclipse.jdt.internal.core.util.HashSetOfArray; 22 import org.eclipse.jdt.internal.core.util.Util; 23 import org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject; 24 25 34 35 36 class JavaProjectElementInfo extends OpenableElementInfo { 37 38 static final IPackageFragmentRoot[] NO_ROOTS = new IPackageFragmentRoot[0]; 39 40 static class ProjectCache { 41 ProjectCache(IPackageFragmentRoot[] allPkgFragmentRootsCache, Map rootToResolvedEntries, Map pkgFragmentsCaches) { 42 this.allPkgFragmentRootsCache = allPkgFragmentRootsCache; 43 this.rootToResolvedEntries = rootToResolvedEntries; 44 this.pkgFragmentsCaches = pkgFragmentsCaches; 45 } 46 47 50 public IPackageFragmentRoot[] allPkgFragmentRootsCache; 51 52 56 public HashtableOfArrayToObject allPkgFragmentsCache; 57 58 62 public Map pkgFragmentsCaches; 63 64 public Map rootToResolvedEntries; 65 } 66 67 70 private Object [] nonJavaResources; 71 72 ProjectCache projectCache; 73 74 78 static void addSuperPackageNames(String [] pkgName, HashtableOfArrayToObject packageFragments) { 79 for (int i = pkgName.length-1; i > 0; i--) { 80 if (packageFragments.getKey(pkgName, i) == null) { 81 System.arraycopy(pkgName, 0, pkgName = new String [i], 0, i); 82 packageFragments.put(pkgName, NO_ROOTS); 83 } 84 } 85 } 86 87 90 public JavaProjectElementInfo() { 91 this.nonJavaResources = null; 92 } 93 94 97 private Object [] computeNonJavaResources(JavaProject project) { 98 99 IPath projectPath = project.getProject().getFullPath(); 101 boolean srcIsProject = false; 102 boolean binIsProject = false; 103 char[][] inclusionPatterns = null; 104 char[][] exclusionPatterns = null; 105 IPath projectOutput = null; 106 boolean isClasspathResolved = true; 107 try { 108 IClasspathEntry entry = project.getClasspathEntryFor(projectPath); 109 if (entry != null) { 110 srcIsProject = true; 111 inclusionPatterns = ((ClasspathEntry)entry).fullInclusionPatternChars(); 112 exclusionPatterns = ((ClasspathEntry)entry).fullExclusionPatternChars(); 113 } 114 projectOutput = project.getOutputLocation(); 115 binIsProject = projectPath.equals(projectOutput); 116 } catch (JavaModelException e) { 117 isClasspathResolved = false; 118 } 119 120 Object [] resources = new IResource[5]; 121 int resourcesCounter = 0; 122 try { 123 IResource[] members = ((IContainer) project.getResource()).members(); 124 int length = members.length; 125 if (length > 0) { 126 String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); 127 String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); 128 IClasspathEntry[] classpath = project.getResolvedClasspath(); 129 for (int i = 0; i < length; i++) { 130 IResource res = members[i]; 131 switch (res.getType()) { 132 case IResource.FILE : 133 IPath resFullPath = res.getFullPath(); 134 String resName = res.getName(); 135 136 if (isClasspathResolved && org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(resName) && this.isClasspathEntryOrOutputLocation(resFullPath, classpath, projectOutput)) { 138 break; 139 } 140 if (srcIsProject 142 && Util.isValidCompilationUnitName(resName, sourceLevel, complianceLevel) 143 && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns)) { 144 break; 145 } 146 if (binIsProject && Util.isValidClassFileName(resName, sourceLevel, complianceLevel)) { 148 break; 149 } 150 if (resources.length == resourcesCounter) { 152 System.arraycopy( 154 resources, 155 0, 156 (resources = new IResource[resourcesCounter * 2]), 157 0, 158 resourcesCounter); 159 } 160 resources[resourcesCounter++] = res; 161 break; 162 case IResource.FOLDER : 163 resFullPath = res.getFullPath(); 164 165 if ((srcIsProject && !Util.isExcluded(res, inclusionPatterns, exclusionPatterns) && Util.isValidFolderNameForPackage(res.getName(), sourceLevel, complianceLevel)) 167 || (isClasspathResolved && this.isClasspathEntryOrOutputLocation(resFullPath, classpath, projectOutput))) { 168 break; 169 } 170 if (resources.length == resourcesCounter) { 172 System.arraycopy( 174 resources, 175 0, 176 (resources = new IResource[resourcesCounter * 2]), 177 0, 178 resourcesCounter); 179 } 180 resources[resourcesCounter++] = res; 181 } 182 } 183 } 184 if (resources.length != resourcesCounter) { 185 System.arraycopy( 186 resources, 187 0, 188 (resources = new IResource[resourcesCounter]), 189 0, 190 resourcesCounter); 191 } 192 } catch (CoreException e) { 193 resources = NO_NON_JAVA_RESOURCES; 194 resourcesCounter = 0; 195 } 196 return resources; 197 } 198 199 ProjectCache getProjectCache(JavaProject project) { 200 ProjectCache cache = this.projectCache; 201 if (cache == null) { 202 IPackageFragmentRoot[] roots; 203 Map reverseMap = new HashMap (3); 204 try { 205 roots = project.getAllPackageFragmentRoots(reverseMap); 206 } catch (JavaModelException e) { 207 roots = new IPackageFragmentRoot[0]; 209 reverseMap.clear(); 210 } 211 212 HashMap rootInfos = JavaModelManager.getJavaModelManager().deltaState.roots; 213 HashMap pkgFragmentsCaches = new HashMap (); 214 int length = roots.length; 215 for (int i = 0; i < length; i++) { 216 IPackageFragmentRoot root = roots[i]; 217 DeltaProcessor.RootInfo rootInfo = (DeltaProcessor.RootInfo) rootInfos.get(root.getPath()); 218 if (rootInfo == null || rootInfo.project.equals(project)) { 219 HashSetOfArray fragmentsCache = new HashSetOfArray(); 221 initializePackageNames(root, fragmentsCache); 222 pkgFragmentsCaches.put(root, fragmentsCache); 223 } 224 } 225 226 cache = new ProjectCache(roots, reverseMap, pkgFragmentsCaches); 227 this.projectCache = cache; 228 } 229 return cache; 230 } 231 232 235 Object [] getNonJavaResources(JavaProject project) { 236 237 if (this.nonJavaResources == null) { 238 this.nonJavaResources = computeNonJavaResources(project); 239 } 240 return this.nonJavaResources; 241 } 242 243 private void initializePackageNames(IPackageFragmentRoot root, HashSetOfArray fragmentsCache) { 244 IJavaElement[] frags = null; 245 try { 246 if (!root.isOpen()) { 247 PackageFragmentRootInfo info = root.isArchive() ? new JarPackageFragmentRootInfo() : new PackageFragmentRootInfo(); 248 ((PackageFragmentRoot) root).computeChildren(info, new HashMap ()); 249 frags = info.children; 250 } else 251 frags = root.getChildren(); 252 } catch (JavaModelException e) { 253 return; 255 } 256 for (int j = 0, length2 = frags.length; j < length2; j++) { 257 fragmentsCache.add(((PackageFragment) frags[j]).names); 258 } 259 } 260 261 264 private boolean isClasspathEntryOrOutputLocation(IPath path, IClasspathEntry[] resolvedClasspath, IPath projectOutput) { 265 if (projectOutput.equals(path)) return true; 266 for (int i = 0, length = resolvedClasspath.length; i < length; i++) { 267 IClasspathEntry entry = resolvedClasspath[i]; 268 if (entry.getPath().equals(path)) { 269 return true; 270 } 271 IPath output; 272 if ((output = entry.getOutputLocation()) != null && output.equals(path)) { 273 return true; 274 } 275 } 276 return false; 277 } 278 279 284 NameLookup newNameLookup(JavaProject project, ICompilationUnit[] workingCopies) { 285 ProjectCache cache = getProjectCache(project); 286 HashtableOfArrayToObject allPkgFragmentsCache = cache.allPkgFragmentsCache; 287 if (allPkgFragmentsCache == null) { 288 HashMap rootInfos = JavaModelManager.getJavaModelManager().deltaState.roots; 289 IPackageFragmentRoot[] allRoots = cache.allPkgFragmentRootsCache; 290 int length = allRoots.length; 291 allPkgFragmentsCache = new HashtableOfArrayToObject(); 292 for (int i = 0; i < length; i++) { 293 IPackageFragmentRoot root = allRoots[i]; 294 DeltaProcessor.RootInfo rootInfo = (DeltaProcessor.RootInfo) rootInfos.get(root.getPath()); 295 JavaProject rootProject = rootInfo == null ? project : rootInfo.project; 296 HashSetOfArray fragmentsCache; 297 if (rootProject.equals(project)) { 298 fragmentsCache = (HashSetOfArray) cache.pkgFragmentsCaches.get(root); 300 } else { 301 ProjectCache rootProjectCache; 303 try { 304 rootProjectCache = rootProject.getProjectCache(); 305 } catch (JavaModelException e) { 306 continue; 308 } 309 fragmentsCache = (HashSetOfArray) rootProjectCache.pkgFragmentsCaches.get(root); 310 } 311 if (fragmentsCache == null) { fragmentsCache = new HashSetOfArray(); 313 initializePackageNames(root, fragmentsCache); 314 } 315 Object [][] set = fragmentsCache.set; 316 for (int j = 0, length2 = set.length; j < length2; j++) { 317 String [] pkgName = (String []) set[j]; 318 if (pkgName == null) 319 continue; 320 Object existing = allPkgFragmentsCache.get(pkgName); 321 if (existing == null || existing == NO_ROOTS) { 322 allPkgFragmentsCache.put(pkgName, root); 323 addSuperPackageNames(pkgName, allPkgFragmentsCache); 326 } else { 327 if (existing instanceof PackageFragmentRoot) { 328 allPkgFragmentsCache.put(pkgName, new IPackageFragmentRoot[] {(PackageFragmentRoot) existing, root}); 329 } else { 330 IPackageFragmentRoot[] roots = (IPackageFragmentRoot[]) existing; 331 int rootLength = roots.length; 332 System.arraycopy(roots, 0, roots = new IPackageFragmentRoot[rootLength+1], 0, rootLength); 333 roots[rootLength] = root; 334 allPkgFragmentsCache.put(pkgName, roots); 335 } 336 } 337 } 338 } 339 cache.allPkgFragmentsCache = allPkgFragmentsCache; 340 } 341 return new NameLookup(cache.allPkgFragmentRootsCache, cache.allPkgFragmentsCache, workingCopies, cache.rootToResolvedEntries); 342 } 343 344 347 void resetCaches() { 348 this.projectCache = null; 349 } 350 351 354 void setNonJavaResources(Object [] resources) { 355 356 this.nonJavaResources = resources; 357 } 358 359 } 360 | Popular Tags |