1 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 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 key; 36 private String 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 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) { return AnnotationBinding.NoAnnotations; 55 } 56 domInstances[i] = annotationInstance; 57 } 58 return domInstances; 59 } 60 61 65 public Object 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 (c.byteValue()); 73 case TypeIds.T_char: 74 return new Character (c.charValue()); 75 case TypeIds.T_double: 76 return new Double (c.doubleValue()); 77 case TypeIds.T_float: 78 return new Float (c.floatValue()); 79 case TypeIds.T_int: 80 return new Integer (c.intValue()); 81 case TypeIds.T_long: 82 return new Long (c.longValue()); 83 case TypeIds.T_short: 84 return new Short (c.shortValue()); 85 case TypeIds.T_JavaLangString: 86 return c.stringValue(); 87 } 88 return null; 89 } 90 91 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 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 131 public IJavaElement getJavaElement() { 132 JavaElement element = getUnresolvedJavaElement(); 133 if (element == null) 134 return null; 135 return element.resolved(this.binding); 136 } 137 138 141 public String getKey() { 142 if (this.key == null) { 143 this.key = new String (this.binding.computeUniqueKey()); 144 } 145 return this.key; 146 } 147 148 151 public int getKind() { 152 return IBinding.VARIABLE; 153 } 154 155 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 171 public String getName() { 172 if (this.name == null) { 173 this.name = new String (this.binding.name); 174 } 175 return this.name; 176 } 177 178 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 FieldBinding fieldBinding = (FieldBinding) this.binding; 192 if (fieldBinding.declaringClass == null) return null; IType declaringType = (IType) getDeclaringClass().getJavaElement(); 194 if (declaringType == null) return null; 195 return (JavaElement) declaringType.getField(getName()); 196 } 197 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 (typeSig)); 223 } 224 225 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 240 public int getVariableId() { 241 return this.binding.id; 242 } 243 244 247 public boolean isParameter() { 248 return (this.binding.tagBits & TagBits.IsArgument) != 0; 249 } 250 253 public boolean isDeprecated() { 254 if (isField()) { 255 return ((FieldBinding) this.binding).isDeprecated(); 256 } 257 return false; 258 } 259 260 264 public boolean isEnumConstant() { 265 return (this.binding.modifiers & ClassFileConstants.AccEnum) != 0; 266 } 267 268 272 public boolean isEqualTo(IBinding other) { 273 if (other == this) { 274 return true; 276 } 277 if (other == null) { 278 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 310 public boolean isField() { 311 return this.binding instanceof FieldBinding; 312 } 313 314 317 public boolean isSynthetic() { 318 if (isField()) { 319 return ((FieldBinding) this.binding).isSynthetic(); 320 } 321 return false; 322 } 323 324 328 public boolean isRecovered() { 329 return false; 330 } 331 332 336 public String toString() { 337 return this.binding.toString(); 338 } 339 } 340 | Popular Tags |