1 19 package org.netbeans.mdr.handlers.gen; 20 21 import java.util.*; 22 import java.io.*; 23 24 import org.netbeans.mdr.storagemodel.*; 25 import org.netbeans.mdr.util.*; 26 import javax.jmi.model.*; 27 28 33 class InstanceGenerator extends FeaturedGenerator { 34 35 InstanceGenerator(String name, Class ifc, Class handler, StorableObject storable, Class custom) { 36 super(name, ifc, handler, storable, custom); 37 } 38 39 protected String getConstructorDescriptor() { 40 return "(Lorg/netbeans/mdr/storagemodel/StorableObject;)V"; } 42 43 protected MethodInfo[] generateMethods() throws IOException { 44 try { 45 ArrayList methods = new ArrayList(); 46 HashSet createdMethods = new HashSet(); 47 methods.add(generateConstructor()); 48 for (Iterator it = new ContainsIterator(obj.getMetaObject()); it.hasNext();) { 49 StorableObject element = (StorableObject) it.next(); 50 String elementName = (String ) element.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 51 String metaTypeName = (String ) element.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 52 String substName = TagSupport.getSubstName(element, elementName, metaTypeName); 53 if (metaTypeName.equals(MOFConstants.SH_MODEL_ATTRIBUTE)) { 54 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 55 ScopeKind scope = (ScopeKind) element.getAttribute(MOFConstants.SH_MODEL_FEATURE_SCOPE); 56 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS) && scope.equals(ScopeKindEnum.INSTANCE_LEVEL)) { 57 String attrName = firstUpper(substName); 58 String attrTypeName = TagSupport.getDataTypeName(getAttrType(element)); 59 MultiplicityType multiplicity = (MultiplicityType) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_MULTIPLICITY); 60 if (multiplicity.getUpper() == 1) { 61 String name; 62 String setterName; 63 if (attrTypeName.equals("java.lang.Boolean")) { if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); else name = "is" + attrName; setterName = "set" + name.substring(2); } else { 68 name = "get" + attrName; setterName = "set" + attrName; } 71 String getterType = attrTypeName; 72 if (multiplicity.getLower() == 1) 73 getterType = getPrimitiveName(getterType); 74 String sign = name + getMethodDescriptor(new String [0], getterType); 75 if (!createdMethods.contains(sign)) { 76 methods.addAll(getFeatureMethod(name, new String [0], getterType, M_GET_NAME, M_GET_DESC, M_GET_TYPE, element, (StorableFeatured)obj, "_getAttribute")); createdMethods.add(sign); 78 } 79 boolean changeable = ((Boolean ) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_IS_CHANGEABLE)).booleanValue(); 80 if (changeable) { 81 sign = setterName + getMethodDescriptor(new String [] {getterType}, "void"); if (!createdMethods.contains(sign)) { 83 methods.addAll(getFeatureMethod(setterName, new String [] {getterType}, "void", M_SET_NAME, M_SET_DESC, M_SET_TYPE, element, (StorableFeatured)obj, "_setAttribute")); createdMethods.add(sign); 85 } 86 } 87 } else if (multiplicity.getUpper() != 0) { 88 String sign = "get" + attrName + getMethodDescriptor(new String [0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED)); if (!createdMethods.contains(sign)) { 90 methods.addAll(getFeatureMethod("get" + attrName, new String [0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED), M_GET_NAME, M_GET_DESC, M_GET_TYPE, element, (StorableFeatured)obj, "_getAttribute")); createdMethods.add(sign); 92 } 93 } 94 } 95 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_OPERATION)) { 96 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 97 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) { 98 Collection parameters = new ArrayList(); 99 String operType = "void"; for (Iterator itt = ((List) element.getReference(MOFConstants.SH_MODEL_NAMESPACE_CONTENTS)).iterator(); itt.hasNext();) { 101 StorableObject el = (StorableObject) itt.next(); 102 String mtName = (String ) el.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 103 if (mtName.equals(MOFConstants.SH_MODEL_PARAMETER)) { 104 DirectionKind direction = (DirectionKind) el.getAttribute(MOFConstants.SH_MODEL_PARAMETER_DIRECTION_KIND); 105 if (direction.equals(DirectionKindEnum.RETURN_DIR)) 106 operType = getAttrTypeName(el); 107 else 108 parameters.add(getAttrTypeName(el) + (direction.equals(DirectionKindEnum.IN_DIR) ? "" : "[]")); } 110 } 111 String [] prms = (String [])parameters.toArray(new String [parameters.size()]); 112 String sign = substName + getMethodDescriptor(prms, operType); 113 if (!createdMethods.contains(sign)) { 114 MethodInfo mInfo = getOperationMethod(substName, prms, operType, elementName); 115 if (mInfo != null) { 116 Collection exceptions = (Collection) element.getReference(MOFConstants.SH_MODEL_OPERATION_EXCEPTIONS); 117 short[] declaredExceptions = new short[exceptions.size()]; 118 int i = 0; 119 for (Iterator itt = exceptions.iterator(); itt.hasNext(); i++) 120 declaredExceptions[i] = cp.getClass(dotToSlash(TagSupport.getTypeFullName((StorableObject)itt.next()))); 121 mInfo.setDeclaredExceptions(declaredExceptions); 122 methods.add(mInfo); 123 } 124 createdMethods.add(sign); 125 } 126 } 127 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_REFERENCE)) { 128 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 129 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) { 130 String refName = firstUpper(substName); 131 String refType = TagSupport.getDataTypeName(getAttrType(element)); 132 MultiplicityType multiplicity = (MultiplicityType) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_MULTIPLICITY); 133 if (multiplicity.getUpper() == 1) { 134 String sign = "get" + refName + getMethodDescriptor(new String [0], refType); if (!createdMethods.contains(sign)) { 136 methods.addAll(getHandlerMethod("get" + refName, new String [0], refType, M_GET_NAME + "R", M_GET_DESC, M_GET_TYPE, elementName)); createdMethods.add(sign); 138 } 139 boolean changeable = ((Boolean ) element.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_IS_CHANGEABLE)).booleanValue(); 140 if (changeable) { 141 sign = "set" + refName + getMethodDescriptor(new String [] {refType}, "void"); if (!createdMethods.contains(sign)) { 143 methods.addAll(getHandlerMethod("set" + refName, new String [] {refType}, "void", M_SET_NAME + "R", M_SET_DESC, M_SET_TYPE, elementName)); createdMethods.add(sign); 145 } 146 } 147 } else if (multiplicity.getUpper() != 0) { 148 String sign = "get" + refName + getMethodDescriptor(new String [0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED)); if (!createdMethods.contains(sign)) { 150 methods.addAll(getHandlerMethod("get" + refName, new String [0], (multiplicity.isOrdered() ? DT_ORDERED : DT_MULTIVALUED), M_GET_NAME + "R", M_GET_DESC, M_GET_TYPE, elementName)); createdMethods.add(sign); 152 } 153 } 154 } 155 } 156 } 157 158 MethodInfo[] mtds = super.generateMethods(); 159 for(int i = 0; i < mtds.length; i++) 160 methods.add(mtds[i]); 161 return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 162 }catch (Exception e) { 163 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 164 } 165 } 166 } 167 | Popular Tags |