1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*******************************************************************************
* Copyright (c) 2000, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.jsdt.core.dom;
import java.util.ArrayList;
import java.util.Iterator;
import org.eclipse.wst.jsdt.core.IFunction;
import org.eclipse.wst.jsdt.core.IJavaScriptElement;
import org.eclipse.wst.jsdt.core.IType;
import org.eclipse.wst.jsdt.core.ITypeRoot;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import org.eclipse.wst.jsdt.core.Signature;
import org.eclipse.wst.jsdt.core.compiler.CharOperation;
import org.eclipse.wst.jsdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.wst.jsdt.internal.compiler.lookup.LookupEnvironment;
import org.eclipse.wst.jsdt.internal.compiler.lookup.MethodVerifier;
import org.eclipse.wst.jsdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.wst.jsdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.wst.jsdt.internal.compiler.problem.AbortCompilation;
import org.eclipse.wst.jsdt.internal.core.JavaElement;
import org.eclipse.wst.jsdt.internal.core.Member;
import org.eclipse.wst.jsdt.internal.core.util.Util;
/**
* Internal implementation of method bindings.
*/
class FunctionBinding implements IFunctionBinding {
private static final int VALID_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED | Modifier.NATIVE |
Modifier.STRICTFP;
private static final ITypeBinding[] NO_TYPE_BINDINGS = new ITypeBinding[0];
private org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding binding;
private BindingResolver resolver;
private ITypeBinding[] parameterTypes;
private String name;
private ITypeBinding declaringClass;
private ITypeBinding returnType;
private String key;
FunctionBinding(BindingResolver resolver, org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding binding) {
this.resolver = resolver;
this.binding = binding;
}
/**
* @see IFunctionBinding#isConstructor()
*/
public boolean isConstructor() {
return this.binding.isConstructor();
}
/**
* @see IFunctionBinding#isDefaultConstructor()
*
*/
public boolean isDefaultConstructor() {
final ReferenceBinding declaringClassBinding = this.binding.declaringClass;
if (declaringClassBinding.isBinaryBinding()) {
return false;
}
return (this.binding.modifiers & ExtraCompilerModifiers.AccIsDefaultConstructor) != 0;
}
/**
* @see IBinding#getName()
*/
public String getName() {
if (name == null) {
if (this.binding.isConstructor()) {
name = this.getDeclaringClass().getName();
} else {
name = (this.binding.selector!=null) ? new String(this.binding.selector) : "";
}
}
return name;
}
/**
* @see IFunctionBinding#getDeclaringClass()
*/
public ITypeBinding getDeclaringClass() {
if (this.declaringClass == null) {
this.declaringClass = this.resolver.getTypeBinding(this.binding.declaringClass);
}
return declaringClass;
}
/**
* @see IFunctionBinding#getParameterTypes()
*/
public ITypeBinding[] getParameterTypes() {
if (this.parameterTypes != null) {
return parameterTypes;
}
org.eclipse.wst.jsdt.internal.compiler.lookup.TypeBinding[] parameters = this.binding.parameters;
int length = parameters == null ? 0 : parameters.length;
if (length == 0) {
return this.parameterTypes = NO_TYPE_BINDINGS;
} else {
ITypeBinding[] paramTypes = new ITypeBinding[length];
for (int i = 0; i < length; i++) {
final TypeBinding parameterBinding = parameters[i];
if (parameterBinding != null) {
ITypeBinding typeBinding = this.resolver.getTypeBinding(parameterBinding);
if (typeBinding == null) {
return this.parameterTypes = NO_TYPE_BINDINGS;
}
paramTypes[i] = typeBinding;
} else {
// log error
StringBuffer message = new StringBuffer("Report method binding where a parameter is null:\n"); //$NON-NLS-1$
message.append(this.toString());
Util.log(new IllegalArgumentException(), message.toString());
// report no binding since one or more parameter has no binding
return this.parameterTypes = NO_TYPE_BINDINGS;
}
}
return this.parameterTypes = paramTypes;
}
}
/**
* @see IFunctionBinding#getReturnType()
*/
public ITypeBinding getReturnType() {
if (this.returnType == null) {
this.returnType = this.resolver.getTypeBinding(this.binding.returnType);
}
return this.returnType;
}
public Object getDefaultValue() {
return null;
}
public IJavaScriptElement getJavaElement() {
JavaElement element = getUnresolvedJavaElement();
if (element == null)
return null;
return element.resolved(this.binding);
}
private JavaElement getUnresolvedJavaElement() {
IJavaScriptElement declaringElement = getDeclaringClass().getJavaElement();
if (declaringElement == null) return null;
if (!(this.resolver instanceof DefaultBindingResolver)) return null;
ASTNode node = (ASTNode) ((DefaultBindingResolver) this.resolver).bindingsToAstNodes.get(this);
ITypeRoot typeRoot=null;
IType declaringType=null;
if (declaringElement instanceof ITypeRoot)
{
typeRoot=(ITypeRoot)declaringElement;
}
else if (declaringElement instanceof IType )
declaringType=(IType)declaringElement;
// IType declaringType=(IType)declaringElement;
if (node != null && declaringElement.getParent().getElementType() != IJavaScriptElement.CLASS_FILE) {
if (node instanceof FunctionDeclaration) {
FunctionDeclaration methodDeclaration = (FunctionDeclaration) node;
ArrayList parameterSignatures = new ArrayList();
Iterator iterator = methodDeclaration.parameters().iterator();
while (iterator.hasNext()) {
SingleVariableDeclaration parameter = (SingleVariableDeclaration) iterator.next();
Type type = parameter.getType();
String typeSig = Util.getSignature(type);
int arrayDim = parameter.getExtraDimensions();
if (parameter.getAST().apiLevel() >= AST.JLS3 && parameter.isVarargs()) {
arrayDim++;
}
if (arrayDim > 0) {
typeSig = Signature.createArraySignature(typeSig, arrayDim);
}
parameterSignatures.add(typeSig);
}
int parameterCount = parameterSignatures.size();
String[] parameters = new String[parameterCount];
parameterSignatures.toArray(parameters);
if (typeRoot!=null)
return (JavaElement) typeRoot.getFunction(getName(), parameters);
else
return (JavaElement) declaringType.getFunction(getName(), parameters);
}
else {
return null;
}
} else {
// case of method not in the created AST, or a binary method
org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding original = this.binding.original();
String selector = original.isConstructor() ? declaringType.getElementName() : new String(original.selector);
boolean isBinary = declaringType.isBinary();
ReferenceBinding enclosingType = original.declaringClass.enclosingType();
boolean isInnerBinaryTypeConstructor = isBinary && original.isConstructor() && enclosingType != null;
TypeBinding[] parameters = original.parameters;
int length = parameters == null ? 0 : parameters.length;
int declaringIndex = isInnerBinaryTypeConstructor ? 1 : 0;
String[] parameterSignatures = new String[declaringIndex + length];
if (isInnerBinaryTypeConstructor)
parameterSignatures[0] = new String(enclosingType.signature()).replace('/', '.');
for (int i = 0; i < length; i++) {
parameterSignatures[declaringIndex + i] = new String(parameters[i].signature()).replace('/', '.');
}
IFunction result = declaringType.getFunction(selector, parameterSignatures);
if (isBinary)
return (JavaElement) result;
IFunction[] methods = null;
try {
methods = declaringType.getFunctions();
} catch (JavaScriptModelException e) {
// declaring type doesn't exist
return null;
}
IFunction[] candidates = Member.findMethods(result, methods);
if (candidates == null || candidates.length == 0)
return null;
return (JavaElement) candidates[0];
}
}
/**
* @see IBinding#getKind()
*/
public int getKind() {
return IBinding.METHOD;
}
/**
* @see IBinding#getModifiers()
*/
public int getModifiers() {
return this.binding.getAccessFlags() & VALID_MODIFIERS;
}
/**
* @see IBinding#isDeprecated()
*/
public boolean isDeprecated() {
return this.binding.isDeprecated();
}
/**
* @see IBinding#isRecovered()
*/
public boolean isRecovered() {
return false;
}
/**
* @see org.eclipse.wst.jsdt.core.dom.IFunctionBinding#isVarargs()
*
*/
public boolean isVarargs() {
return this.binding.isVarargs();
}
/**
* @see IBinding#getKey()
*/
public String getKey() {
if (this.key == null) {
this.key = new String(this.binding.computeUniqueKey());
}
return this.key;
}
/**
* @see IBinding#isEqualTo(IBinding)
*
*/
public boolean isEqualTo(IBinding other) {
if (other == this) {
// identical binding - equal (key or no key)
return true;
}
if (other == null) {
// other binding missing
return false;
}
if (!(other instanceof FunctionBinding)) {
return false;
}
org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding otherBinding = ((FunctionBinding) other).binding;
return BindingComparator.isEqual(this.binding, otherBinding);
}
public boolean isSubsignature(IFunctionBinding otherMethod) {
try {
org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding other = ((FunctionBinding) otherMethod).binding;
if (!CharOperation.equals(this.binding.selector, other.selector))
return false;
return this.binding.areParametersEqual(other);
} catch (AbortCompilation e) {
// don't surface internal exception to clients
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=143013
return false;
}
}
/**
* @see org.eclipse.wst.jsdt.core.dom.IFunctionBinding#getMethodDeclaration()
*/
public IFunctionBinding getMethodDeclaration() {
return this.resolver.getMethodBinding(this.binding.original());
}
/**
* @see IFunctionBinding#overrides(IFunctionBinding)
*/
public boolean overrides(IFunctionBinding overridenMethod) {
try {
org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding overridenCompilerBinding = ((FunctionBinding) overridenMethod).binding;
if (this.binding == overridenCompilerBinding)
return false;
char[] selector = this.binding.selector;
if (!CharOperation.equals(selector, overridenCompilerBinding.selector))
return false;
TypeBinding match = this.binding.declaringClass.findSuperTypeWithSameErasure(overridenCompilerBinding.declaringClass);
if (!(match instanceof ReferenceBinding)) return false;
org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding[] superMethods = ((ReferenceBinding)match).getMethods(selector);
for (int i = 0, length = superMethods.length; i < length; i++) {
if (superMethods[i].original() == overridenCompilerBinding) {
LookupEnvironment lookupEnvironment = this.resolver.lookupEnvironment();
if (lookupEnvironment == null) return false;
MethodVerifier methodVerifier = lookupEnvironment.methodVerifier();
org.eclipse.wst.jsdt.internal.compiler.lookup.MethodBinding superMethod = superMethods[i];
return !superMethod.isPrivate()
&& !(superMethod.isDefault() && (superMethod.declaringClass.getPackage()) != this.binding.declaringClass.getPackage())
&& methodVerifier.doesMethodOverride(this.binding, superMethod);
}
}
return false;
} catch (AbortCompilation e) {
// don't surface internal exception to clients
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=143013
return false;
}
}
/**
* For debugging purpose only.
* @see java.lang.Object#toString()
*/
public String toString() {
return this.binding.toString();
}
}
|