1 11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints2; 12 13 import java.util.Iterator ; 14 import java.util.Stack ; 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 { 24 private final Stack fWorklist; 25 26 public AllSupertypesIterator(TType type) { 27 fWorklist= new Stack (); 28 pushSupertypes(type); 29 } 30 31 public boolean hasNext() { 32 return ! fWorklist.empty(); 33 } 34 35 public Object 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 (); 66 } 67 } 68 69 private static class AllSubtypesIterator implements Iterator { 70 private final Stack fWorklist; 71 72 public AllSubtypesIterator(TType type) { 73 fWorklist= new Stack (); 74 fWorklist.push(type.getTypeDeclaration()); 75 } 76 77 public boolean hasNext() { 78 return ! fWorklist.empty(); 79 } 80 81 public Object 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 (); 92 } 93 } 94 95 private TTypes() { 96 } 98 99 public static TType createArrayType(TType elementType, int dimensions) { 100 return elementType.getEnvironment().createArrayType(elementType, dimensions); 101 } 102 103 106 public static Iterator getAllSubTypesIterator(TType type) { 107 return new AllSubtypesIterator(type); 108 } 109 110 113 public static Iterator getAllSuperTypesIterator(TType type) { 114 return new AllSupertypesIterator(type); 115 } 116 117 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 |