1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IJavaProject;
16 import net.sourceforge.phpdt.core.IPackageFragment;
17 import net.sourceforge.phpdt.core.IType;
18 import net.sourceforge.phpdt.core.JavaModelException;
19 import net.sourceforge.phpdt.core.WorkingCopyOwner;
20 import net.sourceforge.phpdt.core.compiler.CharOperation;
21 import net.sourceforge.phpdt.core.search.IJavaSearchConstants;
22 import net.sourceforge.phpdt.core.search.ITypeNameRequestor;
23 import net.sourceforge.phpdt.internal.codeassist.ISearchRequestor;
24 import net.sourceforge.phpdt.internal.codeassist.ISearchableNameEnvironment;
25 import net.sourceforge.phpdt.internal.compiler.env.IConstants;
26 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
27 import net.sourceforge.phpdt.internal.compiler.env.ISourceType;
28 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
30 import org.eclipse.core.runtime.IProgressMonitor;
33 * This class provides a <code>SearchableBuilderEnvironment</code> for code assist which
34 * uses the Java model as a search tool.
36 public class SearchableEnvironment implements ISearchableNameEnvironment, IJavaSearchConstants {
37 protected NameLookup nameLookup;
38 protected ICompilationUnit unitToSkip;
40 protected IJavaProject project;
41 //protected IJavaSearchScope searchScope;
46 * Creates a SearchableEnvironment on the given project
48 public SearchableEnvironment(IJavaProject project) throws JavaModelException {
49 this.project = project;
50 this.nameLookup = (NameLookup) ((JavaProject) project).getNameLookup();
52 // Create search scope with visible entry on the project's classpath
53 // this.searchScope = SearchEngine.createJavaSearchScope(this.project.getAllPackageFragmentRoots());
59 * Creates a SearchableEnvironment on the given project
61 public SearchableEnvironment(JavaProject project, WorkingCopyOwner owner) throws JavaModelException {
62 this.project = project;
63 this.nameLookup = project.newNameLookup(owner);
65 // Create search scope with visible entry on the project's classpath
66 // this.searchScope = SearchEngine.createJavaSearchScope(this.project.getAllPackageFragmentRoots());
69 * Returns the given type in the the given package if it exists,
70 * otherwise <code>null</code>.
72 protected NameEnvironmentAnswer find(String typeName, String packageName) {
73 if (packageName == null)
74 packageName = IPackageFragment.DEFAULT_PACKAGE_NAME;
76 if (this.nameLookup!=null) { // ins axelcl
77 IType type = this.nameLookup.findType(typeName, packageName, false, NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
79 // if (type instanceof BinaryType) {
81 // return new NameEnvironmentAnswer(
82 // (IBinaryType) ((BinaryType) type).getElementInfo());
83 // } catch (JavaModelException npe) {
86 // } else { //SourceType
88 // retrieve the requested type
89 SourceTypeElementInfo sourceType = (SourceTypeElementInfo) ((SourceType) type).getElementInfo();
90 ISourceType topLevelType = sourceType;
91 while (topLevelType.getEnclosingType() != null) {
92 topLevelType = topLevelType.getEnclosingType();
94 // find all siblings (other types declared in same unit, since may be used for name resolution)
95 IType[] types = sourceType.getHandle().getCompilationUnit().getTypes();
96 ISourceType[] sourceTypes = new ISourceType[types.length];
98 // in the resulting collection, ensure the requested type is the first one
99 sourceTypes[0] = sourceType;
100 for (int i = 0, index = 1; i < types.length; i++) {
101 ISourceType otherType = (ISourceType) ((JavaElement) types[i]).getElementInfo();
102 if (!otherType.equals(topLevelType))
103 sourceTypes[index++] = otherType;
105 return new NameEnvironmentAnswer(sourceTypes);
106 } catch (JavaModelException npe) {
116 * @see ISearchableNameEnvironment#findPackages(char[], ISearchRequestor)
118 public void findPackages(char[] prefix, ISearchRequestor requestor) {
119 // this.nameLookup.seekPackageFragments(
120 // new String(prefix),
122 // new SearchableEnvironmentRequestor(requestor));
126 * @see INameEnvironment#findType(char[][])
128 public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
129 if (compoundTypeName == null)
132 int length = compoundTypeName.length;
136 return find(new String(compoundTypeName[0]), null);
139 int lengthM1 = length - 1;
140 char[][] packageName = new char[lengthM1][];
141 System.arraycopy(compoundTypeName, 0, packageName, 0, lengthM1);
143 return find(new String(compoundTypeName[lengthM1]), CharOperation.toString(packageName));
147 * @see INameEnvironment#findType(char[], char[][])
149 public NameEnvironmentAnswer findType(char[] name, char[][] packageName) {
153 return find(new String(name), packageName == null || packageName.length == 0 ? null : CharOperation.toString(packageName));
157 * @see ISearchableNameEnvironment#findTypes(char[], ISearchRequestor)
159 public void findTypes(char[] prefix, final ISearchRequestor storage) {
163 findTypes(new String(prefix), storage, NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
168 final String excludePath;
169 if (this.unitToSkip != null) {
170 if (!(this.unitToSkip instanceof IJavaElement)) {
171 // revert to model investigation
172 findTypes(new String(prefix), storage, NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
175 excludePath = ((IJavaElement) this.unitToSkip).getPath().toString();
179 int lastDotIndex = CharOperation.lastIndexOf('.', prefix);
180 char[] qualification, simpleName;
181 if (lastDotIndex < 0) {
182 qualification = null;
183 simpleName = CharOperation.toLowerCase(prefix);
185 qualification = CharOperation.subarray(prefix, 0, lastDotIndex);
186 simpleName = CharOperation.toLowerCase(CharOperation.subarray(prefix, lastDotIndex + 1, prefix.length));
189 IProgressMonitor progressMonitor = new IProgressMonitor() {
190 boolean isCanceled = false;
191 public void beginTask(String name, int totalWork) {
195 public void internalWorked(double work) {
197 public boolean isCanceled() {
200 public void setCanceled(boolean value) {
203 public void setTaskName(String name) {
205 public void subTask(String name) {
207 public void worked(int work) {
210 ITypeNameRequestor nameRequestor = new ITypeNameRequestor() {
211 public void acceptClass(char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
212 if (excludePath != null && excludePath.equals(path))
214 if (enclosingTypeNames != null && enclosingTypeNames.length > 0)
215 return; // accept only top level types
216 storage.acceptClass(packageName, simpleTypeName, IConstants.AccPublic);
218 public void acceptInterface(char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path) {
219 if (excludePath != null && excludePath.equals(path))
221 if (enclosingTypeNames != null && enclosingTypeNames.length > 0)
222 return; // accept only top level types
223 storage.acceptInterface(packageName, simpleTypeName, IConstants.AccPublic);
227 // new SearchEngine().searchAllTypeNames(
228 // this.project.getProject().getWorkspace(),
233 // IJavaSearchConstants.TYPE,
236 // CANCEL_IF_NOT_READY_TO_SEARCH,
238 // } catch (OperationCanceledException e) {
240 // new String(prefix),
242 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
244 // } catch (JavaModelException e) {
246 // new String(prefix),
248 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
253 * Returns all types whose name starts with the given (qualified) <code>prefix</code>.
255 * If the <code>prefix</code> is unqualified, all types whose simple name matches
256 * the <code>prefix</code> are returned.
258 private void findTypes(String prefix, ISearchRequestor storage, int type) {
259 SearchableEnvironmentRequestor requestor = new SearchableEnvironmentRequestor(storage, this.unitToSkip);
260 int index = prefix.lastIndexOf('.');
262 this.nameLookup.seekTypes(prefix, null, true, type, requestor);
264 String packageName = prefix.substring(0, index);
265 JavaElementRequestor elementRequestor = new JavaElementRequestor();
266 this.nameLookup.seekPackageFragments(packageName, false, elementRequestor);
267 IPackageFragment[] fragments = elementRequestor.getPackageFragments();
268 if (fragments != null) {
269 String className = prefix.substring(index + 1);
270 for (int i = 0, length = fragments.length; i < length; i++)
271 if (fragments[i] != null)
272 this.nameLookup.seekTypes(className, fragments[i], true, type, requestor);
278 * @see INameEnvironment#isPackage(char[][], char[])
280 public boolean isPackage(char[][] parentPackageName, char[] subPackageName) {
281 if (subPackageName == null || CharOperation.contains('.', subPackageName))
283 if (parentPackageName == null || parentPackageName.length == 0)
284 return isTopLevelPackage(subPackageName);
285 for (int i = 0, length = parentPackageName.length; i < length; i++)
286 if (parentPackageName[i] == null || CharOperation.contains('.', parentPackageName[i]))
289 String packageName = new String(CharOperation.concatWith(parentPackageName, subPackageName, '.'));
290 return this.nameLookup.findPackageFragments(packageName, false) != null;
293 public boolean isTopLevelPackage(char[] packageName) {
294 return packageName != null
295 && !CharOperation.contains('.', packageName)
296 && this.nameLookup.findPackageFragments(new String(packageName), false) != null;
300 * Returns a printable string for the array.
302 protected String toStringChar(char[] name) {
303 return "[" //$NON-NLS-1$
304 + new String(name) + "]"; //$NON-NLS-1$
308 * Returns a printable string for the array.
310 protected String toStringCharChar(char[][] names) {
311 StringBuffer result = new StringBuffer();
312 for (int i = 0; i < names.length; i++) {
313 result.append(toStringChar(names[i]));
315 return result.toString();
318 public void cleanup() {