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.FieldDeclaration;
15 import org.eclipse.jdt.internal.compiler.impl.Constant;
17 public class FieldBinding extends VariableBinding {
18 public ReferenceBinding declaringClass;
19 protected FieldBinding() {
20 super(null, null, 0, null);
21 // for creating problem field
23 public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass, Constant constant) {
24 super(name, type, modifiers, constant);
25 this.declaringClass = declaringClass;
27 // propagate the deprecated modifier
28 if (this.declaringClass != null)
29 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
30 this.modifiers |= AccDeprecatedImplicitly;
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
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;
43 * Answer the receiver's binding type from Binding.BindingID.
46 public final int kind() {
49 /* Answer true if the receiver is visible to the invocationPackage.
52 public final boolean canBeSeenBy(PackageBinding invocationPackage) {
53 if (isPublic()) return true;
54 if (isPrivate()) return false;
56 // isProtected() or isDefault()
57 return invocationPackage == declaringClass.getPackage();
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.
63 * NOTE: Cannot invoke this method with a compilation unit scope.
66 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
67 if (isPublic()) return true;
69 SourceTypeBinding invocationType = scope.enclosingSourceType();
70 if (invocationType == declaringClass && invocationType == receiverType) return true;
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;
81 ReferenceBinding currentType = invocationType;
84 if (declaringClass.isSuperclassOf(currentType)) {
85 if (invocationSite.isSuperAccess()){
88 // receiverType can be an array binding in one case... see if you can change it
89 if (receiverType instanceof ArrayBinding){
93 if (depth > 0) invocationSite.setDepth(depth);
94 return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
96 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
97 if (depth > 0) invocationSite.setDepth(depth);
102 currentType = currentType.enclosingType();
103 } while (currentType != null);
108 // answer true if the receiverType is the declaringClass
109 // AND the invocationType and the declaringClass have a common enclosingType
111 if (receiverType != declaringClass) {
112 // special tolerance for type variable direct bounds
113 if (receiverType.isTypeVariable() && ((TypeVariableBinding) receiverType).isErasureBoundTo(declaringClass.erasure())) {
120 if (invocationType != declaringClass) {
121 ReferenceBinding outerInvocationType = invocationType;
122 ReferenceBinding temp = outerInvocationType.enclosingType();
123 while (temp != null) {
124 outerInvocationType = temp;
125 temp = temp.enclosingType();
128 ReferenceBinding outerDeclaringClass = (ReferenceBinding)declaringClass.erasure();
129 temp = outerDeclaringClass.enclosingType();
130 while (temp != null) {
131 outerDeclaringClass = temp;
132 temp = temp.enclosingType();
134 if (outerInvocationType != outerDeclaringClass) return false;
140 if (invocationType.fPackage != declaringClass.fPackage) return false;
142 // receiverType can be an array binding in one case... see if you can change it
143 if (receiverType instanceof ArrayBinding)
145 ReferenceBinding currentType = (ReferenceBinding) receiverType;
146 PackageBinding declaringPackage = declaringClass.fPackage;
148 if (declaringClass == currentType) return true;
149 if (declaringPackage != currentType.fPackage) return false;
150 } while ((currentType = currentType.superclass()) != null);
154 * declaringUniqueKey dot fieldName
155 * p.X { X<T> x} --> Lp/X;.x;
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);
168 * X<T> t --> LX<TT;>;
170 public char[] genericSignature() {
171 if ((this.modifiers & AccGenericSignature) == 0) return null;
172 return this.type.genericTypeSignature();
175 public final int getAccessFlags() {
176 return modifiers & AccJustFlag;
179 /* Answer true if the receiver has default visibility
182 public final boolean isDefault() {
183 return !isPublic() && !isProtected() && !isPrivate();
185 /* Answer true if the receiver is a deprecated field
188 public final boolean isDeprecated() {
189 return (modifiers & AccDeprecated) != 0;
191 /* Answer true if the receiver has private visibility
194 public final boolean isPrivate() {
195 return (modifiers & AccPrivate) != 0;
197 /* Answer true if the receiver has private visibility and is used locally
200 public final boolean isPrivateUsed() {
201 return (modifiers & AccPrivateUsed) != 0;
203 /* Answer true if the receiver has protected visibility
206 public final boolean isProtected() {
207 return (modifiers & AccProtected) != 0;
209 /* Answer true if the receiver has public visibility
212 public final boolean isPublic() {
213 return (modifiers & AccPublic) != 0;
215 /* Answer true if the receiver is a static field
218 public final boolean isStatic() {
219 return (modifiers & AccStatic) != 0;
221 /* Answer true if the receiver is not defined in the source of the declaringClass
224 public final boolean isSynthetic() {
225 return (modifiers & AccSynthetic) != 0;
227 /* Answer true if the receiver is a transient field
230 public final boolean isTransient() {
231 return (modifiers & AccTransient) != 0;
233 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
236 public final boolean isViewedAsDeprecated() {
237 return (modifiers & AccDeprecated) != 0 ||
238 (modifiers & AccDeprecatedImplicitly) != 0;
240 /* Answer true if the receiver is a volatile field
243 public final boolean isVolatile() {
244 return (modifiers & AccVolatile) != 0;
247 * Returns the original field (as opposed to parameterized instances)
249 public FieldBinding original() {
252 public FieldDeclaration sourceField() {
253 SourceTypeBinding sourceType;
255 sourceType = (SourceTypeBinding) declaringClass;
256 } catch (ClassCastException e) {
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)