KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > gen > PackageGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.*;
32
33 /**
34  *
35  * @author Martin Matula, Dusan Balek
36  * @version
37  */

38 class PackageGenerator extends HandlerGenerator {
39     private static final String JavaDoc CLASS_SUFFIX = "Class"; //NOI18N
40
private static final String JavaDoc PACKAGE_SUFFIX = "Package"; //NOI18N
41

42     private static final String JavaDoc M_ASSOC_NAME = "GetAssociationProxy"; //NOI18N
43
private static final String JavaDoc M_ASSOC_DESC = "(Ljava/lang/String;)"; //NOI18N
44
private static final String JavaDoc M_ASSOC_TYPE = "Ljavax/jmi/reflect/RefAssociation;"; //NOI18N
45
private static final String JavaDoc M_CLASS_NAME = "GetClassProxy"; //NOI18N
46
private static final String JavaDoc M_CLASS_DESC = "(Ljava/lang/String;)"; //NOI18N
47
private static final String JavaDoc M_CLASS_TYPE = "Ljavax/jmi/reflect/RefClass;"; //NOI18N
48
private static final String JavaDoc M_PACKAGE_NAME = "GetPackageProxy"; //NOI18N
49
private static final String JavaDoc M_PACKAGE_DESC = "(Ljava/lang/String;)"; //NOI18N
50
private static final String JavaDoc M_PACKAGE_TYPE = "Ljavax/jmi/reflect/RefPackage;"; //NOI18N
51
private static final String JavaDoc M_STRUCT_NAME = "Struct"; //NOI18N
52
private static final String JavaDoc M_STRUCT_DESC = "(Ljava/lang/String;[Ljava/lang/Object;)"; //NOI18N
53
private static final String JavaDoc M_STRUCT_TYPE = "Ljavax/jmi/reflect/RefStruct;"; //NOI18N
54

55     PackageGenerator(String JavaDoc name, Class JavaDoc ifc, Class JavaDoc handler, StorablePackage storable, Class JavaDoc custom) {
56         super(name, ifc, handler, storable, custom);
57         dispatchMethods.put("_getClass", new HashMap()); //NOI18N
58
dispatchMethods.put("_getAssociation", new HashMap()); //NOI18N
59
dispatchMethods.put("_getPackage", new HashMap()); //NOI18N
60
dispatchMethods.put("_createStruct", new HashMap()); //NOI18N
61
}
62     
63     protected String JavaDoc getConstructorDescriptor() {
64         return "(Lorg/netbeans/mdr/storagemodel/StorablePackage;)V"; //NOI18N
65
}
66     
67     protected MethodInfo[] generateMethods() throws IOException JavaDoc {
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 JavaDoc elementName = (String JavaDoc) element.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
75                 String JavaDoc metaTypeName = (String JavaDoc) element.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
76                 String JavaDoc substName = TagSupport.getSubstName(element, elementName, metaTypeName);
77                 String JavaDoc 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 JavaDoc sign = "get" + substName + getMethodDescriptor(new String JavaDoc[0], typeName); //NOI18N
82
if (!createdMethods.contains(sign)) {
83                             methods.addAll(getPackageMethod("get" + substName, new String JavaDoc[0], typeName, M_ASSOC_NAME, M_ASSOC_DESC, M_ASSOC_TYPE, elementName, "_getAssociation")); //NOI18N
84
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 JavaDoc sign = "get" + substName + getMethodDescriptor(new String JavaDoc[0], typeName + CLASS_SUFFIX); //NOI18N
91
if (!createdMethods.contains(sign)) {
92                             methods.addAll(getPackageMethod("get" + substName, new String JavaDoc[0], typeName + CLASS_SUFFIX, M_CLASS_NAME, M_CLASS_DESC, M_CLASS_TYPE, elementName, "_getClass")); //NOI18N
93
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 JavaDoc sign = "get" + substName + getMethodDescriptor(new String JavaDoc[0], typeName + PACKAGE_SUFFIX); //NOI18N
100
if (!createdMethods.contains(sign)) {
101                             methods.addAll(getPackageMethod("get" + substName, new String JavaDoc[0], typeName + PACKAGE_SUFFIX, M_PACKAGE_NAME, M_PACKAGE_DESC, M_PACKAGE_TYPE, elementName, "_getPackage")); //NOI18N
102
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 JavaDoc fieldTypeName = (String JavaDoc) 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 JavaDoc[] prms = (String JavaDoc[])parameters.toArray(new String JavaDoc[parameters.size()]);
114                     String JavaDoc sign = "create" + firstUpper(substName) + getMethodDescriptor(prms, typeName); //NOI18N
115
if (!createdMethods.contains(sign)) {
116                         methods.addAll(getCreateMethod("create" + firstUpper(substName), prms, typeName, M_STRUCT_NAME, M_STRUCT_DESC, M_STRUCT_TYPE, elementName)); //NOI18N
117
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 JavaDoc) 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 JavaDoc retName = TagSupport.getTypeFullName(namespace) + PACKAGE_SUFFIX;
126                         String JavaDoc sign = "get" + substName + getMethodDescriptor(new String JavaDoc[0], retName); //NOI18N
127
if (!createdMethods.contains(sign)) {
128                             methods.addAll(getPackageMethod("get" + substName, new String JavaDoc[0], retName, M_PACKAGE_NAME, M_PACKAGE_DESC, M_PACKAGE_TYPE, elementName, "_getPackage")); //NOI18N
129
createdMethods.add(sign);
130                         }
131                     }
132                 }
133             }
134             methods.add(getDispatcherMethod("_getClass", new String JavaDoc[] {"java.lang.String"}, "javax.jmi.reflect.RefClass", (HashMap) dispatchMethods.get("_getClass"))); //NOI18N
135
methods.add(getDispatcherMethod("_getAssociation", new String JavaDoc[] {"java.lang.String"}, "javax.jmi.reflect.RefAssociation", (HashMap) dispatchMethods.get("_getAssociation"))); //NOI18N
136
methods.add(getDispatcherMethod("_getPackage", new String JavaDoc[] {"java.lang.String"}, "javax.jmi.reflect.RefPackage", (HashMap) dispatchMethods.get("_getPackage"))); //NOI18N
137
methods.add(getDispatcherMethod("_createStruct", new String JavaDoc[] {"java.lang.String", "java.lang.Object[]"}, "javax.jmi.reflect.RefStruct", (HashMap) dispatchMethods.get("_createStruct"))); //NOI18N
138
return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
139         } catch (Exception JavaDoc e) {
140             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
141         }
142     }
143     
144     protected Collection getPackageMethod(String JavaDoc methodName, String JavaDoc[] parameterTypes, String JavaDoc returnType, String JavaDoc handlerName, String JavaDoc handlerDescriptor, String JavaDoc handlerType, String JavaDoc featureName, String JavaDoc dispatcher) throws IOException JavaDoc, 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 JavaDoc methodName, String JavaDoc[] parameterTypes, String JavaDoc returnType, String JavaDoc handlerName, String JavaDoc handlerDescriptor, String JavaDoc handlerType, String JavaDoc featureName) throws IOException JavaDoc {
153
154         if (featureName != null) {
155             HashMap item = (HashMap) dispatchMethods.get("_createStruct"); //NOI18N
156
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 JavaDoc 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 JavaDoc out = new DataOutputStream JavaDoc(minfo.getCodeStream());
181         MethodInfo cminfo = null;
182         DataOutputStream JavaDoc cout = null;
183         if (isCustom) {
184             cminfo = new MethodInfo(CUSTOM_PREFIX + methodName, desc, ACC_PUBLIC | ACC_FINAL);
185             cout = new DataOutputStream JavaDoc(cminfo.getCodeStream());
186         }
187         
188         // store "true" in the fail variable
189
out.writeByte(opc_iconst_1);
190         code_istore(fail, out);
191         // store "null" in the extraInfo variable
192
out.writeByte(opc_aconst_null);
193         out.writeByte(opc_dup);
194         code_astore(extraInfo, out);
195         // store "null" in result variable
196
code_astore(result, out);
197         
198         // I'll pass this instance as the first parameter
199
code_aload(0, out);
200         
201         if (featureName != null)
202             // feature name stored in the static variable as the second parameter
203
code_ldc(cp.getString(featureName), out);
204         
205         // The rest of parameters we'll put into Object[] array
206
code_ipush(parameterTypes.length, out);
207         out.writeByte(opc_anewarray);
208         out.writeShort(cp.getClass("java/lang/Object")); //NOI18N
209

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         // call the _pre method
218
out.writeByte(opc_invokespecial);
219         out.writeShort(preMethod);
220         
221         // store the returned object in a local variable
222
code_astore(extraInfo, out);
223         
224         // start of the try block
225
short tryStart = (short) out.size();
226
227         if (isCustom) {
228             // I'll pass this instance as the first parameter
229
code_aload(0, out);
230
231             // The rest of parameters follows
232
for (int i = 0; i < parameterTypes.length; i++) {
233                 codeLoad(parameterSlot[i], parameterTypes[i], out);
234             }
235
236             // call the custom method
237
out.writeByte(opc_invokespecial);
238             out.writeShort(cp.getMethodRef(dotToSlash(superclassName), methodName, desc));
239
240             code_astore(result, out);
241
242             // I'll pass this instance as the first parameter
243
code_aload(0, cout);
244
245             if (featureName != null)
246                 // feature name as the second parameter
247
code_ldc(cp.getString(featureName), cout);
248
249             // The rest of parameters we'll put into Object[] array
250
code_ipush(parameterTypes.length, cout);
251             cout.writeByte(opc_anewarray);
252             cout.writeShort(cp.getClass("java/lang/Object")); //NOI18N
253

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             // call the delegate method
262
cout.writeByte(opc_invokespecial);
263             cout.writeShort(delegateMethod);
264
265             // convert returned object to the result type and return
266
cout.writeByte(opc_checkcast);
267             cout.writeShort(cp.getClass(dotToSlash(returnType)));
268             cout.writeByte(opc_areturn);
269         }
270         else {
271             // I'll pass this instance as the first parameter
272
code_aload(0, out);
273
274             if (featureName != null)
275                 // feature name as the second parameter
276
code_ldc(cp.getString(featureName), out);
277
278             // The rest of parameters we'll put into Object[] array
279
code_ipush(parameterTypes.length, out);
280             out.writeByte(opc_anewarray);
281             out.writeShort(cp.getClass("java/lang/Object")); //NOI18N
282

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             // call the delegate method
291
out.writeByte(opc_invokespecial);
292             out.writeShort(delegateMethod);
293
294             code_astore(result, out);
295         }
296
297         // change value of fail to "false"
298
out.writeByte(opc_iconst_0);
299         code_istore(fail, out);
300         
301         // call the finally block and return
302
out.writeByte(opc_jsr);
303         
304         if (isCustom) {
305             out.writeShort(3 + getBytesForLoadOrStore(result) + 1 + 2*getBytesForLoadOrStore(exception) + 4);
306
307             // load the returned value
308
code_aload(result, out);
309
310             // return
311
out.writeByte(opc_areturn);
312         }
313         else {
314             out.writeShort(3 + getBytesForLoadOrStore(result) + 4 + 2*getBytesForLoadOrStore(exception) + 4);
315
316             // load the returned value
317
code_aload(result, out);
318
319             // convert it to the result type and return
320
out.writeByte(opc_checkcast);
321             out.writeShort(cp.getClass(dotToSlash(returnType)));
322             out.writeByte(opc_areturn);
323         }
324         
325         // start of catch block
326
short catchStart = (short) out.size();
327         
328         // store exception
329
code_astore(exception, out);
330         
331         // call finally
332
out.writeByte(opc_jsr);
333         out.writeShort(3 + getBytesForLoadOrStore(exception) + 1);
334         
335         // load exception
336
code_aload(exception, out);
337         
338         // rethrow exception
339
out.writeByte(opc_athrow);
340         
341         // start of finally block
342
// store the return address
343
code_astore(addr, out);
344         
345         // load parameters
346
code_aload(0, out);
347         code_aload(result, out);
348         code_aload(extraInfo, out);
349         code_iload(fail, out);
350         
351         // call the _post method
352
out.writeByte(opc_invokespecial);
353         out.writeShort(postMethod);
354         
355         // return from finally
356
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