1d11344f0b74e3bab683fbd29239da17ccb471cb
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / BindingComparator.java
1 /*******************************************************************************
2  * Copyright (c) 2004, 2008 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 net.sourceforge.phpdt.core.dom;
12
13 import java.util.HashSet;
14
15 import net.sourceforge.phpdt.core.compiler.CharOperation;
16 import net.sourceforge.phpdt.internal.compiler.classfmt.ClassFileConstants;
17 import net.sourceforge.phpdt.internal.compiler.lookup.Binding;
18 import net.sourceforge.phpdt.internal.compiler.lookup.CaptureBinding;
19 import net.sourceforge.phpdt.internal.compiler.lookup.ExtraCompilerModifiers;
20 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.ImportBinding;
22 import net.sourceforge.phpdt.internal.compiler.lookup.ParameterizedTypeBinding;
23 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
24 import net.sourceforge.phpdt.internal.compiler.lookup.TypeVariableBinding;
25 import net.sourceforge.phpdt.internal.compiler.lookup.VariableBinding;
26 import net.sourceforge.phpdt.internal.compiler.lookup.WildcardBinding;
27
28 /**
29  * Internal helper class for comparing bindings.
30  * 
31  * @since 3.1
32  */
33 class BindingComparator {
34         /**
35          * @param bindings
36          * @param otherBindings
37          * @return true if both parameters are equals, false otherwise
38          */
39         static boolean isEqual(TypeVariableBinding[] bindings, TypeVariableBinding[] otherBindings) {
40                 if (bindings == null) {
41                         return otherBindings == null;
42                 }
43                 if (otherBindings == null) {
44                         return false;
45                 }
46                 int length = bindings.length;
47                 int otherLength = otherBindings.length;
48                 if (length != otherLength) {
49                         return false;
50                 }
51                 for (int i = 0; i < length; i++) {
52                         TypeVariableBinding typeVariableBinding = bindings[i];
53                         TypeVariableBinding typeVariableBinding2 = otherBindings[i];
54                         if (!isEqual(typeVariableBinding, typeVariableBinding2)) {
55                                 return false;
56                         }
57                 }
58                 return true;
59         }
60         
61         /**
62          * @param declaringElement
63          * @param declaringElement2
64          * @return true if both parameters are equals, false otherwise
65          */
66         static boolean isEqual(Binding declaringElement, Binding declaringElement2, HashSet visitedTypes) {
67                 if (declaringElement instanceof net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) {
68                         if (!(declaringElement2 instanceof net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding)){
69                                 return false;
70                         }
71                         return isEqual((net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) declaringElement,
72                                         (net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) declaringElement2,
73                                         visitedTypes);
74                 } else if (declaringElement instanceof net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding) {
75                         if (!(declaringElement2 instanceof net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding)) {
76                                 return false;
77                         }
78                         return isEqual((net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding) declaringElement,
79                                         (net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding) declaringElement2,
80                                         visitedTypes);
81                 } else if (declaringElement instanceof VariableBinding) {
82                         if (!(declaringElement2 instanceof VariableBinding)) {
83                                 return false;
84                         }
85                         return isEqual((VariableBinding) declaringElement,
86                                         (VariableBinding) declaringElement2);
87                 } else if (declaringElement instanceof net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding) {
88                         if (!(declaringElement2 instanceof net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding)) {
89                                 return false;
90                         }
91                         net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding packageBinding = (net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding) declaringElement;
92                         net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding packageBinding2 = (net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding) declaringElement2;
93                         return CharOperation.equals(packageBinding.compoundName, packageBinding2.compoundName);
94                 } else if (declaringElement instanceof ImportBinding) {
95                         if (!(declaringElement2 instanceof ImportBinding)) {
96                                 return false;
97                         }
98                         ImportBinding importBinding = (ImportBinding) declaringElement;
99                         ImportBinding importBinding2 = (ImportBinding) declaringElement2;
100                         return importBinding.isStatic() == importBinding2.isStatic()
101                                 && importBinding.onDemand == importBinding2.onDemand
102                                 && CharOperation.equals(importBinding.compoundName, importBinding2.compoundName);
103                 }
104                 return false;
105         }
106         
107         static boolean isEqual(net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding methodBinding,
108                         net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding methodBinding2) {
109                 return isEqual(methodBinding, methodBinding2, new HashSet());
110         }
111                         
112         static boolean isEqual(net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding methodBinding,
113                         net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding methodBinding2,
114                         HashSet visitedTypes) {
115                 if (methodBinding == null) {
116                         return methodBinding2 == null;
117                 }
118                 if (methodBinding2 == null) return false;
119                 return CharOperation.equals(methodBinding.selector, methodBinding2.selector)
120                                 && isEqual(methodBinding.returnType, methodBinding2.returnType, visitedTypes) 
121                                 && isEqual(methodBinding.thrownExceptions, methodBinding2.thrownExceptions, visitedTypes)
122                                 && isEqual(methodBinding.declaringClass, methodBinding2.declaringClass, visitedTypes)
123                                 && isEqual(methodBinding.typeVariables, methodBinding2.typeVariables, visitedTypes)
124                                 && isEqual(methodBinding.parameters, methodBinding2.parameters, visitedTypes);
125         }
126
127         static boolean isEqual(VariableBinding variableBinding, VariableBinding variableBinding2) {
128                 return (variableBinding.modifiers & ExtraCompilerModifiers.AccJustFlag) == (variableBinding2.modifiers & ExtraCompilerModifiers.AccJustFlag)
129                                 && CharOperation.equals(variableBinding.name, variableBinding2.name)
130                                 && isEqual(variableBinding.type, variableBinding2.type)
131                                 && (variableBinding.id == variableBinding2.id);
132         }
133
134         static boolean isEqual(FieldBinding fieldBinding, FieldBinding fieldBinding2) {
135                 HashSet visitedTypes = new HashSet();
136                 return (fieldBinding.modifiers & ExtraCompilerModifiers.AccJustFlag) == (fieldBinding2.modifiers & ExtraCompilerModifiers.AccJustFlag)
137                                 && CharOperation.equals(fieldBinding.name, fieldBinding2.name)
138                                 && isEqual(fieldBinding.type, fieldBinding2.type, visitedTypes)
139                                 && isEqual(fieldBinding.declaringClass, fieldBinding2.declaringClass, visitedTypes);
140         }
141
142         /**
143          * @param bindings
144          * @param otherBindings
145          * @return true if both parameters are equals, false otherwise
146          */
147         static boolean isEqual(net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding[] bindings, net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding[] otherBindings) {
148                 return isEqual(bindings, otherBindings, new HashSet());
149         }
150         /**
151          * @param bindings
152          * @param otherBindings
153          * @return true if both parameters are equals, false otherwise
154          */
155         static boolean isEqual(net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding[] bindings, net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding[] otherBindings, HashSet visitedTypes) {
156                 if (bindings == null) {
157                         return otherBindings == null;
158                 }
159                 if (otherBindings == null) {
160                         return false;
161                 }
162                 int length = bindings.length;
163                 int otherLength = otherBindings.length;
164                 if (length != otherLength) {
165                         return false;
166                 }
167                 for (int i = 0; i < length; i++) {
168                         if (!isEqual(bindings[i], otherBindings[i], visitedTypes)) {
169                                 return false;
170                         }
171                 }
172                 return true;
173         }
174         static boolean isEqual(net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding, net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding2, HashSet visitedTypes) {
175                 if (typeBinding == typeBinding2)
176                         return true;
177                 if (typeBinding == null || typeBinding2 == null)
178                         return false;
179
180                 switch (typeBinding.kind()) {
181                         case Binding.BASE_TYPE :
182                                 if (!typeBinding2.isBaseType()) {
183                                         return false;
184                                 }
185                                 return typeBinding.id == typeBinding2.id;
186                                 
187                         case Binding.ARRAY_TYPE :
188                                 if (!typeBinding2.isArrayType()) {
189                                         return false;
190                                 }
191                                 return typeBinding.dimensions() == typeBinding2.dimensions()
192                                                 && isEqual(typeBinding.leafComponentType(), typeBinding2.leafComponentType(), visitedTypes);
193                                 
194                         case Binding.PARAMETERIZED_TYPE :
195                                 if (!typeBinding2.isParameterizedType()) {
196                                         return false;
197                                 }
198                                 ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) typeBinding;
199                                 ParameterizedTypeBinding parameterizedTypeBinding2 = (ParameterizedTypeBinding) typeBinding2;
200                                 return CharOperation.equals(parameterizedTypeBinding.compoundName, parameterizedTypeBinding2.compoundName)
201                                         && (parameterizedTypeBinding.modifiers & (ExtraCompilerModifiers.AccJustFlag | ClassFileConstants.AccInterface | ClassFileConstants.AccEnum | ClassFileConstants.AccAnnotation))
202                                                         == (parameterizedTypeBinding2.modifiers & (ExtraCompilerModifiers.AccJustFlag | ClassFileConstants.AccInterface | ClassFileConstants.AccEnum | ClassFileConstants.AccAnnotation))
203                                         && isEqual(parameterizedTypeBinding.arguments, parameterizedTypeBinding2.arguments, visitedTypes)
204                                         && isEqual(parameterizedTypeBinding.enclosingType(), parameterizedTypeBinding2.enclosingType(), visitedTypes);
205                                                         
206                         case Binding.WILDCARD_TYPE :
207                                 if (typeBinding2.kind() != Binding.WILDCARD_TYPE) {
208                                         return false;
209                                 }
210                                 WildcardBinding wildcardBinding = (WildcardBinding) typeBinding;
211                                 WildcardBinding wildcardBinding2 = (WildcardBinding) typeBinding2;
212                                 return isEqual(wildcardBinding.bound, wildcardBinding2.bound, visitedTypes)
213                                         && wildcardBinding.boundKind == wildcardBinding2.boundKind;
214                                 
215                         case Binding.INTERSECTION_TYPE:
216                                 if (typeBinding2.kind() != Binding.INTERSECTION_TYPE) {
217                                         return false;
218                                 }
219                                 WildcardBinding intersectionBinding = (WildcardBinding) typeBinding;
220                                 WildcardBinding intersectionBinding2 = (WildcardBinding) typeBinding2;
221                                 return isEqual(intersectionBinding.bound, intersectionBinding2.bound, visitedTypes)
222                                         && isEqual(intersectionBinding.otherBounds, intersectionBinding2.otherBounds, visitedTypes);
223                                 
224                         case Binding.TYPE_PARAMETER :
225                                 if (!(typeBinding2.isTypeVariable())) {
226                                         return false;
227                                 }
228                                 if (typeBinding.isCapture()) {
229                                         if (!(typeBinding2.isCapture())) {
230                                                 return false;
231                                         }
232                                         CaptureBinding captureBinding = (CaptureBinding) typeBinding;
233                                         CaptureBinding captureBinding2 = (CaptureBinding) typeBinding2;
234                                         if (captureBinding.position == captureBinding2.position) {
235                                                 if (visitedTypes.contains(typeBinding)) return true;
236                                                 visitedTypes.add(typeBinding);
237                                                 
238                                                 return isEqual(captureBinding.wildcard, captureBinding2.wildcard, visitedTypes)
239                                                         && isEqual(captureBinding.sourceType, captureBinding2.sourceType, visitedTypes);
240                                         }
241                                         return false;
242                                 }
243                                 TypeVariableBinding typeVariableBinding = (TypeVariableBinding) typeBinding;
244                                 TypeVariableBinding typeVariableBinding2 = (TypeVariableBinding) typeBinding2;
245                                 if (CharOperation.equals(typeVariableBinding.sourceName, typeVariableBinding2.sourceName)) {
246                                         if (visitedTypes.contains(typeBinding)) return true;
247                                         visitedTypes.add(typeBinding);
248
249                                         return isEqual(typeVariableBinding.declaringElement, typeVariableBinding2.declaringElement, visitedTypes)
250                                         && isEqual(typeVariableBinding.superclass(), typeVariableBinding2.superclass(), visitedTypes)
251                                         && isEqual(typeVariableBinding.superInterfaces(), typeVariableBinding2.superInterfaces(), visitedTypes);
252                                 }
253                                 return false;
254                         case Binding.GENERIC_TYPE :
255                                 if (!typeBinding2.isGenericType()) {
256                                         return false;
257                                 }
258                                 ReferenceBinding referenceBinding = (ReferenceBinding) typeBinding;
259                                 ReferenceBinding referenceBinding2 = (ReferenceBinding) typeBinding2;
260                                 return CharOperation.equals(referenceBinding.compoundName, referenceBinding2.compoundName)
261                                         && (referenceBinding.modifiers & (ExtraCompilerModifiers.AccJustFlag | ClassFileConstants.AccInterface | ClassFileConstants.AccEnum | ClassFileConstants.AccAnnotation))
262                                                         == (referenceBinding2.modifiers & (ExtraCompilerModifiers.AccJustFlag | ClassFileConstants.AccInterface | ClassFileConstants.AccEnum | ClassFileConstants.AccAnnotation))
263                                         && isEqual(referenceBinding.typeVariables(), referenceBinding2.typeVariables(), visitedTypes)
264                                         && isEqual(referenceBinding.enclosingType(), referenceBinding2.enclosingType(), visitedTypes);
265
266                         case Binding.RAW_TYPE :
267                         default :
268                                 if (!(typeBinding2 instanceof ReferenceBinding)) {
269                                         return false;
270                                 }                               
271                                 referenceBinding = (ReferenceBinding) typeBinding;
272                                 referenceBinding2 = (ReferenceBinding) typeBinding2;
273                                 char[] constantPoolName = referenceBinding.constantPoolName();
274                                 char[] constantPoolName2 = referenceBinding2.constantPoolName();
275                                 // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=116833
276                                 if (constantPoolName == null) {
277                                         if (constantPoolName2 != null) {
278                                                 return false;
279                                         }
280                                         if (!CharOperation.equals(referenceBinding.computeUniqueKey(), referenceBinding2.computeUniqueKey())) {
281                                                 return false;
282                                         }
283                                 } else {
284                                         if (constantPoolName2 == null) {
285                                                 return false;
286                                         }
287                                         if (!CharOperation.equals(constantPoolName, constantPoolName2)) {
288                                                 return false;
289                                         }
290                                 }
291                                 return CharOperation.equals(referenceBinding.compoundName, referenceBinding2.compoundName)
292                                         && (!referenceBinding2.isGenericType())
293                                         && (referenceBinding.isRawType() == referenceBinding2.isRawType())
294                                         && ((referenceBinding.modifiers & ~ClassFileConstants.AccSuper) & (ExtraCompilerModifiers.AccJustFlag | ClassFileConstants.AccInterface | ClassFileConstants.AccEnum | ClassFileConstants.AccAnnotation))
295                                                         == ((referenceBinding2.modifiers & ~ClassFileConstants.AccSuper) & (ExtraCompilerModifiers.AccJustFlag | ClassFileConstants.AccInterface | ClassFileConstants.AccEnum | ClassFileConstants.AccAnnotation))
296                                         && isEqual(referenceBinding.enclosingType(), referenceBinding2.enclosingType(), visitedTypes);
297                 }
298         }
299         /**
300          * @param typeBinding
301          * @param typeBinding2
302          * @return true if both parameters are equals, false otherwise
303          */
304         static boolean isEqual(net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding, net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding2) {
305                 return isEqual(typeBinding, typeBinding2, new HashSet());
306         }
307 }