1 11 package org.eclipse.jdt.internal.corext.refactoring.structure.constraints; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.HierarchyType; 16 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType; 17 import org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ITypeSet; 18 19 22 public abstract class SuperTypeSet implements ITypeSet { 23 24 25 private static class SuperTypeEmptySet extends SuperTypeSet { 26 27 30 public final TType chooseSingleType() { 31 return null; 32 } 33 34 37 public final boolean isEmpty() { 38 return true; 39 } 40 41 44 public final ITypeSet restrictedTo(final ITypeSet set) { 45 return this; 46 } 47 48 51 public final String toString() { 52 return "EMPTY"; } 54 } 55 56 57 private static class SuperTypeSingletonSet extends SuperTypeSet { 58 59 60 private final TType fType; 61 62 67 private SuperTypeSingletonSet(final TType type) { 68 fType= type; 69 } 70 71 74 public final TType chooseSingleType() { 75 return fType; 76 } 77 78 81 public final boolean isEmpty() { 82 return false; 83 } 84 85 88 public final ITypeSet restrictedTo(final ITypeSet set) { 89 final TType leftErasure= fType.getErasure(); 90 if (set instanceof SuperTypeUniverse) { 91 return this; 92 } else if (set instanceof SuperTypeSingletonSet) { 93 if (this == set) 94 return this; 95 if (fType.isNullType()) 96 return this; 97 final SuperTypeSingletonSet singleton= (SuperTypeSingletonSet) set; 98 final TType rightErasure= singleton.fType.getErasure(); 99 if (leftErasure.isGenericType() || rightErasure.isGenericType()) { 100 if (rightErasure.equals(leftErasure) || ((HierarchyType) leftErasure).isSubType((HierarchyType) rightErasure)) 101 return this; 102 } 103 if (rightErasure.isJavaLangObject()) 104 return this; 105 if (leftErasure.canAssignTo(rightErasure)) 106 return this; 107 return SuperTypeSet.getEmpty(); 108 } else if (set instanceof SuperTypeTuple) { 109 if (fType.isNullType()) 110 return this; 111 final SuperTypeTuple tuple= (SuperTypeTuple) set; 112 final TType rightErasure= tuple.fSuperType.getErasure(); 113 if (leftErasure.isGenericType() || rightErasure.isGenericType()) { 114 if (rightErasure.equals(leftErasure) || ((HierarchyType) leftErasure).isSubType((HierarchyType) rightErasure)) 115 return this; 116 } 117 if (rightErasure.isJavaLangObject()) 118 return this; 119 if (leftErasure.canAssignTo(rightErasure)) 120 return this; 121 return SuperTypeSet.createTypeSet(tuple.fSubType); 122 } else if (set instanceof SuperTypeEmptySet) { 123 return set; 124 } else 125 Assert.isTrue(false); 126 return null; 127 } 128 129 132 public final String toString() { 133 return fType.getPrettySignature(); 134 } 135 } 136 137 138 private static class SuperTypeTuple extends SuperTypeSet { 139 140 141 private final TType fSubType; 142 143 144 private final TType fSuperType; 145 146 152 private SuperTypeTuple(final TType subType, final TType superType) { 153 fSubType= subType; 154 fSuperType= superType; 155 } 156 157 160 public final TType chooseSingleType() { 161 return fSuperType; 162 } 163 164 167 public final boolean isEmpty() { 168 return false; 169 } 170 171 174 public final ITypeSet restrictedTo(final ITypeSet set) { 175 if (set instanceof SuperTypeUniverse) { 176 return this; 177 } else if (set instanceof SuperTypeSingletonSet) { 178 final SuperTypeSingletonSet singleton= (SuperTypeSingletonSet) set; 179 final TType rightErasure= singleton.fType.getErasure(); 180 final TType subErasure= fSubType.getErasure(); 181 final TType superErasure= fSuperType.getErasure(); 182 if (subErasure.isGenericType() || superErasure.isGenericType() || rightErasure.isGenericType()) { 183 if ((rightErasure.equals(subErasure) || ((HierarchyType) subErasure).isSubType((HierarchyType) rightErasure)) && (rightErasure.equals(superErasure) || ((HierarchyType) superErasure).isSubType((HierarchyType) rightErasure))) 184 return this; 185 } 186 if (rightErasure.isJavaLangObject()) 187 return this; 188 if (subErasure.canAssignTo(rightErasure) && superErasure.canAssignTo(rightErasure)) 189 return this; 190 return SuperTypeSet.createTypeSet(fSubType); 191 } else if (set instanceof SuperTypeTuple) { 192 return this; 193 } else if (set instanceof SuperTypeEmptySet) { 194 return set; 195 } else 196 Assert.isTrue(false); 197 return null; 198 } 199 200 203 public final String toString() { 204 return "[" + fSubType.getPrettySignature() + ", " + fSuperType.getPrettySignature() + "]"; } 206 } 207 208 209 private static class SuperTypeUniverse extends SuperTypeSet { 210 211 214 public final TType chooseSingleType() { 215 return null; 216 } 217 218 221 public final boolean isEmpty() { 222 return false; 223 } 224 225 228 public final ITypeSet restrictedTo(final ITypeSet set) { 229 return set; 230 } 231 232 235 public final String toString() { 236 return "UNIVERSE"; } 238 } 239 240 241 private static final ITypeSet fgEmpty= new SuperTypeEmptySet(); 242 243 244 private static final ITypeSet fgUniverse= new SuperTypeUniverse(); 245 246 252 public static ITypeSet createTypeSet(final TType type) { 253 if (type == null) 254 return fgUniverse; 255 return new SuperTypeSingletonSet(type); 256 } 257 258 265 public static ITypeSet createTypeSet(final TType subType, final TType superType) { 266 if (subType == null || superType == null) 267 return fgUniverse; 268 return new SuperTypeTuple(subType, superType); 269 } 270 271 276 public static ITypeSet getEmpty() { 277 return fgEmpty; 278 } 279 280 285 public static ITypeSet getUniverse() { 286 return fgUniverse; 287 } 288 } 289 | Popular Tags |