KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints > TypeBindings


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

11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.jdt.core.dom.ITypeBinding;
18 import org.eclipse.jdt.core.dom.Modifier;
19
20 import org.eclipse.jdt.internal.corext.dom.Bindings;
21
22 /**
23  * TODO: Should find out if this is the same functionality as Bindings.canAssign or Bindings.canCast
24  */

25 public final class TypeBindings {
26     
27     private TypeBindings() {}
28     
29     private static boolean isNullBinding(ITypeBinding binding){
30         return binding == null;
31     }
32     
33     public static boolean isEqualTo(ITypeBinding binding, ITypeBinding otherBinding){
34         if (isNullBinding(binding)) //null binding is not comparable to anybody
35
return false;
36         else
37             return Bindings.equals(binding, otherBinding);
38     }
39     
40     public static boolean isSubtypeBindingOf(ITypeBinding binding, ITypeBinding otherBinding){
41         if (isNullBinding(binding) || isNullBinding(otherBinding)) //null binding is not comparable to anybodyO
42
return false;
43         return isSubtypeOf(binding, otherBinding);
44     }
45
46     private static boolean isSubtypeOf(ITypeBinding b1, ITypeBinding b2){
47         if (b1.isNullType()) //null type is bottom
48
return true;
49         else if (b1.isPrimitive() || b2.isPrimitive()) //cannot compare these
50
return false;
51         else if (isThisType(b2, "java.lang.Object")) //$NON-NLS-1$
52
return true;
53         else if (Modifier.isFinal(b2.getModifiers()))
54             return false;
55         else if (b1.isArray()){
56             if (b2.isArray())
57                 return isSubtypeOf(b1.getElementType(), b2.getElementType());
58             else{
59                 return isThisType(b2, "java.lang.Cloneable") //$NON-NLS-1$
60
|| isThisType(b2, "java.io.Serializable"); //$NON-NLS-1$
61
}
62         } else if (b2.isArray())
63             return false;
64         else
65             return (getSuperTypes(b1).contains(b2));//TODO could optimize here - we just a yes or no
66
}
67
68     private static boolean isThisType(ITypeBinding binding, String JavaDoc qualifiedName){
69         return binding.getQualifiedName().equals(qualifiedName);
70     }
71
72     /**
73      * returns a Set of ITypeBindings
74      */

75     public static Set JavaDoc getSuperTypes(ITypeBinding type) {
76         Set JavaDoc result= new HashSet JavaDoc();
77         ITypeBinding superClass= type.getSuperclass();
78         if (superClass != null){
79             result.add(superClass);
80             result.addAll(getSuperTypes(superClass));
81         }
82         ITypeBinding[] superInterfaces= type.getInterfaces();
83         result.addAll(Arrays.asList(superInterfaces));
84         for (int i= 0; i < superInterfaces.length; i++) {
85             result.addAll(getSuperTypes(superInterfaces[i]));
86         }
87         return result;
88     }
89     
90     public static String JavaDoc toString(ITypeBinding binding){
91         if (isNullBinding(binding))
92             return "<NULL BINDING>"; //$NON-NLS-1$
93
return Bindings.asString(binding);
94     }
95
96     public static boolean isClassBinding(ITypeBinding typeBinding){
97         return typeBinding != null && typeBinding.isClass();
98     }
99 }
100
Popular Tags