KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > typeconstraints2 > TTypes


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints2;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Stack JavaDoc;
15
16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.AbstractTypeVariable;
17 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.HierarchyType;
18 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType;
19 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TypeVariable;
20
21 public class TTypes {
22     
23     private static class AllSupertypesIterator implements Iterator JavaDoc {
24         private final Stack JavaDoc fWorklist;
25         
26         public AllSupertypesIterator(TType type) {
27             fWorklist= new Stack JavaDoc();
28             pushSupertypes(type);
29         }
30     
31         public boolean hasNext() {
32             return ! fWorklist.empty();
33         }
34     
35         public Object JavaDoc next() {
36             TType result= (TType) fWorklist.pop();
37             pushSupertypes(result);
38             return result;
39         }
40     
41         private void pushSupertypes(TType type) {
42             if (type.isJavaLangObject())
43                 return;
44             
45             if (type.isTypeVariable() || type.isCaptureType()) {
46                 TType[] bounds= ((AbstractTypeVariable) type).getBounds();
47                 for (int i= 0; i < bounds.length; i++)
48                     fWorklist.push(bounds[i].getTypeDeclaration());
49             
50             } else {
51                 TType superclass= type.getSuperclass();
52                 if (superclass == null) {
53                     if (type.isInterface())
54                         fWorklist.push(type.getEnvironment().getJavaLangObject());
55                 } else {
56                     fWorklist.push(superclass.getTypeDeclaration());
57                 }
58                 TType[] interfaces= type.getInterfaces();
59                 for (int i= 0; i < interfaces.length; i++)
60                     fWorklist.push(interfaces[i].getTypeDeclaration());
61             }
62         }
63     
64         public void remove() {
65             throw new UnsupportedOperationException JavaDoc();
66         }
67     }
68     
69     private static class AllSubtypesIterator implements Iterator JavaDoc {
70         private final Stack JavaDoc fWorklist;
71         
72         public AllSubtypesIterator(TType type) {
73             fWorklist= new Stack JavaDoc();
74             fWorklist.push(type.getTypeDeclaration());
75         }
76     
77         public boolean hasNext() {
78             return ! fWorklist.empty();
79         }
80     
81         public Object JavaDoc next() {
82             TType result= (TType) fWorklist.pop();
83             TType[] subTypes= result.getSubTypes();
84             for (int i= 0; i < subTypes.length; i++)
85                 fWorklist.push(subTypes[i].getTypeDeclaration());
86             
87             return result;
88         }
89     
90         public void remove() {
91             throw new UnsupportedOperationException JavaDoc();
92         }
93     }
94     
95     private TTypes() {
96         // no instances
97
}
98
99     public static TType createArrayType(TType elementType, int dimensions) {
100         return elementType.getEnvironment().createArrayType(elementType, dimensions);
101     }
102
103     /**
104      * @return all subtypes of this type (including this type)
105      */

106     public static Iterator JavaDoc getAllSubTypesIterator(TType type) {
107         return new AllSubtypesIterator(type);
108     }
109
110     /**
111      * @return all proper supertypes of this type
112      */

113     public static Iterator JavaDoc getAllSuperTypesIterator(TType type) {
114         return new AllSupertypesIterator(type);
115     }
116     
117     /**
118      * @param rhs
119      * @param lhs
120      * @return <code>true</code> iff an expression of type 'rhs' can be assigned to a variable of type 'lhs'.
121      * Type arguments of generic / raw / parameterized types are <b>not</b> considered.
122      */

123     public static boolean canAssignTo(TType rhs, TType lhs) {
124         if (rhs.isHierarchyType() && lhs.isHierarchyType()) {
125             HierarchyType rhsGeneric= (HierarchyType) rhs.getTypeDeclaration();
126             HierarchyType lhsGeneric= (HierarchyType) lhs.getTypeDeclaration();
127             return lhs.isJavaLangObject() || rhsGeneric.equals(lhsGeneric) || rhsGeneric.isSubType(lhsGeneric);
128             
129         } else if (rhs.isTypeVariable()) {
130             if (rhs.canAssignTo(lhs))
131                 return true;
132             TType[] bounds= ((TypeVariable) rhs).getBounds();
133             for (int i= 0; i < bounds.length; i++) {
134                 if (canAssignTo(bounds[i], lhs))
135                     return true;
136             }
137             return lhs.isJavaLangObject();
138             
139         } else {
140             return rhs.canAssignTo(lhs);
141         }
142     }
143     
144 }
145
Popular Tags