KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > JavaSearchNameEnvironment


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search.matching;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.zip.ZipFile JavaDoc;
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.*;
21
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.JavaCore;
26
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 /*
41  * A name environment based on the classpath of a Java project.
42  */

43 public class JavaSearchNameEnvironment implements INameEnvironment, SuffixConstants {
44     
45     ClasspathLocation[] locations;
46     
47     /*
48      * A map from the fully qualified slash-separated name of the main type (String) to the working copy
49      */

50     HashMap JavaDoc 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 JavaDoc(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 JavaDoc pkg = pkgs.length > 0 ? pkgs[0].getElementName() : ""; //$NON-NLS-1$
62
String JavaDoc cuName = workingCopy.getElementName();
63                 String JavaDoc mainTypeName = Util.getNameWithoutJavaLikeExtension(cuName);
64                 String JavaDoc qualifiedMainTypeName = pkg.length() == 0 ? mainTypeName : pkg.replace('.', '/') + '/' + mainTypeName;
65                 this.workingCopies.put(qualifiedMainTypeName, workingCopy);
66             }
67         }
68     } catch (JavaModelException e) {
69         // working copy doesn't exist: cannot happen
70
}
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         // project doesn't exist
86
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 JavaDoc zipFile = manager.getZipFile(path);
99                 cpLocations[index++] = new ClasspathJar(zipFile, ((ClasspathEntry) root.getRawClasspathEntry()).getAccessRuleSet());
100             } else {
101                 Object JavaDoc target = JavaModel.getTarget(workspaceRoot, path, false);
102                 if (target == null) {
103                     // target doesn't exist any longer
104
// just resize cpLocations
105
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             // problem opening zip file or getting root kind
114
// consider root corrupt and ignore
115
// just resize cpLocations
116
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 JavaDoc qualifiedTypeName, char[] typeName) {
123     String JavaDoc
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; // doesn't include the file extension
134
sourceFileName = qSourceFileName;
135                 qPackageName = ""; //$NON-NLS-1$
136
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 /*no access restriction*/);
145             } else {
146                 answer = location.findClass(
147                     sourceFileName, // doesn't include the file extension
148
qPackageName,
149                     qSourceFileName); // doesn't include the file extension
150
}
151         } else {
152             if (binaryFileName == null) {
153                 qBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class;
154                 binaryFileName = qBinaryFileName;
155                 qPackageName = ""; //$NON-NLS-1$
156
if (qualifiedTypeName.length() > typeName.length) {
157                     int typeNameStart = qBinaryFileName.length() - typeName.length - 6; // size of ".class"
158
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                 // remember suggestion and keep looking
174
suggestedAnswer = answer;
175         }
176     }
177     if (suggestedAnswer != null)
178         // no better answer was found
179
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 JavaDoc(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 JavaDoc(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 JavaDoc(CharOperation.concatWith(compoundName, packageName, '/')));
201 }
202
203 public boolean isPackage(String JavaDoc 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