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.internal.compiler.ast.FieldDeclaration;
14 import org.eclipse.jdt.internal.compiler.impl.Constant;
16 public class FieldBinding extends VariableBinding {
17 public ReferenceBinding declaringClass;
18 protected FieldBinding() {
19 // for creating problem field
21 public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass, Constant constant) {
22 this.modifiers = modifiers;
25 this.declaringClass = declaringClass;
26 this.constant = constant;
28 // propagate the deprecated modifier
29 if (this.declaringClass != null)
30 if (this.declaringClass.isViewedAsDeprecated() && !isDeprecated())
31 this.modifiers |= AccDeprecatedImplicitly;
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
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;
47 * Answer the receiver's binding type from Binding.BindingID.
50 public final int bindingType() {
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.
57 * NOTE: Cannot invoke this method with a compilation unit scope.
60 public final boolean canBeSeenBy(TypeBinding receiverType, InvocationSite invocationSite, Scope scope) {
61 if (isPublic()) return true;
63 SourceTypeBinding invocationType = scope.enclosingSourceType();
64 if (invocationType == declaringClass && invocationType == receiverType) return true;
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;
75 ReferenceBinding currentType = invocationType;
78 if (declaringClass.isSuperclassOf(currentType)) {
79 if (invocationSite.isSuperAccess()){
82 // receiverType can be an array binding in one case... see if you can change it
83 if (receiverType instanceof ArrayBinding){
87 if (depth > 0) invocationSite.setDepth(depth);
88 return true; // see 1FMEPDL - return invocationSite.isTypeAccess();
90 if (currentType == receiverType || currentType.isSuperclassOf((ReferenceBinding) receiverType)){
91 if (depth > 0) invocationSite.setDepth(depth);
96 currentType = currentType.enclosingType();
97 } while (currentType != null);
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;
106 if (invocationType != declaringClass) {
107 ReferenceBinding outerInvocationType = invocationType;
108 ReferenceBinding temp = outerInvocationType.enclosingType();
109 while (temp != null) {
110 outerInvocationType = temp;
111 temp = temp.enclosingType();
114 ReferenceBinding outerDeclaringClass = declaringClass;
115 temp = outerDeclaringClass.enclosingType();
116 while (temp != null) {
117 outerDeclaringClass = temp;
118 temp = temp.enclosingType();
120 if (outerInvocationType != outerDeclaringClass) return false;
126 if (invocationType.fPackage != declaringClass.fPackage) return false;
128 // receiverType can be an array binding in one case... see if you can change it
129 if (receiverType instanceof ArrayBinding)
131 ReferenceBinding currentType = (ReferenceBinding) receiverType;
132 PackageBinding declaringPackage = declaringClass.fPackage;
134 if (declaringClass == currentType) return true;
135 if (declaringPackage != currentType.fPackage) return false;
136 } while ((currentType = currentType.superclass()) != null);
139 public final int getAccessFlags() {
140 return modifiers & AccJustFlag;
143 /* Answer true if the receiver has default visibility
146 public final boolean isDefault() {
147 return !isPublic() && !isProtected() && !isPrivate();
149 /* Answer true if the receiver is a deprecated field
152 public final boolean isDeprecated() {
153 return (modifiers & AccDeprecated) != 0;
155 /* Answer true if the receiver has private visibility
158 public final boolean isPrivate() {
159 return (modifiers & AccPrivate) != 0;
161 /* Answer true if the receiver has private visibility and is used locally
164 public final boolean isPrivateUsed() {
165 return (modifiers & AccPrivateUsed) != 0;
167 /* Answer true if the receiver has protected visibility
170 public final boolean isProtected() {
171 return (modifiers & AccProtected) != 0;
173 /* Answer true if the receiver has public visibility
176 public final boolean isPublic() {
177 return (modifiers & AccPublic) != 0;
179 /* Answer true if the receiver is a static field
182 public final boolean isStatic() {
183 return (modifiers & AccStatic) != 0;
185 /* Answer true if the receiver is not defined in the source of the declaringClass
188 public final boolean isSynthetic() {
189 return (modifiers & AccSynthetic) != 0;
191 /* Answer true if the receiver is a transient field
194 public final boolean isTransient() {
195 return (modifiers & AccTransient) != 0;
197 /* Answer true if the receiver's declaring type is deprecated (or any of its enclosing types)
200 public final boolean isViewedAsDeprecated() {
201 return (modifiers & AccDeprecated) != 0 ||
202 (modifiers & AccDeprecatedImplicitly) != 0;
204 /* Answer true if the receiver is a volatile field
207 public final boolean isVolatile() {
208 return (modifiers & AccVolatile) != 0;