1 20 package org.objectweb.modfact.jmi.impl.tayloredBased; 21 22 import org.objectweb.modfact.jmi.helper.*; 23 24 25 import javax.jmi.model.*; 26 import java.util.*; 27 28 import org.objectweb.modfact.jmi.generator.PrintGenerator; 29 30 34 public abstract class CommonImplementationGenerator 35 extends PrintGenerator { 36 37 public void attributeTemplate(Attribute attribute) { 38 if (attribute 39 .getVisibility() 40 .equals(VisibilityKindEnum.forName("public_vis"))) { 41 43 structuralFeatureAccessor(attribute); 45 46 structuralFeatureMutator(attribute); 48 49 } 50 } 51 52 public void referenceTemplate(Reference reference) { 53 if (reference 54 .getVisibility() 55 .equals(VisibilityKindEnum.forName("public_vis"))) { 56 58 structuralFeatureAccessor(reference); 60 61 structuralFeatureMutator(reference); 63 64 } 65 } 66 67 public void structuralFeatureAccessor(StructuralFeature f) { 68 69 String javaType = JMIProvider.typeTemplate(f); 70 String accessorName = JMIProvider.jmiAccessorName(f); 71 72 out.println( 73 "\tpublic " 74 + javaType 75 + " " 76 + accessorName 77 + "() throws javax.jmi.reflect.JmiException { "); 78 79 out.println( 80 "\t return (" 81 + javaType 82 + ") " 83 + ImplHelper.unwrap(f, "refGetValue(\"" + f.getName() + "\")") 84 + ";"); 85 out.println("\t } "); 86 } 87 88 public void structuralFeatureMutator(StructuralFeature f) { 89 90 String mutatorName = JMIProvider.jmiMutatorName(f); 91 String javaType = JMIProvider.typeTemplate(f); 92 93 if (f.getMultiplicity().getUpper() == 1 && f.isChangeable()) { 94 out.println( 95 "\tpublic void " 96 + mutatorName 97 + "(" 98 + javaType 99 + " newValue) throws javax.jmi.reflect.JmiException { "); 100 101 out.println( 102 "\t refSetValue(" 103 + "\"" 104 + f.getName() 105 + "\"," 106 + ImplHelper.wrap(f, "newValue") 107 + ");"); 108 out.println("\t } "); 109 } 110 } 111 112 public void operationTemplate(Operation operation) { 114 if (operation 115 .getVisibility() 116 .equals(VisibilityKindEnum.forName("public_vis"))) { 117 Parameter[] parameters = MofHelper.parametersOf(operation); 120 MofException[] exceptions = MofHelper.exceptionsOf(operation); 121 Parameter returnParameter = null; 122 123 for (int i = 0; i < parameters.length; i++) { 124 if (parameters[i] 125 .getDirection() 126 .equals(DirectionKindEnum.RETURN_DIR)) { 127 returnParameter = parameters[i]; 128 } 129 } 130 131 if (returnParameter == null) 132 out.print("\tpublic void "); 133 else 134 out.print( 135 "\tpublic " 136 + JMIProvider.typeTemplate(returnParameter) 137 + " "); 138 139 out.print(operation.getName() + "("); 140 141 boolean comma = false; 142 for (int i = 0; i < parameters.length; i++) { 143 DirectionKind dir = parameters[i].getDirection(); 144 if (!dir.equals(DirectionKindEnum.RETURN_DIR)) { 145 if (comma) 146 out.print(","); 147 out.print( 148 JMIProvider.typeTemplate(parameters[i]) 149 + " " 150 + parameters[i].getName()); 151 comma = true; 152 } 153 } 154 out.print(") throws "); 155 156 for (int i = 0; i < exceptions.length; i++) { 158 out.print(exceptions[i].getName() + ","); 159 } 160 out.println("javax.jmi.reflect.JmiException { "); 161 out.println("throw new RuntimeException(\"No Implementation\");"); 162 out.println("\t } "); 163 } 164 } 165 166 public void dataTypeTemplates(Namespace ns) { 168 DataType[] containedDataTypes = MofHelper.datatypesOf(ns, true); 171 for (int i = 0; i < containedDataTypes.length; i++) { 172 if (containedDataTypes[i] instanceof StructureType) { 173 structTemplate((StructureType) containedDataTypes[i]); 174 175 } 176 } 177 } 178 179 public void structTemplate(StructureType t) { 180 181 String javaType = JMIProvider.jmiDataTypeQualifiedName(t); 182 out.print("\tpublic " + javaType + " create" + t.getName() + " ("); 183 184 ArgList argList = new ArgList(); 185 StructureField[] fields = MofHelper.structureFieldsOf(t); 186 for (int j = 0; j < fields.length; j++) { 187 out.print(JMIProvider.typeTemplate(fields[j])); 188 out.print(" "); 190 out.print(fields[j].getName()); 192 argList.add(fields[j]); 193 194 if (j != fields.length - 1) 195 out.print(" , "); 196 } 197 out.println(") throws javax.jmi.reflect.JmiException { "); 198 out.print(argList.code); 199 out.println( 200 "\t return (" 201 + javaType 202 + ") refCreateStruct(\"" 203 + t.getName() 204 + "\",list);"); 205 out.println("}"); 206 } 207 208 void newStructTemplate(Namespace p) { 209 out.println("\tpublic RefStructImpl newStruct(String n) {"); 210 Iterator it = 211 MofHelper 212 .filterContentsByClass(p, StructureType.class, true) 213 .iterator(); 214 while (it.hasNext()) { 215 StructureType content = (StructureType) it.next(); 216 out.println( 217 "\t\t if(n.equals(\"" 218 + content.getName() 219 + "\")) " 220 + "return new " +ImplHelper.implPrefix 221 + JMIProvider.shortQualifierOf(content) 222 + "." 223 + content.getName() 224 + "Impl" 225 + "();"); 226 } 227 out.println("\t\t throw new RuntimeException(\"invalide StructureType: '\" + n + \"'\");"); 228 out.println("\t}"); 229 } 230 231 void newEnumTemplate(Namespace p) { 232 out.println("\tpublic Class newEnum(String n) {"); 233 Iterator it = 234 MofHelper 235 .filterContentsByClass(p, EnumerationType.class, true) 236 .iterator(); 237 while (it.hasNext()) { 238 EnumerationType content = (EnumerationType) it.next(); 239 out.println( 240 "\t\t if(n.equals(\"" 241 + content.getName() 242 + "\")) " 243 + "return " 244 + JMIProvider.jmiDataTypeQualifiedName(content) 245 + "Enum.class;"); 246 } 247 out.println("\t\t throw new RuntimeException(\"invalide EnumType : '\" + n + \"'\");"); 248 out.println("\t}"); 249 } 250 251 static class ArgList { 253 String code = "\t\tjava.util.List list = new java.util.Vector(); \n"; 254 255 void add(TypedElement t) { 256 code = 257 code 258 + "\t\t list.add(" 259 + ImplHelper.wrap(t, t.getName()) 260 + ");\n"; 261 } 262 263 } 264 265 } 266 | Popular Tags |