1 23 24 package org.objectweb.jorm.type.lib; 25 26 import org.objectweb.jorm.type.api.PExceptionTyping; 27 import org.objectweb.jorm.type.api.PType; 28 import org.objectweb.jorm.type.api.PTypeSpace; 29 30 import java.util.ArrayList ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Arrays ; 34 import java.io.Serializable ; 35 36 43 44 public class PTypeSpacePAAH implements PTypeSpace { 45 46 51 private HashMap types = null; 52 53 public PTypeSpacePAAH() { 54 types = new HashMap (); 55 for (int i = 0; i < PTypeSpace.PREDEFINEDPTYPES.length; i++) 57 types.put(PTypeSpace.PREDEFINEDPTYPES[i].getJormName(), 58 new PTypeHashMapEntry(PTypeSpace.PREDEFINEDPTYPES[i], this)); 59 } 60 61 70 public synchronized PType createPType(String classname) 71 throws PExceptionTyping { 72 if (isConflict(classname, NULLSUPERCLASSNAMES)) 73 throw new PExceptionTyping("Cannot create type <" + classname 74 + ">: it conflicts with existing types."); 75 return definePType(classname, NULLSUPERCLASSNAMES).entryPType; 76 } 77 78 96 public synchronized PType createPType(String classname, 97 String [][] superclassnames) 98 throws PExceptionTyping { 99 if (isConflict(classname, superclassnames)) 100 throw new PExceptionTyping("Cannot create type <" + classname 101 + ">: it conflicts with existing types."); 102 return definePType(classname, superclassnames).entryPType; 103 } 104 105 122 public PType createPType(String [] gcnames, PType ptype) 123 throws PExceptionTyping { 124 return ((PTypeHashMapEntry) types.get(ptype.getJormName())) 125 .addGcPType(gcnames, (short) (gcnames.length - 1)); 126 } 127 128 149 public synchronized PType createPType(String [] gcnames, String typename, 150 String [][] superclassnames) 151 throws PExceptionTyping { 152 if (isConflict(typename, superclassnames)) 153 throw new PExceptionTyping("Cannot create type <" + typename 154 + ">: it conflicts with existing types."); 155 return defineGcPType(gcnames, typename, superclassnames); 156 } 157 158 164 public PType getPType(String classname) { 165 synchronized (types) { 166 PTypeHashMapEntry entry = (PTypeHashMapEntry) types.get(classname); 167 if (entry == null) { 168 return null; 169 } else { 170 return entry.entryPType; 171 } 172 } 173 } 174 175 180 public PType[] getSubTypes(PType pt) { 181 ArrayList subtypes = new ArrayList (); 182 Iterator it = types.values().iterator(); 183 while (it.hasNext()) { 184 PTypeHashMapEntry entry = (PTypeHashMapEntry) it.next(); 185 if ((entry.entryPType != pt) && entry.entryPType.isa(pt)) { 186 subtypes.add(entry.entryPType); 187 } 188 } 189 return (PType[]) subtypes.toArray(new PType[subtypes.size()]); 190 } 191 192 199 public boolean isa(PType pt1, PType pt2) { 200 if ((pt1 == null) || (pt2 == null)) 201 return false; 202 if (pt1 == pt2) 203 return true; 204 if ((pt1 instanceof PTypePAAH) && (pt2 instanceof PTypePAAH)) { 205 return ((PTypePAAH) pt1).belongsToInherited(pt2); 206 } else if ((pt1 instanceof PTypeGenClass) && (pt2 instanceof PTypeGenClass)) { 207 if (pt1.getJormName().equals(pt2.getJormName())) 208 return isa(pt1.getNestedPType(), pt2.getNestedPType()); 209 } 210 return false; 211 } 212 213 216 private boolean isConflict(String classname, String [][] superclasses) { 217 PTypeHashMapEntry entry = (PTypeHashMapEntry) types.get(classname); 218 if (entry == null) 219 return false; 220 short i = 0; 221 while ((i < superclasses.length) && 222 !(superclasses[i][0].equals(classname))) 223 i++; 224 if (i == superclasses.length) { 226 if (((PTypePAAH) entry.entryPType).superTypes.length == 0) 227 return false; 228 return true; 229 } 230 if (((PTypePAAH) entry.entryPType).superTypes.length 231 != (superclasses[i].length -1)) 232 return true; 233 short j; 234 for (short k = 0; k < ((PTypePAAH) entry.entryPType).superTypes.length; 235 k++) { 236 j = 1; 237 while (j < superclasses[i].length) { 238 if (((PTypePAAH) entry.entryPType).superTypes[k].getJormName() 239 .equals(superclasses[i][j])) { 240 if (isConflict(superclasses[i][j], superclasses)) 241 return true; 242 break; 243 } 244 j++; 245 } 246 if (j == superclasses[i].length) 247 return true; 248 } 249 return false; 250 } 251 252 255 private PTypeHashMapEntry definePType(String classname, 256 String [][] superclasses) { 257 PTypeHashMapEntry entry = (PTypeHashMapEntry) types.get(classname); 258 if (entry != null) 259 return entry; 260 short i = 0; 261 while ((i < superclasses.length) && 262 !(superclasses[i][0].equals(classname))) { 263 i++; 264 } 265 if (i == superclasses.length) { 267 entry = new PTypeHashMapEntry( 268 new PTypePAAH(classname, this, NULLSUPERTYPES), this); 269 } else { 270 PType[] supertypes = new PType[superclasses[i].length - 1]; 271 for (short j = 1; j < superclasses[i].length; j++) { 272 supertypes[j - 1] 273 = definePType(superclasses[i][j], superclasses).entryPType; 274 } 275 entry = new PTypeHashMapEntry(new PTypePAAH(classname, this, supertypes), this); 276 } 277 types.put(classname, entry); 278 return entry; 279 } 280 281 284 private PType defineGcPType(String [] gcnames, String typename, 285 String [][] superclasses) { 286 PTypeHashMapEntry typeentry = (PTypeHashMapEntry) types.get(typename); 287 if (typeentry == null) { 288 typeentry = definePType(typename, superclasses); 289 } 290 return typeentry.addGcPType(gcnames, (short) (gcnames.length - 1)); 291 } 292 } 293 294 314 class PTypeHashMapEntry implements Serializable { 315 318 public final static int INCREMENT = 5; 319 320 323 PType entryPType; 324 PTypeSpace pTypeSpace; 325 326 329 private PTypeHashMapEntry[] gcInstances = null; 330 331 public PTypeHashMapEntry(PType ptype, PTypeSpace ptypespace) { 332 entryPType = ptype; 333 pTypeSpace = ptypespace; 334 } 335 336 344 public PType addGcPType(String [] gcnames, short curgc) { 345 if (curgc < 0) { 346 return entryPType; 348 } 349 350 if (gcInstances == null) { 352 gcInstances = new PTypeHashMapEntry[INCREMENT]; 353 for (short i = 1; i < INCREMENT; i++) 354 gcInstances[i] = null; 355 gcInstances[0] = new PTypeHashMapEntry( 356 new PTypeGenClass(gcnames[curgc], entryPType, pTypeSpace), 357 pTypeSpace); 358 return gcInstances[0].addGcPType(gcnames, (short) (curgc - 1)); 359 } 360 361 int i = 0; 363 while ((i < gcInstances.length) 364 && gcInstances[i] != null 365 && !gcInstances[i++].entryPType.getJormName().equals(gcnames[curgc])) 366 ; 367 368 if (i == gcInstances.length) { 369 if (i == gcInstances.length) { 371 PTypeHashMapEntry[] newgcinstances 372 = new PTypeHashMapEntry[gcInstances.length + INCREMENT]; 373 System.arraycopy(gcInstances, 0, newgcinstances, 0, gcInstances.length); 374 Arrays.fill(newgcinstances, gcInstances.length, newgcinstances.length, null); 375 gcInstances = newgcinstances; 376 } 377 } 378 if (gcInstances[i] == null) { 379 gcInstances[i] = new PTypeHashMapEntry( 380 new PTypeGenClass(gcnames[curgc], entryPType, pTypeSpace), 381 pTypeSpace); 382 } 383 384 return gcInstances[i].addGcPType(gcnames, (short) (curgc - 1)); 386 } 387 } 388 | Popular Tags |