KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > VariableBinding


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.core.dom;
13
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IType;
16 import org.eclipse.jdt.core.util.IModifierConstants;
17 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
18 import org.eclipse.jdt.internal.compiler.impl.Constant;
19 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
20 import org.eclipse.jdt.internal.compiler.lookup.TagBits;
21 import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
22 import org.eclipse.jdt.internal.core.JavaElement;
23 import org.eclipse.jdt.internal.core.LocalVariable;
24
25 /**
26  * Internal implementation of variable bindings.
27  */

28 class VariableBinding implements IVariableBinding {
29
30     private static final int VALID_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
31         Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | Modifier.VOLATILE;
32
33     private org.eclipse.jdt.internal.compiler.lookup.VariableBinding binding;
34     private ITypeBinding declaringClass;
35     private String JavaDoc key;
36     private String JavaDoc name;
37     private BindingResolver resolver;
38     private ITypeBinding type;
39
40     VariableBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.VariableBinding binding) {
41         this.resolver = resolver;
42         this.binding = binding;
43     }
44
45     public IAnnotationBinding[] getAnnotations() {
46         org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding[] internalAnnotations = this.binding.getAnnotations();
47         // the variable is not an enum constant nor a field nor an argument.
48
int length = internalAnnotations == null ? 0 : internalAnnotations.length;
49         IAnnotationBinding[] domInstances =
50             length == 0 ? AnnotationBinding.NoAnnotations : new AnnotationBinding[length];
51         for (int i = 0; i < length; i++) {
52             final IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(internalAnnotations[i]);
53             if (annotationInstance == null) {// not resolving binding
54
return AnnotationBinding.NoAnnotations;
55             }
56             domInstances[i] = annotationInstance;
57         }
58         return domInstances;
59     }
60
61     /* (non-Javadoc)
62      * @see IVariableBinding#getConstantValue()
63      * @since 3.0
64      */

65     public Object JavaDoc getConstantValue() {
66         Constant c = this.binding.constant();
67         if (c == null || c == Constant.NotAConstant) return null;
68         switch (c.typeID()) {
69             case TypeIds.T_boolean:
70                 return Boolean.valueOf(c.booleanValue());
71             case TypeIds.T_byte:
72                 return new Byte JavaDoc(c.byteValue());
73             case TypeIds.T_char:
74                 return new Character JavaDoc(c.charValue());
75             case TypeIds.T_double:
76                 return new Double JavaDoc(c.doubleValue());
77             case TypeIds.T_float:
78                 return new Float JavaDoc(c.floatValue());
79             case TypeIds.T_int:
80                 return new Integer JavaDoc(c.intValue());
81             case TypeIds.T_long:
82                 return new Long JavaDoc(c.longValue());
83             case TypeIds.T_short:
84                 return new Short JavaDoc(c.shortValue());
85             case TypeIds.T_JavaLangString:
86                 return c.stringValue();
87         }
88         return null;
89     }
90
91     /*
92      * @see IVariableBinding#getDeclaringClass()
93      */

94     public ITypeBinding getDeclaringClass() {
95         if (isField()) {
96             if (this.declaringClass == null) {
97                 FieldBinding fieldBinding = (FieldBinding) this.binding;
98                 this.declaringClass = this.resolver.getTypeBinding(fieldBinding.declaringClass);
99             }
100             return this.declaringClass;
101         } else {
102             return null;
103         }
104     }
105
106     /*
107      * @see IVariableBinding#getDeclaringMethod()
108      */

109     public IMethodBinding getDeclaringMethod() {
110         if (!isField()) {
111             ASTNode node = this.resolver.findDeclaringNode(this);
112             while (true) {
113                 if (node == null) break;
114                 switch(node.getNodeType()) {
115                     case ASTNode.INITIALIZER :
116                         return null;
117                     case ASTNode.METHOD_DECLARATION :
118                         MethodDeclaration methodDeclaration = (MethodDeclaration) node;
119                         return methodDeclaration.resolveBinding();
120                     default:
121                         node = node.getParent();
122                 }
123             }
124         }
125         return null;
126     }
127
128     /*
129      * @see IBinding#getJavaElement()
130      */

131     public IJavaElement getJavaElement() {
132         JavaElement element = getUnresolvedJavaElement();
133         if (element == null)
134             return null;
135         return element.resolved(this.binding);
136     }
137
138     /*
139      * @see IBinding#getKey()
140      */

141     public String JavaDoc getKey() {
142         if (this.key == null) {
143             this.key = new String JavaDoc(this.binding.computeUniqueKey());
144         }
145         return this.key;
146     }
147
148     /*
149      * @see IBinding#getKind()
150      */

151     public int getKind() {
152         return IBinding.VARIABLE;
153     }
154
155     /*
156      * @see IBinding#getModifiers()
157      */

158     public int getModifiers() {
159         if (isField()) {
160             return ((FieldBinding) this.binding).getAccessFlags() & VALID_MODIFIERS;
161         }
162         if (binding.isFinal()) {
163             return IModifierConstants.ACC_FINAL;
164         }
165         return Modifier.NONE;
166     }
167
168     /*
169      * @see IBinding#getName()
170      */

171     public String JavaDoc getName() {
172         if (this.name == null) {
173             this.name = new String JavaDoc(this.binding.name);
174         }
175         return this.name;
176     }
177
178     /*
179      * @see IVariableBinding#getType()
180      */

181     public ITypeBinding getType() {
182         if (this.type == null) {
183             this.type = this.resolver.getTypeBinding(this.binding.type);
184         }
185         return this.type;
186     }
187
188     private JavaElement getUnresolvedJavaElement() {
189         if (isField()) {
190             // field
191
FieldBinding fieldBinding = (FieldBinding) this.binding;
192             if (fieldBinding.declaringClass == null) return null; // arraylength
193
IType declaringType = (IType) getDeclaringClass().getJavaElement();
194             if (declaringType == null) return null;
195             return (JavaElement) declaringType.getField(getName());
196         }
197         // local variable
198
IMethodBinding declaringMethod = getDeclaringMethod();
199         if (declaringMethod == null) return null;
200         JavaElement method = (JavaElement) declaringMethod.getJavaElement();
201         if (!(this.resolver instanceof DefaultBindingResolver)) return null;
202         VariableDeclaration localVar = (VariableDeclaration) ((DefaultBindingResolver) this.resolver).bindingsToAstNodes.get(this);
203         if (localVar == null) return null;
204         int nameStart;
205         int nameLength;
206         int sourceStart;
207         int sourceLength;
208         if (localVar instanceof SingleVariableDeclaration) {
209             sourceStart = localVar.getStartPosition();
210             sourceLength = localVar.getLength();
211             SimpleName simpleName = ((SingleVariableDeclaration) localVar).getName();
212             nameStart = simpleName.getStartPosition();
213             nameLength = simpleName.getLength();
214         } else {
215             nameStart = localVar.getStartPosition();
216             nameLength = localVar.getLength();
217             ASTNode node = localVar.getParent();
218             sourceStart = node.getStartPosition();
219             sourceLength = node.getLength();
220         }
221         char[] typeSig = this.binding.type.genericTypeSignature();
222         return new LocalVariable(method, localVar.getName().getIdentifier(), sourceStart, sourceStart+sourceLength-1, nameStart, nameStart+nameLength-1, new String JavaDoc(typeSig));
223     }
224
225     /*
226      * @see IVariableBinding#getVariableDeclaration()
227      * @since 3.1
228      */

229     public IVariableBinding getVariableDeclaration() {
230         if (this.isField()) {
231             FieldBinding fieldBinding = (FieldBinding) this.binding;
232             return this.resolver.getVariableBinding(fieldBinding.original());
233         }
234         return this;
235     }
236
237     /*
238      * @see IVariableBinding#getVariableId()
239      */

240     public int getVariableId() {
241         return this.binding.id;
242     }
243
244     /*
245      * @see IVariableBinding#isParameter()
246      */

247     public boolean isParameter() {
248         return (this.binding.tagBits & TagBits.IsArgument) != 0;
249     }
250     /*
251      * @see IBinding#isDeprecated()
252      */

253     public boolean isDeprecated() {
254         if (isField()) {
255             return ((FieldBinding) this.binding).isDeprecated();
256         }
257         return false;
258     }
259
260     /*
261      * @see IVariableBinding#isEnumConstant()
262      * @since 3.1
263      */

264     public boolean isEnumConstant() {
265         return (this.binding.modifiers & ClassFileConstants.AccEnum) != 0;
266     }
267
268     /*
269      * @see IBinding#isEqualTo(Binding)
270      * @since 3.1
271      */

272     public boolean isEqualTo(IBinding other) {
273         if (other == this) {
274             // identical binding - equal (key or no key)
275
return true;
276         }
277         if (other == null) {
278             // other binding missing
279
return false;
280         }
281         if (!(other instanceof VariableBinding)) {
282             return false;
283         }
284         org.eclipse.jdt.internal.compiler.lookup.VariableBinding otherBinding = ((VariableBinding) other).binding;
285         if (this.binding instanceof FieldBinding) {
286             if (otherBinding instanceof FieldBinding) {
287                 return BindingComparator.isEqual((FieldBinding) this.binding, (FieldBinding) otherBinding);
288             } else {
289                 return false;
290             }
291         } else {
292             if (BindingComparator.isEqual(this.binding, otherBinding)) {
293                 IMethodBinding declaringMethod = this.getDeclaringMethod();
294                 IMethodBinding otherDeclaringMethod = ((VariableBinding) other).getDeclaringMethod();
295                 if (declaringMethod == null) {
296                     if (otherDeclaringMethod != null) {
297                         return false;
298                     }
299                     return true;
300                 }
301                 return declaringMethod.isEqualTo(otherDeclaringMethod);
302             }
303             return false;
304         }
305     }
306
307     /*
308      * @see IVariableBinding#isField()
309      */

310     public boolean isField() {
311         return this.binding instanceof FieldBinding;
312     }
313
314     /*
315      * @see IBinding#isSynthetic()
316      */

317     public boolean isSynthetic() {
318         if (isField()) {
319             return ((FieldBinding) this.binding).isSynthetic();
320         }
321         return false;
322     }
323
324     /*
325      * (non-Javadoc)
326      * @see org.eclipse.jdt.core.dom.IBinding#isRecovered()
327      */

328     public boolean isRecovered() {
329         return false;
330     }
331
332     /*
333      * For debugging purpose only.
334      * @see java.lang.Object#toString()
335      */

336     public String JavaDoc toString() {
337         return this.binding.toString();
338     }
339 }
340
Popular Tags