1 11 package org.eclipse.jdt.internal.core.search.matching; 12 13 import java.util.HashMap ; 14 import java.util.zip.ZipFile ; 15 16 import org.eclipse.core.resources.IContainer; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.jdt.core.*; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.IPackageFragmentRoot; 24 import org.eclipse.jdt.core.JavaModelException; 25 import org.eclipse.jdt.core.compiler.CharOperation; 27 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 28 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 29 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; 30 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 31 import org.eclipse.jdt.internal.core.ClasspathEntry; 32 import org.eclipse.jdt.internal.core.JavaModel; 33 import org.eclipse.jdt.internal.core.JavaModelManager; 34 import org.eclipse.jdt.internal.core.JavaProject; 35 import org.eclipse.jdt.internal.core.PackageFragmentRoot; 36 import org.eclipse.jdt.internal.core.builder.ClasspathJar; 37 import org.eclipse.jdt.internal.core.builder.ClasspathLocation; 38 import org.eclipse.jdt.internal.core.util.Util; 39 40 43 public class JavaSearchNameEnvironment implements INameEnvironment, SuffixConstants { 44 45 ClasspathLocation[] locations; 46 47 50 HashMap workingCopies; 51 52 public JavaSearchNameEnvironment(IJavaProject javaProject, org.eclipse.jdt.core.ICompilationUnit[] copies) { 53 computeClasspathLocations(javaProject.getProject().getWorkspace().getRoot(), (JavaProject) javaProject); 54 try { 55 int length = copies == null ? 0 : copies.length; 56 this.workingCopies = new HashMap (length); 57 if (copies != null) { 58 for (int i = 0; i < length; i++) { 59 org.eclipse.jdt.core.ICompilationUnit workingCopy = copies[i]; 60 IPackageDeclaration[] pkgs = workingCopy.getPackageDeclarations(); 61 String pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; String cuName = workingCopy.getElementName(); 63 String mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName); 64 String qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName; 65 this.workingCopies.put(qualifiedMainTypeName, workingCopy); 66 } 67 } 68 } catch (JavaModelException e) { 69 } 71 } 72 73 public void cleanup() { 74 for (int i = 0, length = this.locations.length; i < length; i++) { 75 this.locations[i].cleanup(); 76 } 77 } 78 79 private void computeClasspathLocations(IWorkspaceRoot workspaceRoot, JavaProject javaProject) { 80 81 IPackageFragmentRoot[] roots = null; 82 try { 83 roots = javaProject.getAllPackageFragmentRoots(); 84 } catch (JavaModelException e) { 85 this.locations = new ClasspathLocation[0]; 87 return; 88 } 89 int length = roots.length; 90 ClasspathLocation[] cpLocations = new ClasspathLocation[length]; 91 int index = 0; 92 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 93 for (int i = 0; i < length; i++) { 94 PackageFragmentRoot root = (PackageFragmentRoot) roots[i]; 95 IPath path = root.getPath(); 96 try { 97 if (root.isArchive()) { 98 ZipFile zipFile = manager.getZipFile(path); 99 cpLocations[index++] = new ClasspathJar(zipFile, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet()); 100 } else { 101 Object target = JavaModel.getTarget(workspaceRoot, path, false); 102 if (target == null) { 103 System.arraycopy(cpLocations, 0, cpLocations = new ClasspathLocation[cpLocations.length-1], 0, index); 106 } else if (root.getKind() == IPackageFragmentRoot.K_SOURCE) { 107 cpLocations[index++] = new ClasspathSourceDirectory((IContainer)target, root.fullExclusionPatternChars(), root.fullInclusionPatternChars()); 108 } else { 109 cpLocations[index++] = ClasspathLocation.forBinaryFolder((IContainer) target, false, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet()); 110 } 111 } 112 } catch (CoreException e1) { 113 System.arraycopy(cpLocations, 0, cpLocations = new ClasspathLocation[cpLocations.length-1], 0, index); 117 } 118 } 119 this.locations = cpLocations; 120 } 121 122 private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeName) { 123 String 124 binaryFileName = null, qBinaryFileName = null, 125 sourceFileName = null, qSourceFileName = null, 126 qPackageName = null; 127 NameEnvironmentAnswer suggestedAnswer = null; 128 for (int i = 0, length = this.locations.length; i < length; i++) { 129 ClasspathLocation location = this.locations[i]; 130 NameEnvironmentAnswer answer; 131 if (location instanceof ClasspathSourceDirectory) { 132 if (sourceFileName == null) { 133 qSourceFileName = qualifiedTypeName; sourceFileName = qSourceFileName; 135 qPackageName = ""; if (qualifiedTypeName.length() > typeName.length) { 137 int typeNameStart = qSourceFileName.length() - typeName.length; 138 qPackageName = qSourceFileName.substring(0, typeNameStart - 1); 139 sourceFileName = qSourceFileName.substring(typeNameStart); 140 } 141 } 142 ICompilationUnit workingCopy = (ICompilationUnit) this.workingCopies.get(qualifiedTypeName); 143 if (workingCopy != null) { 144 answer = new NameEnvironmentAnswer(workingCopy, null ); 145 } else { 146 answer = location.findClass( 147 sourceFileName, qPackageName, 149 qSourceFileName); } 151 } else { 152 if (binaryFileName == null) { 153 qBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class; 154 binaryFileName = qBinaryFileName; 155 qPackageName = ""; if (qualifiedTypeName.length() > typeName.length) { 157 int typeNameStart = qBinaryFileName.length() - typeName.length - 6; qPackageName = qBinaryFileName.substring(0, typeNameStart - 1); 159 binaryFileName = qBinaryFileName.substring(typeNameStart); 160 } 161 } 162 answer = 163 location.findClass( 164 binaryFileName, 165 qPackageName, 166 qBinaryFileName); 167 } 168 if (answer != null) { 169 if (!answer.ignoreIfBetter()) { 170 if (answer.isBetter(suggestedAnswer)) 171 return answer; 172 } else if (answer.isBetter(suggestedAnswer)) 173 suggestedAnswer = answer; 175 } 176 } 177 if (suggestedAnswer != null) 178 return suggestedAnswer; 180 return null; 181 } 182 183 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) { 184 if (typeName != null) 185 return findClass( 186 new String (CharOperation.concatWith(packageName, typeName, '/')), 187 typeName); 188 return null; 189 } 190 191 public NameEnvironmentAnswer findType(char[][] compoundName) { 192 if (compoundName != null) 193 return findClass( 194 new String (CharOperation.concatWith(compoundName, '/')), 195 compoundName[compoundName.length - 1]); 196 return null; 197 } 198 199 public boolean isPackage(char[][] compoundName, char[] packageName) { 200 return isPackage(new String (CharOperation.concatWith(compoundName, packageName, '/'))); 201 } 202 203 public boolean isPackage(String qualifiedPackageName) { 204 for (int i = 0, length = this.locations.length; i < length; i++) 205 if (this.locations[i].isPackage(qualifiedPackageName)) 206 return true; 207 return false; 208 } 209 210 } 211 | Popular Tags |