1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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.corext.template.php;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 //import java.util.Iterator;
16 import java.util.List;
18 //import java.util.Vector;
20 import net.sourceforge.phpdt.core.CompletionRequestorAdapter;
21 import net.sourceforge.phpdt.core.ICompilationUnit;
22 import net.sourceforge.phpdt.core.compiler.IProblem;
25 * A completion requestor to collect informations on local variables. This class
26 * is used for guessing variable names like arrays, collections, etc.
28 class CompilationUnitCompletion extends CompletionRequestorAdapter {
30 static class LocalVariable {
33 String typePackageName;
37 LocalVariable(String name, String typePackageName, String typeName) {
39 this.typePackageName = typePackageName;
40 this.typeName = typeName;
44 //private ICompilationUnit fUnit;
46 private List fLocalVariables = new ArrayList();
48 private Map fTypes = new HashMap();
50 //private boolean fError;
53 * Creates a compilation unit completion.
56 * the compilation unit, may be <code>null</code>.
58 public CompilationUnitCompletion(ICompilationUnit unit) {
63 * Resets the completion requestor.
66 * the compilation unit, may be <code>null</code>.
68 public void reset(ICompilationUnit unit) {
71 fLocalVariables.clear();
78 * @see ICompletionRequestor#acceptError(IProblem)
80 public void acceptError(IProblem error) {
85 * @see ICodeCompletionRequestor#acceptLocalVariable
87 public void acceptLocalVariable(char[] name, char[] typePackageName,
88 char[] typeName, int modifiers, int completionStart,
89 int completionEnd, int relevance) {
90 fLocalVariables.add(new LocalVariable(new String(name), new String(
91 typePackageName), new String(typeName)));
97 * Tests if the code completion process produced errors.
99 // public boolean hasErrors() {
103 // boolean existsLocalName(String name) {
104 // for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
105 // LocalVariable localVariable = (LocalVariable) iterator.next();
107 // if (localVariable.name.equals(name))
114 // String[] getLocalVariableNames() {
115 // String[] res = new String[fLocalVariables.size()];
117 // for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
118 // LocalVariable localVariable = (LocalVariable) iterator.next();
119 // res[i++] = localVariable.name;
124 // LocalVariable[] findLocalArrays() {
125 // Vector vector = new Vector();
127 // for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
128 // LocalVariable localVariable = (LocalVariable) iterator.next();
130 // if (isArray(localVariable.typeName))
131 // vector.add(localVariable);
134 // return (LocalVariable[]) vector
135 // .toArray(new LocalVariable[vector.size()]);
138 // LocalVariable[] findLocalCollections() throws JavaModelException {
139 // Vector vector= new Vector();
141 // for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();)
143 // LocalVariable localVariable= (LocalVariable) iterator.next();
145 // String typeName= qualify(localVariable.typeName);
147 // if (typeName == null)
150 // if (isSubclassOf(typeName, "java.util.Collection")) //$NON-NLS-1$
151 // vector.add(localVariable);
154 // return (LocalVariable[]) vector.toArray(new
155 // LocalVariable[vector.size()]);
158 // String simplifyTypeName(String qualifiedName) {
159 // return (String) fTypes.get(qualifiedName);
162 // private static boolean isArray(String type) {
163 // return type.endsWith("[]"); //$NON-NLS-1$
166 // returns fully qualified name if successful
167 // private String qualify(String typeName) throws JavaModelException {
168 // if (fUnit == null)
171 // IType[] types= fUnit.getTypes();
173 // if (types.length == 0)
176 // String[][] resolvedTypeNames= types[0].resolveType(typeName);
178 // if (resolvedTypeNames == null)
181 // return resolvedTypeNames[0][0] + '.' + resolvedTypeNames[0][1];
184 // type names must be fully qualified
185 // private boolean isSubclassOf(String typeName0, String typeName1) throws
186 // JavaModelException {
187 // if (typeName0.equals(typeName1))
190 // if (fUnit == null)
193 // IJavaProject project= fUnit.getJavaProject();
195 // IType type0= project.findType(typeName0);
196 // if (type0 == null)
199 // IType type1= project.findType(typeName1);
200 // if (type1 == null)
203 // ITypeHierarchy hierarchy= type0.newSupertypeHierarchy(null);
204 // IType[] superTypes= hierarchy.getAllSupertypes(type0);
206 // for (int i= 0; i < superTypes.length; i++)
207 // if (superTypes[i].equals(type1))
214 * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptClass(char[],
215 * char[], char[], int, int, int, int)
217 public void acceptClass(char[] packageName, char[] className,
218 char[] completionName, int modifiers, int completionStart,
219 int completionEnd, int relevance) {
220 final String qualifiedName = createQualifiedTypeName(packageName,
222 fTypes.put(qualifiedName, String.valueOf(completionName));
226 * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptInterface(char[],
227 * char[], char[], int, int, int, int)
229 public void acceptInterface(char[] packageName, char[] interfaceName,
230 char[] completionName, int modifiers, int completionStart,
231 int completionEnd, int relevance) {
232 final String qualifiedName = createQualifiedTypeName(packageName,
234 fTypes.put(qualifiedName, String.valueOf(completionName));
237 private static String createQualifiedTypeName(char[] packageName,
239 StringBuffer buffer = new StringBuffer();
241 if (packageName.length != 0) {
242 buffer.append(packageName);
245 buffer.append(className);
247 return buffer.toString();