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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
13 import java.util.HashSet;
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;
29 * Internal helper class for comparing bindings.
33 class BindingComparator {
36 * @param otherBindings
37 * @return true if both parameters are equals, false otherwise
39 static boolean isEqual(TypeVariableBinding[] bindings, TypeVariableBinding[] otherBindings) {
40 if (bindings == null) {
41 return otherBindings == null;
43 if (otherBindings == null) {
46 int length = bindings.length;
47 int otherLength = otherBindings.length;
48 if (length != otherLength) {
51 for (int i = 0; i < length; i++) {
52 TypeVariableBinding typeVariableBinding = bindings[i];
53 TypeVariableBinding typeVariableBinding2 = otherBindings[i];
54 if (!isEqual(typeVariableBinding, typeVariableBinding2)) {
62 * @param declaringElement
63 * @param declaringElement2
64 * @return true if both parameters are equals, false otherwise
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)){
71 return isEqual((net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) declaringElement,
72 (net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) declaringElement2,
74 } else if (declaringElement instanceof net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding) {
75 if (!(declaringElement2 instanceof net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding)) {
78 return isEqual((net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding) declaringElement,
79 (net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding) declaringElement2,
81 } else if (declaringElement instanceof VariableBinding) {
82 if (!(declaringElement2 instanceof VariableBinding)) {
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)) {
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)) {
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);
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());
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;
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);
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);
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);
144 * @param otherBindings
145 * @return true if both parameters are equals, false otherwise
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());
152 * @param otherBindings
153 * @return true if both parameters are equals, false otherwise
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;
159 if (otherBindings == null) {
162 int length = bindings.length;
163 int otherLength = otherBindings.length;
164 if (length != otherLength) {
167 for (int i = 0; i < length; i++) {
168 if (!isEqual(bindings[i], otherBindings[i], visitedTypes)) {
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)
177 if (typeBinding == null || typeBinding2 == null)
180 switch (typeBinding.kind()) {
181 case Binding.BASE_TYPE :
182 if (!typeBinding2.isBaseType()) {
185 return typeBinding.id == typeBinding2.id;
187 case Binding.ARRAY_TYPE :
188 if (!typeBinding2.isArrayType()) {
191 return typeBinding.dimensions() == typeBinding2.dimensions()
192 && isEqual(typeBinding.leafComponentType(), typeBinding2.leafComponentType(), visitedTypes);
194 case Binding.PARAMETERIZED_TYPE :
195 if (!typeBinding2.isParameterizedType()) {
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);
206 case Binding.WILDCARD_TYPE :
207 if (typeBinding2.kind() != Binding.WILDCARD_TYPE) {
210 WildcardBinding wildcardBinding = (WildcardBinding) typeBinding;
211 WildcardBinding wildcardBinding2 = (WildcardBinding) typeBinding2;
212 return isEqual(wildcardBinding.bound, wildcardBinding2.bound, visitedTypes)
213 && wildcardBinding.boundKind == wildcardBinding2.boundKind;
215 case Binding.INTERSECTION_TYPE:
216 if (typeBinding2.kind() != Binding.INTERSECTION_TYPE) {
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);
224 case Binding.TYPE_PARAMETER :
225 if (!(typeBinding2.isTypeVariable())) {
228 if (typeBinding.isCapture()) {
229 if (!(typeBinding2.isCapture())) {
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);
238 return isEqual(captureBinding.wildcard, captureBinding2.wildcard, visitedTypes)
239 && isEqual(captureBinding.sourceType, captureBinding2.sourceType, visitedTypes);
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);
249 return isEqual(typeVariableBinding.declaringElement, typeVariableBinding2.declaringElement, visitedTypes)
250 && isEqual(typeVariableBinding.superclass(), typeVariableBinding2.superclass(), visitedTypes)
251 && isEqual(typeVariableBinding.superInterfaces(), typeVariableBinding2.superInterfaces(), visitedTypes);
254 case Binding.GENERIC_TYPE :
255 if (!typeBinding2.isGenericType()) {
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);
266 case Binding.RAW_TYPE :
268 if (!(typeBinding2 instanceof ReferenceBinding)) {
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) {
280 if (!CharOperation.equals(referenceBinding.computeUniqueKey(), referenceBinding2.computeUniqueKey())) {
284 if (constantPoolName2 == null) {
287 if (!CharOperation.equals(constantPoolName, constantPoolName2)) {
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);
301 * @param typeBinding2
302 * @return true if both parameters are equals, false otherwise
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());