1 19 package org.netbeans.mdr.handlers.gen; 20 21 import org.netbeans.mdr.persistence.StorageException; 22 import org.netbeans.mdr.storagemodel.StorableFeatured; 23 import org.netbeans.mdr.storagemodel.StorableObject; 24 import org.netbeans.mdr.util.DebugException; 25 import org.netbeans.mdr.util.Logger; 26 import org.netbeans.mdr.util.MOFConstants; 27 import java.io.DataOutputStream ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 34 39 abstract class FeaturedGenerator extends HandlerGenerator { 40 41 protected static final String M_GET_NAME = "Get"; protected static final String M_GET_DESC = "(Ljava/lang/String;)"; protected static final String M_GET_TYPE = "Ljava/lang/Object;"; protected static final String M_SET_NAME = "Set"; protected static final String M_SET_DESC = "(Ljava/lang/String;Ljava/lang/Object;)"; protected static final String M_SET_TYPE = "V"; 48 FeaturedGenerator(String name, Class ifc, Class handler, StorableFeatured storable, Class custom) { 49 super(name, ifc, handler, storable, custom); 50 dispatchMethods.put("_invokeOperation", new HashMap ()); dispatchMethods.put("_setAttribute", new HashMap ()); dispatchMethods.put("_getAttribute", new HashMap ()); } 54 55 protected MethodInfo[] generateMethods() throws IOException { 56 try { 57 ArrayList methods = new ArrayList (); 58 methods.add(getDispatcherMethod("_invokeOperation", new String [] {"java.lang.String", "java.lang.Object[]"}, "java.lang.Object", (HashMap ) dispatchMethods.get("_invokeOperation"))); methods.add(getDispatcherMethod("_setAttribute", new String [] {"java.lang.String", "java.lang.Object"}, "void", (HashMap ) dispatchMethods.get("_setAttribute"))); methods.add(getDispatcherMethod("_getAttribute", new String [] {"java.lang.String"}, "java.lang.Object", (HashMap ) dispatchMethods.get("_getAttribute"))); return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]); 62 } catch (Exception e) { 63 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 64 } 65 } 66 67 protected Collection getFeatureMethod(String methodName, String [] parameterTypes, String returnType, String handlerName, String handlerDescriptor, String handlerType, StorableObject feature, StorableFeatured parent, String dispatcher) throws IOException , StorageException { 68 String name = (String ) feature.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 69 if (dispatcher != null) { 70 HashMap item = (HashMap ) dispatchMethods.get(dispatcher); 71 item.put(name, new MethodDescHolder(methodName, parameterTypes, returnType)); 72 } 73 if (((Boolean ) feature.getAttribute(MOFConstants.SH_MODEL_ASSOCIATION_IS_DERIVED)).booleanValue()) { 74 return Collections.EMPTY_LIST; 75 } else if (parent == null) { 76 return getHandlerMethod(methodName, parameterTypes, returnType, handlerName, handlerDescriptor, handlerType, name); 77 } else { 78 return getAttributeMethod(methodName, parameterTypes, returnType, handlerName, handlerDescriptor, handlerType, name, parent.getClassProxy().getAttrIndex(name)); 79 } 80 } 81 82 protected Collection getAttributeMethod(String methodName, String [] parameterTypes, String returnType, String handlerName, String handlerDescriptor, String handlerType, String featureName, int attrIndex) throws IOException { 83 short delegateMethod = getHandlerIndex(handlerName, "(I" + handlerDescriptor.substring("(Ljava/lang/String;".length()), handlerType); short preMethod = getPreIndex(handlerName, handlerDescriptor); 85 short postMethod = getPostIndex(handlerName, handlerType); 86 String desc = getMethodDescriptor(parameterTypes, returnType); 87 boolean isCustom = customImplContainsMethod(customImpl, methodName, desc); 88 89 int[] parameterSlot = new int[parameterTypes.length]; 90 int nextSlot = 1; 91 for (int i = 0; i < parameterSlot.length; i++) { 92 parameterSlot[i] = nextSlot; 93 nextSlot += getWordsPerType(parameterTypes[i]); 94 } 95 int localSlot0 = nextSlot; 96 97 short fail = (short) localSlot0; 98 short extraInfo = (short) (localSlot0 + 1); 99 short result = (short) (localSlot0 + 2); 100 short addr = (short) (localSlot0 + 3); 101 short exception = (short) (localSlot0 + 4); 102 short customResult = (short) (localSlot0 + 5); 103 104 MethodInfo minfo = new MethodInfo(methodName, desc, ACC_PUBLIC | ACC_FINAL); 105 DataOutputStream out = new DataOutputStream (minfo.getCodeStream()); 106 MethodInfo cminfo = null; 107 DataOutputStream cout = null; 108 if (isCustom) { 109 cminfo = new MethodInfo(CUSTOM_PREFIX + methodName, desc, ACC_PUBLIC | ACC_FINAL); 110 cout = new DataOutputStream (cminfo.getCodeStream()); 111 } 112 113 out.writeByte(opc_iconst_1); 115 code_istore(fail, out); 116 out.writeByte(opc_aconst_null); 118 out.writeByte(opc_dup); 119 code_astore(extraInfo, out); 120 code_astore(result, out); 122 123 code_aload(0, out); 125 126 code_ldc(cp.getString(featureName), out); 128 129 for (int i = 0; i < parameterTypes.length; i++) { 131 codeWrapArgument(parameterTypes[i], parameterSlot[i], out); 132 } 133 134 out.writeByte(opc_invokespecial); 136 out.writeShort(preMethod); 137 138 code_astore(extraInfo, out); 140 141 short tryStart = (short) out.size(); 143 144 if (isCustom) { 145 code_aload(0, out); 147 148 for (int i = 0; i < parameterTypes.length; i++) { 150 codeLoad(parameterSlot[i], parameterTypes[i], out); 151 } 152 153 out.writeByte(opc_invokespecial); 155 out.writeShort(cp.getMethodRef(dotToSlash(superclassName), methodName, desc)); 156 157 if (!returnType.equals("void")) { if (PrimitiveTypeInfo.get(returnType) == null) 159 code_astore(result, out); 160 else { 161 codeStore(customResult, returnType, out); 162 codeWrapArgument(returnType, customResult, out); 163 code_astore(result, out); 164 } 165 } 166 167 code_aload(0, cout); 169 170 code_ipush(attrIndex, cout); 171 172 for (int i = 0; i < parameterTypes.length; i++) { 174 codeWrapArgument(parameterTypes[i], parameterSlot[i], cout); 175 } 176 177 cout.writeByte(opc_invokespecial); 179 cout.writeShort(delegateMethod); 180 181 if (returnType.equals("void")) cout.writeByte(opc_return); 183 else 184 codeUnwrapReturnValue(returnType, cout); 185 } 186 else { 187 code_aload(0, out); 189 190 code_ipush(attrIndex, out); 191 192 for (int i = 0; i < parameterTypes.length; i++) { 194 codeWrapArgument(parameterTypes[i], parameterSlot[i], out); 195 } 196 197 out.writeByte(opc_invokespecial); 199 out.writeShort(delegateMethod); 200 201 if (!returnType.equals("void")) { code_astore(result, out); 203 } 204 } 205 206 out.writeByte(opc_iconst_0); 208 code_istore(fail, out); 209 210 out.writeByte(opc_jsr); 212 if (returnType.equals("void")) { out.writeShort(3 + 1 + 2*getBytesForLoadOrStore(exception) + 4); 214 out.writeByte(opc_return); 215 } else { 216 if (isCustom) { 217 if (PrimitiveTypeInfo.get(returnType) != null) { 218 out.writeShort(3 + getBytesForLoadOrStore(customResult) + 1 + 2*getBytesForLoadOrStore(exception) + 4); 219 codeLoad(customResult, returnType, out); 220 if (returnType.equals("int") || returnType.equals("boolean") || returnType.equals("byte") || returnType.equals("char") || returnType.equals("short")) out.writeByte(opc_ireturn); 222 else if (returnType.equals("long")) out.writeByte(opc_lreturn); 224 else if (returnType.equals("float")) out.writeByte(opc_freturn); 226 else if (returnType.equals("double")) out.writeByte(opc_dreturn); 228 else 229 _assert(false); 230 } else { 231 out.writeShort(3 + getBytesForLoadOrStore(result) + 1 + 2*getBytesForLoadOrStore(exception) + 4); 232 code_aload(result, out); 233 out.writeByte(opc_areturn); 234 } 235 } 236 else { 237 out.writeShort(3 + getBytesForLoadOrStore(result) + getBytesForUnwrapReturn(returnType) + 2*getBytesForLoadOrStore(exception) + 4); 238 code_aload(result, out); 240 codeUnwrapReturnValue(returnType, out); 242 } 243 } 244 short catchStart = (short) out.size(); 246 247 code_astore(exception, out); 249 250 out.writeByte(opc_jsr); 252 out.writeShort(3 + getBytesForLoadOrStore(exception) + 1); 253 254 out.writeByte(opc_aload); 256 out.writeByte(exception); 257 258 out.writeByte(opc_athrow); 260 261 code_astore(addr, out); 264 265 code_aload(0, out); 267 if (!returnType.equals("void")) { code_aload(result, out); 269 } 270 code_aload(extraInfo, out); 271 code_iload(fail, out); 272 273 out.writeByte(opc_invokespecial); 275 out.writeShort(postMethod); 276 277 out.writeByte(opc_ret); 279 out.writeByte(addr); 280 281 minfo.getExceptionTable().add(new ExceptionTableEntry(tryStart, catchStart, catchStart, (short) 0)); 282 283 minfo.setMaxStack((short)10); 284 minfo.setMaxLocals((short) (localSlot0 + 7)); 285 if (isCustom) { 286 cminfo.setMaxStack((short)10); 287 cminfo.setMaxLocals((short) (localSlot0 + 5)); 288 } 289 290 ArrayList ret = new ArrayList (); 291 ret.add(minfo); 292 if (isCustom) 293 ret.add(cminfo); 294 return ret; 295 } 296 297 protected MethodInfo getOperationMethod(String methodName, String [] parameterTypes, String returnType, String featureName) throws IOException { 298 HashMap item = (HashMap ) dispatchMethods.get("_invokeOperation"); item.put(featureName, new MethodDescHolder(methodName, parameterTypes, returnType)); 300 return null; 301 } 302 } 303 | Popular Tags |