Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / lookup / MethodBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.lookup;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
15 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
16
17 public class MethodBinding extends Binding implements BaseTypes, TypeConstants {
18         public int modifiers;
19         public char[] selector;
20         public TypeBinding returnType;
21         public TypeBinding[] parameters;
22         public ReferenceBinding[] thrownExceptions;
23         public ReferenceBinding declaringClass;
24
25         char[] signature;
26
27 protected MethodBinding() {
28         // for creating problem or synthetic method
29 }
30 public MethodBinding(int modifiers, char[] selector, TypeBinding returnType, TypeBinding[] args, ReferenceBinding[] exceptions, ReferenceBinding declaringClass) {
31         this.modifiers = modifiers;
32         this.selector = selector;
33         this.returnType = returnType;
34         this.parameters = (args == null || args.length == 0) ? NoParameters : args;
35         this.thrownExceptions = (exceptions == null || exceptions.length == 0) ? NoExceptions : exceptions;
36         this.declaringClass = declaringClass;
37
38         // propagate the strictfp & deprecated modifiers
39         if (this.declaringClass != null) {
40                 if (this.declaringClass.isStrictfp())
41                         if (!(isNative() || isAbstract()))
42                                 this.modifiers |= AccStrictfp;
43                 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
44                         this.modifiers |= AccDeprecatedImplicitly;
45         }
46 }
47 public MethodBinding(int modifiers, TypeBinding[] args, ReferenceBinding[] exceptions, ReferenceBinding declaringClass) {
48         this(modifiers, ConstructorDeclaration.ConstantPoolName, VoidBinding, args, exceptions, declaringClass);
49 }
50 // special API used to change method declaring class for runtime visibility check
51 public MethodBinding(MethodBinding initialMethodBinding, ReferenceBinding declaringClass) {
52         this.modifiers = initialMethodBinding.modifiers;
53         this.selector = initialMethodBinding.selector;
54         this.returnType = initialMethodBinding.returnType;
55         this.parameters = initialMethodBinding.parameters;
56         this.thrownExceptions = initialMethodBinding.thrownExceptions;
57         this.declaringClass = declaringClass;
58 }
59 /* Answer true if the argument types & the receiver's parameters are equal
60 */
61
62 public final boolean areParametersEqual(MethodBinding method) {
63         TypeBinding[] args = method.parameters;
64         if (parameters == args)
65                 return true;
66
67         int length = parameters.length;
68         if (length != args.length)
69                 return false;
70         
71         for (int i = 0; i < length; i++)
72                 if (parameters[i] != args[i])
73                         return false;
74         return true;
75 }
76 /* API
77 * Answer the receiver's binding type from Binding.BindingID.
78 */
79
80 public final int bindingType() {
81         return METHOD;
82 }
83 /* Answer true if the receiver is visible to the type provided by the scope.
84 * InvocationSite implements isSuperAccess() to provide additional information
85 * if the receiver is protected.
86 *
87 * NOTE: This method should ONLY be sent if the receiver is a constructor.
88 *
89 * NOTE: Cannot invoke this method with a compilation unit scope.
90 */
91
92 public final boolean canBeSeenBy(InvocationSite invocationSite, Scope scope) {
93         if (isPublic()) return true;
94
95         SourceTypeBinding invocationType = scope.enclosingSourceType();
96         if (invocationType == declaringClass) return true;
97
98         if (isProtected()) {
99                 // answer true if the receiver is in the same package as the invocationType
100                 if (invocationType.fPackage == declaringClass.fPackage) return true;
101                 return invocationSite.isSuperAccess();
102         }
103
104         if (isPrivate()) {
105                 // answer true if the invocationType and the declaringClass have a common enclosingType
106                 // already know they are not the identical type
107                 ReferenceBinding outerInvocationType = invocationType;
108                 ReferenceBinding temp = outerInvocationType.enclosingType();
109                 while (temp != null) {
110                         outerInvocationType = temp;
111                         temp = temp.enclosingType();
112                 }
113
114                 ReferenceBinding outerDeclaringClass = declaringClass;
115                 temp = outerDeclaringClass.enclosingType();
116                 while (temp != null) {
117                         outerDeclaringClass = temp;
118                         temp = temp.enclosingType();
119                 }
120                 return outerInvocationType == outerDeclaringClass;
121         }
122
123         // isDefault()
124         return invocationType.fPackage == declaringClass.fPackage;
125 }
126 /* Answer true if the receiver is visible to the type provided by the scope.
127 * InvocationSite implements isSuperAccess() to provide additional information
128 * if the receiver is protected.
129 *
130 * NOTE: Cannot invoke this method with a compilation unit scope.
131 */
132 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
133         if (isPublic()) return true;
134
135         SourceTypeBinding invocationType = scope.enclosingSourceType();
136         if (invocationType == declaringClass && invocationType == receiverType) return true;
137
138         if (isProtected()) {
139                 // answer true if the invocationType is the declaringClass or they are in the same package
140                 // OR the invocationType is a subclass of the declaringClass
141                 //    AND the receiverType is the invocationType or its subclass
142                 //    OR the method is a static method accessed directly through a type
143                 //    OR previous assertions are true for one of the enclosing type
144                 if (invocationType == declaringClass) return true;
145                 if (invocationType.fPackage == declaringClass.fPackage) return true;
146                 
147                 ReferenceBinding currentType = invocationType;
148                 int depth = 0;
149                 do {
150                         if (declaringClass.isSuperclassOf(currentType)) {
151                                 if (invocationSite.isSuperAccess()){
152                                         return true;
153                                 }
154                                 // receiverType can be an array binding in one case... see if you can change it
155                                 if (receiverType instanceof ArrayBinding){
156                                         return false;
157                                 }
158                                 if (isStatic()){
159                                         if (depth > 0) invocationSite.setDepth(depth);
160                                         return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
161                                 }
162                                 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
163                                         if (depth > 0) invocationSite.setDepth(depth);
164                                         return true;
165                                 }
166                         }
167                         depth++;
168                         currentType = currentType.enclosingType();
169                 } while (currentType != null);
170                 return false;
171         }
172
173         if (isPrivate()) {
174                 // answer true if the receiverType is the declaringClass
175                 // AND the invocationType and the declaringClass have a common enclosingType
176                 if (receiverType != declaringClass) return false;
177
178                 if (invocationType != declaringClass) {
179                         ReferenceBinding outerInvocationType = invocationType;
180                         ReferenceBinding temp = outerInvocationType.enclosingType();
181                         while (temp != null) {
182                                 outerInvocationType = temp;
183                                 temp = temp.enclosingType();
184                         }
185
186                         ReferenceBinding outerDeclaringClass = declaringClass;
187                         temp = outerDeclaringClass.enclosingType();
188                         while (temp != null) {
189                                 outerDeclaringClass = temp;
190                                 temp = temp.enclosingType();
191                         }
192                         if (outerInvocationType != outerDeclaringClass) return false;
193                 }
194                 return true;
195         }
196
197         // isDefault()
198         if (invocationType.fPackage != declaringClass.fPackage) return false;
199
200         // receiverType can be an array binding in one case... see if you can change it
201         if (receiverType instanceof ArrayBinding)
202                 return false;
203         ReferenceBinding type = (ReferenceBinding) receiverType;
204         PackageBinding declaringPackage = declaringClass.fPackage;
205         do {
206                 if (declaringClass == type) return true;
207                 if (declaringPackage != type.fPackage) return false;
208         } while ((type = type.superclass()) != null);
209         return false;
210 }
211 /* 
212  * Answer the declaring class to use in the constant pool
213  * may not be a reference binding (see subtypes)
214  */
215 public TypeBinding constantPoolDeclaringClass() {
216         return this.declaringClass;
217 }
218 /* Answer the receiver's constant pool name.
219 *
220 * <init> for constructors
221 * <clinit> for clinit methods
222 * or the source name of the method
223 */
224 public final char[] constantPoolName() {
225         return selector;
226 }
227 public final int getAccessFlags() {
228         return modifiers & AccJustFlag;
229 }
230
231 /* Answer true if the receiver is an abstract method
232 */
233 public final boolean isAbstract() {
234         return (modifiers & AccAbstract) != 0;
235 }
236
237 /* Answer true if the receiver is a bridge method
238 */
239 public final boolean isBridge() {
240         return (modifiers & AccBridge) != 0;
241 }
242
243 /* Answer true if the receiver is a constructor
244 */
245 public final boolean isConstructor() {
246         return selector == ConstructorDeclaration.ConstantPoolName;
247 }
248 protected boolean isConstructorRelated() {
249         return isConstructor();
250 }
251
252 /* Answer true if the receiver has default visibility
253 */
254 public final boolean isDefault() {
255         return !isPublic() && !isProtected() && !isPrivate();
256 }
257
258 /* Answer true if the receiver is a system generated default abstract method
259 */
260 public final boolean isDefaultAbstract() {
261         return (modifiers & AccDefaultAbstract) != 0;
262 }
263
264 /* Answer true if the receiver is a deprecated method
265 */
266 public final boolean isDeprecated() {
267         return (modifiers & AccDeprecated) != 0;
268 }
269
270 /* Answer true if the receiver is final and cannot be overridden
271 */
272 public final boolean isFinal() {
273         return (modifiers & AccFinal) != 0;
274 }
275
276 /* Answer true if the receiver is implementing another method
277  * in other words, it is overriding and concrete, and overriden method is abstract
278  * Only set for source methods
279 */
280 public final boolean isImplementing() {
281         return (modifiers & AccImplementing) != 0;
282 }
283
284 /* Answer true if the receiver is a native method
285 */
286 public final boolean isNative() {
287         return (modifiers & AccNative) != 0;
288 }
289
290 /* Answer true if the receiver is overriding another method
291  * Only set for source methods
292 */
293 public final boolean isOverriding() {
294         return (modifiers & AccOverriding) != 0;
295 }
296 /*
297  * Answer true if the receiver is a "public static void main(String[])" method
298  */
299 public final boolean isMain() {
300         if (this.selector.length == 4 && CharOperation.equals(this.selector, MAIN)
301                         && ((this.modifiers & (AccPublic | AccStatic)) != 0)
302                         && VoidBinding == this.returnType  
303                         && this.parameters.length == 1) {
304                 TypeBinding paramType = this.parameters[0];
305                 if (paramType.dimensions() == 1 && paramType.leafComponentType().id == TypeIds.T_JavaLangString) {
306                         return true;
307                 }
308         }
309         return false;
310 }
311 /* Answer true if the receiver has private visibility
312 */
313 public final boolean isPrivate() {
314         return (modifiers & AccPrivate) != 0;
315 }
316
317 /* Answer true if the receiver has private visibility and is used locally
318 */
319 public final boolean isPrivateUsed() {
320         return (modifiers & AccPrivateUsed) != 0;
321 }
322
323 /* Answer true if the receiver has protected visibility
324 */
325 public final boolean isProtected() {
326         return (modifiers & AccProtected) != 0;
327 }
328
329 /* Answer true if the receiver has public visibility
330 */
331 public final boolean isPublic() {
332         return (modifiers & AccPublic) != 0;
333 }
334
335 /* Answer true if the receiver got requested to clear the private modifier
336  * during private access emulation.
337  */
338 public final boolean isRequiredToClearPrivateModifier() {
339         return (modifiers & AccClearPrivateModifier) != 0;
340 }
341
342 /* Answer true if the receiver is a static method
343 */
344 public final boolean isStatic() {
345         return (modifiers & AccStatic) != 0;
346 }
347
348 /* Answer true if all float operations must adher to IEEE 754 float/double rules
349 */
350 public final boolean isStrictfp() {
351         return (modifiers & AccStrictfp) != 0;
352 }
353
354 /* Answer true if the receiver is a synchronized method
355 */
356 public final boolean isSynchronized() {
357         return (modifiers & AccSynchronized) != 0;
358 }
359
360 /* Answer true if the receiver has public visibility
361 */
362 public final boolean isSynthetic() {
363         return (modifiers & AccSynthetic) != 0;
364 }
365
366 /* Answer true if the receiver is a vararg method
367 */
368 public final boolean isVararg() {
369         return (modifiers & AccVarargs) != 0;
370 }
371
372 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
373 */
374 public final boolean isViewedAsDeprecated() {
375         return (modifiers & AccDeprecated) != 0 ||
376                 (modifiers & AccDeprecatedImplicitly) != 0;
377 }
378
379 public char[] readableName() /* foo(int, Thread) */ {
380         StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
381         if (isConstructor())
382                 buffer.append(declaringClass.sourceName());
383         else
384                 buffer.append(selector);
385         buffer.append('(');
386         if (parameters != NoParameters) {
387                 for (int i = 0, length = parameters.length; i < length; i++) {
388                         if (i > 0)
389                                 buffer.append(", "); //$NON-NLS-1$
390                         buffer.append(parameters[i].sourceName());
391                 }
392         }
393         buffer.append(')');
394         return buffer.toString().toCharArray();
395 }
396
397 /**
398  * @see org.eclipse.jdt.internal.compiler.lookup.Binding#shortReadableName()
399  */
400 public char[] shortReadableName() {
401         StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
402         if (isConstructor())
403                 buffer.append(declaringClass.shortReadableName());
404         else
405                 buffer.append(selector);
406         buffer.append('(');
407         if (parameters != NoParameters) {
408                 for (int i = 0, length = parameters.length; i < length; i++) {
409                         if (i > 0)
410                                 buffer.append(", "); //$NON-NLS-1$
411                         buffer.append(parameters[i].shortReadableName());
412                 }
413         }
414         buffer.append(')');
415         return buffer.toString().toCharArray();
416 }
417
418 protected final void setSelector(char[] selector) {
419         this.selector = selector;
420         this.signature = null;
421 }
422
423 /* Answer the receiver's signature.
424 *
425 * NOTE: This method should only be used during/after code gen.
426 * The signature is cached so if the signature of the return type or any parameter
427 * type changes, the cached state is invalid.
428 */
429 public final char[] signature() /* (ILjava/lang/Thread;)Ljava/lang/Object; */ {
430         if (signature != null)
431                 return signature;
432
433         StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
434         buffer.append('(');
435         
436         TypeBinding[] targetParameters = this.parameters;
437         boolean considerSynthetics = isConstructorRelated() && declaringClass.isNestedType();
438         if (considerSynthetics) {
439                 
440                 // take into account the synthetic argument type signatures as well
441                 ReferenceBinding[] syntheticArgumentTypes = declaringClass.syntheticEnclosingInstanceTypes();
442                 int count = syntheticArgumentTypes == null ? 0 : syntheticArgumentTypes.length;
443                 for (int i = 0; i < count; i++) {
444                         buffer.append(syntheticArgumentTypes[i].signature());
445                 }
446                 
447                 if (this instanceof SyntheticAccessMethodBinding) {
448                         targetParameters = ((SyntheticAccessMethodBinding)this).targetMethod.parameters;
449                 }
450         }
451
452         if (targetParameters != NoParameters) {
453                 for (int i = 0; i < targetParameters.length; i++) {
454                         buffer.append(targetParameters[i].signature());
455                 }
456         }
457         if (considerSynthetics) {
458                 SyntheticArgumentBinding[] syntheticOuterArguments = declaringClass.syntheticOuterLocalVariables();
459                 int count = syntheticOuterArguments == null ? 0 : syntheticOuterArguments.length;
460                 for (int i = 0; i < count; i++) {
461                         buffer.append(syntheticOuterArguments[i].type.signature());
462                 }
463                 // move the extra padding arguments of the synthetic constructor invocation to the end          
464                 for (int i = targetParameters.length, extraLength = parameters.length; i < extraLength; i++) {
465                         buffer.append(parameters[i].signature());
466                 }
467         }
468         buffer.append(')');
469         buffer.append(returnType.signature());
470         return signature = buffer.toString().toCharArray();
471 }
472 public final int sourceEnd() {
473         AbstractMethodDeclaration method = sourceMethod();
474         if (method == null)
475                 return 0;
476         return method.sourceEnd;
477 }
478 AbstractMethodDeclaration sourceMethod() {
479         SourceTypeBinding sourceType;
480         try {
481                 sourceType = (SourceTypeBinding) declaringClass;
482         } catch (ClassCastException e) {
483                 return null;            
484         }
485
486         AbstractMethodDeclaration[] methods = sourceType.scope.referenceContext.methods;
487         for (int i = methods.length; --i >= 0;)
488                 if (this == methods[i].binding)
489                         return methods[i];
490         return null;            
491 }
492 public final int sourceStart() {
493         AbstractMethodDeclaration method = sourceMethod();
494         if (method == null)
495                 return 0;
496         return method.sourceStart;
497 }
498 /* During private access emulation, the binding can be requested to loose its
499  * private visibility when the class file is dumped.
500  */
501
502 public final void tagForClearingPrivateModifier() {
503         modifiers |= AccClearPrivateModifier;
504 }
505 public String toString() {
506         String s = (returnType != null) ? returnType.debugName() : "NULL TYPE"; //$NON-NLS-1$
507         s += " "; //$NON-NLS-1$
508         s += (selector != null) ? new String(selector) : "UNNAMED METHOD"; //$NON-NLS-1$
509
510         s += "("; //$NON-NLS-1$
511         if (parameters != null) {
512                 if (parameters != NoParameters) {
513                         for (int i = 0, length = parameters.length; i < length; i++) {
514                                 if (i  > 0)
515                                         s += ", "; //$NON-NLS-1$
516                                 s += (parameters[i] != null) ? parameters[i].debugName() : "NULL TYPE"; //$NON-NLS-1$
517                         }
518                 }
519         } else {
520                 s += "NULL PARAMETERS"; //$NON-NLS-1$
521         }
522         s += ") "; //$NON-NLS-1$
523
524         if (thrownExceptions != null) {
525                 if (thrownExceptions != NoExceptions) {
526                         s += "throws "; //$NON-NLS-1$
527                         for (int i = 0, length = thrownExceptions.length; i < length; i++) {
528                                 if (i  > 0)
529                                         s += ", "; //$NON-NLS-1$
530                                 s += (thrownExceptions[i] != null) ? thrownExceptions[i].debugName() : "NULL TYPE"; //$NON-NLS-1$
531                         }
532                 }
533         } else {
534                 s += "NULL THROWN EXCEPTIONS"; //$NON-NLS-1$
535         }
536         return s;
537 }
538 }