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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.lookup;
13 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
14 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
18 public class FieldBinding extends VariableBinding {
19 public ReferenceBinding declaringClass;
20 protected FieldBinding() {
22 public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass, Constant constant) {
23 this.modifiers = modifiers;
26 this.declaringClass = declaringClass;
27 this.constant = constant;
29 // propagate the deprecated modifier
30 if (this.declaringClass != null)
31 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
32 this.modifiers |= AccDeprecatedImplicitly;
34 public FieldBinding(FieldDeclaration field, TypeBinding type, ReferenceBinding declaringClass) {
35 this(field.name, type, field.modifiers, declaringClass, null);
39 // special API used to change field declaring class for runtime visibility check
40 public FieldBinding(FieldBinding initialFieldBinding, ReferenceBinding declaringClass) {
41 this.modifiers = initialFieldBinding.modifiers;
42 this.type = initialFieldBinding.type;
43 this.name = initialFieldBinding.name;
44 this.declaringClass = declaringClass;
45 this.constant = initialFieldBinding.constant;
46 this.id = initialFieldBinding.id;
49 * Answer the receiver's binding type from Binding.BindingID.
52 public final int bindingType() {
55 /* Answer true if the receiver is visible to the type provided by the scope.
56 * InvocationSite implements isSuperAccess() to provide additional information
57 * if the receiver is protected.
59 * NOTE: Cannot invoke this method with a compilation unit scope.
62 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
63 if (isPublic()) return true;
65 SourceTypeBinding invocationType = scope.enclosingSourceType();
66 if (invocationType == declaringClass && invocationType == receiverType) return true;
69 // answer true if the invocationType is the declaringClass or they are in the same package
70 // OR the invocationType is a subclass of the declaringClass
71 // AND the receiverType is the invocationType or its subclass
72 // OR the method is a static method accessed directly through a type
73 // OR previous assertions are true for one of the enclosing type
74 if (invocationType == declaringClass) return true;
75 if (invocationType.fPackage == declaringClass.fPackage) return true;
77 ReferenceBinding currentType = invocationType;
80 if (declaringClass.isSuperclassOf(currentType)) {
81 if (invocationSite.isSuperAccess()){
84 // receiverType can be an array binding in one case... see if you can change it
85 if (receiverType instanceof ArrayBinding){
89 return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
91 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
92 if (depth > 0) invocationSite.setDepth(depth);
97 currentType = currentType.enclosingType();
98 } while (currentType != null);
103 // answer true if the receiverType is the declaringClass
104 // AND the invocationType and the declaringClass have a common enclosingType
105 if (receiverType != declaringClass) return false;
107 if (invocationType != declaringClass) {
108 ReferenceBinding outerInvocationType = invocationType;
109 ReferenceBinding temp = outerInvocationType.enclosingType();
110 while (temp != null) {
111 outerInvocationType = temp;
112 temp = temp.enclosingType();
115 ReferenceBinding outerDeclaringClass = declaringClass;
116 temp = outerDeclaringClass.enclosingType();
117 while (temp != null) {
118 outerDeclaringClass = temp;
119 temp = temp.enclosingType();
121 if (outerInvocationType != outerDeclaringClass) return false;
127 if (invocationType.fPackage != declaringClass.fPackage) return false;
129 // receiverType can be an array binding in one case... see if you can change it
130 if (receiverType instanceof ArrayBinding)
132 ReferenceBinding type = (ReferenceBinding) receiverType;
133 PackageBinding declaringPackage = declaringClass.fPackage;
135 if (declaringClass == type) return true;
136 if (declaringPackage != type.fPackage) return false;
137 } while ((type = type.superclass()) != null);
140 public final int getAccessFlags() {
141 return modifiers & AccJustFlag;
144 /* Answer true if the receiver has default visibility
147 public final boolean isDefault() {
148 return !isPublic() && !isProtected() && !isPrivate();
150 /* Answer true if the receiver is a deprecated field
153 public final boolean isDeprecated() {
154 return (modifiers & AccDeprecated) != 0;
156 /* Answer true if the receiver has private visibility
159 public final boolean isPrivate() {
160 return (modifiers & AccPrivate) != 0;
162 /* Answer true if the receiver has private visibility and is used locally
165 public final boolean isPrivateUsed() {
166 return (modifiers & AccPrivateUsed) != 0;
168 /* Answer true if the receiver has protected visibility
171 public final boolean isProtected() {
172 return (modifiers & AccProtected) != 0;
174 /* Answer true if the receiver has public visibility
177 public final boolean isPublic() {
178 return (modifiers & AccPublic) != 0;
180 /* Answer true if the receiver is a static field
183 public final boolean isStatic() {
184 return (modifiers & AccStatic) != 0;
186 /* Answer true if the receiver is not defined in the source of the declaringClass
189 //public final boolean isSynthetic() {
190 // return (modifiers & AccSynthetic) != 0;
192 /* Answer true if the receiver is a transient field
195 //public final boolean isTransient() {
196 // return (modifiers & AccTransient) != 0;
198 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
201 public final boolean isViewedAsDeprecated() {
202 return (modifiers & AccDeprecated) != 0 ||
203 (modifiers & AccDeprecatedImplicitly) != 0;
205 /* Answer true if the receiver is a volatile field
208 //public final boolean isVolatile() {
209 // return (modifiers & AccVolatile) != 0;