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.
26 * This class is used for guessing variable names like arrays, collections, etc.
28 class CompilationUnitCompletion extends CompletionRequestorAdapter {
30 static class LocalVariable {
32 String typePackageName;
35 LocalVariable(String name, String typePackageName, String typeName) {
37 this.typePackageName= typePackageName;
38 this.typeName= typeName;
42 private ICompilationUnit fUnit;
44 private List fLocalVariables= new ArrayList();
45 private Map fTypes= new HashMap();
48 private boolean fError;
51 * Creates a compilation unit completion.
53 * @param unit the compilation unit, may be <code>null</code>.
55 public CompilationUnitCompletion(ICompilationUnit unit) {
60 * Resets the completion requestor.
62 * @param unit the compilation unit, may be <code>null</code>.
64 public void reset(ICompilationUnit unit) {
67 fLocalVariables.clear();
74 * @see ICompletionRequestor#acceptError(IProblem)
76 public void acceptError(IProblem error) {
82 * @see ICodeCompletionRequestor#acceptLocalVariable
84 public void acceptLocalVariable(char[] name, char[] typePackageName, char[] typeName,
85 int modifiers, int completionStart, int completionEnd, int relevance)
87 fLocalVariables.add(new LocalVariable(
88 new String(name), new String(typePackageName), new String(typeName)));
95 * Tests if the code completion process produced errors.
97 public boolean hasErrors() {
102 boolean existsLocalName(String name) {
103 for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
104 LocalVariable localVariable = (LocalVariable) iterator.next();
106 if (localVariable.name.equals(name))
113 String[] getLocalVariableNames() {
114 String[] res= new String[fLocalVariables.size()];
116 for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
117 LocalVariable localVariable = (LocalVariable) iterator.next();
118 res[i++]= localVariable.name;
123 LocalVariable[] findLocalArrays() {
124 Vector vector= new Vector();
126 for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
127 LocalVariable localVariable= (LocalVariable) iterator.next();
129 if (isArray(localVariable.typeName))
130 vector.add(localVariable);
133 return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
136 // LocalVariable[] findLocalCollections() throws JavaModelException {
137 // Vector vector= new Vector();
139 // for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
140 // LocalVariable localVariable= (LocalVariable) iterator.next();
142 // String typeName= qualify(localVariable.typeName);
144 // if (typeName == null)
147 // if (isSubclassOf(typeName, "java.util.Collection")) //$NON-NLS-1$
148 // vector.add(localVariable);
151 // return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
154 String simplifyTypeName(String qualifiedName) {
155 return (String) fTypes.get(qualifiedName);
158 private static boolean isArray(String type) {
159 return type.endsWith("[]"); //$NON-NLS-1$
162 // returns fully qualified name if successful
163 // private String qualify(String typeName) throws JavaModelException {
164 // if (fUnit == null)
167 // IType[] types= fUnit.getTypes();
169 // if (types.length == 0)
172 // String[][] resolvedTypeNames= types[0].resolveType(typeName);
174 // if (resolvedTypeNames == null)
177 // return resolvedTypeNames[0][0] + '.' + resolvedTypeNames[0][1];
180 // type names must be fully qualified
181 // private boolean isSubclassOf(String typeName0, String typeName1) throws JavaModelException {
182 // if (typeName0.equals(typeName1))
185 // if (fUnit == null)
188 // IJavaProject project= fUnit.getJavaProject();
190 // IType type0= project.findType(typeName0);
191 // if (type0 == null)
194 // IType type1= project.findType(typeName1);
195 // if (type1 == null)
198 // ITypeHierarchy hierarchy= type0.newSupertypeHierarchy(null);
199 // IType[] superTypes= hierarchy.getAllSupertypes(type0);
201 // for (int i= 0; i < superTypes.length; i++)
202 // if (superTypes[i].equals(type1))
209 * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptClass(char[], char[], char[], int, int, int, int)
211 public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers,
212 int completionStart, int completionEnd, int relevance)
214 final String qualifiedName= createQualifiedTypeName(packageName, className);
215 fTypes.put(qualifiedName, String.valueOf(completionName));
219 * @see net.sourceforge.phpdt.core.ICompletionRequestor#acceptInterface(char[], char[], char[], int, int, int, int)
221 public void acceptInterface(char[] packageName, char[] interfaceName, char[] completionName,
222 int modifiers, int completionStart, int completionEnd, int relevance)
224 final String qualifiedName= createQualifiedTypeName(packageName, interfaceName);
225 fTypes.put(qualifiedName, String.valueOf(completionName));
228 private static String createQualifiedTypeName(char[] packageName, char[] className) {
229 StringBuffer buffer= new StringBuffer();
231 if (packageName.length != 0) {
232 buffer.append(packageName);
235 buffer.append(className);
237 return buffer.toString();