6d2ef2860443070d446b7aedd04bac123e8fe692
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / TypeBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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 net.sourceforge.phpdt.core.dom;
13
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.compiler.CharOperation;
16 import net.sourceforge.phpdt.internal.compiler.ast.Expression;
17 import net.sourceforge.phpdt.internal.compiler.ast.Wildcard;
18 import net.sourceforge.phpdt.internal.compiler.classfmt.ClassFileConstants;
19 import net.sourceforge.phpdt.internal.compiler.lookup.ArrayBinding;
20 import net.sourceforge.phpdt.internal.compiler.lookup.BaseTypeBinding;
21 import net.sourceforge.phpdt.internal.compiler.lookup.Binding;
22 import net.sourceforge.phpdt.internal.compiler.lookup.CaptureBinding;
23 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
24 import net.sourceforge.phpdt.internal.compiler.lookup.LocalTypeBinding;
25 import net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding;
26 import net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding;
27 import net.sourceforge.phpdt.internal.compiler.lookup.ParameterizedTypeBinding;
28 import net.sourceforge.phpdt.internal.compiler.lookup.RawTypeBinding;
29 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
30 import net.sourceforge.phpdt.internal.compiler.lookup.Scope;
31 import net.sourceforge.phpdt.internal.compiler.lookup.TagBits;
32 import net.sourceforge.phpdt.internal.compiler.lookup.TypeConstants;
33 import net.sourceforge.phpdt.internal.compiler.lookup.TypeVariableBinding;
34 import net.sourceforge.phpdt.internal.compiler.lookup.WildcardBinding;
35 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
36 import net.sourceforge.phpdt.internal.compiler.util.SuffixConstants;
37 import net.sourceforge.phpdt.internal.core.JavaElement;
38 import net.sourceforge.phpdt.internal.core.PackageFragment;
39
40 /**
41  * Internal implementation of type bindings.
42  */
43 class TypeBinding implements ITypeBinding {
44         protected static final IMethodBinding[] NO_METHOD_BINDINGS = new IMethodBinding[0];
45
46         private static final String NO_NAME = ""; //$NON-NLS-1$
47         protected static final ITypeBinding[] NO_TYPE_BINDINGS = new ITypeBinding[0];
48         protected static final IVariableBinding[] NO_VARIABLE_BINDINGS = new IVariableBinding[0];
49
50         private static final int VALID_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
51                 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.STRICTFP;
52
53         net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding binding;
54         private String key;
55         private BindingResolver resolver;
56         private IVariableBinding[] fields;
57         private IAnnotationBinding[] annotations;
58         private IMethodBinding[] methods;
59         private ITypeBinding[] members;
60         private ITypeBinding[] interfaces;
61         private ITypeBinding[] typeArguments;
62         private ITypeBinding[] bounds;
63         private ITypeBinding[] typeParameters;
64
65         public TypeBinding(BindingResolver resolver, net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding binding) {
66                 this.binding = binding;
67                 this.resolver = resolver;
68         }
69
70         public ITypeBinding createArrayType(int dimension) {
71                 int realDimensions = dimension;
72                 realDimensions += this.getDimensions();
73                 if (realDimensions < 1 || realDimensions > 255) {
74                         throw new IllegalArgumentException();
75                 }
76                 return this.resolver.resolveArrayType(this, dimension);
77         }
78
79         public IAnnotationBinding[] getAnnotations() {
80                 if (this.annotations != null) {
81                         return this.annotations;
82                 }
83                 if (this.binding.isAnnotationType() || this.binding.isClass() || this.binding.isEnum() || this.binding.isInterface()) {
84                         net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding refType =
85                                 (net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding) this.binding;
86                         net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding[] internalAnnotations = refType.getAnnotations();
87                         int length = internalAnnotations == null ? 0 : internalAnnotations.length;
88                         if (length != 0) {
89                                 IAnnotationBinding[] tempAnnotations = new IAnnotationBinding[length];
90                                 int convertedAnnotationCount = 0;
91                                 for (int i = 0; i < length; i++) {
92                                         net.sourceforge.phpdt.internal.compiler.lookup.AnnotationBinding internalAnnotation = internalAnnotations[i];
93                                         IAnnotationBinding annotationInstance = this.resolver.getAnnotationInstance(internalAnnotation);
94                                         if (annotationInstance == null) {
95                                                 continue;
96                                         }
97                                         tempAnnotations[convertedAnnotationCount++] = annotationInstance;
98                                 }
99                                 if (convertedAnnotationCount != length) {
100                                         if (convertedAnnotationCount == 0) {
101                                                 return this.annotations = AnnotationBinding.NoAnnotations;
102                                         }
103                                         System.arraycopy(tempAnnotations, 0, (tempAnnotations = new IAnnotationBinding[convertedAnnotationCount]), 0, convertedAnnotationCount);
104                                 }
105                                 return this.annotations = tempAnnotations;
106                         }
107                 }
108                 return this.annotations = AnnotationBinding.NoAnnotations;
109         }
110
111         /*
112          * @see ITypeBinding#getBinaryName()
113          * @since 3.0
114          */
115         public String getBinaryName() {
116                 if (this.binding.isCapture()) {
117                         return null; // no binary name for capture binding
118                 } else if (this.binding.isTypeVariable()) {
119                         TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
120                         net.sourceforge.phpdt.internal.compiler.lookup.Binding declaring = typeVariableBinding.declaringElement;
121                         StringBuffer binaryName = new StringBuffer();
122                         switch(declaring.kind()) {
123                                 case net.sourceforge.phpdt.internal.compiler.lookup.Binding.METHOD :
124                                         MethodBinding methodBinding = (MethodBinding) declaring;
125                                         char[] constantPoolName = methodBinding.declaringClass.constantPoolName();
126                                         if (constantPoolName == null) return null;
127                                         binaryName
128                                                 .append(CharOperation.replaceOnCopy(constantPoolName, '/', '.'))
129                                                 .append('$')
130                                                 .append(methodBinding.signature())
131                                                 .append('$')
132                                                 .append(typeVariableBinding.sourceName);
133                                         break;
134                                 default :
135                                         net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding = (net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) declaring;
136                                         constantPoolName = typeBinding.constantPoolName();
137                                         if (constantPoolName == null) return null;
138                                         binaryName
139                                                 .append(CharOperation.replaceOnCopy(constantPoolName, '/', '.'))
140                                                 .append('$')
141                                                 .append(typeVariableBinding.sourceName);
142                         }
143                         return String.valueOf(binaryName);
144                 }
145                 char[] constantPoolName = this.binding.constantPoolName();
146                 if (constantPoolName == null) return null;
147                 char[] dotSeparated = CharOperation.replaceOnCopy(constantPoolName, '/', '.');
148                 return new String(dotSeparated);
149         }
150
151         /* (non-Javadoc)
152          * @see org.eclipse.jdt.core.dom.ITypeBinding#getBound()
153          */
154         public ITypeBinding getBound() {
155                 switch (this.binding.kind()) {
156                         case Binding.WILDCARD_TYPE :
157                         case Binding.INTERSECTION_TYPE :
158                                 WildcardBinding wildcardBinding = (WildcardBinding) this.binding;
159                                 if (wildcardBinding.bound != null) {
160                                         return this.resolver.getTypeBinding(wildcardBinding.bound);
161                                 }
162                                 break;
163                 }
164                 return null;
165         }
166
167         /*
168          * @see ITypeBinding#getComponentType()
169          */
170         public ITypeBinding getComponentType() {
171                 if (!this.isArray()) {
172                         return null;
173                 }
174                 ArrayBinding arrayBinding = (ArrayBinding) binding;
175                 return resolver.getTypeBinding(arrayBinding.elementsType());
176         }
177
178         /*
179          * @see ITypeBinding#getDeclaredFields()
180          */
181         public synchronized IVariableBinding[] getDeclaredFields() {
182                 if (this.fields != null) {
183                         return this.fields;
184                 }
185                 try {
186                         if (isClass() || isInterface() || isEnum()) {
187                                 ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
188                                 FieldBinding[] fieldBindings = referenceBinding.availableFields(); // resilience
189                                 int length = fieldBindings.length;
190                                 if (length != 0) {
191                                         int convertedFieldCount = 0;
192                                         IVariableBinding[] newFields = new IVariableBinding[length];
193                                         for (int i = 0; i < length; i++) {
194                                                 FieldBinding fieldBinding = fieldBindings[i];
195                                                 IVariableBinding variableBinding = this.resolver.getVariableBinding(fieldBinding);
196                                                 if (variableBinding == null) {
197                                                         return this.fields = NO_VARIABLE_BINDINGS;
198                                                 }
199                                                 newFields[convertedFieldCount++] = variableBinding;
200                                         }
201
202                                         if (convertedFieldCount != length) {
203                                                 if (convertedFieldCount == 0) {
204                                                         return this.fields = NO_VARIABLE_BINDINGS;
205                                                 }                                               
206                                                 System.arraycopy(newFields, 0, (newFields = new IVariableBinding[convertedFieldCount]), 0, convertedFieldCount);
207                                         }                                       
208                                         return this.fields = newFields;
209                                 }
210                         }
211                 } catch (RuntimeException e) {
212                         /* in case a method cannot be resolvable due to missing jars on the classpath
213                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
214                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
215                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
216                          */
217                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declared fields"); //$NON-NLS-1$
218                 }
219                 return this.fields = NO_VARIABLE_BINDINGS;
220         }
221
222         /*
223          * @see ITypeBinding#getDeclaredMethods()
224          */
225         public synchronized IMethodBinding[] getDeclaredMethods() {
226                 if (this.methods != null) {
227                         return this.methods;
228                 }
229                 try {
230                         if (isClass() || isInterface() || isEnum()) {
231                                 ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
232                                 net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding[] internalMethods = referenceBinding.availableMethods(); // be resilient
233                                 int length = internalMethods.length;
234                                 if (length != 0) {
235                                         int convertedMethodCount = 0;
236                                         IMethodBinding[] newMethods = new IMethodBinding[length];
237                                         for (int i = 0; i < length; i++) {
238                                                 net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding methodBinding = internalMethods[i];
239                                                 if (methodBinding.isDefaultAbstract() || methodBinding.isSynthetic() || (methodBinding.isConstructor() && isInterface())) {
240                                                         continue;
241                                                 }
242                                                 IMethodBinding methodBinding2 = this.resolver.getMethodBinding(methodBinding);
243                                                 if (methodBinding2 != null) {
244                                                         newMethods[convertedMethodCount++] = methodBinding2;
245                                                 }
246                                         }
247                                         if (convertedMethodCount != length) {
248                                                 if (convertedMethodCount == 0) {
249                                                         return this.methods = NO_METHOD_BINDINGS;
250                                                 }
251                                                 System.arraycopy(newMethods, 0, (newMethods = new IMethodBinding[convertedMethodCount]), 0, convertedMethodCount);
252                                         }
253                                         return this.methods = newMethods;
254                                 }
255                         }
256                 } catch (RuntimeException e) {
257                         /* in case a method cannot be resolvable due to missing jars on the classpath
258                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
259                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
260                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
261                          */
262                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declared methods"); //$NON-NLS-1$
263                 }
264                 return this.methods = NO_METHOD_BINDINGS;
265         }
266
267         /*
268          * @see ITypeBinding#getDeclaredModifiers()
269          */
270         public int getDeclaredModifiers() {
271                 return getModifiers();
272         }
273
274         /*
275          * @see ITypeBinding#getDeclaredTypes()
276          */
277         public synchronized ITypeBinding[] getDeclaredTypes() {
278                 if (this.members != null) {
279                         return this.members;
280                 }
281                 try {
282                         if (isClass() || isInterface() || isEnum()) {
283                                 ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
284                                 ReferenceBinding[] internalMembers = referenceBinding.memberTypes();
285                                 int length = internalMembers.length;
286                                 if (length != 0) {
287                                         ITypeBinding[] newMembers = new ITypeBinding[length];
288                                         for (int i = 0; i < length; i++) {
289                                                 ITypeBinding typeBinding = this.resolver.getTypeBinding(internalMembers[i]);
290                                                 if (typeBinding == null) {
291                                                         return this.members = NO_TYPE_BINDINGS;
292                                                 }
293                                                 newMembers[i] = typeBinding;
294                                         }
295                                         return this.members = newMembers;
296                                 }
297                         }
298                 } catch (RuntimeException e) {
299                         /* in case a method cannot be resolvable due to missing jars on the classpath
300                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
301                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
302                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
303                          */
304                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declared methods"); //$NON-NLS-1$
305                 }
306                 return this.members = NO_TYPE_BINDINGS;
307         }
308
309         /*
310          * @see ITypeBinding#getDeclaringMethod()
311          */
312         public synchronized IMethodBinding getDeclaringMethod() {
313                 if (this.binding instanceof LocalTypeBinding) {
314                         LocalTypeBinding localTypeBinding = (LocalTypeBinding) this.binding;
315                         MethodBinding methodBinding = localTypeBinding.enclosingMethod;
316                         if (methodBinding != null) {
317                                 try {
318                                         return this.resolver.getMethodBinding(localTypeBinding.enclosingMethod);
319                                 } catch (RuntimeException e) {
320                                         /* in case a method cannot be resolvable due to missing jars on the classpath
321                                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
322                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
323                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
324                                          */
325                                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declaring method"); //$NON-NLS-1$
326                                 }
327                         }
328                 } else if (this.binding.isTypeVariable()) {
329                         TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
330                         Binding declaringElement = typeVariableBinding.declaringElement;
331                         if (declaringElement instanceof MethodBinding) {
332                                 try {
333                                         return this.resolver.getMethodBinding((MethodBinding)declaringElement);
334                                 } catch (RuntimeException e) {
335                                         /* in case a method cannot be resolvable due to missing jars on the classpath
336                                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
337                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
338                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
339                                          */
340                                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declaring method"); //$NON-NLS-1$
341                                 }
342                         }
343                 }
344                 return null;
345         }
346
347         /*
348          * @see ITypeBinding#getDeclaringClass()
349          */
350         public synchronized ITypeBinding getDeclaringClass() {
351                 if (isClass() || isInterface() || isEnum()) {
352                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
353                         if (referenceBinding.isNestedType()) {
354                                 try {
355                                         return this.resolver.getTypeBinding(referenceBinding.enclosingType());
356                                 } catch (RuntimeException e) {
357                                         /* in case a method cannot be resolvable due to missing jars on the classpath
358                                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
359                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
360                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
361                                          */
362                                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declaring class"); //$NON-NLS-1$
363                                 }
364                         }
365                 } else if (this.binding.isTypeVariable()) {
366                         TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
367                         Binding declaringElement = typeVariableBinding.isCapture() ? ((CaptureBinding) typeVariableBinding).sourceType : typeVariableBinding.declaringElement;
368                         if (declaringElement instanceof ReferenceBinding) {
369                                 try {
370                                         return this.resolver.getTypeBinding((ReferenceBinding)declaringElement);
371                                 } catch (RuntimeException e) {
372                                         /* in case a method cannot be resolvable due to missing jars on the classpath
373                                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
374                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
375                                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
376                                          */
377                                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve declaring class"); //$NON-NLS-1$
378                                 }
379                         }
380                 }
381                 return null;
382         }
383
384         /*
385          * @see ITypeBinding#getDimensions()
386          */
387         public int getDimensions() {
388                 if (!this.isArray()) {
389                         return 0;
390                 }
391                 ArrayBinding arrayBinding = (ArrayBinding) binding;
392                 return arrayBinding.dimensions;
393         }
394
395         /*
396          * @see ITypeBinding#getElementType()
397          */
398         public ITypeBinding getElementType() {
399                 if (!this.isArray()) {
400                         return null;
401                 }
402                 ArrayBinding arrayBinding = (ArrayBinding) binding;
403                 return resolver.getTypeBinding(arrayBinding.leafComponentType);
404         }
405
406         /* (non-Javadoc)
407          * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeDeclaration()
408          */
409         public ITypeBinding getTypeDeclaration() {
410                 if (this.binding instanceof ParameterizedTypeBinding)
411                         return this.resolver.getTypeBinding(((ParameterizedTypeBinding)this.binding).genericType());
412                 return this;
413         }
414
415         /* (non-Javadoc)
416          * @see org.eclipse.jdt.core.dom.ITypeBinding#getErasure()
417          */
418         public ITypeBinding getErasure() {
419                 return this.resolver.getTypeBinding(this.binding.erasure());
420         }
421
422         public synchronized ITypeBinding[] getInterfaces() {
423                 if (this.interfaces != null) {
424                         return this.interfaces;
425                 }
426                 if (this.binding == null)
427                         return this.interfaces = NO_TYPE_BINDINGS;
428                 switch (this.binding.kind()) {
429                         case Binding.ARRAY_TYPE :
430                         case Binding.BASE_TYPE :
431                                 return this.interfaces = NO_TYPE_BINDINGS;
432                 }
433                 ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
434                 ReferenceBinding[] internalInterfaces = null;
435                 try {
436                         internalInterfaces = referenceBinding.superInterfaces();
437                 } catch (RuntimeException e) {
438                         /* in case a method cannot be resolvable due to missing jars on the classpath
439                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
440                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
441                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
442                          */
443                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve interfaces"); //$NON-NLS-1$
444                 }
445                 int length = internalInterfaces == null ? 0 : internalInterfaces.length;
446                 if (length != 0) {
447                         ITypeBinding[] newInterfaces = new ITypeBinding[length];
448                         int interfacesCounter = 0;
449                         for (int i = 0; i < length; i++) {
450                                 ITypeBinding typeBinding = this.resolver.getTypeBinding(internalInterfaces[i]);
451                                 if (typeBinding == null) {
452                                         continue;
453                                 }
454                                 newInterfaces[interfacesCounter++] = typeBinding;
455                         }
456                         if (length != interfacesCounter) {
457                                 System.arraycopy(newInterfaces, 0, (newInterfaces = new ITypeBinding[interfacesCounter]), 0, interfacesCounter);
458                         }
459                         return this.interfaces = newInterfaces;
460                 }
461                 return this.interfaces = NO_TYPE_BINDINGS;
462         }
463
464         public IJavaElement getJavaElement() {
465                 JavaElement element = getUnresolvedJavaElement();
466                 if (element != null)
467                         return element.resolved(this.binding);
468                 if (isRecovered()) {
469                         IPackageBinding packageBinding = getPackage();
470                         if (packageBinding != null) {
471                                 final IJavaElement javaElement = packageBinding.getJavaElement();
472                                 if (javaElement != null && javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
473                                         // best effort: we don't know if the recovered binding is a binary or source binding, so go with a compilation unit
474                                         return ((PackageFragment) javaElement).getCompilationUnit(new String(this.binding.sourceName()) + SuffixConstants.SUFFIX_STRING_java);
475                                 }
476                         }
477                         return null;
478                 }
479                 return null;
480         }
481
482         private JavaElement getUnresolvedJavaElement() {
483                 return getUnresolvedJavaElement(this.binding);
484         }
485         private JavaElement getUnresolvedJavaElement(net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding ) {
486                 if (this.resolver instanceof DefaultBindingResolver) {
487                         DefaultBindingResolver defaultBindingResolver = (DefaultBindingResolver) this.resolver;
488                         return net.sourceforge.phpdt.internal.core.util.Util.getUnresolvedJavaElement(
489                                         typeBinding,
490                                         defaultBindingResolver.workingCopyOwner,
491                                         defaultBindingResolver.getBindingsToNodesMap());
492                 } else {
493                         return net.sourceforge.phpdt.internal.core.util.Util.getUnresolvedJavaElement(typeBinding, null, null);
494                 }
495         }
496
497         /*
498          * @see IBinding#getKey()
499          */
500         public String getKey() {
501                 if (this.key == null) {
502                         this.key = new String(this.binding.computeUniqueKey());
503                 }
504                 return this.key;
505         }
506
507         /*
508          * @see IBinding#getKind()
509          */
510         public int getKind() {
511                 return IBinding.TYPE;
512         }
513
514         /*
515          * @see IBinding#getModifiers()
516          */
517         public int getModifiers() {
518                 if (isClass()) {
519                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
520                         final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
521                         if (referenceBinding.isAnonymousType()) {
522                                 return accessFlags & ~Modifier.FINAL;
523                         }
524                         return accessFlags;
525                 } else if (isAnnotation()) {
526                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
527                         final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
528                         // clear the AccAbstract, AccAnnotation and the AccInterface bits
529                         return accessFlags & ~(ClassFileConstants.AccAbstract | ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation);
530                 } else if (isInterface()) {
531                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
532                         final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
533                         // clear the AccAbstract and the AccInterface bits
534                         return accessFlags & ~(ClassFileConstants.AccAbstract | ClassFileConstants.AccInterface);
535                 } else if (isEnum()) {
536                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
537                         final int accessFlags = referenceBinding.getAccessFlags() & VALID_MODIFIERS;
538                         // clear the AccEnum bits
539                         return accessFlags & ~ClassFileConstants.AccEnum;
540                 } else {
541                         return Modifier.NONE;
542                 }
543         }
544
545         public String getName() {
546                 StringBuffer buffer;
547                 switch (this.binding.kind()) {
548
549                         case Binding.WILDCARD_TYPE :
550                         case Binding.INTERSECTION_TYPE:
551                                 WildcardBinding wildcardBinding = (WildcardBinding) this.binding;
552                                 buffer = new StringBuffer();
553                                 buffer.append(TypeConstants.WILDCARD_NAME);
554                                 if (wildcardBinding.bound != null) {
555                                         switch(wildcardBinding.boundKind) {
556                                         case Wildcard.SUPER :
557                                                 buffer.append(TypeConstants.WILDCARD_SUPER);
558                                             break;
559                                         case Wildcard.EXTENDS :
560                                                 buffer.append(TypeConstants.WILDCARD_EXTENDS);
561                                         }
562                                         buffer.append(getBound().getName());
563                                 }
564                                 return String.valueOf(buffer);
565
566                         case Binding.TYPE_PARAMETER :
567                                 if (isCapture()) {
568                                         return NO_NAME;
569                                 }
570                                 TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
571                                 return new String(typeVariableBinding.sourceName);
572
573                         case Binding.PARAMETERIZED_TYPE :
574                                 ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) this.binding;
575                                 buffer = new StringBuffer();
576                                 buffer.append(parameterizedTypeBinding.sourceName());
577                                 ITypeBinding[] tArguments = getTypeArguments();
578                                 final int typeArgumentsLength = tArguments.length;
579                                 if (typeArgumentsLength != 0) {
580                                         buffer.append('<');
581                                         for (int i = 0; i < typeArgumentsLength; i++) {
582                                                 if (i > 0) {
583                                                         buffer.append(',');
584                                                 }
585                                                 buffer.append(tArguments[i].getName());
586                                         }
587                                         buffer.append('>');
588                                 }
589                                 return String.valueOf(buffer);
590
591                         case Binding.RAW_TYPE :
592                                 return getTypeDeclaration().getName();
593
594                         case Binding.ARRAY_TYPE :
595                                 ITypeBinding elementType = getElementType();
596                                 if (elementType.isLocal() || elementType.isAnonymous() || elementType.isCapture()) {
597                                         return NO_NAME;
598                                 }
599                                 int dimensions = getDimensions();
600                                 char[] brackets = new char[dimensions * 2];
601                                 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
602                                         brackets[i] = ']';
603                                         brackets[i - 1] = '[';
604                                 }
605                                 buffer = new StringBuffer(elementType.getName());
606                                 buffer.append(brackets);
607                                 return String.valueOf(buffer);
608
609                         default :
610                                 if (isPrimitive() || isNullType()) {
611                                         BaseTypeBinding baseTypeBinding = (BaseTypeBinding) this.binding;
612                                         return new String(baseTypeBinding.simpleName);
613                                 }
614                                 if (isAnonymous()) {
615                                         return NO_NAME;
616                                 }
617                                 return new String(this.binding.sourceName());
618                 }
619         }
620
621         /*
622          * @see ITypeBinding#getPackage()
623          */
624         public IPackageBinding getPackage() {
625                 switch (this.binding.kind()) {
626                         case Binding.BASE_TYPE :
627                         case Binding.ARRAY_TYPE :
628                         case Binding.TYPE_PARAMETER : // includes capture scenario
629                         case Binding.WILDCARD_TYPE :
630                         case Binding.INTERSECTION_TYPE:
631                                 return null;
632                 }
633                 ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
634                 return this.resolver.getPackageBinding(referenceBinding.getPackage());
635         }
636
637         /**
638          * @see org.eclipse.jdt.core.dom.ITypeBinding#getQualifiedName()
639          */
640         public String getQualifiedName() {
641                 StringBuffer buffer;
642                 switch (this.binding.kind()) {
643
644                         case Binding.WILDCARD_TYPE :
645                         case Binding.INTERSECTION_TYPE:
646                                 WildcardBinding wildcardBinding = (WildcardBinding) this.binding;
647                                 buffer = new StringBuffer();
648                                 buffer.append(TypeConstants.WILDCARD_NAME);
649                                 final ITypeBinding bound = getBound();
650                                 if (bound != null) {
651                                         switch(wildcardBinding.boundKind) {
652                                                 case Wildcard.SUPER :
653                                                         buffer.append(TypeConstants.WILDCARD_SUPER);
654                                                         break;
655                                                 case Wildcard.EXTENDS :
656                                                         buffer.append(TypeConstants.WILDCARD_EXTENDS);
657                                         }
658                                         buffer.append(bound.getQualifiedName());
659                                 }
660                                 return String.valueOf(buffer);
661
662                         case Binding.RAW_TYPE :
663                                 return getTypeDeclaration().getQualifiedName();
664
665                         case Binding.ARRAY_TYPE :
666                                 ITypeBinding elementType = getElementType();
667                                 if (elementType.isLocal() || elementType.isAnonymous() || elementType.isCapture()) {
668                                         return elementType.getQualifiedName();
669                                 }
670                                 final int dimensions = getDimensions();
671                                 char[] brackets = new char[dimensions * 2];
672                                 for (int i = dimensions * 2 - 1; i >= 0; i -= 2) {
673                                         brackets[i] = ']';
674                                         brackets[i - 1] = '[';
675                                 }
676                                 buffer = new StringBuffer(elementType.getQualifiedName());
677                                 buffer.append(brackets);
678                                 return String.valueOf(buffer);
679
680                         case Binding.TYPE_PARAMETER :
681                                 if (isCapture()) {
682                                         return NO_NAME;
683                                 }
684                                 TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
685                                 return new String(typeVariableBinding.sourceName);
686
687                         case Binding.PARAMETERIZED_TYPE :
688                                 buffer = new StringBuffer();
689                                 if (isMember()) {
690                                         buffer
691                                                 .append(getDeclaringClass().getQualifiedName())
692                                                 .append('.');
693                                         ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) this.binding;
694                                         buffer.append(parameterizedTypeBinding.sourceName());
695                                         ITypeBinding[] tArguments = getTypeArguments();
696                                         final int typeArgumentsLength = tArguments.length;
697                                         if (typeArgumentsLength != 0) {
698                                                 buffer.append('<');
699                                                 for (int i = 0; i < typeArgumentsLength; i++) {
700                                                         if (i > 0) {
701                                                                 buffer.append(',');
702                                                         }
703                                                         buffer.append(tArguments[i].getQualifiedName());
704                                                 }
705                                                 buffer.append('>');
706                                         }
707                                         return String.valueOf(buffer);
708                                 }
709                                 buffer.append(getTypeDeclaration().getQualifiedName());
710                                 ITypeBinding[] tArguments = getTypeArguments();
711                                 final int typeArgumentsLength = tArguments.length;
712                                 if (typeArgumentsLength != 0) {
713                                         buffer.append('<');
714                                         for (int i = 0; i < typeArgumentsLength; i++) {
715                                                 if (i > 0) {
716                                                         buffer.append(',');
717                                                 }
718                                                 buffer.append(tArguments[i].getQualifiedName());
719                                         }
720                                         buffer.append('>');
721                                 }
722                                 return String.valueOf(buffer);
723
724                         default :
725                                 if (isAnonymous() || this.binding.isLocalType()) {
726                                         return NO_NAME;
727                                 }
728                                 if (isPrimitive() || isNullType()) {
729                                         BaseTypeBinding baseTypeBinding = (BaseTypeBinding) this.binding;
730                                         return new String(baseTypeBinding.simpleName);
731                                 }
732                                 if (isMember()) {
733                                         buffer = new StringBuffer();
734                                         buffer
735                                                 .append(getDeclaringClass().getQualifiedName())
736                                                 .append('.');
737                                         buffer.append(getName());
738                                         return String.valueOf(buffer);
739                                 }
740                                 PackageBinding packageBinding = this.binding.getPackage();
741                                 buffer = new StringBuffer();
742                                 if (packageBinding != null && packageBinding.compoundName != CharOperation.NO_CHAR_CHAR) {
743                                         buffer.append(CharOperation.concatWith(packageBinding.compoundName, '.')).append('.');
744                                 }
745                                 buffer.append(getName());
746                                 return String.valueOf(buffer);
747                 }
748         }
749
750         /*
751          * @see ITypeBinding#getSuperclass()
752          */
753         public synchronized ITypeBinding getSuperclass() {
754                 if (this.binding == null)
755                         return null;
756                 switch (this.binding.kind()) {
757                         case Binding.ARRAY_TYPE :
758                         case Binding.BASE_TYPE :
759                                 return null;
760                         default:
761                                 // no superclass for interface types (interface | annotation type)
762                                 if (this.binding.isInterface())
763                                         return null;
764                 }
765                 ReferenceBinding superclass = null;
766                 try {
767                         superclass = ((ReferenceBinding)this.binding).superclass();
768                 } catch (RuntimeException e) {
769                         /* in case a method cannot be resolvable due to missing jars on the classpath
770                          * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=57871
771                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=63550
772                          * https://bugs.eclipse.org/bugs/show_bug.cgi?id=64299
773                          */
774                         net.sourceforge.phpdt.internal.core.util.Util.log(e, "Could not retrieve superclass"); //$NON-NLS-1$
775                         return this.resolver.resolveWellKnownType("java.lang.Object"); //$NON-NLS-1$
776                 }
777                 if (superclass == null) {
778                         return null;
779                 }
780                 return this.resolver.getTypeBinding(superclass);
781         }
782
783         /* (non-Javadoc)
784          * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeArguments()
785          */
786         public ITypeBinding[] getTypeArguments() {
787                 if (this.typeArguments != null) {
788                         return this.typeArguments;
789                 }
790                 if (this.binding.isParameterizedType()) {
791                         ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) this.binding;
792                         final net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding[] arguments = parameterizedTypeBinding.arguments;
793                         if (arguments != null) {
794                                 int argumentsLength = arguments.length;
795                                 ITypeBinding[] newTypeArguments = new ITypeBinding[argumentsLength];
796                                 for (int i = 0; i < argumentsLength; i++) {
797                                         ITypeBinding typeBinding = this.resolver.getTypeBinding(arguments[i]);
798                                         if (typeBinding == null) {
799                                                 return this.typeArguments = NO_TYPE_BINDINGS;
800                                         }
801                                         newTypeArguments[i] = typeBinding;
802                                 }
803                                 return this.typeArguments = newTypeArguments;
804                         }
805                 }
806                 return this.typeArguments = NO_TYPE_BINDINGS;
807         }
808
809         /* (non-Javadoc)
810          * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeBounds()
811          */
812         public ITypeBinding[] getTypeBounds() {
813                 if (this.bounds != null) {
814                         return this.bounds;
815                 }
816                 if (this.binding instanceof TypeVariableBinding) {
817                         TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
818                         ReferenceBinding varSuperclass = typeVariableBinding.superclass();
819                         net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding firstClassOrArrayBound = typeVariableBinding.firstBound;
820                         int boundsLength = 0;
821                         if (firstClassOrArrayBound != null) {
822                                 if (firstClassOrArrayBound == varSuperclass) {
823                                         boundsLength++;
824                                 } else if (firstClassOrArrayBound.isArrayType()) { // capture of ? extends/super arrayType
825                                         boundsLength++;
826                                 } else {
827                                         firstClassOrArrayBound = null;
828                                 }
829                         }
830                         ReferenceBinding[] superinterfaces = typeVariableBinding.superInterfaces();
831                         int superinterfacesLength = 0;
832                         if (superinterfaces != null) {
833                                 superinterfacesLength = superinterfaces.length;
834                                 boundsLength += superinterfacesLength;
835                         }
836                         if (boundsLength != 0) {
837                                 ITypeBinding[] typeBounds = new ITypeBinding[boundsLength];
838                                 int boundsIndex = 0;
839                                 if (firstClassOrArrayBound != null) {
840                                         ITypeBinding typeBinding = this.resolver.getTypeBinding(firstClassOrArrayBound);
841                                         if (typeBinding == null) {
842                                                 return this.bounds = NO_TYPE_BINDINGS;
843                                         }
844                                         typeBounds[boundsIndex++] = typeBinding;
845                                 }
846                                 if (superinterfaces != null) {
847                                         for (int i = 0; i < superinterfacesLength; i++, boundsIndex++) {
848                                                 ITypeBinding typeBinding = this.resolver.getTypeBinding(superinterfaces[i]);
849                                                 if (typeBinding == null) {
850                                                         return this.bounds = NO_TYPE_BINDINGS;
851                                                 }
852                                                 typeBounds[boundsIndex] = typeBinding;
853                                         }
854                                 }
855                                 return this.bounds = typeBounds;
856                         }
857                 }
858                 return this.bounds = NO_TYPE_BINDINGS;
859         }
860
861         /* (non-Javadoc)
862          * @see org.eclipse.jdt.core.dom.ITypeBinding#getTypeParameters()
863          */
864         public ITypeBinding[] getTypeParameters() {
865                 if (this.typeParameters != null) {
866                         return this.typeParameters;
867                 }
868                 switch(this.binding.kind()) {
869                         case Binding.RAW_TYPE :
870                         case Binding.PARAMETERIZED_TYPE :
871                                 return this.typeParameters = NO_TYPE_BINDINGS;
872                 }
873                 TypeVariableBinding[] typeVariableBindings = this.binding.typeVariables();
874                 int typeVariableBindingsLength = typeVariableBindings == null ? 0 : typeVariableBindings.length;
875                 if (typeVariableBindingsLength != 0) {
876                         ITypeBinding[] newTypeParameters = new ITypeBinding[typeVariableBindingsLength];
877                         for (int i = 0; i < typeVariableBindingsLength; i++) {
878                                 ITypeBinding typeBinding = this.resolver.getTypeBinding(typeVariableBindings[i]);
879                                 if (typeBinding == null) {
880                                         return this.typeParameters = NO_TYPE_BINDINGS;
881                                 }
882                                 newTypeParameters[i] = typeBinding;
883                         }
884                         return this.typeParameters = newTypeParameters;
885                 }
886                 return this.typeParameters = NO_TYPE_BINDINGS;
887         }
888
889         /* (non-Javadoc)
890          * @see org.eclipse.jdt.core.dom.ITypeBinding#getWildcard()
891          * @since 3.1
892          */
893         public ITypeBinding getWildcard() {
894                 if (this.binding instanceof CaptureBinding) {
895                         CaptureBinding captureBinding = (CaptureBinding) this.binding;
896                         return this.resolver.getTypeBinding(captureBinding.wildcard);
897                 }
898                 return null;
899         }
900
901         /* (non-Javadoc)
902          * @see org.eclipse.jdt.core.dom.ITypeBinding#isGenericType()
903          * @since 3.1
904          */
905         public boolean isGenericType() {
906                 // equivalent to return getTypeParameters().length > 0;
907                 if (isRawType()) {
908                         return false;
909                 }
910                 TypeVariableBinding[] typeVariableBindings = this.binding.typeVariables();
911                 return (typeVariableBindings != null && typeVariableBindings.length > 0);
912         }
913
914         /* (non-Javadoc)
915          * @see org.eclipse.jdt.core.dom.ITypeBinding#isAnnotation()
916          */
917         public boolean isAnnotation() {
918                 return this.binding.isAnnotationType();
919         }
920
921         /*
922          * @see ITypeBinding#isAnonymous()
923          */
924         public boolean isAnonymous() {
925                 if (isClass() || isInterface() || isEnum()) {
926                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
927                         return referenceBinding.isAnonymousType();
928                 }
929                 return false;
930         }
931
932         /*
933          * @see ITypeBinding#isArray()
934          */
935         public boolean isArray() {
936                 return binding.isArrayType();
937         }
938
939         /* (non-Javadoc)
940          * @see ITypeBinding#isAssignmentCompatible(ITypeBinding)
941          */
942         public boolean isAssignmentCompatible(ITypeBinding type) {
943                 try {
944                         if (this == type) return true;
945                         if (!(type instanceof TypeBinding)) return false;
946                         TypeBinding other = (TypeBinding) type;
947                         Scope scope = this.resolver.scope();
948                         if (scope == null) return false;
949                         return this.binding.isCompatibleWith(other.binding) || scope.isBoxingCompatibleWith(this.binding, other.binding);
950                 } catch (AbortCompilation e) {
951                         // don't surface internal exception to clients
952                         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=143013
953                         return false;
954                 }
955         }
956
957         /* (non-Javadoc)
958          * @see ITypeBinding#isCapture()
959          */
960         public boolean isCapture() {
961                 return this.binding.isCapture();
962         }
963
964         /* (non-Javadoc)
965          * @see ITypeBinding#isCastCompatible(ITypeBinding)
966          */
967         public boolean isCastCompatible(ITypeBinding type) {
968                 try {
969                         Expression expression = new Expression() {
970                                 public StringBuffer printExpression(int indent,StringBuffer output) {
971                                         return null;
972                                 }
973                         };
974                         Scope scope = this.resolver.scope();
975                         if (scope == null) return false;
976                         if (!(type instanceof TypeBinding)) return false;
977                         net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding expressionType = ((TypeBinding) type).binding;
978                         // simulate capture in case checked binding did not properly get extracted from a reference
979                         expressionType = expressionType.capture(scope, 0);
980                         return expression.checkCastTypesCompatibility(scope, this.binding, expressionType, null);
981                 } catch (AbortCompilation e) {
982                         // don't surface internal exception to clients
983                         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=143013
984                         return false;
985                 }
986         }
987
988         /*
989          * @see ITypeBinding#isClass()
990          */
991         public boolean isClass() {
992                 switch (this.binding.kind()) {
993                         case Binding.TYPE_PARAMETER :
994                         case Binding.WILDCARD_TYPE :
995                         case Binding.INTERSECTION_TYPE :
996                                 return false;
997                 }
998                 return this.binding.isClass();
999         }
1000
1001         /*
1002          * @see IBinding#isDeprecated()
1003          */
1004         public boolean isDeprecated() {
1005                 if (isClass() || isInterface() || isEnum()) {
1006                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
1007                         return referenceBinding.isDeprecated();
1008                 }
1009                 return false;
1010         }
1011
1012         /* (non-Javadoc)
1013          * @see ITypeBinding#isEnum()
1014          */
1015         public boolean isEnum() {
1016                 return this.binding.isEnum();
1017         }
1018
1019         /*
1020          * @see IBinding#isEqualTo(Binding)
1021          * @since 3.1
1022          */
1023         public boolean isEqualTo(IBinding other) {
1024                 if (other == this) {
1025                         // identical binding - equal (key or no key)
1026                         return true;
1027                 }
1028                 if (other == null) {
1029                         // other binding missing
1030                         return false;
1031                 }
1032                 if (!(other instanceof TypeBinding)) {
1033                         return false;
1034                 }
1035                 net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding otherBinding = ((TypeBinding) other).binding;
1036                 // check return type
1037                 return BindingComparator.isEqual(this.binding, otherBinding);
1038         }
1039
1040         /*
1041          * @see ITypeBinding#isFromSource()
1042          */
1043         public boolean isFromSource() {
1044                 if (isClass() || isInterface() || isEnum()) {
1045                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
1046                         if (referenceBinding.isRawType()) {
1047                                 return !((RawTypeBinding) referenceBinding).genericType().isBinaryBinding();
1048                         } else if (referenceBinding.isParameterizedType()) {
1049                                 ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) referenceBinding;
1050                                 net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding erasure = parameterizedTypeBinding.erasure();
1051                                 if (erasure instanceof ReferenceBinding) {
1052                                         return !((ReferenceBinding) erasure).isBinaryBinding();
1053                                 }
1054                                 return false;
1055                         } else {
1056                                 return !referenceBinding.isBinaryBinding();
1057                         }
1058                 } else if (isTypeVariable()) {
1059                         final TypeVariableBinding typeVariableBinding = (TypeVariableBinding) this.binding;
1060                         final Binding declaringElement = typeVariableBinding.declaringElement;
1061                         if (declaringElement instanceof MethodBinding) {
1062                                 MethodBinding methodBinding = (MethodBinding) declaringElement;
1063                                 return !methodBinding.declaringClass.isBinaryBinding();
1064                         } else {
1065                                 final net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding typeBinding = (net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding) declaringElement;
1066                                 if (typeBinding instanceof ReferenceBinding) {
1067                                         return !((ReferenceBinding) typeBinding).isBinaryBinding();
1068                                 } else if (typeBinding instanceof ArrayBinding) {
1069                                         final ArrayBinding arrayBinding = (ArrayBinding) typeBinding;
1070                                         final net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding leafComponentType = arrayBinding.leafComponentType;
1071                                         if (leafComponentType instanceof ReferenceBinding) {
1072                                                 return !((ReferenceBinding) leafComponentType).isBinaryBinding();
1073                                         }
1074                                 }
1075                         }
1076
1077                 } else if (isCapture()) {
1078                         CaptureBinding captureBinding = (CaptureBinding) this.binding;
1079                         return !captureBinding.sourceType.isBinaryBinding();
1080                 }
1081                 return false;
1082         }
1083
1084         /*
1085          * @see ITypeBinding#isInterface()
1086          */
1087         public boolean isInterface() {
1088                 switch (this.binding.kind()) {
1089                         case Binding.TYPE_PARAMETER :
1090                         case Binding.WILDCARD_TYPE :
1091                         case Binding.INTERSECTION_TYPE :
1092                                 return false;
1093                 }
1094                 return this.binding.isInterface();
1095         }
1096
1097         /*
1098          * @see ITypeBinding#isLocal()
1099          */
1100         public boolean isLocal() {
1101                 if (isClass() || isInterface() || isEnum()) {
1102                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
1103                         return referenceBinding.isLocalType() && !referenceBinding.isMemberType();
1104                 }
1105                 return false;
1106         }
1107
1108         /*
1109          * @see ITypeBinding#isMember()
1110          */
1111         public boolean isMember() {
1112                 if (isClass() || isInterface() || isEnum()) {
1113                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
1114                         return referenceBinding.isMemberType();
1115                 }
1116                 return false;
1117         }
1118
1119         /*
1120          * @see ITypeBinding#isNested()
1121          */
1122         public boolean isNested() {
1123                 if (isClass() || isInterface() || isEnum()) {
1124                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
1125                         return referenceBinding.isNestedType();
1126                 }
1127                 return false;
1128         }
1129
1130         /**
1131          * @see ITypeBinding#isNullType()
1132          */
1133         public boolean isNullType() {
1134                 return this.binding == net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding.NULL;
1135         }
1136
1137         /* (non-Javadoc)
1138          * @see org.eclipse.jdt.core.dom.ITypeBinding#isParameterizedType()
1139          */
1140         public boolean isParameterizedType() {
1141                 return this.binding.isParameterizedType() && ((ParameterizedTypeBinding) this.binding).arguments != null;
1142         }
1143
1144         /*
1145          * @see ITypeBinding#isPrimitive()
1146          */
1147         public boolean isPrimitive() {
1148                 return !isNullType() && binding.isBaseType();
1149         }
1150
1151         /* (non-Javadoc)
1152          * @see org.eclipse.jdt.core.dom.ITypeBinding#isRawType()
1153          */
1154         public boolean isRawType() {
1155                 return this.binding.isRawType();
1156         }
1157
1158         /* (non-Javadoc)
1159          * @see IBinding#isRecovered()
1160          */
1161         public boolean isRecovered() {
1162                 return (this.binding.tagBits & TagBits.HasMissingType) != 0;
1163         }
1164
1165         /* (non-Javadoc)
1166          * @see ITypeBinding#isSubTypeCompatible(ITypeBinding)
1167          */
1168         public boolean isSubTypeCompatible(ITypeBinding type) {
1169                 try {
1170                         if (this == type) return true;
1171                         if (this.binding.isBaseType()) return false;
1172                         if (!(type instanceof TypeBinding)) return false;
1173                         TypeBinding other = (TypeBinding) type;
1174                         if (other.binding.isBaseType()) return false;
1175                         return this.binding.isCompatibleWith(other.binding);
1176                 } catch (AbortCompilation e) {
1177                         // don't surface internal exception to clients
1178                         // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=143013
1179                         return false;
1180                 }
1181         }
1182
1183         /**
1184          * @see IBinding#isSynthetic()
1185          */
1186         public boolean isSynthetic() {
1187                 return false;
1188         }
1189
1190         /*
1191          * @see ITypeBinding#isTopLevel()
1192          */
1193         public boolean isTopLevel() {
1194                 if (isClass() || isInterface() || isEnum()) {
1195                         ReferenceBinding referenceBinding = (ReferenceBinding) this.binding;
1196                         return !referenceBinding.isNestedType();
1197                 }
1198                 return false;
1199         }
1200
1201         /*
1202          * @see ITypeBinding#isTypeVariable()
1203          */
1204         public boolean isTypeVariable() {
1205                 return this.binding.isTypeVariable() && !this.binding.isCapture();
1206         }
1207
1208         /* (non-Javadoc)
1209          * @see org.eclipse.jdt.core.dom.ITypeBinding#isUpperbound()
1210          */
1211         public boolean isUpperbound() {
1212                 switch (this.binding.kind()) {
1213                         case Binding.WILDCARD_TYPE :
1214                                 return ((WildcardBinding) this.binding).boundKind == Wildcard.EXTENDS;
1215                         case Binding.INTERSECTION_TYPE :
1216                                 return true;
1217                 }
1218                 return false;
1219         }
1220
1221         /* (non-Javadoc)
1222          * @see org.eclipse.jdt.core.dom.ITypeBinding#isWildcardType()
1223          */
1224         public boolean isWildcardType() {
1225                 return this.binding.isWildcard();
1226         }
1227
1228         /*
1229          * For debugging purpose only.
1230          * @see java.lang.Object#toString()
1231          */
1232         public String toString() {
1233                 return this.binding.toString();
1234         }
1235 }