KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 java.util.List JavaDoc;
15
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.JavaModelException;
18 import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
19 import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
20 import org.eclipse.jdt.internal.compiler.util.Util;
21 import org.eclipse.jdt.internal.core.CompilationUnit;
22 import org.eclipse.jdt.internal.core.PackageFragment;
23
24 /**
25  * This class represents the recovered binding for a type
26  */

27 class RecoveredTypeBinding implements ITypeBinding {
28
29     private VariableDeclaration variableDeclaration;
30     private Type currentType;
31     private BindingResolver resolver;
32     private int dimensions;
33     private RecoveredTypeBinding innerTypeBinding;
34     private ITypeBinding[] typeArguments;
35     private org.eclipse.jdt.internal.compiler.lookup.TypeBinding referenceBinding;
36
37     RecoveredTypeBinding(BindingResolver resolver, VariableDeclaration variableDeclaration) {
38         this.variableDeclaration = variableDeclaration;
39         this.resolver = resolver;
40         this.currentType = getType();
41         this.dimensions = variableDeclaration.getExtraDimensions();
42         if (this.currentType.isArrayType()) {
43             this.dimensions += ((ArrayType) this.currentType).getDimensions();
44         }
45     }
46
47     RecoveredTypeBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.TypeBinding referenceBinding) {
48         this.resolver = resolver;
49         this.dimensions = referenceBinding.dimensions();
50         this.referenceBinding = referenceBinding;
51     }
52
53     RecoveredTypeBinding(BindingResolver resolver, Type type) {
54         this.currentType = type;
55         this.resolver = resolver;
56         this.dimensions = 0;
57         if (type.isArrayType()) {
58             this.dimensions += ((ArrayType) type).getDimensions();
59         }
60     }
61
62     RecoveredTypeBinding(BindingResolver resolver, RecoveredTypeBinding typeBinding, int dimensions) {
63         this.innerTypeBinding = typeBinding;
64         this.dimensions = typeBinding.getDimensions() + dimensions;
65         this.resolver = resolver;
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.jdt.core.dom.ITypeBinding#createArrayType(int)
70      */

71     public ITypeBinding createArrayType(int dims) {
72         return this.resolver.getTypeBinding(this, dims);
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.jdt.core.dom.ITypeBinding#getBinaryName()
77      */

78     public String JavaDoc getBinaryName() {
79         return null;
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.jdt.core.dom.ITypeBinding#getBound()
84      */

85     public ITypeBinding getBound() {
86         return null;
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.jdt.core.dom.ITypeBinding#getComponentType()
91      */

92     public ITypeBinding getComponentType() {
93         if (this.dimensions == 0) return null;
94         return this.resolver.getTypeBinding(this, -1);
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredFields()
99      */

100     public IVariableBinding[] getDeclaredFields() {
101         return TypeBinding.NO_VARIABLE_BINDINGS;
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredMethods()
106      */

107     public IMethodBinding[] getDeclaredMethods() {
108         return TypeBinding.NO_METHOD_BINDINGS;
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredModifiers()
113      */

114     public int getDeclaredModifiers() {
115         return 0;
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDeclaredTypes()
120      */

121     public ITypeBinding[] getDeclaredTypes() {
122         return TypeBinding.NO_TYPE_BINDINGS;
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDeclaringClass()
127      */

128     public ITypeBinding getDeclaringClass() {
129         return null;
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDeclaringMethod()
134      */

135     public IMethodBinding getDeclaringMethod() {
136         return null;
137     }
138
139     /* (non-Javadoc)
140      * @see org.eclipse.jdt.core.dom.ITypeBinding#getDimensions()
141      */

142     public int getDimensions() {
143         return this.dimensions;
144     }
145
146     /* (non-Javadoc)
147      * @see org.eclipse.jdt.core.dom.ITypeBinding#getElementType()
148      */

149     public ITypeBinding getElementType() {
150         if (this.referenceBinding != null) {
151             if (this.referenceBinding.isArrayType()) {
152                 ArrayBinding arrayBinding = (ArrayBinding) this.referenceBinding;
153                 return new RecoveredTypeBinding(this.resolver, arrayBinding.leafComponentType);
154             } else {
155                 return new RecoveredTypeBinding(this.resolver, this.referenceBinding);
156             }
157         }
158         if (this.innerTypeBinding != null) {
159             return this.innerTypeBinding.getElementType();
160         }
161         if (this.currentType!= null && this.currentType.isArrayType()) {
162             return this.resolver.getTypeBinding(((ArrayType) this.currentType).getElementType());
163         }
164         if (this.variableDeclaration != null && this.variableDeclaration.getExtraDimensions() != 0) {
165             return this.resolver.getTypeBinding(getType());
166         }
167         return null;
168     }
169
170     /* (non-Javadoc)
171      * @see org.eclipse.jdt.core.dom.ITypeBinding#getErasure()
172      */

173     public ITypeBinding getErasure() {
174         return this;
175     }
176
177     /* (non-Javadoc)
178      * @see org.eclipse.jdt.core.dom.ITypeBinding#getInterfaces()
179      */

180     public ITypeBinding[] getInterfaces() {
181         return TypeBinding.NO_TYPE_BINDINGS;
182     }
183
184     /* (non-Javadoc)
185      * @see org.eclipse.jdt.core.dom.ITypeBinding#getModifiers()
186      */

187     public int getModifiers() {
188         return Modifier.NONE;
189     }
190
191     /* (non-Javadoc)
192      * @see org.eclipse.jdt.core.dom.ITypeBinding#getName()
193      */

194     public String JavaDoc getName() {
195         char[] brackets = new char[this.dimensions * 2];
196         for (int i = this.dimensions * 2 - 1; i >= 0; i -= 2) {
197             brackets[i] = ']';
198             brackets[i - 1] = '[';
199         }
200         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(this.getInternalName());
201         buffer.append(brackets);
202         return String.valueOf(buffer);
203     }
204
205     private String JavaDoc getInternalName() {
206         if (this.innerTypeBinding != null) {
207             return this.innerTypeBinding.getInternalName();
208         } else if (this.referenceBinding != null) {
209             org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding typeBinding = null;
210             if (this.referenceBinding.isArrayType()) {
211                 ArrayBinding arrayBinding = (ArrayBinding) this.referenceBinding;
212                 if (arrayBinding.leafComponentType instanceof org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) {
213                     typeBinding = (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) arrayBinding.leafComponentType;
214                 }
215             } else if (this.referenceBinding instanceof org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) {
216                 typeBinding = (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) this.referenceBinding;
217             }
218             return new String JavaDoc(typeBinding.compoundName[typeBinding.compoundName.length - 1]);
219         }
220         return this.getTypeNameFrom(getType());
221     }
222
223     /* (non-Javadoc)
224      * @see org.eclipse.jdt.core.dom.ITypeBinding#getPackage()
225      */

226     public IPackageBinding getPackage() {
227         CompilationUnitScope scope = this.resolver.scope();
228         if (scope != null) {
229             return this.resolver.getPackageBinding(scope.getCurrentPackage());
230         }
231         return null;
232     }
233
234     /* (non-Javadoc)
235      * @see org.eclipse.jdt.core.dom.ITypeBinding#getQualifiedName()
236      */

237     public String JavaDoc getQualifiedName() {
238         return this.getName();
239     }
240
241     /* (non-Javadoc)
242      * @see org.eclipse.jdt.core.dom.ITypeBinding#getSuperclass()
243      */

244     public ITypeBinding getSuperclass() {
245         return this.resolver.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
246
}
247
248     /* (non-Javadoc)
249      * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeArguments()
250      */

251     public ITypeBinding[] getTypeArguments() {
252         if (this.referenceBinding != null) {
253             return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS;
254         }
255         if (this.typeArguments != null) {
256             return typeArguments;
257         }
258
259         if (this.innerTypeBinding != null) {
260             return this.innerTypeBinding.getTypeArguments();
261         }
262
263         if (this.currentType.isParameterizedType()) {
264             ParameterizedType parameterizedType = (ParameterizedType) this.currentType;
265             List JavaDoc typeArgumentsList = parameterizedType.typeArguments();
266             int size = typeArgumentsList.size();
267             ITypeBinding[] temp = new ITypeBinding[size];
268             for (int i = 0; i < size; i++) {
269                 ITypeBinding currentTypeBinding = ((Type) typeArgumentsList.get(i)).resolveBinding();
270                 if (currentTypeBinding == null) {
271                     return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS;
272                 }
273                 temp[i] = currentTypeBinding;
274             }
275             return this.typeArguments = temp;
276         }
277         return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS;
278     }
279
280     /* (non-Javadoc)
281      * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeBounds()
282      */

283     public ITypeBinding[] getTypeBounds() {
284         return TypeBinding.NO_TYPE_BINDINGS;
285     }
286
287     /* (non-Javadoc)
288      * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeDeclaration()
289      */

290     public ITypeBinding getTypeDeclaration() {
291         return this;
292     }
293
294     /* (non-Javadoc)
295      * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeParameters()
296      */

297     public ITypeBinding[] getTypeParameters() {
298         return TypeBinding.NO_TYPE_BINDINGS;
299     }
300
301     /* (non-Javadoc)
302      * @see org.eclipse.jdt.core.dom.ITypeBinding#getWildcard()
303      */

304     public ITypeBinding getWildcard() {
305         return null;
306     }
307
308     /* (non-Javadoc)
309      * @see org.eclipse.jdt.core.dom.ITypeBinding#isAnnotation()
310      */

311     public boolean isAnnotation() {
312         return false;
313     }
314
315     /* (non-Javadoc)
316      * @see org.eclipse.jdt.core.dom.ITypeBinding#isAnonymous()
317      */

318     public boolean isAnonymous() {
319         return false;
320     }
321
322     /* (non-Javadoc)
323      * @see org.eclipse.jdt.core.dom.ITypeBinding#isArray()
324      */

325     public boolean isArray() {
326         return false;
327     }
328
329     /* (non-Javadoc)
330      * @see org.eclipse.jdt.core.dom.ITypeBinding#isAssignmentCompatible(org.eclipse.jdt.core.dom.ITypeBinding)
331      */

332     public boolean isAssignmentCompatible(ITypeBinding typeBinding) {
333         if ("java.lang.Object".equals(typeBinding.getQualifiedName())) { //$NON-NLS-1$
334
return true;
335         }
336         // since recovered binding are not unique isEqualTo is required
337
return this.isEqualTo(typeBinding);
338     }
339
340     /* (non-Javadoc)
341      * @see org.eclipse.jdt.core.dom.ITypeBinding#isCapture()
342      */

343     public boolean isCapture() {
344         return false;
345     }
346
347     /* (non-Javadoc)
348      * @see org.eclipse.jdt.core.dom.ITypeBinding#isCastCompatible(org.eclipse.jdt.core.dom.ITypeBinding)
349      */

350     public boolean isCastCompatible(ITypeBinding typeBinding) {
351         if ("java.lang.Object".equals(typeBinding.getQualifiedName())) { //$NON-NLS-1$
352
return true;
353         }
354         // since recovered binding are not unique isEqualTo is required
355
return this.isEqualTo(typeBinding);
356     }
357
358     /* (non-Javadoc)
359      * @see org.eclipse.jdt.core.dom.ITypeBinding#isClass()
360      */

361     public boolean isClass() {
362         return true;
363     }
364
365     /* (non-Javadoc)
366      * @see org.eclipse.jdt.core.dom.ITypeBinding#isEnum()
367      */

368     public boolean isEnum() {
369         return false;
370     }
371
372     /* (non-Javadoc)
373      * @see org.eclipse.jdt.core.dom.ITypeBinding#isFromSource()
374      */

375     public boolean isFromSource() {
376         return false;
377     }
378
379     /* (non-Javadoc)
380      * @see org.eclipse.jdt.core.dom.ITypeBinding#isGenericType()
381      */

382     public boolean isGenericType() {
383         return false;
384     }
385
386     /* (non-Javadoc)
387      * @see org.eclipse.jdt.core.dom.ITypeBinding#isInterface()
388      */

389     public boolean isInterface() {
390         return false;
391     }
392
393     /* (non-Javadoc)
394      * @see org.eclipse.jdt.core.dom.ITypeBinding#isLocal()
395      */

396     public boolean isLocal() {
397         return false;
398     }
399
400     /* (non-Javadoc)
401      * @see org.eclipse.jdt.core.dom.ITypeBinding#isMember()
402      */

403     public boolean isMember() {
404         return false;
405     }
406
407     /* (non-Javadoc)
408      * @see org.eclipse.jdt.core.dom.ITypeBinding#isNested()
409      */

410     public boolean isNested() {
411         return false;
412     }
413
414     /* (non-Javadoc)
415      * @see org.eclipse.jdt.core.dom.ITypeBinding#isNullType()
416      */

417     public boolean isNullType() {
418         return false;
419     }
420
421     /* (non-Javadoc)
422      * @see org.eclipse.jdt.core.dom.ITypeBinding#isParameterizedType()
423      */

424     public boolean isParameterizedType() {
425         if (this.innerTypeBinding != null) {
426             return this.innerTypeBinding.isParameterizedType();
427         }
428         if (this.currentType != null) {
429             return this.currentType.isParameterizedType();
430         }
431         return false;
432     }
433
434     /* (non-Javadoc)
435      * @see org.eclipse.jdt.core.dom.ITypeBinding#isPrimitive()
436      */

437     public boolean isPrimitive() {
438         return false;
439     }
440
441     /* (non-Javadoc)
442      * @see org.eclipse.jdt.core.dom.ITypeBinding#isRawType()
443      */

444     public boolean isRawType() {
445         return false;
446     }
447
448     /* (non-Javadoc)
449      * @see org.eclipse.jdt.core.dom.ITypeBinding#isSubTypeCompatible(org.eclipse.jdt.core.dom.ITypeBinding)
450      */

451     public boolean isSubTypeCompatible(ITypeBinding typeBinding) {
452         if ("java.lang.Object".equals(typeBinding.getQualifiedName())) { //$NON-NLS-1$
453
return true;
454         }
455         // since recovered binding are not unique isEqualTo is required
456
return this.isEqualTo(typeBinding);
457     }
458
459     /* (non-Javadoc)
460      * @see org.eclipse.jdt.core.dom.ITypeBinding#isTopLevel()
461      */

462     public boolean isTopLevel() {
463         return true;
464     }
465
466     /* (non-Javadoc)
467      * @see org.eclipse.jdt.core.dom.ITypeBinding#isTypeVariable()
468      */

469     public boolean isTypeVariable() {
470         return false;
471     }
472
473     /* (non-Javadoc)
474      * @see org.eclipse.jdt.core.dom.ITypeBinding#isUpperbound()
475      */

476     public boolean isUpperbound() {
477         return false;
478     }
479
480     /* (non-Javadoc)
481      * @see org.eclipse.jdt.core.dom.ITypeBinding#isWildcardType()
482      */

483     public boolean isWildcardType() {
484         return false;
485     }
486
487     /* (non-Javadoc)
488      * @see org.eclipse.jdt.core.dom.IBinding#getAnnotations()
489      */

490     public IAnnotationBinding[] getAnnotations() {
491         return AnnotationBinding.NoAnnotations;
492     }
493
494     /* (non-Javadoc)
495      * @see org.eclipse.jdt.core.dom.IBinding#getJavaElement()
496      */

497     public IJavaElement getJavaElement() {
498         try {
499             IPackageBinding packageBinding = getPackage();
500             if (packageBinding != null) {
501                 final IJavaElement javaElement = packageBinding.getJavaElement();
502                 if (javaElement!= null && javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
503                     return new CompilationUnit((PackageFragment) javaElement, this.getInternalName(), this.resolver.getWorkingCopyOwner()).getWorkingCopy(this.resolver.getWorkingCopyOwner(), null);
504                 }
505             }
506         } catch (JavaModelException e) {
507             //ignore
508
}
509         return null;
510     }
511
512     /* (non-Javadoc)
513      * @see org.eclipse.jdt.core.dom.IBinding#getKey()
514      */

515     public String JavaDoc getKey() {
516         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
517         buffer.append("Recovered#"); //$NON-NLS-1$
518
if (this.innerTypeBinding != null) {
519             buffer.append("innerTypeBinding") //$NON-NLS-1$
520
.append(this.innerTypeBinding.getKey());
521         } else if (this.currentType != null) {
522             buffer.append("currentType") //$NON-NLS-1$
523
.append(this.currentType.toString());
524         } else if (this.referenceBinding != null) {
525             buffer.append("referenceBinding") //$NON-NLS-1$
526
.append(this.referenceBinding.computeUniqueKey());
527         } else if (variableDeclaration != null) {
528             buffer
529                 .append("variableDeclaration") //$NON-NLS-1$
530
.append(this.variableDeclaration.getClass())
531                 .append(this.variableDeclaration.getName().getIdentifier())
532                 .append(this.variableDeclaration.getExtraDimensions());
533         }
534         buffer.append(this.getDimensions());
535         if (this.typeArguments != null) {
536             buffer.append('<');
537             for (int i = 0, max = this.typeArguments.length; i < max; i++) {
538                 if (i != 0) {
539                     buffer.append(',');
540                 }
541                 buffer.append(this.typeArguments[i].getKey());
542             }
543             buffer.append('>');
544         }
545         return String.valueOf(buffer);
546     }
547
548     /* (non-Javadoc)
549      * @see org.eclipse.jdt.core.dom.IBinding#getKind()
550      */

551     public int getKind() {
552         return IBinding.TYPE;
553     }
554
555     /* (non-Javadoc)
556      * @see org.eclipse.jdt.core.dom.IBinding#isDeprecated()
557      */

558     public boolean isDeprecated() {
559         return false;
560     }
561
562     /* (non-Javadoc)
563      * @see org.eclipse.jdt.core.dom.IBinding#isEqualTo(org.eclipse.jdt.core.dom.IBinding)
564      */

565     public boolean isEqualTo(IBinding other) {
566         if (!other.isRecovered() || other.getKind() != IBinding.TYPE) return false;
567         return this.getKey().equals(other.getKey());
568     }
569
570     /* (non-Javadoc)
571      * @see org.eclipse.jdt.core.dom.IBinding#isRecovered()
572      */

573     public boolean isRecovered() {
574         return true;
575     }
576
577     /* (non-Javadoc)
578      * @see org.eclipse.jdt.core.dom.IBinding#isSynthetic()
579      */

580     public boolean isSynthetic() {
581         return false;
582     }
583
584     private String JavaDoc getTypeNameFrom(Type type) {
585         if (type == null) return Util.EMPTY_STRING;
586         switch(type.getNodeType0()) {
587             case ASTNode.ARRAY_TYPE :
588                 ArrayType arrayType = (ArrayType) type;
589                 type = arrayType.getElementType();
590                 return getTypeNameFrom(type);
591             case ASTNode.PARAMETERIZED_TYPE :
592                 ParameterizedType parameterizedType = (ParameterizedType) type;
593                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(getTypeNameFrom(parameterizedType.getType()));
594                 ITypeBinding[] tArguments = getTypeArguments();
595                 final int typeArgumentsLength = tArguments.length;
596                 if (typeArgumentsLength != 0) {
597                     buffer.append('<');
598                     for (int i = 0; i < typeArgumentsLength; i++) {
599                         if (i > 0) {
600                             buffer.append(',');
601                         }
602                         buffer.append(tArguments[i].getName());
603                     }
604                     buffer.append('>');
605                 }
606                 return String.valueOf(buffer);
607             case ASTNode.PRIMITIVE_TYPE :
608                 PrimitiveType primitiveType = (PrimitiveType) type;
609                 return primitiveType.getPrimitiveTypeCode().toString();
610             case ASTNode.QUALIFIED_TYPE :
611                 QualifiedType qualifiedType = (QualifiedType) type;
612                 return qualifiedType.getName().getIdentifier();
613             case ASTNode.SIMPLE_TYPE :
614                 SimpleType simpleType = (SimpleType) type;
615                 Name name = simpleType.getName();
616                 if (name.isQualifiedName()) {
617                     QualifiedName qualifiedName = (QualifiedName) name;
618                     return qualifiedName.getName().getIdentifier();
619                 }
620                 return ((SimpleName) name).getIdentifier();
621         }
622         return Util.EMPTY_STRING;
623     }
624
625     private Type getType() {
626         if (this.currentType != null) {
627             return this.currentType;
628         }
629         if (this.variableDeclaration == null) return null;
630         switch(this.variableDeclaration.getNodeType()) {
631             case ASTNode.SINGLE_VARIABLE_DECLARATION :
632                 SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) this.variableDeclaration;
633                 return singleVariableDeclaration.getType();
634             default :
635                 // this is a variable declaration fragment
636
ASTNode parent = this.variableDeclaration.getParent();
637                 switch(parent.getNodeType()) {
638                     case ASTNode.VARIABLE_DECLARATION_EXPRESSION :
639                         VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) parent;
640                         return variableDeclarationExpression.getType();
641                     case ASTNode.VARIABLE_DECLARATION_STATEMENT :
642                         VariableDeclarationStatement statement = (VariableDeclarationStatement) parent;
643                         return statement.getType();
644                     case ASTNode.FIELD_DECLARATION :
645                         FieldDeclaration fieldDeclaration = (FieldDeclaration) parent;
646                         return fieldDeclaration.getType();
647                 }
648         }
649         return null; // should not happen
650
}
651 }
652
Popular Tags