new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / lookup / FieldBinding.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.internal.compiler.impl.Constant;
14 import net.sourceforge.phpeclipse.internal.compiler.ast.FieldDeclaration;
15
16
17
18 public class FieldBinding extends VariableBinding {
19         public ReferenceBinding declaringClass;
20 protected FieldBinding() {
21 }
22 public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass, Constant constant) {
23         this.modifiers = modifiers;
24         this.type = type;
25         this.name = name;
26         this.declaringClass = declaringClass;
27         this.constant = constant;
28
29         // propagate the deprecated modifier
30         if (this.declaringClass != null)
31                 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
32                         this.modifiers |= AccDeprecatedImplicitly;
33 }
34 public FieldBinding(FieldDeclaration field, TypeBinding type, ReferenceBinding declaringClass) {
35         this(field.name, type, field.modifiers, declaringClass, null);
36
37         field.binding = this;
38 }
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;
47 }
48 /* API
49 * Answer the receiver's binding type from Binding.BindingID.
50 */
51
52 public final int bindingType() {
53         return FIELD;
54 }
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.
58 *
59 * NOTE: Cannot invoke this method with a compilation unit scope.
60 */
61
62 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
63         if (isPublic()) return true;
64
65         SourceTypeBinding invocationType = scope.enclosingSourceType();
66         if (invocationType == declaringClass && invocationType == receiverType) return true;
67
68         if (isProtected()) {
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;
76                 
77                 ReferenceBinding currentType = invocationType;
78                 int depth = 0;
79                 do {
80                         if (declaringClass.isSuperclassOf(currentType)) {
81                                 if (invocationSite.isSuperAccess()){
82                                         return true;
83                                 }
84                                 // receiverType can be an array binding in one case... see if you can change it
85                                 if (receiverType instanceof ArrayBinding){
86                                         return false;
87                                 }
88                                 if (isStatic()){
89                                         return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
90                                 }
91                                 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
92                                         if (depth > 0) invocationSite.setDepth(depth);
93                                         return true;
94                                 }
95                         }
96                         depth++;
97                         currentType = currentType.enclosingType();
98                 } while (currentType != null);
99                 return false;
100         }
101
102         if (isPrivate()) {
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;
106
107                 if (invocationType != declaringClass) {
108                         ReferenceBinding outerInvocationType = invocationType;
109                         ReferenceBinding temp = outerInvocationType.enclosingType();
110                         while (temp != null) {
111                                 outerInvocationType = temp;
112                                 temp = temp.enclosingType();
113                         }
114
115                         ReferenceBinding outerDeclaringClass = declaringClass;
116                         temp = outerDeclaringClass.enclosingType();
117                         while (temp != null) {
118                                 outerDeclaringClass = temp;
119                                 temp = temp.enclosingType();
120                         }
121                         if (outerInvocationType != outerDeclaringClass) return false;
122                 }
123                 return true;
124         }
125
126         // isDefault()
127         if (invocationType.fPackage != declaringClass.fPackage) return false;
128
129         // receiverType can be an array binding in one case... see if you can change it
130         if (receiverType instanceof ArrayBinding)
131                 return false;
132         ReferenceBinding type = (ReferenceBinding) receiverType;
133         PackageBinding declaringPackage = declaringClass.fPackage;
134         do {
135                 if (declaringClass == type) return true;
136                 if (declaringPackage != type.fPackage) return false;
137         } while ((type = type.superclass()) != null);
138         return false;
139 }
140 public final int getAccessFlags() {
141         return modifiers & AccJustFlag;
142 }
143
144 /* Answer true if the receiver has default visibility
145 */
146
147 public final boolean isDefault() {
148         return !isPublic() && !isProtected() && !isPrivate();
149 }
150 /* Answer true if the receiver is a deprecated field
151 */
152
153 public final boolean isDeprecated() {
154         return (modifiers & AccDeprecated) != 0;
155 }
156 /* Answer true if the receiver has private visibility
157 */
158
159 public final boolean isPrivate() {
160         return (modifiers & AccPrivate) != 0;
161 }
162 /* Answer true if the receiver has private visibility and is used locally
163 */
164
165 public final boolean isPrivateUsed() {
166         return (modifiers & AccPrivateUsed) != 0;
167 }
168 /* Answer true if the receiver has protected visibility
169 */
170
171 public final boolean isProtected() {
172         return (modifiers & AccProtected) != 0;
173 }
174 /* Answer true if the receiver has public visibility
175 */
176
177 public final boolean isPublic() {
178         return (modifiers & AccPublic) != 0;
179 }
180 /* Answer true if the receiver is a static field
181 */
182
183 public final boolean isStatic() {
184         return (modifiers & AccStatic) != 0;
185 }
186 /* Answer true if the receiver is not defined in the source of the declaringClass
187 */
188
189 //public final boolean isSynthetic() {
190 //      return (modifiers & AccSynthetic) != 0;
191 //}
192 /* Answer true if the receiver is a transient field
193 */
194
195 //public final boolean isTransient() {
196 //      return (modifiers & AccTransient) != 0;
197 //}
198 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
199 */
200
201 public final boolean isViewedAsDeprecated() {
202         return (modifiers & AccDeprecated) != 0 ||
203                 (modifiers & AccDeprecatedImplicitly) != 0;
204 }
205 /* Answer true if the receiver is a volatile field
206 */
207
208 //public final boolean isVolatile() {
209 //      return (modifiers & AccVolatile) != 0;
210 //}
211 }