added -J option to preserve unmodified files in preexisting jarfile
[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.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
15 import org.eclipse.jdt.internal.compiler.impl.Constant;
16
17 public class FieldBinding extends VariableBinding {
18         public ReferenceBinding declaringClass;
19 protected FieldBinding() {
20         super(null, null, 0, null);
21         // for creating problem field
22 }
23 public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass, Constant constant) {
24         super(name, type, modifiers, constant);
25         this.declaringClass = declaringClass;
26
27         // propagate the deprecated modifier
28         if (this.declaringClass != null)
29                 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
30                         this.modifiers |= AccDeprecatedImplicitly;
31 }
32 public FieldBinding(FieldDeclaration field, TypeBinding type, int modifiers, ReferenceBinding declaringClass) {
33         this(field.name, type, modifiers, declaringClass, null);
34         field.binding = this; // record binding in declaration
35 }
36 // special API used to change field declaring class for runtime visibility check
37 public FieldBinding(FieldBinding initialFieldBinding, ReferenceBinding declaringClass) {
38         super(initialFieldBinding.name, initialFieldBinding.type, initialFieldBinding.modifiers, initialFieldBinding.constant());
39         this.declaringClass = declaringClass;
40         this.id = initialFieldBinding.id;
41 }
42 /* API
43 * Answer the receiver's binding type from Binding.BindingID.
44 */
45
46 public final int kind() {
47         return FIELD;
48 }
49 /* Answer true if the receiver is visible to the invocationPackage.
50 */
51
52 public final boolean canBeSeenBy(PackageBinding invocationPackage) {
53         if (isPublic()) return true;
54         if (isPrivate()) return false;
55
56         // isProtected() or isDefault()
57         return invocationPackage == declaringClass.getPackage();
58 }
59 /* Answer true if the receiver is visible to the type provided by the scope.
60 * InvocationSite implements isSuperAccess() to provide additional information
61 * if the receiver is protected.
62 *
63 * NOTE: Cannot invoke this method with a compilation unit scope.
64 */
65
66 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
67         if (isPublic()) return true;
68
69         SourceTypeBinding invocationType = scope.enclosingSourceType();
70         if (invocationType == declaringClass && invocationType == receiverType) return true;
71
72         if (isProtected()) {
73                 // answer true if the invocationType is the declaringClass or they are in the same package
74                 // OR the invocationType is a subclass of the declaringClass
75                 //    AND the receiverType is the invocationType or its subclass
76                 //    OR the method is a static method accessed directly through a type
77                 //    OR previous assertions are true for one of the enclosing type
78                 if (invocationType == declaringClass) return true;
79                 if (invocationType.fPackage == declaringClass.fPackage) return true;
80                 
81                 ReferenceBinding currentType = invocationType;
82                 int depth = 0;
83                 do {
84                         if (declaringClass.isSuperclassOf(currentType)) {
85                                 if (invocationSite.isSuperAccess()){
86                                         return true;
87                                 }
88                                 // receiverType can be an array binding in one case... see if you can change it
89                                 if (receiverType instanceof ArrayBinding){
90                                         return false;
91                                 }
92                                 if (isStatic()){
93                                         if (depth > 0) invocationSite.setDepth(depth);
94                                         return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
95                                 }
96                                 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
97                                         if (depth > 0) invocationSite.setDepth(depth);
98                                         return true;
99                                 }
100                         }
101                         depth++;
102                         currentType = currentType.enclosingType();
103                 } while (currentType != null);
104                 return false;
105         }
106
107         if (isPrivate()) {
108                 // answer true if the receiverType is the declaringClass
109                 // AND the invocationType and the declaringClass have a common enclosingType
110                 receiverCheck: {
111                         if (receiverType != declaringClass) {
112                                 // special tolerance for type variable direct bounds
113                                 if (receiverType.isTypeVariable() && ((TypeVariableBinding) receiverType).isErasureBoundTo(declaringClass.erasure())) {
114                                         break receiverCheck;
115                                 }
116                                 return false;
117                         }
118                 }
119
120                 if (invocationType != declaringClass) {
121                         ReferenceBinding outerInvocationType = invocationType;
122                         ReferenceBinding temp = outerInvocationType.enclosingType();
123                         while (temp != null) {
124                                 outerInvocationType = temp;
125                                 temp = temp.enclosingType();
126                         }
127
128                         ReferenceBinding outerDeclaringClass = (ReferenceBinding)declaringClass.erasure();
129                         temp = outerDeclaringClass.enclosingType();
130                         while (temp != null) {
131                                 outerDeclaringClass = temp;
132                                 temp = temp.enclosingType();
133                         }
134                         if (outerInvocationType != outerDeclaringClass) return false;
135                 }
136                 return true;
137         }
138
139         // isDefault()
140         if (invocationType.fPackage != declaringClass.fPackage) return false;
141
142         // receiverType can be an array binding in one case... see if you can change it
143         if (receiverType instanceof ArrayBinding)
144                 return false;
145         ReferenceBinding currentType = (ReferenceBinding) receiverType;
146         PackageBinding declaringPackage = declaringClass.fPackage;
147         do {
148                 if (declaringClass == currentType) return true;
149                 if (declaringPackage != currentType.fPackage) return false;
150         } while ((currentType = currentType.superclass()) != null);
151         return false;
152 }
153 /*
154  * declaringUniqueKey dot fieldName
155  * p.X { X<T> x} --> Lp/X;.x;
156  */
157 public char[] computeUniqueKey() {
158         char[] declaringKey = this.declaringClass == null /*case of length field for an array*/ ? CharOperation.NO_CHAR : this.declaringClass.computeUniqueKey();
159         int declaringLength = declaringKey.length;
160         int nameLength = this.name.length;
161         char[] uniqueKey = new char[declaringLength + 1 + nameLength];
162         System.arraycopy(declaringKey, 0, uniqueKey, 0, declaringLength);
163         uniqueKey[declaringLength] = '.';
164         System.arraycopy(this.name, 0, uniqueKey, declaringLength + 1, nameLength);
165         return uniqueKey;
166 }
167 /**
168  * X<T> t   -->  LX<TT;>;
169  */
170 public char[] genericSignature() {
171     if ((this.modifiers & AccGenericSignature) == 0) return null;
172     return this.type.genericTypeSignature();
173 }
174
175 public final int getAccessFlags() {
176         return modifiers & AccJustFlag;
177 }
178
179 /* Answer true if the receiver has default visibility
180 */
181
182 public final boolean isDefault() {
183         return !isPublic() && !isProtected() && !isPrivate();
184 }
185 /* Answer true if the receiver is a deprecated field
186 */
187
188 public final boolean isDeprecated() {
189         return (modifiers & AccDeprecated) != 0;
190 }
191 /* Answer true if the receiver has private visibility
192 */
193
194 public final boolean isPrivate() {
195         return (modifiers & AccPrivate) != 0;
196 }
197 /* Answer true if the receiver has private visibility and is used locally
198 */
199
200 public final boolean isPrivateUsed() {
201         return (modifiers & AccPrivateUsed) != 0;
202 }
203 /* Answer true if the receiver has protected visibility
204 */
205
206 public final boolean isProtected() {
207         return (modifiers & AccProtected) != 0;
208 }
209 /* Answer true if the receiver has public visibility
210 */
211
212 public final boolean isPublic() {
213         return (modifiers & AccPublic) != 0;
214 }
215 /* Answer true if the receiver is a static field
216 */
217
218 public final boolean isStatic() {
219         return (modifiers & AccStatic) != 0;
220 }
221 /* Answer true if the receiver is not defined in the source of the declaringClass
222 */
223
224 public final boolean isSynthetic() {
225         return (modifiers & AccSynthetic) != 0;
226 }
227 /* Answer true if the receiver is a transient field
228 */
229
230 public final boolean isTransient() {
231         return (modifiers & AccTransient) != 0;
232 }
233 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
234 */
235
236 public final boolean isViewedAsDeprecated() {
237         return (modifiers & AccDeprecated) != 0 ||
238                 (modifiers & AccDeprecatedImplicitly) != 0;
239 }
240 /* Answer true if the receiver is a volatile field
241 */
242
243 public final boolean isVolatile() {
244         return (modifiers & AccVolatile) != 0;
245 }
246 /**
247  * Returns the original field (as opposed to parameterized instances)
248  */
249 public FieldBinding original() {
250         return this;
251 }
252 public FieldDeclaration sourceField() {
253         SourceTypeBinding sourceType;
254         try {
255                 sourceType = (SourceTypeBinding) declaringClass;
256         } catch (ClassCastException e) {
257                 return null;            
258         }
259
260         FieldDeclaration[] fields = sourceType.scope.referenceContext.fields;
261         if (fields != null) {
262                 for (int i = fields.length; --i >= 0;)
263                         if (this == fields[i].binding)
264                                 return fields[i];
265         }
266         return null;            
267 }
268 }