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