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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.lookup;
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;
17 public class MethodBinding extends Binding implements BaseTypes, TypeConstants {
19 public char[] selector;
20 public TypeBinding returnType;
21 public TypeBinding[] parameters;
22 public ReferenceBinding[] thrownExceptions;
23 public ReferenceBinding declaringClass;
27 protected MethodBinding() {
28 // for creating problem or synthetic method
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;
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;
47 public MethodBinding(int modifiers, TypeBinding[] args, ReferenceBinding[] exceptions, ReferenceBinding declaringClass) {
48 this(modifiers, ConstructorDeclaration.ConstantPoolName, VoidBinding, args, exceptions, declaringClass);
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;
59 /* Answer true if the argument types & the receiver's parameters are equal
62 public final boolean areParametersEqual(MethodBinding method) {
63 TypeBinding[] args = method.parameters;
64 if (parameters == args)
67 int length = parameters.length;
68 if (length != args.length)
71 for (int i = 0; i < length; i++)
72 if (parameters[i] != args[i])
77 * Answer the receiver's binding type from Binding.BindingID.
80 public final int bindingType() {
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.
87 * NOTE: This method should ONLY be sent if the receiver is a constructor.
89 * NOTE: Cannot invoke this method with a compilation unit scope.
92 public final boolean canBeSeenBy(InvocationSite invocationSite, Scope scope) {
93 if (isPublic()) return true;
95 SourceTypeBinding invocationType = scope.enclosingSourceType();
96 if (invocationType == declaringClass) return true;
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();
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();
114 ReferenceBinding outerDeclaringClass = declaringClass;
115 temp = outerDeclaringClass.enclosingType();
116 while (temp != null) {
117 outerDeclaringClass = temp;
118 temp = temp.enclosingType();
120 return outerInvocationType == outerDeclaringClass;
124 return invocationType.fPackage == declaringClass.fPackage;
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.
130 * NOTE: Cannot invoke this method with a compilation unit scope.
132 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
133 if (isPublic()) return true;
135 SourceTypeBinding invocationType = scope.enclosingSourceType();
136 if (invocationType == declaringClass && invocationType == receiverType) return true;
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;
147 ReferenceBinding currentType = invocationType;
150 if (declaringClass.isSuperclassOf(currentType)) {
151 if (invocationSite.isSuperAccess()){
154 // receiverType can be an array binding in one case... see if you can change it
155 if (receiverType instanceof ArrayBinding){
159 if (depth > 0) invocationSite.setDepth(depth);
160 return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
162 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
163 if (depth > 0) invocationSite.setDepth(depth);
168 currentType = currentType.enclosingType();
169 } while (currentType != null);
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;
178 if (invocationType != declaringClass) {
179 ReferenceBinding outerInvocationType = invocationType;
180 ReferenceBinding temp = outerInvocationType.enclosingType();
181 while (temp != null) {
182 outerInvocationType = temp;
183 temp = temp.enclosingType();
186 ReferenceBinding outerDeclaringClass = declaringClass;
187 temp = outerDeclaringClass.enclosingType();
188 while (temp != null) {
189 outerDeclaringClass = temp;
190 temp = temp.enclosingType();
192 if (outerInvocationType != outerDeclaringClass) return false;
198 if (invocationType.fPackage != declaringClass.fPackage) return false;
200 // receiverType can be an array binding in one case... see if you can change it
201 if (receiverType instanceof ArrayBinding)
203 ReferenceBinding type = (ReferenceBinding) receiverType;
204 PackageBinding declaringPackage = declaringClass.fPackage;
206 if (declaringClass == type) return true;
207 if (declaringPackage != type.fPackage) return false;
208 } while ((type = type.superclass()) != null);
212 * Answer the declaring class to use in the constant pool
213 * may not be a reference binding (see subtypes)
215 public TypeBinding constantPoolDeclaringClass() {
216 return this.declaringClass;
218 /* Answer the receiver's constant pool name.
220 * <init> for constructors
221 * <clinit> for clinit methods
222 * or the source name of the method
224 public final char[] constantPoolName() {
227 public final int getAccessFlags() {
228 return modifiers & AccJustFlag;
231 /* Answer true if the receiver is an abstract method
233 public final boolean isAbstract() {
234 return (modifiers & AccAbstract) != 0;
237 /* Answer true if the receiver is a bridge method
239 public final boolean isBridge() {
240 return (modifiers & AccBridge) != 0;
243 /* Answer true if the receiver is a constructor
245 public final boolean isConstructor() {
246 return selector == ConstructorDeclaration.ConstantPoolName;
248 protected boolean isConstructorRelated() {
249 return isConstructor();
252 /* Answer true if the receiver has default visibility
254 public final boolean isDefault() {
255 return !isPublic() && !isProtected() && !isPrivate();
258 /* Answer true if the receiver is a system generated default abstract method
260 public final boolean isDefaultAbstract() {
261 return (modifiers & AccDefaultAbstract) != 0;
264 /* Answer true if the receiver is a deprecated method
266 public final boolean isDeprecated() {
267 return (modifiers & AccDeprecated) != 0;
270 /* Answer true if the receiver is final and cannot be overridden
272 public final boolean isFinal() {
273 return (modifiers & AccFinal) != 0;
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
280 public final boolean isImplementing() {
281 return (modifiers & AccImplementing) != 0;
284 /* Answer true if the receiver is a native method
286 public final boolean isNative() {
287 return (modifiers & AccNative) != 0;
290 /* Answer true if the receiver is overriding another method
291 * Only set for source methods
293 public final boolean isOverriding() {
294 return (modifiers & AccOverriding) != 0;
297 * Answer true if the receiver is a "public static void main(String[])" method
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) {
311 /* Answer true if the receiver has private visibility
313 public final boolean isPrivate() {
314 return (modifiers & AccPrivate) != 0;
317 /* Answer true if the receiver has private visibility and is used locally
319 public final boolean isPrivateUsed() {
320 return (modifiers & AccPrivateUsed) != 0;
323 /* Answer true if the receiver has protected visibility
325 public final boolean isProtected() {
326 return (modifiers & AccProtected) != 0;
329 /* Answer true if the receiver has public visibility
331 public final boolean isPublic() {
332 return (modifiers & AccPublic) != 0;
335 /* Answer true if the receiver got requested to clear the private modifier
336 * during private access emulation.
338 public final boolean isRequiredToClearPrivateModifier() {
339 return (modifiers & AccClearPrivateModifier) != 0;
342 /* Answer true if the receiver is a static method
344 public final boolean isStatic() {
345 return (modifiers & AccStatic) != 0;
348 /* Answer true if all float operations must adher to IEEE 754 float/double rules
350 public final boolean isStrictfp() {
351 return (modifiers & AccStrictfp) != 0;
354 /* Answer true if the receiver is a synchronized method
356 public final boolean isSynchronized() {
357 return (modifiers & AccSynchronized) != 0;
360 /* Answer true if the receiver has public visibility
362 public final boolean isSynthetic() {
363 return (modifiers & AccSynthetic) != 0;
366 /* Answer true if the receiver is a vararg method
368 public final boolean isVararg() {
369 return (modifiers & AccVarargs) != 0;
372 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
374 public final boolean isViewedAsDeprecated() {
375 return (modifiers & AccDeprecated) != 0 ||
376 (modifiers & AccDeprecatedImplicitly) != 0;
379 public char[] readableName() /* foo(int, Thread) */ {
380 StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
382 buffer.append(declaringClass.sourceName());
384 buffer.append(selector);
386 if (parameters != NoParameters) {
387 for (int i = 0, length = parameters.length; i < length; i++) {
389 buffer.append(", "); //$NON-NLS-1$
390 buffer.append(parameters[i].sourceName());
394 return buffer.toString().toCharArray();
398 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#shortReadableName()
400 public char[] shortReadableName() {
401 StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
403 buffer.append(declaringClass.shortReadableName());
405 buffer.append(selector);
407 if (parameters != NoParameters) {
408 for (int i = 0, length = parameters.length; i < length; i++) {
410 buffer.append(", "); //$NON-NLS-1$
411 buffer.append(parameters[i].shortReadableName());
415 return buffer.toString().toCharArray();
418 protected final void setSelector(char[] selector) {
419 this.selector = selector;
420 this.signature = null;
423 /* Answer the receiver's signature.
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.
429 public final char[] signature() /* (ILjava/lang/Thread;)Ljava/lang/Object; */ {
430 if (signature != null)
433 StringBuffer buffer = new StringBuffer(parameters.length + 1 * 20);
436 TypeBinding[] targetParameters = this.parameters;
437 boolean considerSynthetics = isConstructorRelated() && declaringClass.isNestedType();
438 if (considerSynthetics) {
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());
447 if (this instanceof SyntheticAccessMethodBinding) {
448 targetParameters = ((SyntheticAccessMethodBinding)this).targetMethod.parameters;
452 if (targetParameters != NoParameters) {
453 for (int i = 0; i < targetParameters.length; i++) {
454 buffer.append(targetParameters[i].signature());
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());
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());
469 buffer.append(returnType.signature());
470 return signature = buffer.toString().toCharArray();
472 public final int sourceEnd() {
473 AbstractMethodDeclaration method = sourceMethod();
476 return method.sourceEnd;
478 AbstractMethodDeclaration sourceMethod() {
479 SourceTypeBinding sourceType;
481 sourceType = (SourceTypeBinding) declaringClass;
482 } catch (ClassCastException e) {
486 AbstractMethodDeclaration[] methods = sourceType.scope.referenceContext.methods;
487 for (int i = methods.length; --i >= 0;)
488 if (this == methods[i].binding)
492 public final int sourceStart() {
493 AbstractMethodDeclaration method = sourceMethod();
496 return method.sourceStart;
498 /* During private access emulation, the binding can be requested to loose its
499 * private visibility when the class file is dumped.
502 public final void tagForClearingPrivateModifier() {
503 modifiers |= AccClearPrivateModifier;
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$
510 s += "("; //$NON-NLS-1$
511 if (parameters != null) {
512 if (parameters != NoParameters) {
513 for (int i = 0, length = parameters.length; i < length; i++) {
515 s += ", "; //$NON-NLS-1$
516 s += (parameters[i] != null) ? parameters[i].debugName() : "NULL TYPE"; //$NON-NLS-1$
520 s += "NULL PARAMETERS"; //$NON-NLS-1$
522 s += ") "; //$NON-NLS-1$
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++) {
529 s += ", "; //$NON-NLS-1$
530 s += (thrownExceptions[i] != null) ? thrownExceptions[i].debugName() : "NULL TYPE"; //$NON-NLS-1$
534 s += "NULL THROWN EXCEPTIONS"; //$NON-NLS-1$