removed Makefile; lifted repo/org.ibex.tool/src/ to src/
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / lookup / FieldBinding.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.internal.compiler.ast.FieldDeclaration;
14 import org.eclipse.jdt.internal.compiler.impl.Constant;
15
16 public class FieldBinding extends VariableBinding {
17         public ReferenceBinding declaringClass;
18 protected FieldBinding() {
19         // for creating problem field
20 }
21 public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass, Constant constant) {
22         this.modifiers = modifiers;
23         this.type = type;
24         this.name = name;
25         this.declaringClass = declaringClass;
26         this.constant = constant;
27
28         // propagate the deprecated modifier
29         if (this.declaringClass != null)
30                 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
31                         this.modifiers |= AccDeprecatedImplicitly;
32 }
33 public FieldBinding(FieldDeclaration field, TypeBinding type, int modifiers, ReferenceBinding declaringClass) {
34         this(field.name, type, modifiers, declaringClass, null);
35         field.binding = this; // record binding in declaration
36 }
37 // special API used to change field declaring class for runtime visibility check
38 public FieldBinding(FieldBinding initialFieldBinding, ReferenceBinding declaringClass) {
39         this.modifiers = initialFieldBinding.modifiers;
40         this.type = initialFieldBinding.type;
41         this.name = initialFieldBinding.name;
42         this.declaringClass = declaringClass;
43         this.constant = initialFieldBinding.constant;
44         this.id = initialFieldBinding.id;
45 }
46 /* API
47 * Answer the receiver's binding type from Binding.BindingID.
48 */
49
50 public final int bindingType() {
51         return FIELD;
52 }
53 /* Answer true if the receiver is visible to the type provided by the scope.
54 * InvocationSite implements isSuperAccess() to provide additional information
55 * if the receiver is protected.
56 *
57 * NOTE: Cannot invoke this method with a compilation unit scope.
58 */
59
60 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
61         if (isPublic()) return true;
62
63         SourceTypeBinding invocationType = scope.enclosingSourceType();
64         if (invocationType == declaringClass && invocationType == receiverType) return true;
65
66         if (isProtected()) {
67                 // answer true if the invocationType is the declaringClass or they are in the same package
68                 // OR the invocationType is a subclass of the declaringClass
69                 //    AND the receiverType is the invocationType or its subclass
70                 //    OR the method is a static method accessed directly through a type
71                 //    OR previous assertions are true for one of the enclosing type
72                 if (invocationType == declaringClass) return true;
73                 if (invocationType.fPackage == declaringClass.fPackage) return true;
74                 
75                 ReferenceBinding currentType = invocationType;
76                 int depth = 0;
77                 do {
78                         if (declaringClass.isSuperclassOf(currentType)) {
79                                 if (invocationSite.isSuperAccess()){
80                                         return true;
81                                 }
82                                 // receiverType can be an array binding in one case... see if you can change it
83                                 if (receiverType instanceof ArrayBinding){
84                                         return false;
85                                 }
86                                 if (isStatic()){
87                                         if (depth > 0) invocationSite.setDepth(depth);
88                                         return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
89                                 }
90                                 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
91                                         if (depth > 0) invocationSite.setDepth(depth);
92                                         return true;
93                                 }
94                         }
95                         depth++;
96                         currentType = currentType.enclosingType();
97                 } while (currentType != null);
98                 return false;
99         }
100
101         if (isPrivate()) {
102                 // answer true if the receiverType is the declaringClass
103                 // AND the invocationType and the declaringClass have a common enclosingType
104                 if (receiverType != declaringClass) return false;
105
106                 if (invocationType != declaringClass) {
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                         if (outerInvocationType != outerDeclaringClass) return false;
121                 }
122                 return true;
123         }
124
125         // isDefault()
126         if (invocationType.fPackage != declaringClass.fPackage) return false;
127
128         // receiverType can be an array binding in one case... see if you can change it
129         if (receiverType instanceof ArrayBinding)
130                 return false;
131         ReferenceBinding currentType = (ReferenceBinding) receiverType;
132         PackageBinding declaringPackage = declaringClass.fPackage;
133         do {
134                 if (declaringClass == currentType) return true;
135                 if (declaringPackage != currentType.fPackage) return false;
136         } while ((currentType = currentType.superclass()) != null);
137         return false;
138 }
139 public final int getAccessFlags() {
140         return modifiers & AccJustFlag;
141 }
142
143 /* Answer true if the receiver has default visibility
144 */
145
146 public final boolean isDefault() {
147         return !isPublic() && !isProtected() && !isPrivate();
148 }
149 /* Answer true if the receiver is a deprecated field
150 */
151
152 public final boolean isDeprecated() {
153         return (modifiers & AccDeprecated) != 0;
154 }
155 /* Answer true if the receiver has private visibility
156 */
157
158 public final boolean isPrivate() {
159         return (modifiers & AccPrivate) != 0;
160 }
161 /* Answer true if the receiver has private visibility and is used locally
162 */
163
164 public final boolean isPrivateUsed() {
165         return (modifiers & AccPrivateUsed) != 0;
166 }
167 /* Answer true if the receiver has protected visibility
168 */
169
170 public final boolean isProtected() {
171         return (modifiers & AccProtected) != 0;
172 }
173 /* Answer true if the receiver has public visibility
174 */
175
176 public final boolean isPublic() {
177         return (modifiers & AccPublic) != 0;
178 }
179 /* Answer true if the receiver is a static field
180 */
181
182 public final boolean isStatic() {
183         return (modifiers & AccStatic) != 0;
184 }
185 /* Answer true if the receiver is not defined in the source of the declaringClass
186 */
187
188 public final boolean isSynthetic() {
189         return (modifiers & AccSynthetic) != 0;
190 }
191 /* Answer true if the receiver is a transient field
192 */
193
194 public final boolean isTransient() {
195         return (modifiers & AccTransient) != 0;
196 }
197 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
198 */
199
200 public final boolean isViewedAsDeprecated() {
201         return (modifiers & AccDeprecated) != 0 ||
202                 (modifiers & AccDeprecatedImplicitly) != 0;
203 }
204 /* Answer true if the receiver is a volatile field
205 */
206
207 public final boolean isVolatile() {
208         return (modifiers & AccVolatile) != 0;
209 }
210 }