1 19 package org.netbeans.mdr.handlers.gen; 20 21 import org.netbeans.mdr.persistence.StorageException; 22 import org.netbeans.mdr.storagemodel.StorableObject; 23 import org.netbeans.mdr.storagemodel.StorablePackage; 24 import org.netbeans.mdr.util.DebugException; 25 import org.netbeans.mdr.util.Logger; 26 import org.netbeans.mdr.util.MOFConstants; 27 import javax.jmi.model.VisibilityKind; 28 import javax.jmi.model.VisibilityKindEnum; 29 import java.io.DataOutputStream ; 30 import java.io.IOException ; 31 import java.util.*; 32 33 38 class PackageGenerator extends HandlerGenerator { 39 private static final String CLASS_SUFFIX = "Class"; private static final String PACKAGE_SUFFIX = "Package"; 42 private static final String M_ASSOC_NAME = "GetAssociationProxy"; private static final String M_ASSOC_DESC = "(Ljava/lang/String;)"; private static final String M_ASSOC_TYPE = "Ljavax/jmi/reflect/RefAssociation;"; private static final String M_CLASS_NAME = "GetClassProxy"; private static final String M_CLASS_DESC = "(Ljava/lang/String;)"; private static final String M_CLASS_TYPE = "Ljavax/jmi/reflect/RefClass;"; private static final String M_PACKAGE_NAME = "GetPackageProxy"; private static final String M_PACKAGE_DESC = "(Ljava/lang/String;)"; private static final String M_PACKAGE_TYPE = "Ljavax/jmi/reflect/RefPackage;"; private static final String M_STRUCT_NAME = "Struct"; private static final String M_STRUCT_DESC = "(Ljava/lang/String;[Ljava/lang/Object;)"; private static final String M_STRUCT_TYPE = "Ljavax/jmi/reflect/RefStruct;"; 55 PackageGenerator(String name, Class ifc, Class handler, StorablePackage storable, Class custom) { 56 super(name, ifc, handler, storable, custom); 57 dispatchMethods.put("_getClass", new HashMap()); dispatchMethods.put("_getAssociation", new HashMap()); dispatchMethods.put("_getPackage", new HashMap()); dispatchMethods.put("_createStruct", new HashMap()); } 62 63 protected String getConstructorDescriptor() { 64 return "(Lorg/netbeans/mdr/storagemodel/StorablePackage;)V"; } 66 67 protected MethodInfo[] generateMethods() throws IOException { 68 try { 69 ArrayList methods = new ArrayList(); 70 HashSet createdMethods = new HashSet(); 71 methods.add(generateConstructor()); 72 for (Iterator it = new ContainsIterator(obj.getMetaObject()); it.hasNext();) { 73 StorableObject element = (StorableObject) it.next(); 74 String elementName = (String ) element.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 75 String metaTypeName = (String ) element.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 76 String substName = TagSupport.getSubstName(element, elementName, metaTypeName); 77 String typeName = TagSupport.getTypeFullName(element, substName); 78 if (metaTypeName.equals(MOFConstants.SH_MODEL_ASSOCIATION)) { 79 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 80 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) { 81 String sign = "get" + substName + getMethodDescriptor(new String [0], typeName); if (!createdMethods.contains(sign)) { 83 methods.addAll(getPackageMethod("get" + substName, new String [0], typeName, M_ASSOC_NAME, M_ASSOC_DESC, M_ASSOC_TYPE, elementName, "_getAssociation")); createdMethods.add(sign); 85 } 86 } 87 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_CLASS)) { 88 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 89 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) { 90 String sign = "get" + substName + getMethodDescriptor(new String [0], typeName + CLASS_SUFFIX); if (!createdMethods.contains(sign)) { 92 methods.addAll(getPackageMethod("get" + substName, new String [0], typeName + CLASS_SUFFIX, M_CLASS_NAME, M_CLASS_DESC, M_CLASS_TYPE, elementName, "_getClass")); createdMethods.add(sign); 94 } 95 } 96 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_PACKAGE)) { 97 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 98 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS)) { 99 String sign = "get" + substName + getMethodDescriptor(new String [0], typeName + PACKAGE_SUFFIX); if (!createdMethods.contains(sign)) { 101 methods.addAll(getPackageMethod("get" + substName, new String [0], typeName + PACKAGE_SUFFIX, M_PACKAGE_NAME, M_PACKAGE_DESC, M_PACKAGE_TYPE, elementName, "_getPackage")); createdMethods.add(sign); 103 } 104 } 105 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_STRUCTURE_TYPE)) { 106 ArrayList parameters = new ArrayList(); 107 for (Iterator itt = ((List) element.getReference(MOFConstants.SH_MODEL_NAMESPACE_CONTENTS)).iterator(); itt.hasNext();) { 108 StorableObject field = (StorableObject) itt.next(); 109 String fieldTypeName = (String ) field.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 110 if (fieldTypeName.equals(MOFConstants.SH_MODEL_STRUCTURE_FIELD)) 111 parameters.add(getTypeName2(field)); 112 } 113 String [] prms = (String [])parameters.toArray(new String [parameters.size()]); 114 String sign = "create" + firstUpper(substName) + getMethodDescriptor(prms, typeName); if (!createdMethods.contains(sign)) { 116 methods.addAll(getCreateMethod("create" + firstUpper(substName), prms, typeName, M_STRUCT_NAME, M_STRUCT_DESC, M_STRUCT_TYPE, elementName)); createdMethods.add(sign); 118 } 119 } else if (metaTypeName.equals(MOFConstants.SH_MODEL_IMPORT)) { 120 VisibilityKind visibility = (VisibilityKind) element.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 121 boolean clustered = ((Boolean ) element.getAttribute(MOFConstants.SH_MODEL_IMPORT_IS_CLUSTERED)).booleanValue(); 122 StorableObject namespace = (StorableObject) element.getReference(MOFConstants.SH_MODEL_IMPORT_IMPORTED_NAMESPACE); 123 VisibilityKind nsVisibility = (VisibilityKind) namespace.getAttribute(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_VISIBILITY); 124 if (visibility.equals(VisibilityKindEnum.PUBLIC_VIS) && nsVisibility.equals(VisibilityKindEnum.PUBLIC_VIS) && clustered) { 125 String retName = TagSupport.getTypeFullName(namespace) + PACKAGE_SUFFIX; 126 String sign = "get" + substName + getMethodDescriptor(new String [0], retName); if (!createdMethods.contains(sign)) { 128 methods.addAll(getPackageMethod("get" + substName, new String [0], retName, M_PACKAGE_NAME, M_PACKAGE_DESC, M_PACKAGE_TYPE, elementName, "_getPackage")); createdMethods.add(sign); 130 } 131 } 132 } 133 } 134 methods.add(getDispatcherMethod("_getClass", new String [] {"java.lang.String"}, "javax.jmi.reflect.RefClass", (HashMap) dispatchMethods.get("_getClass"))); methods.add(getDispatcherMethod("_getAssociation", new String [] {"java.lang.String"}, "javax.jmi.reflect.RefAssociation", (HashMap) dispatchMethods.get("_getAssociation"))); methods.add(getDispatcherMethod("_getPackage", new String [] {"java.lang.String"}, "javax.jmi.reflect.RefPackage", (HashMap) dispatchMethods.get("_getPackage"))); methods.add(getDispatcherMethod("_createStruct", new String [] {"java.lang.String", "java.lang.Object[]"}, "javax.jmi.reflect.RefStruct", (HashMap) dispatchMethods.get("_createStruct"))); return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 139 } catch (Exception e) { 140 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 141 } 142 } 143 144 protected Collection getPackageMethod(String methodName, String [] parameterTypes, String returnType, String handlerName, String handlerDescriptor, String handlerType, String featureName, String dispatcher) throws IOException , StorageException { 145 if (dispatcher != null) { 146 HashMap item = (HashMap) dispatchMethods.get(dispatcher); 147 item.put(featureName, new MethodDescHolder(methodName, parameterTypes, returnType)); 148 } 149 return getHandlerMethod(methodName, parameterTypes, returnType, handlerName, handlerDescriptor, handlerType, featureName); 150 } 151 152 protected Collection getCreateMethod(String methodName, String [] parameterTypes, String returnType, String handlerName, String handlerDescriptor, String handlerType, String featureName) throws IOException { 153 154 if (featureName != null) { 155 HashMap item = (HashMap) dispatchMethods.get("_createStruct"); item.put(featureName, new MethodDescHolder(methodName, parameterTypes, returnType)); 157 } 158 159 short delegateMethod = getHandlerIndex(handlerName, handlerDescriptor, handlerType); 160 short preMethod = getPreIndex(handlerName, handlerDescriptor); 161 short postMethod = getPostIndex(handlerName, handlerType); 162 String desc = getMethodDescriptor(parameterTypes, returnType); 163 boolean isCustom = customImplContainsMethod(customImpl, methodName, desc); 164 165 int[] parameterSlot = new int[parameterTypes.length]; 166 int nextSlot = 1; 167 for (int i = 0; i < parameterSlot.length; i++) { 168 parameterSlot[i] = nextSlot; 169 nextSlot += getWordsPerType(parameterTypes[i]); 170 } 171 int localSlot0 = nextSlot; 172 173 short fail = (short) localSlot0; 174 short extraInfo = (short) (localSlot0 + 1); 175 short result = (short) (localSlot0 + 2); 176 short addr = (short) (localSlot0 + 3); 177 short exception = (short) (localSlot0 + 4); 178 179 MethodInfo minfo = new MethodInfo(methodName, desc, ACC_PUBLIC | ACC_FINAL); 180 DataOutputStream out = new DataOutputStream (minfo.getCodeStream()); 181 MethodInfo cminfo = null; 182 DataOutputStream cout = null; 183 if (isCustom) { 184 cminfo = new MethodInfo(CUSTOM_PREFIX + methodName, desc, ACC_PUBLIC | ACC_FINAL); 185 cout = new DataOutputStream (cminfo.getCodeStream()); 186 } 187 188 out.writeByte(opc_iconst_1); 190 code_istore(fail, out); 191 out.writeByte(opc_aconst_null); 193 out.writeByte(opc_dup); 194 code_astore(extraInfo, out); 195 code_astore(result, out); 197 198 code_aload(0, out); 200 201 if (featureName != null) 202 code_ldc(cp.getString(featureName), out); 204 205 code_ipush(parameterTypes.length, out); 207 out.writeByte(opc_anewarray); 208 out.writeShort(cp.getClass("java/lang/Object")); 210 for (int i = 0; i < parameterTypes.length; i++) { 211 out.writeByte(opc_dup); 212 code_ipush(i, out); 213 codeWrapArgument(parameterTypes[i], parameterSlot[i], out); 214 out.writeByte(opc_aastore); 215 } 216 217 out.writeByte(opc_invokespecial); 219 out.writeShort(preMethod); 220 221 code_astore(extraInfo, out); 223 224 short tryStart = (short) out.size(); 226 227 if (isCustom) { 228 code_aload(0, out); 230 231 for (int i = 0; i < parameterTypes.length; i++) { 233 codeLoad(parameterSlot[i], parameterTypes[i], out); 234 } 235 236 out.writeByte(opc_invokespecial); 238 out.writeShort(cp.getMethodRef(dotToSlash(superclassName), methodName, desc)); 239 240 code_astore(result, out); 241 242 code_aload(0, cout); 244 245 if (featureName != null) 246 code_ldc(cp.getString(featureName), cout); 248 249 code_ipush(parameterTypes.length, cout); 251 cout.writeByte(opc_anewarray); 252 cout.writeShort(cp.getClass("java/lang/Object")); 254 for (int i = 0; i < parameterTypes.length; i++) { 255 cout.writeByte(opc_dup); 256 code_ipush(i, cout); 257 codeWrapArgument(parameterTypes[i], parameterSlot[i], cout); 258 cout.writeByte(opc_aastore); 259 } 260 261 cout.writeByte(opc_invokespecial); 263 cout.writeShort(delegateMethod); 264 265 cout.writeByte(opc_checkcast); 267 cout.writeShort(cp.getClass(dotToSlash(returnType))); 268 cout.writeByte(opc_areturn); 269 } 270 else { 271 code_aload(0, out); 273 274 if (featureName != null) 275 code_ldc(cp.getString(featureName), out); 277 278 code_ipush(parameterTypes.length, out); 280 out.writeByte(opc_anewarray); 281 out.writeShort(cp.getClass("java/lang/Object")); 283 for (int i = 0; i < parameterTypes.length; i++) { 284 out.writeByte(opc_dup); 285 code_ipush(i, out); 286 codeWrapArgument(parameterTypes[i], parameterSlot[i], out); 287 out.writeByte(opc_aastore); 288 } 289 290 out.writeByte(opc_invokespecial); 292 out.writeShort(delegateMethod); 293 294 code_astore(result, out); 295 } 296 297 out.writeByte(opc_iconst_0); 299 code_istore(fail, out); 300 301 out.writeByte(opc_jsr); 303 304 if (isCustom) { 305 out.writeShort(3 + getBytesForLoadOrStore(result) + 1 + 2*getBytesForLoadOrStore(exception) + 4); 306 307 code_aload(result, out); 309 310 out.writeByte(opc_areturn); 312 } 313 else { 314 out.writeShort(3 + getBytesForLoadOrStore(result) + 4 + 2*getBytesForLoadOrStore(exception) + 4); 315 316 code_aload(result, out); 318 319 out.writeByte(opc_checkcast); 321 out.writeShort(cp.getClass(dotToSlash(returnType))); 322 out.writeByte(opc_areturn); 323 } 324 325 short catchStart = (short) out.size(); 327 328 code_astore(exception, out); 330 331 out.writeByte(opc_jsr); 333 out.writeShort(3 + getBytesForLoadOrStore(exception) + 1); 334 335 code_aload(exception, out); 337 338 out.writeByte(opc_athrow); 340 341 code_astore(addr, out); 344 345 code_aload(0, out); 347 code_aload(result, out); 348 code_aload(extraInfo, out); 349 code_iload(fail, out); 350 351 out.writeByte(opc_invokespecial); 353 out.writeShort(postMethod); 354 355 out.writeByte(opc_ret); 357 out.writeByte(addr); 358 359 minfo.getExceptionTable().add(new ExceptionTableEntry(tryStart, catchStart, catchStart, (short) 0)); 360 361 minfo.setMaxStack((short) 15); 362 minfo.setMaxLocals((short) (localSlot0 + 5)); 363 if (isCustom) { 364 cminfo.setMaxStack((short)10); 365 cminfo.setMaxLocals((short) (localSlot0 + 5)); 366 } 367 368 ArrayList ret = new ArrayList(); 369 ret.add(minfo); 370 if (isCustom) 371 ret.add(cminfo); 372 return ret; 373 } 374 } 375 | Popular Tags |