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