1 19 package org.netbeans.lib.jmi.mapping; 20 21 import org.netbeans.api.mdr.JMIStreamFactory; 22 import org.netbeans.lib.jmi.util.ClassFileGenerator; 23 import org.netbeans.lib.jmi.util.ContainsIterator; 24 import javax.jmi.model.*; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.DataOutputStream ; 27 import java.io.IOException ; 28 import java.io.OutputStream ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 39 public class ClassFileMapper extends GenericMapper { 40 protected String [] getAncestorNames(GeneralizableElement ge, String postfix, String defaultAncestor) { 41 Collection supertypes = ge.getSupertypes(); 42 if (supertypes.size() == 0) 43 return new String [] {defaultAncestor}; 44 String [] ancestors = new String [supertypes.size()]; 45 int i = 0; 46 for (Iterator it = supertypes.iterator(); it.hasNext(); i++) 47 ancestors[i] = tagProvider.getTypeFullName((ModelElement) it.next()) + postfix; 48 return ancestors; 49 } 50 51 protected static Object getTypedValue(String typeName, String value) { 52 if (value == null) 53 return null; 54 else if (typeName.equals("String")) return value; 56 else if (typeName.equals("Integer")) return new Integer (value); 58 else if (typeName.equals("Boolean")) return new Integer (value.equalsIgnoreCase("true") ? 1 : 0); else if (typeName.equals("Double")) return new Double (value); 62 else if (typeName.equals("Float")) return new Float (value); 64 else if (typeName.equals("Long")) return new Long (value); 66 else 67 return null; 68 } 69 70 public ClassFileMapper(JMIStreamFactory sf) { 71 this.generator = sf; 72 } 73 74 private final JMIStreamFactory generator; 75 private OutputStream stream; 76 77 protected boolean createStream(List pkg, String fileName) throws IOException { 78 if (stream != null) { 79 closeStream(); 80 throw new IllegalStateException ("Attempting to create stream before previous stream was closed."); 81 } 82 stream = generator.createStream(pkg, fileName, JMIStreamFactory.EXT_CLASS); 83 return stream != null; 84 } 85 86 protected void closeStream() throws IOException { 87 if (stream == null) 88 throw new IllegalStateException ("Attempting to close the stream without opening it first."); 89 OutputStream os = stream; 90 stream = null; 91 os.close(); 92 } 93 94 protected void classProxyTemplate(javax.jmi.model.MofClass objClass) throws IOException { 95 new ClassProxyGenerator(objClass).generate(); 96 } 97 98 protected void classInstanceTemplate(javax.jmi.model.MofClass objClass) throws IOException { 99 new ClassInstanceGenerator(objClass).generate(); 100 } 101 102 protected void associationTemplate(Association objAssociation) throws IOException { 104 new AssociationGenerator(objAssociation).generate(); 105 } 106 107 protected void packageTemplate(MofPackage objPackage) throws IOException { 109 new PackageGenerator(objPackage).generate(); 110 } 111 112 protected void exceptionTemplate(MofException objException) throws IOException { 114 new ExceptionGenerator(objException).generate(); 115 } 116 117 protected void enumerationInterfaceTemplate(EnumerationType objEnumeration) throws IOException { 119 new EnumerationGenerator(objEnumeration).generate(); 120 } 121 122 protected void enumerationClassTemplate(EnumerationType objEnumeration) throws IOException { 124 new EnumerationImplGenerator(objEnumeration).generate(); 125 } 126 127 protected void structureTemplate(StructureType objStructure) throws IOException { 129 new StructureGenerator(objStructure).generate(); 130 } 131 132 abstract class JMIClassFileGenerator extends ClassFileGenerator { 133 protected JMIClassFileGenerator(String className, String [] interfaces, String superclass, int accessFlags) { 134 super(className, interfaces, superclass, accessFlags); 135 } 136 137 protected void generate() throws IOException { 138 generateClassFile(stream); 139 } 140 } 141 142 class ClassProxyGenerator extends JMIClassFileGenerator { 143 144 protected javax.jmi.model.MofClass objClass; 145 146 public ClassProxyGenerator(javax.jmi.model.MofClass objClass) { 147 super(tagProvider.getTypeFullName(objClass) + CLASS_POSTFIX, new String [] {DT_CLASS}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); 148 this.objClass = objClass; 149 } 150 151 protected MethodInfo[] generateMethods() throws IOException { 152 ArrayList methods = new ArrayList (); 153 for (Iterator it = objClass.getContents().iterator(); it.hasNext();) { 154 ModelElement element = (ModelElement) it.next(); 155 if (element instanceof StructureType) { 156 ArrayList parameters = new ArrayList (); 157 for (Iterator itt = ((StructureType)element).getContents().iterator(); itt.hasNext();) { 158 ModelElement field = (ModelElement) itt.next(); 159 if (field instanceof StructureField) 160 parameters.add(getTypeName2((StructureField) field)); 161 } 162 methods.add(new MethodInfo("create" + firstUpper(tagProvider.getSubstName(element)), getMethodDescriptor((String [])parameters.toArray(new String [parameters.size()]), tagProvider.getTypeFullName(element)), ACC_PUBLIC | ACC_ABSTRACT)); } 164 } 165 ArrayList allAttributes = new ArrayList (); 166 for (Iterator it = new ContainsIterator(objClass); it.hasNext();) { 167 ModelElement element = (ModelElement) it.next(); 168 if (element instanceof Attribute) { 169 Attribute attr = (Attribute) element; 170 if (attr.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) { 171 String attrName = firstUpper(tagProvider.getSubstName(attr)); 172 Classifier attrType = getAttrType(attr); 173 String attrTypeName = getTypeName(attrType); 174 if (attr.getMultiplicity().getUpper() == 1) { 175 String name; 176 String setterName; 177 if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); else name = "is" + attrName; setterName = "set" + name.substring(2); } else { 182 name = "get" + attrName; setterName = "set" + attrName; } 185 String getterType = attrTypeName; 186 if (attr.getMultiplicity().getLower() == 1) 187 getterType = getPrimitiveName(getterType); 188 methods.add(new MethodInfo(name, getMethodDescriptor(new String [0], getterType), ACC_PUBLIC | ACC_ABSTRACT)); 189 if (attr.isChangeable()) 190 methods.add(new MethodInfo(setterName, getMethodDescriptor(new String [] {getterType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); } else if (attr.getMultiplicity().getUpper() != 0) 192 methods.add(new MethodInfo("get" + attrName, getMethodDescriptor(new String [0], (attr.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); } else if (!attr.isDerived()) 194 allAttributes.add(getTypeName(attr)); 195 } else if (element instanceof Operation) { 196 Operation oper = (Operation) element; 197 if (oper.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) { 198 Collection parameters = new ArrayList (); 199 String operType = "void"; for (Iterator itt = oper.getContents().iterator(); itt.hasNext();) { 201 ModelElement el = (ModelElement) itt.next(); 202 if (el instanceof Parameter) { 203 Parameter param = (Parameter) el; 204 if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR)) 205 operType = getTypeName(param); 206 else 207 parameters.add(getTypeName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); } 209 } 210 MethodInfo mInfo = new MethodInfo(tagProvider.getSubstName(oper), getMethodDescriptor((String [])parameters.toArray(new String [parameters.size()]), operType), ACC_PUBLIC | ACC_ABSTRACT); 211 Collection exceptions = oper.getExceptions(); 212 short[] declaredExceptions = new short[exceptions.size()]; 213 int i = 0; 214 for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++) 215 declaredExceptions[i] = cp.getClass(dotToSlash(tagProvider.getTypeFullName((ModelElement)itt.next()))); 216 mInfo.setDeclaredExceptions(declaredExceptions); 217 methods.add(mInfo); 218 } 219 } 220 } 221 if (!objClass.isAbstract()) { 222 methods.add(new MethodInfo("create" + tagProvider.getSubstName(objClass), getMethodDescriptor(new String [0], tagProvider.getTypeFullName(objClass)), ACC_PUBLIC | ACC_ABSTRACT)); if (allAttributes.size() > 0) 224 methods.add(new MethodInfo("create" + tagProvider.getSubstName(objClass), getMethodDescriptor((String [])allAttributes.toArray(new String [allAttributes.size()]), tagProvider.getTypeFullName(objClass)), ACC_PUBLIC | ACC_ABSTRACT)); } 226 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 227 } 228 229 protected FieldInfo[] generateFields() throws IOException { 230 return new FieldInfo[0]; 231 } 232 } 233 234 class ClassInstanceGenerator extends JMIClassFileGenerator { 235 236 protected javax.jmi.model.MofClass objClass; 237 238 public ClassInstanceGenerator(javax.jmi.model.MofClass objClass) { 239 super(tagProvider.getTypeFullName(objClass), getAncestorNames(objClass, "", DT_INSTANCE), DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); this.objClass = objClass; 241 } 242 243 protected MethodInfo[] generateMethods() throws IOException { 244 ArrayList methods = new ArrayList (); 245 for (Iterator it = objClass.getContents().iterator(); it.hasNext();) { 246 ModelElement element = (ModelElement) it.next(); 247 if (element instanceof Feature) { 248 Feature feature = (Feature) element; 249 if (feature.getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) { 250 if (feature instanceof Attribute) { 251 Attribute attr = (Attribute) feature; 252 String attrName = firstUpper(tagProvider.getSubstName(attr)); 253 Classifier attrType = getAttrType(attr); 254 String attrTypeName = getTypeName(attrType); 255 if (attr.getMultiplicity().getUpper() == 1) { 256 String name; 257 String setterName; 258 if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); else name = "is" + attrName; setterName = "set" + name.substring(2); } else { 263 name = "get" + attrName; setterName = "set" + attrName; } 266 String getterType = attrTypeName; 267 if (attr.getMultiplicity().getLower() == 1) 268 getterType = getPrimitiveName(getterType); 269 methods.add(new MethodInfo(name, getMethodDescriptor(new String [0], getterType), ACC_PUBLIC | ACC_ABSTRACT)); 270 if (attr.isChangeable()) 271 methods.add(new MethodInfo(setterName, getMethodDescriptor(new String [] {getterType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); } else if (attr.getMultiplicity().getUpper() != 0) 273 methods.add(new MethodInfo("get" + attrName, getMethodDescriptor(new String [0], (attr.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); } else if (feature instanceof Operation) { 275 Operation oper = (Operation) feature; 276 Collection parameters = new ArrayList (); 277 String operType = "void"; for (Iterator itt = oper.getContents().iterator(); itt.hasNext();) { 279 ModelElement el = (ModelElement) itt.next(); 280 if (el instanceof Parameter) { 281 Parameter param = (Parameter) el; 282 if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR)) 283 operType = getTypeName(param); 284 else 285 parameters.add(getTypeName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); } 287 } 288 MethodInfo mInfo = new MethodInfo(tagProvider.getSubstName(oper), getMethodDescriptor((String [])parameters.toArray(new String [parameters.size()]), operType), ACC_PUBLIC | ACC_ABSTRACT); 289 Collection exceptions = oper.getExceptions(); 290 short[] declaredExceptions = new short[exceptions.size()]; 291 int i = 0; 292 for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++) 293 declaredExceptions[i] = cp.getClass(dotToSlash(tagProvider.getTypeFullName((ModelElement)itt.next()))); 294 mInfo.setDeclaredExceptions(declaredExceptions); 295 methods.add(mInfo); 296 } else if (feature instanceof Reference) { 297 Reference ref = (Reference) feature; 298 String refName = firstUpper(tagProvider.getSubstName(ref)); 299 String refType = getTypeName(getAttrType(ref)); 300 if (ref.getMultiplicity().getUpper() == 1) { 301 methods.add(new MethodInfo("get" + refName, getMethodDescriptor(new String [0], refType), ACC_PUBLIC | ACC_ABSTRACT)); if (ref.isChangeable()) 303 methods.add(new MethodInfo("set" + refName, getMethodDescriptor(new String [] {refType}, "void"), ACC_PUBLIC | ACC_ABSTRACT)); } else if (ref.getMultiplicity().getUpper() != 0) { 305 methods.add(new MethodInfo("get" + refName, getMethodDescriptor(new String [0], (ref.getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); } 307 } 308 } 309 } 310 } 311 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 312 } 313 314 protected FieldInfo[] generateFields() throws IOException { 315 ArrayList fields = new ArrayList (); 316 for (Iterator it = objClass.getContents().iterator(); it.hasNext();) { 317 ModelElement element = (ModelElement) it.next(); 318 if (element instanceof Constant) { 319 DataType type = (DataType) getAttrType((Constant)element); 320 String value = ((Constant)element).getValue(); 321 FieldInfo fInfo = new FieldInfo(tagProvider.getSubstName(element), getFieldType(getTypeName2((Constant)element)), ACC_PUBLIC | ACC_STATIC | ACC_FINAL); 322 fInfo.setConstValue(getTypedValue(type.getName(), value)); 323 fields.add(fInfo); 324 } 325 } 326 return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]); 327 } 328 } 329 330 class AssociationGenerator extends JMIClassFileGenerator { 331 332 protected Association objAssociation; 333 334 public AssociationGenerator(Association objAssociation) { 335 super(tagProvider.getTypeFullName(objAssociation), new String [] {DT_ASSOCIATION}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); 336 this.objAssociation = objAssociation; 337 338 339 } 340 341 protected MethodInfo[] generateMethods() throws IOException { 342 ArrayList methods = new ArrayList (); 343 AssociationEnd[] ends = new AssociationEnd[2]; 344 int i = 0; 345 for (Iterator it = objAssociation.getContents().iterator(); it.hasNext() && i < 2;) { 346 Object element = it.next(); 347 if (element instanceof AssociationEnd) 348 ends[i++] = (AssociationEnd) element; 349 } 350 String end1Name = tagProvider.getSubstName(ends[0]); 351 String end1Class = tagProvider.getTypeFullName(getAttrType(ends[0])); 352 String end2Name = tagProvider.getSubstName(ends[1]); 353 String end2Class = tagProvider.getTypeFullName(getAttrType(ends[1])); 354 methods.add(new MethodInfo("exists", getMethodDescriptor(new String [] {end1Class, end2Class}, "boolean"), ACC_PUBLIC | ACC_ABSTRACT)); if (ends[0].isNavigable()) { 358 methods.add(new MethodInfo("get" + firstUpper(end1Name), getMethodDescriptor(new String [] {end2Class}, ends[0].getMultiplicity().getUpper() == 1 ? end1Class : (ends[0].getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); 360 } 361 if (ends[1].isNavigable()) { 363 methods.add(new MethodInfo("get" + firstUpper(end2Name), getMethodDescriptor(new String [] {end1Class}, ends[1].getMultiplicity().getUpper() == 1 ? end2Class : (ends[1].getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED)), ACC_PUBLIC | ACC_ABSTRACT)); 365 } 366 if (ends[0].isChangeable() && ends[1].isChangeable()) { 367 methods.add(new MethodInfo("add", getMethodDescriptor(new String [] {end1Class, end2Class}, "boolean"), ACC_PUBLIC | ACC_ABSTRACT)); methods.add(new MethodInfo("remove", getMethodDescriptor(new String [] {end1Class, end2Class}, "boolean"), ACC_PUBLIC | ACC_ABSTRACT)); } 372 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 373 } 374 375 protected FieldInfo[] generateFields() throws IOException { 376 return new FieldInfo[0]; 377 } 378 } 379 380 class PackageGenerator extends JMIClassFileGenerator { 381 382 protected MofPackage objPackage; 383 384 public PackageGenerator(MofPackage objPackage) { 385 super(tagProvider.getTypeFullName(objPackage) + PACKAGE_POSTFIX, getAncestorNames(objPackage, PACKAGE_POSTFIX, DT_PACKAGE), DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); 386 this.objPackage = objPackage; 387 } 388 389 protected MethodInfo[] generateMethods() throws IOException { 390 ArrayList methods = new ArrayList (); 391 for (Iterator it = objPackage.getContents().iterator(); it.hasNext();) { 392 ModelElement element = (ModelElement) it.next(); 393 String typeName = tagProvider.getTypeFullName(element); 394 String elementName = tagProvider.getSubstName(element); 395 if (element instanceof Association) { 396 } else if (element instanceof javax.jmi.model.MofClass) { 397 typeName += CLASS_POSTFIX; 398 } else if (element instanceof MofPackage) { 399 typeName += PACKAGE_POSTFIX; 400 } else if (element instanceof EnumerationType) { 401 continue; 402 } else if (element instanceof StructureType) { 403 ArrayList parameters = new ArrayList (); 404 for (Iterator itt = ((StructureType)element).getContents().iterator(); itt.hasNext();) { 405 ModelElement field = (ModelElement) itt.next(); 406 if (field instanceof StructureField) 407 parameters.add(getTypeName2((StructureField) field)); 408 } 409 methods.add(new MethodInfo("create" + firstUpper(tagProvider.getSubstName(element)), getMethodDescriptor((String [])parameters.toArray(new String [parameters.size()]), tagProvider.getTypeFullName(element)), ACC_PUBLIC | ACC_ABSTRACT)); continue; 411 } else if (element instanceof Import) { 412 Import imp = (Import) element; 413 if (imp.isClustered() && imp.getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) { 414 Namespace namespace = imp.getImportedNamespace(); 415 if (namespace instanceof MofPackage) { 416 typeName = tagProvider.getTypeFullName(namespace) + PACKAGE_POSTFIX; 417 element = namespace; 418 } 419 } else 420 continue; 421 } else 422 continue; 423 if (((GeneralizableElement) element).getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) { 424 methods.add(new MethodInfo("get" + elementName, getMethodDescriptor(new String [0], typeName), ACC_PUBLIC | ACC_ABSTRACT)); } 426 } 427 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 428 } 429 430 protected FieldInfo[] generateFields() throws IOException { 431 return new FieldInfo[] {}; 432 } 433 } 434 435 class ExceptionGenerator extends JMIClassFileGenerator { 436 437 protected MofException objException; 438 439 public ExceptionGenerator(MofException objException) { 440 super(tagProvider.getTypeFullName(objException), new String [0], DT_EXCEPTION, ACC_PUBLIC | ACC_SUPER); 441 this.objException = objException; 442 } 443 444 protected MethodInfo[] generateMethods() throws IOException { 445 ArrayList methods = new ArrayList (); 446 ArrayList paramInits = new ArrayList (); 447 ArrayList paramTypes = new ArrayList (); 448 short i = 1; 450 for (Iterator it = objException.getContents().iterator(); it.hasNext();) { 451 Object element = it.next(); 452 if (element instanceof Parameter) { 453 Parameter param = (Parameter) element; 454 String typeName = getTypeName(param); 455 paramTypes.add(typeName); 456 String getterName = firstLower(tagProvider.getSubstName(param)); 457 String paramName = removeUnderscores(getterName); 458 if (param.getType() instanceof PrimitiveType && param.getType().getName().equals("Boolean")) { if (getterName.indexOf("is") != 0) getterName = removeUnderscores("is_" + getterName); } else 462 getterName = removeUnderscores("get_" + getterName); MethodInfo mInfo = new MethodInfo(getterName, getMethodDescriptor(new String [0], typeName), ACC_PUBLIC); 464 DataOutputStream code = new DataOutputStream (mInfo.getCodeStream()); 465 codeReturnFieldValue(className, paramName, getFieldType(typeName), false, code); 466 mInfo.setMaxStack((short)1); 467 mInfo.setMaxLocals((short)1); 468 methods.add(mInfo); 469 ByteArrayOutputStream bout = new ByteArrayOutputStream (6); 470 DataOutputStream paramInitCode = new DataOutputStream (bout); 471 code_aload(0, paramInitCode); 472 code_aload(i++, paramInitCode); 473 paramInitCode.writeByte(opc_putfield); 474 paramInitCode.writeShort(cp.getFieldRef(dotToSlash(className), paramName, getFieldType(typeName))); 475 paramInits.add(bout.toByteArray()); 476 } 482 } 483 MethodInfo mInfo = new MethodInfo("<init>", getMethodDescriptor((String [])paramTypes.toArray(new String [paramTypes.size()]), "void"), ACC_PUBLIC); DataOutputStream code = new DataOutputStream (mInfo.getCodeStream()); 486 code_aload(0, code); 488 code.writeByte(opc_invokespecial); 489 code.writeShort(cp.getMethodRef(dotToSlash(className), "<init>", getMethodDescriptor(new String [0], "void"))); for (Iterator it = paramInits.iterator(); it.hasNext();) 491 code.write((byte[]) it.next()); 492 code.writeByte(opc_return); 493 mInfo.setMaxStack((short)2); 494 mInfo.setMaxLocals(i); 495 methods.add(mInfo); 496 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 497 } 498 499 protected FieldInfo[] generateFields() throws IOException { 500 ArrayList fields = new ArrayList (); 501 for (Iterator it = objException.getContents().iterator(); it.hasNext();) { 503 Object element = it.next(); 504 if (element instanceof Parameter) { 505 Parameter param = (Parameter) element; 506 String typeName = getTypeName(param); 507 String paramName = removeUnderscores(firstLower(tagProvider.getSubstName(param))); 508 fields.add(new FieldInfo(paramName, getFieldType(typeName), ACC_PRIVATE | ACC_FINAL)); 509 } 510 } 511 return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]); 512 } 513 } 514 515 class EnumerationGenerator extends JMIClassFileGenerator { 516 517 protected EnumerationType objEnumeration; 518 519 public EnumerationGenerator(EnumerationType objEnumeration) { 520 super(tagProvider.getTypeFullName(objEnumeration), new String [] {DT_ENUMERATION}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); 521 this.objEnumeration = objEnumeration; 522 } 523 524 protected MethodInfo[] generateMethods() throws IOException { 525 return new MethodInfo[0]; 526 } 527 528 protected FieldInfo[] generateFields() throws IOException { 529 return new FieldInfo[0]; 530 } 531 } 532 533 class EnumerationImplGenerator extends JMIClassFileGenerator { 534 535 protected EnumerationType objEnumeration; 536 537 public EnumerationImplGenerator(EnumerationType objEnumeration) { 538 super(tagProvider.getTypeFullName(objEnumeration) + ENUM_POSTFIX, new String [] {tagProvider.getTypeFullName(objEnumeration)}, DT_ANY, ACC_PUBLIC | ACC_FINAL | ACC_SUPER); 539 this.objEnumeration = objEnumeration; 540 } 541 542 protected MethodInfo[] generateMethods() throws IOException { 543 ArrayList methods = new ArrayList (); 544 MethodInfo mInfo = new MethodInfo("<init>", getMethodDescriptor(new String [] {"java.lang.String"}, "void"), ACC_PRIVATE); DataOutputStream code = new DataOutputStream (mInfo.getCodeStream()); 547 code_aload(0, code); 548 code.writeByte(opc_invokespecial); 549 code.writeShort(cp.getMethodRef("java/lang/Object", "<init>", getMethodDescriptor(new String [0], "void"))); code_aload(0, code); 551 code_aload(1, code); 552 code.writeByte(opc_putfield); 553 code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); code.writeByte(opc_return); 555 mInfo.setMaxStack((short)2); 556 mInfo.setMaxLocals((short)2); 557 methods.add(mInfo); 558 mInfo = new MethodInfo("refTypeName", getMethodDescriptor(new String [0], "java.util.List"), ACC_PUBLIC); code = new DataOutputStream (mInfo.getCodeStream()); 561 codeReturnFieldValue(className, "typeName", getFieldType("java.util.List"), false, code); mInfo.setMaxStack((short)1); 563 mInfo.setMaxLocals((short)1); 564 methods.add(mInfo); 565 mInfo = new MethodInfo("toString", getMethodDescriptor(new String [0], "java.lang.String"), ACC_PUBLIC); code = new DataOutputStream (mInfo.getCodeStream()); 568 codeReturnFieldValue(className, "literalName", getFieldType("java.lang.String"), false, code); mInfo.setMaxStack((short)1); 570 mInfo.setMaxLocals((short)1); 571 methods.add(mInfo); 572 mInfo = new MethodInfo("hashCode", getMethodDescriptor(new String [0], "int"), ACC_PUBLIC); code = new DataOutputStream (mInfo.getCodeStream()); 575 code_aload(0, code); 576 code.writeByte(opc_getfield); 577 code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); code.write(opc_invokevirtual); 579 code.writeShort(cp.getMethodRef("java/lang/Object", "hashCode", getMethodDescriptor(new String [0], "int"))); code.writeByte(opc_ireturn); 581 mInfo.setMaxStack((short)1); 582 mInfo.setMaxLocals((short)1); 583 methods.add(mInfo); 584 mInfo = new MethodInfo("equals", getMethodDescriptor(new String [] {"java.lang.Object"}, "boolean"), ACC_PUBLIC); code = new DataOutputStream (mInfo.getCodeStream()); 587 code_aload(1, code); 589 code.writeByte(opc_instanceof); 590 code.writeShort(cp.getClass(dotToSlash(className))); 591 code.writeByte(opc_ifeq); 592 code.writeShort(12); 593 code.writeByte(opc_aload_0 + 1); 594 code.writeByte(opc_aload_0); 595 code.writeByte(opc_if_acmpne); 596 code.writeShort(5); 597 code.writeByte(opc_iconst_1); 598 code.writeByte(opc_ireturn); 599 code.writeByte(opc_iconst_0); 600 code.writeByte(opc_ireturn); 601 code_aload(1, code); 604 code.writeByte(opc_instanceof); 605 code.writeShort(cp.getClass(dotToSlash(tagProvider.getTypeFullName(objEnumeration)))); 606 code.writeByte(opc_ifeq); 607 code.writeShort(15); 608 code.writeByte(opc_aload_0 + 1); 609 code.writeByte(opc_invokevirtual); 610 code.writeShort(cp.getMethodRef("java/lang/Object", "toString", getMethodDescriptor(new String [0], "java.lang.String"))); code.writeByte(opc_aload_0); 612 code.writeByte(opc_getfield); 613 code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); code.writeByte(opc_invokevirtual); 615 code.writeShort(cp.getMethodRef("java/lang/Object", "equals", getMethodDescriptor(new String [] {"java.lang.Object"}, "boolean"))); code.writeByte(opc_ireturn); 617 code_aload(1, code); 621 code.writeByte(opc_instanceof); 622 code.writeShort(cp.getClass(dotToSlash(DT_ENUMERATION))); 623 code.writeByte(opc_ifeq); 624 code.writeShort(39); 625 code.writeByte(opc_aload_0 + 1); 626 code.writeByte(opc_checkcast); 627 code.writeShort(cp.getClass(dotToSlash(DT_ENUMERATION))); 628 code.writeByte(opc_invokeinterface); 629 code.writeShort(cp.getInterfaceMethodRef(dotToSlash(DT_ENUMERATION), "refTypeName", getMethodDescriptor(new String [0], "java.util.List"))); code.writeByte(1); 631 code.writeByte(0); 632 code.writeByte(opc_getstatic); 633 code.writeShort(cp.getFieldRef(dotToSlash(className), "typeName", getFieldType("java.util.List"))); code.writeByte(opc_invokeinterface); 635 code.writeShort(cp.getInterfaceMethodRef("java/lang/Object", "equals", getMethodDescriptor(new String [] {"java.lang.Object"}, "boolean"))); code.writeByte(2); 637 code.writeByte(0); 638 code.writeByte(opc_ifeq); 639 code.writeShort(19); 640 code.writeByte(opc_aload_0 + 1); 641 code.writeByte(opc_invokevirtual); 642 code.writeShort(cp.getMethodRef("java/lang/Object", "toString", getMethodDescriptor(new String [0], "java.lang.String"))); code.writeByte(opc_aload_0); 644 code.writeByte(opc_getfield); 645 code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); code.writeByte(opc_invokevirtual); 647 code.writeShort(cp.getMethodRef("java/lang/Object", "equals", getMethodDescriptor(new String [] {"java.lang.Object"}, "boolean"))); code.writeByte(opc_ifeq); 649 code.writeShort(5); 650 code.writeByte(opc_iconst_1); 651 code.writeByte(opc_ireturn); 652 code.writeByte(opc_iconst_0); 653 code.writeByte(opc_ireturn); 654 mInfo.setMaxStack((short)2); 655 mInfo.setMaxLocals((short)2); 656 methods.add(mInfo); 657 mInfo = new MethodInfo("readResolve", getMethodDescriptor(new String [0], "java.lang.Object"), ACC_PROTECTED); mInfo.setDeclaredExceptions(new short[] {cp.getClass("java/io/ObjectStreamException")}); code = new DataOutputStream (mInfo.getCodeStream()); 661 code_aload(0, code); 662 code.write(opc_getfield); 663 code.writeShort(cp.getFieldRef(dotToSlash(className), "literalName", getFieldType("java.lang.String"))); code.writeByte(opc_invokestatic); 665 code.writeShort(cp.getMethodRef(dotToSlash(className), "forName", getMethodDescriptor(new String [] {"java.lang.String"}, tagProvider.getTypeFullName(objEnumeration)))); code.writeByte(opc_areturn); 667 code_astore(1, code); 668 code.writeByte(opc_new); 669 code.writeShort(cp.getClass("java/io/ObjectStreamException")); code.writeByte(opc_dup); 671 code_aload(1, code); 672 code.writeByte(opc_invokevirtual); 673 code.writeShort(cp.getMethodRef("java/lang/Throwable", "getMessage", getMethodDescriptor(new String [0], "java.lang.String"))); code.writeByte(opc_invokespecial); 675 code.writeShort(cp.getMethodRef("java/io/ObjectStreamException", "<init>", getMethodDescriptor(new String [] {"java.lang.String"}, "void"))); code.writeByte(opc_athrow); 677 mInfo.getExceptionTable().add(new ExceptionTableEntry((short) 0, (short) 8, (short) 8, cp.getClass("java/lang/IllegalArgumentException"))); mInfo.setMaxStack((short)3); 679 mInfo.setMaxLocals((short)2); 680 methods.add(mInfo); 681 mInfo = new MethodInfo("forName", getMethodDescriptor(new String [] {"java.lang.String"}, tagProvider.getTypeFullName(objEnumeration)), ACC_PUBLIC | ACC_STATIC); code = new DataOutputStream (mInfo.getCodeStream()); 684 MethodInfo initInfo = new MethodInfo("<clinit>", getMethodDescriptor(new String [0], "void"), ACC_STATIC); DataOutputStream initCode = new DataOutputStream (initInfo.getCodeStream()); 686 for (Iterator it = objEnumeration.getLabels().iterator(); it.hasNext();) { 687 String literal = (String ) it.next(); 688 code_aload(0, code); 689 code_ldc(cp.getString(literal), code); 690 code.writeByte(opc_invokevirtual); 691 code.writeShort(cp.getMethodRef("java/lang/String", "equals", getMethodDescriptor(new String [] {"java.lang.Object"}, "boolean"))); code.writeByte(opc_ifeq); 693 code.writeShort(7); 694 code.writeByte(opc_getstatic); 695 code.writeShort(cp.getFieldRef(dotToSlash(className), tagProvider.mapEnumLiteral(literal), getFieldType(className))); 696 code.write(opc_areturn); 697 initCode.writeByte(opc_new); 698 initCode.writeShort(cp.getClass(dotToSlash(className))); 699 initCode.writeByte(opc_dup); 700 code_ldc(cp.getString(literal), initCode); 701 initCode.writeByte(opc_invokespecial); 702 initCode.writeShort(cp.getMethodRef(dotToSlash(className), "<init>", getMethodDescriptor(new String [] {"java.lang.String"}, "void"))); initCode.writeByte(opc_putstatic); 704 initCode.writeShort(cp.getFieldRef(dotToSlash(className), tagProvider.mapEnumLiteral(literal), getFieldType(className))); 705 } 706 code.writeByte(opc_new); 707 code.writeShort(cp.getClass("java/lang/IllegalArgumentException")); code.writeByte(opc_dup); 709 code.writeByte(opc_new); 710 code.writeShort(cp.getClass("java/lang/StringBuffer")); code.writeByte(opc_dup); 712 code.writeByte(opc_invokespecial); 713 code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "<init>", getMethodDescriptor(new String [0], "void"))); code_ldc(cp.getString("Unknown enumeration value "), code); code.writeByte(opc_invokevirtual); 716 code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "append", getMethodDescriptor(new String [] {"java.lang.String"}, "java.lang.StringBuffer"))); code_aload(0, code); 718 code.writeByte(opc_invokevirtual); 719 code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "append", getMethodDescriptor(new String [] {"java.lang.String"}, "java.lang.StringBuffer"))); code_ldc(cp.getString(" for type " + tagProvider.getTypeFullName(objEnumeration)), code); code.writeByte(opc_invokevirtual); 722 code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "append", getMethodDescriptor(new String [] {"java.lang.String"}, "java.lang.StringBuffer"))); code.writeByte(opc_invokevirtual); 724 code.writeShort(cp.getMethodRef("java/lang/StringBuffer", "toString", getMethodDescriptor(new String [0], "java.lang.String"))); code.writeByte(opc_invokespecial); 726 code.writeShort(cp.getMethodRef("java/lang/IllegalArgumentException", "<init>", getMethodDescriptor(new String [] {"java.lang.String"}, "void"))); code.writeByte(opc_athrow); 728 mInfo.setMaxStack((short)4); 729 mInfo.setMaxLocals((short)1); 730 methods.add(mInfo); 731 initCode.writeByte(opc_new); 732 initCode.writeShort(cp.getClass("java/util/ArrayList")); initCode.writeByte(opc_dup); 734 initCode.writeByte(opc_invokespecial); 735 initCode.writeShort(cp.getMethodRef("java/util/ArrayList", "<init>", getMethodDescriptor(new String [0], "void"))); code_astore(0, initCode); 737 for (Iterator it = objEnumeration.getQualifiedName().iterator(); it.hasNext();) { 738 code_aload(0, initCode); 739 code_ldc(cp.getString((String )it.next()), initCode); 740 initCode.writeByte(opc_invokevirtual); 741 initCode.writeShort(cp.getMethodRef("java/util/ArrayList", "add", getMethodDescriptor(new String [] {"java.lang.Object"}, "boolean"))); initCode.writeByte(opc_pop); 743 } 744 code_aload(0, initCode); 745 initCode.writeByte(opc_invokestatic); 746 initCode.writeShort(cp.getMethodRef("java/util/Collections", "unmodifiableList", getMethodDescriptor(new String [] {"java.util.List"}, "java.util.List"))); initCode.writeByte(opc_putstatic); 748 initCode.writeShort(cp.getFieldRef(dotToSlash(className), "typeName", getFieldType("java.util.List"))); initCode.writeByte(opc_return); 750 initInfo.setMaxStack((short)3); 751 initInfo.setMaxLocals((short)1); 752 methods.add(initInfo); 753 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 754 } 755 756 protected FieldInfo[] generateFields() throws IOException { 757 ArrayList fields = new ArrayList (); 758 for (Iterator it = objEnumeration.getLabels().iterator(); it.hasNext();) { 760 String literal = (String ) it.next(); 761 FieldInfo fInfo = new FieldInfo(tagProvider.mapEnumLiteral(literal), getFieldType(className), ACC_PUBLIC | ACC_STATIC | ACC_FINAL); 762 fields.add(fInfo); 763 } 764 fields.add(new FieldInfo("typeName", getFieldType("java.util.List"), ACC_PRIVATE | ACC_STATIC | ACC_FINAL)); fields.add(new FieldInfo("literalName", getFieldType("java.lang.String"), ACC_PRIVATE | ACC_FINAL)); return (FieldInfo[]) fields.toArray(new FieldInfo[fields.size()]); 768 } 769 } 770 771 class StructureGenerator extends JMIClassFileGenerator { 772 773 protected StructureType objStructure; 774 775 public StructureGenerator(StructureType objStructure) { 776 super(tagProvider.getTypeFullName(objStructure), new String [] {DT_STRUCTURE}, DT_ANY, ACC_PUBLIC | ACC_INTERFACE | ACC_ABSTRACT); 777 this.objStructure = objStructure; 778 } 779 780 protected MethodInfo[] generateMethods() throws IOException { 781 ArrayList methods = new ArrayList (); 782 for (Iterator it = objStructure.getContents().iterator(); it.hasNext();) { 784 ModelElement field = (ModelElement) it.next(); 785 if (field instanceof StructureField) { 786 Classifier fieldType = ((StructureField) field).getType(); 787 String memberName = firstUpper(tagProvider.getSubstName(field)); 788 if (fieldType instanceof PrimitiveType && fieldType.getName().equals("Boolean")) { if (memberName.indexOf("Is") != 0) memberName = "is" + memberName; else 792 memberName = firstLower(memberName); 793 } else 794 memberName = "get" + memberName; MethodInfo mInfo = new MethodInfo(memberName, getMethodDescriptor(new String [0], getPrimitiveName(getTypeName(fieldType))), ACC_PUBLIC | ACC_ABSTRACT); 796 methods.add(mInfo); 798 } 799 } 800 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 801 } 802 803 protected FieldInfo[] generateFields() throws IOException { 804 return new FieldInfo[0]; 805 } 806 } 807 } 808 | Popular Tags |