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.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.JavaModelException;
23 import net.sourceforge.phpdt.core.compiler.IProblem;
28 * A completion requestor to collect informations on local variables.
29 * This class is used for guessing variable names like arrays, collections, etc.
31 class CompilationUnitCompletion extends CompletionRequestorAdapter {
33 static class LocalVariable {
35 String typePackageName;
38 LocalVariable(String name, String typePackageName, String typeName) {
40 this.typePackageName= typePackageName;
41 this.typeName= typeName;
45 private ICompilationUnit fUnit;
47 private List fLocalVariables= new ArrayList();
48 private Map fTypes= new HashMap();
51 private boolean fError;
54 * Creates a compilation unit completion.
56 * @param unit the compilation unit, may be <code>null</code>.
58 public CompilationUnitCompletion(ICompilationUnit unit) {
63 * Resets the completion requestor.
65 * @param unit the compilation unit, may be <code>null</code>.
67 public void reset(ICompilationUnit unit) {
70 fLocalVariables.clear();
77 * @see ICompletionRequestor#acceptError(IProblem)
79 public void acceptError(IProblem error) {
85 * @see ICodeCompletionRequestor#acceptLocalVariable
87 public void acceptLocalVariable(char[] name, char[] typePackageName, char[] typeName,
88 int modifiers, int completionStart, int completionEnd, int relevance)
90 fLocalVariables.add(new LocalVariable(
91 new String(name), new String(typePackageName), new String(typeName)));
98 * Tests if the code completion process produced errors.
100 public boolean hasErrors() {
105 boolean existsLocalName(String name) {
106 for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
107 LocalVariable localVariable = (LocalVariable) iterator.next();
109 if (localVariable.name.equals(name))
116 String[] getLocalVariableNames() {
117 String[] res= new String[fLocalVariables.size()];
119 for (Iterator iterator = fLocalVariables.iterator(); iterator.hasNext();) {
120 LocalVariable localVariable = (LocalVariable) iterator.next();
121 res[i++]= localVariable.name;
126 LocalVariable[] findLocalArrays() {
127 Vector vector= new Vector();
129 for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
130 LocalVariable localVariable= (LocalVariable) iterator.next();
132 if (isArray(localVariable.typeName))
133 vector.add(localVariable);
136 return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
139 LocalVariable[] findLocalCollections() throws JavaModelException {
140 Vector vector= new Vector();
142 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 LocalVariable[vector.size()]);
157 String simplifyTypeName(String qualifiedName) {
158 return (String) fTypes.get(qualifiedName);
161 private LocalVariable[] findLocalIntegers() {
162 Vector vector= new Vector();
164 for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
165 LocalVariable localVariable= (LocalVariable) iterator.next();
167 if (localVariable.typeName.equals("int")) //$NON-NLS-1$
168 vector.add(localVariable);
171 return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
174 private LocalVariable[] findLocalIterator() throws JavaModelException {
175 Vector vector= new Vector();
177 for (Iterator iterator= fLocalVariables.iterator(); iterator.hasNext();) {
178 LocalVariable localVariable= (LocalVariable) iterator.next();
180 String typeName= qualify(localVariable.typeName);
182 if (typeName == null)
185 if (isSubclassOf(typeName, "java.util.Iterator")) //$NON-NLS-1$
186 vector.add(localVariable);
189 return (LocalVariable[]) vector.toArray(new LocalVariable[vector.size()]);
192 private static boolean isArray(String type) {
193 return type.endsWith("[]"); //$NON-NLS-1$
196 // returns fully qualified name if successful
197 private String qualify(String typeName) throws JavaModelException {
201 // IType[] types= fUnit.getTypes();
203 // if (types.length == 0)
206 // String[][] resolvedTypeNames= types[0].resolveType(typeName);
208 // if (resolvedTypeNames == null)
211 // return resolvedTypeNames[0][0] + '.' + resolvedTypeNames[0][1];
215 // type names must be fully qualified
216 private boolean isSubclassOf(String typeName0, String typeName1) throws JavaModelException {
217 if (typeName0.equals(typeName1))
223 // IJavaProject project= fUnit.getJavaProject();
224 // IType type0= project.findType(typeName0);
225 // IType type1= project.findType(typeName1);
227 // ITypeHierarchy hierarchy= type0.newSupertypeHierarchy(null);
228 // IType[] superTypes= hierarchy.getAllSupertypes(type0);
230 // for (int i= 0; i < superTypes.length; i++)
231 // if (superTypes[i].equals(type1))
238 * @see org.eclipse.jdt.core.ICompletionRequestor#acceptClass(char[], char[], char[], int, int, int, int)
240 public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers,
241 int completionStart, int completionEnd, int relevance)
243 final String qualifiedName= createQualifiedTypeName(packageName, className);
244 fTypes.put(qualifiedName, String.valueOf(completionName));
248 * @see org.eclipse.jdt.core.ICompletionRequestor#acceptInterface(char[], char[], char[], int, int, int, int)
250 public void acceptInterface(char[] packageName, char[] interfaceName, char[] completionName,
251 int modifiers, int completionStart, int completionEnd, int relevance)
253 final String qualifiedName= createQualifiedTypeName(packageName, interfaceName);
254 fTypes.put(qualifiedName, String.valueOf(completionName));
257 private static String createQualifiedTypeName(char[] packageName, char[] className) {
258 StringBuffer buffer= new StringBuffer();
260 if (packageName.length != 0) {
261 buffer.append(packageName);
264 buffer.append(className);
266 return buffer.toString();