1 23 24 package org.objectweb.jorm.metainfo.lib; 25 26 import org.objectweb.jorm.metainfo.api.Class; 27 import org.objectweb.jorm.metainfo.api.ClassRef; 28 import org.objectweb.jorm.metainfo.api.GenClassRef; 29 import org.objectweb.jorm.metainfo.api.MetaObject; 30 import org.objectweb.jorm.metainfo.api.NameDef; 31 import org.objectweb.jorm.metainfo.api.PrimitiveElement; 32 import org.objectweb.jorm.metainfo.api.ScalarField; 33 import org.objectweb.jorm.metainfo.api.TypedElement; 34 import org.objectweb.jorm.type.api.PType; 35 import org.objectweb.jorm.type.api.PExceptionTyping; 36 import org.objectweb.jorm.lib.JormPathHelper; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 import java.util.ArrayList ; 40 import java.util.Collection ; 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.Map ; 44 import java.util.List ; 45 46 51 public class BasicGenClassRef 52 extends BasicMetaObject 53 implements GenClassRef, Comparable { 54 55 private static final String PRIMITIVE_ELEMENT_NAME = "element"; 56 59 private String name; 60 61 64 private String genClassName; 65 private String genClassId; 66 67 70 private ScalarField indexField; 71 72 78 private ArrayList refNameDefs; 79 80 86 private ArrayList idNameDefs; 87 88 94 private Map hiddenFields; 95 96 101 private List indexFields; 102 103 106 private boolean primitive; 107 108 111 private boolean classRef; 112 113 116 private boolean genClassRef; 117 118 122 private GenClassRef currentGenClassRef; 123 124 128 private ClassRef currentClassRef; 129 130 133 private PrimitiveElement primitiveElement; 134 135 private PType type; 136 137 147 public BasicGenClassRef(String genClassName, 148 String name, 149 MetaObject parent) { 150 super(parent); 151 this.name = name; 152 this.genClassName = genClassName; 153 indexField = null; 154 hiddenFields = new HashMap (); 155 refNameDefs = new ArrayList (); 156 idNameDefs = new ArrayList (); 157 indexFields = new ArrayList (); 158 genClassId = JormPathHelper.getPath(this, false); 159 } 160 161 public int compareTo(Object o) { 162 return name.compareTo(((TypedElement) o).getName()); 163 } 164 165 178 public PrimitiveElement createPrimitiveElement(PType type, int size, int scale) { 179 primitiveElement = new BasicPrimitiveElement( 180 PRIMITIVE_ELEMENT_NAME + '_' + type.getJormName(), type, size, scale, this); 181 setLoggingOnChild(primitiveElement); 182 183 primitive = true; 185 classRef = false; 186 genClassRef = false; 187 return primitiveElement; 188 } 189 190 197 public ScalarField createIndexField(String indexFieldName, 198 PType type, int size, int scale) { 199 if (debug) 200 logger.log(BasicLevel.DEBUG, 201 "create a new PrimitiveElement (index field (" + 202 indexFieldName + ")) for the current " + "GenClass (" + 203 this.genClassName + ")"); 204 ScalarField sf = new BasicScalarField(indexFieldName, type, size, scale, this); 205 setLoggingOnChild(sf); 206 return sf; 207 } 208 209 213 public ScalarField getIndexField() { 214 return indexField; 215 } 216 217 227 public ClassRef createClassRef(Class clazz) { 228 String classname = clazz.getName(); 229 int idx = classname.lastIndexOf('.'); 230 if (idx != -1) { 231 classname = classname.substring(idx + 1, classname.length()); 232 } 233 currentClassRef = new BasicClassRef(classname, clazz, this); 234 setLoggingOnChild(currentClassRef); 235 classRef = true; 237 genClassRef = false; 238 primitive = false; 239 240 return currentClassRef; 241 } 242 243 252 public GenClassRef createGenClassRef(String genClassRefName) { 253 classRef = false; 255 genClassRef = true; 256 primitive = false; 257 258 if (debug) 259 logger.log(BasicLevel.DEBUG, "create a new GenClassRef (" 260 + genClassRefName + ") for the current GenClassRef (" 261 + name + ")"); 262 263 if (currentGenClassRef == null) { 264 currentGenClassRef = new BasicGenClassRef(genClassRefName, null, this); 265 setLoggingOnChild(currentGenClassRef); 266 } 267 return currentGenClassRef; 268 } 269 270 280 public PrimitiveElement getPrimitiveElement() { 281 return primitiveElement; 282 } 283 284 public TypedElement removeTypedElement(String fieldName){ 285 TypedElement res = (TypedElement) hiddenFields.remove(fieldName); 286 return res; 287 } 288 289 299 public ClassRef getClassRef() { 300 return currentClassRef; 301 } 302 303 314 public GenClassRef getGenClassRef() { 315 return currentGenClassRef; 316 } 317 318 326 public boolean isPrimitive() { 327 return primitive; 328 } 329 330 337 public boolean isClassRef() { 338 return classRef; 339 } 340 341 349 public boolean isGenClassRef() { 350 return genClassRef; 351 } 352 353 361 362 public NameDef createIdNameDef() { 363 if (debug) 364 logger.log(BasicLevel.DEBUG, 365 "create a new ID NameDef () for the current GenClassRef (" + name + ")"); 366 NameDef newNameDef = new BasicNameDef(this); 368 setLoggingOnChild(newNameDef); 369 idNameDefs.add(newNameDef); 370 return newNameDef; 371 } 372 373 374 380 public Collection getIdNameDef() { 381 return idNameDefs; 382 } 383 384 385 390 public ScalarField createHiddenField(String fieldName, PType type, int size, int scale) { 391 if (debug) 392 logger.log(BasicLevel.DEBUG, 393 "create a new HiddenField (" + fieldName + ") for the current refgenclass (" + name + ")"); 394 395 if (hiddenFields.get(fieldName) != null) { 397 if (logger != null) { 398 logger.log(BasicLevel.WARN, 399 "attention, try to create an existing HiddenField (" + 400 fieldName + "), return existing one"); 401 } 402 return (ScalarField) hiddenFields.get(fieldName); 403 } 404 405 ScalarField newHiddenField = new BasicScalarField(fieldName, type, size, scale, this); 407 setLoggingOnChild(newHiddenField); 408 hiddenFields.put(fieldName, newHiddenField); 409 return newHiddenField; 410 } 411 412 417 public Collection getHiddenFields() { 418 return hiddenFields.values(); 419 } 420 421 422 427 public ScalarField getHiddenField(String fieldName) { 428 if (debug) { 429 logger.log(BasicLevel.DEBUG, 430 "try to return an hidden field (" + fieldName + ")"); 431 } 432 return (ScalarField) hiddenFields.get(fieldName); 433 } 434 435 440 public String getGenClassName() { 441 return genClassName; 442 } 443 444 public String getGenClassId() { 445 return genClassId; 446 } 447 448 457 458 public NameDef createRefNameDef() { 459 if (debug) 460 logger.log(BasicLevel.DEBUG, 461 "create a new REF NameDef () for the current Class (" + name + ")"); 462 NameDef newNameDef = new BasicNameDef(this); 464 setLoggingOnChild(newNameDef); 465 refNameDefs.add(newNameDef); 466 return newNameDef; 467 } 468 469 476 public NameDef getRefNameDef(String name) { 477 Iterator iter = refNameDefs.iterator(); 478 while (iter.hasNext()) { 479 NameDef nameDef = (NameDef) iter.next(); 480 if (nameDef.getName().equals(name)) { 481 return nameDef; 482 } 483 } 484 return null; 485 } 486 487 494 public NameDef getIdNameDef(String name) { 495 Iterator iter = idNameDefs.iterator(); 496 while (iter.hasNext()) { 497 NameDef nameDef = (NameDef) iter.next(); 498 if (nameDef.getName().equals(name)) { 499 return nameDef; 500 } 501 } 502 return null; 503 } 504 505 512 513 public Collection getRefNameDef() { 514 return refNameDefs; 515 } 516 517 public void addIndexField(String indexFieldName) { 518 indexFields.add(hiddenFields.get(indexFieldName)); 519 } 520 521 530 public Collection getIndexFields() { 531 return indexFields; 532 } 533 534 protected Collection getChildren() { 535 ArrayList res = new ArrayList (); 536 res.addAll(hiddenFields.values()); 537 res.addAll(refNameDefs); 538 res.addAll(idNameDefs); 539 return res; 540 } 541 542 546 547 552 public String getName() { 553 return name; 554 } 555 556 561 public PType getType() { 562 if (type != null) 563 return type; 564 try { 565 if (primitive) { 566 type = getManager().getPTypeSpace().createPType( 567 new String []{genClassName}, primitiveElement.getType()); 568 } else if (classRef) { 569 type = getManager().getPTypeSpace().createPType( 570 new String []{genClassName}, currentClassRef.getType()); 571 } else { ArrayList al = new ArrayList (); 573 GenClassRef gcr = this; 574 al.add(gcr.getGenClassName()); 575 while (gcr.isGenClassRef()) { 576 al.add(gcr.getGenClassName()); 577 gcr = gcr.getGenClassRef(); 578 } 579 PType lasttype = null; 580 if (gcr.isClassRef()) 581 lasttype = gcr.getClassRef().getType(); 582 else 583 lasttype = gcr.getPrimitiveElement().getType(); 584 type = getManager().getPTypeSpace().createPType( 585 (String []) al.toArray(new String [0]), lasttype); 586 } 587 } catch (PExceptionTyping e) { 588 if (logger != null) { 589 logger.log(BasicLevel.ERROR, 590 "Impossible to calculate the type of the GenClassRef field " 591 + genClassId, e); 592 } 593 } 594 return type; 595 } 596 } 597 | Popular Tags |