KickJava   Java API By Example, From Geeks To Geeks.

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


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

39 abstract class FeaturedGenerator extends HandlerGenerator {
40     
41     protected static final String JavaDoc M_GET_NAME = "Get"; //NOI18N
42
protected static final String JavaDoc M_GET_DESC = "(Ljava/lang/String;)"; //NOI18N
43
protected static final String JavaDoc M_GET_TYPE = "Ljava/lang/Object;"; //NOI18N
44
protected static final String JavaDoc M_SET_NAME = "Set"; //NOI18N
45
protected static final String JavaDoc M_SET_DESC = "(Ljava/lang/String;Ljava/lang/Object;)"; //NOI18N
46
protected static final String JavaDoc M_SET_TYPE = "V"; //NOI18N
47

48     FeaturedGenerator(String JavaDoc name, Class JavaDoc ifc, Class JavaDoc handler, StorableFeatured storable, Class JavaDoc custom) {
49         super(name, ifc, handler, storable, custom);
50         dispatchMethods.put("_invokeOperation", new HashMap JavaDoc()); //NOI18N
51
dispatchMethods.put("_setAttribute", new HashMap JavaDoc()); //NOI18N
52
dispatchMethods.put("_getAttribute", new HashMap JavaDoc()); //NOI18N
53
}
54     
55     protected MethodInfo[] generateMethods() throws IOException JavaDoc {
56         try {
57             ArrayList JavaDoc methods = new ArrayList JavaDoc();
58             methods.add(getDispatcherMethod("_invokeOperation", new String JavaDoc[] {"java.lang.String", "java.lang.Object[]"}, "java.lang.Object", (HashMap JavaDoc) dispatchMethods.get("_invokeOperation"))); //NOI18N
59
methods.add(getDispatcherMethod("_setAttribute", new String JavaDoc[] {"java.lang.String", "java.lang.Object"}, "void", (HashMap JavaDoc) dispatchMethods.get("_setAttribute"))); //NOI18N
60
methods.add(getDispatcherMethod("_getAttribute", new String JavaDoc[] {"java.lang.String"}, "java.lang.Object", (HashMap JavaDoc) dispatchMethods.get("_getAttribute"))); //NOI18N
61
return (MethodInfo[]) methods.toArray(new MethodInfo[methods.size()]);
62         } catch (Exception JavaDoc e) {
63             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
64         }
65     }
66     
67     protected Collection JavaDoc getFeatureMethod(String JavaDoc methodName, String JavaDoc[] parameterTypes, String JavaDoc returnType, String JavaDoc handlerName, String JavaDoc handlerDescriptor, String JavaDoc handlerType, StorableObject feature, StorableFeatured parent, String JavaDoc dispatcher) throws IOException JavaDoc, StorageException {
68         String JavaDoc name = (String JavaDoc) feature.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
69         if (dispatcher != null) {
70             HashMap JavaDoc item = (HashMap JavaDoc) dispatchMethods.get(dispatcher);
71             item.put(name, new MethodDescHolder(methodName, parameterTypes, returnType));
72         }
73         if (((Boolean JavaDoc) 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 JavaDoc getAttributeMethod(String JavaDoc methodName, String JavaDoc[] parameterTypes, String JavaDoc returnType, String JavaDoc handlerName, String JavaDoc handlerDescriptor, String JavaDoc handlerType, String JavaDoc featureName, int attrIndex) throws IOException JavaDoc {
83         short delegateMethod = getHandlerIndex(handlerName, "(I" + handlerDescriptor.substring("(Ljava/lang/String;".length()), handlerType); //NOI18N
84
short preMethod = getPreIndex(handlerName, handlerDescriptor);
85         short postMethod = getPostIndex(handlerName, handlerType);
86         String JavaDoc 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 JavaDoc out = new DataOutputStream JavaDoc(minfo.getCodeStream());
106         MethodInfo cminfo = null;
107         DataOutputStream JavaDoc cout = null;
108         if (isCustom) {
109             cminfo = new MethodInfo(CUSTOM_PREFIX + methodName, desc, ACC_PUBLIC | ACC_FINAL);
110             cout = new DataOutputStream JavaDoc(cminfo.getCodeStream());
111         }
112         
113         // store "true" in the fail variable
114
out.writeByte(opc_iconst_1);
115         code_istore(fail, out);
116         // store "null" in the extraInfo variable
117
out.writeByte(opc_aconst_null);
118         out.writeByte(opc_dup);
119         code_astore(extraInfo, out);
120         // store "null" in result variable
121
code_astore(result, out);
122         
123         // I'll pass this instance as the first parameter
124
code_aload(0, out);
125         
126         // feature name as the second parameter
127
code_ldc(cp.getString(featureName), out);
128         
129         // The rest of parameters follows
130
for (int i = 0; i < parameterTypes.length; i++) {
131             codeWrapArgument(parameterTypes[i], parameterSlot[i], out);
132         }
133         
134         // call the _pre method
135
out.writeByte(opc_invokespecial);
136         out.writeShort(preMethod);
137         
138         // store the returned object in a local variable
139
code_astore(extraInfo, out);
140         
141         // start of the try block
142
short tryStart = (short) out.size();
143
144         if (isCustom) {
145             // I'll pass this instance as the first parameter
146
code_aload(0, out);
147
148             // The rest of parameters follows
149
for (int i = 0; i < parameterTypes.length; i++) {
150                 codeLoad(parameterSlot[i], parameterTypes[i], out);
151             }
152
153             // call the custom method
154
out.writeByte(opc_invokespecial);
155             out.writeShort(cp.getMethodRef(dotToSlash(superclassName), methodName, desc));
156
157             if (!returnType.equals("void")) { //NOI18N
158
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             // I'll pass this instance as the first parameter
168
code_aload(0, cout);
169
170             code_ipush(attrIndex, cout);
171
172             // The rest of parameters follows
173
for (int i = 0; i < parameterTypes.length; i++) {
174                 codeWrapArgument(parameterTypes[i], parameterSlot[i], cout);
175             }
176
177             // call the delegate method
178
cout.writeByte(opc_invokespecial);
179             cout.writeShort(delegateMethod);
180
181             if (returnType.equals("void")) //NOI18N
182
cout.writeByte(opc_return);
183             else
184                 codeUnwrapReturnValue(returnType, cout);
185         }
186         else {
187             // I'll pass this instance as the first parameter
188
code_aload(0, out);
189
190             code_ipush(attrIndex, out);
191
192             // The rest of parameters follows
193
for (int i = 0; i < parameterTypes.length; i++) {
194                 codeWrapArgument(parameterTypes[i], parameterSlot[i], out);
195             }
196
197             // call the delegate method
198
out.writeByte(opc_invokespecial);
199             out.writeShort(delegateMethod);
200
201             if (!returnType.equals("void")) { //NOI18N
202
code_astore(result, out);
203             }
204         }
205         
206         // change value of fail to "false"
207
out.writeByte(opc_iconst_0);
208         code_istore(fail, out);
209         
210         // call the finally block and return
211
out.writeByte(opc_jsr);
212         if (returnType.equals("void")) { //NOI18N
213
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")) //NOI18N
221
out.writeByte(opc_ireturn);
222                     else if (returnType.equals("long")) //NOI18N
223
out.writeByte(opc_lreturn);
224                     else if (returnType.equals("float")) //NOI18N
225
out.writeByte(opc_freturn);
226                     else if (returnType.equals("double")) //NOI18N
227
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                 // load the returned value
239
code_aload(result, out);
240                 // convert it to the result type
241
codeUnwrapReturnValue(returnType, out);
242             }
243         }
244         // start of catch block
245
short catchStart = (short) out.size();
246         
247         // store exception
248
code_astore(exception, out);
249         
250         // call finally
251
out.writeByte(opc_jsr);
252         out.writeShort(3 + getBytesForLoadOrStore(exception) + 1);
253         
254         // load exception
255
out.writeByte(opc_aload);
256         out.writeByte(exception);
257         
258         // rethrow exception
259
out.writeByte(opc_athrow);
260         
261         // start of finally block
262
// store the return address
263
code_astore(addr, out);
264         
265         // load parameters
266
code_aload(0, out);
267         if (!returnType.equals("void")) { //NOI18N
268
code_aload(result, out);
269         }
270         code_aload(extraInfo, out);
271         code_iload(fail, out);
272         
273         // call the _post method
274
out.writeByte(opc_invokespecial);
275         out.writeShort(postMethod);
276         
277         // return from finally
278
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 JavaDoc ret = new ArrayList JavaDoc();
291         ret.add(minfo);
292         if (isCustom)
293             ret.add(cminfo);
294         return ret;
295     }
296     
297     protected MethodInfo getOperationMethod(String JavaDoc methodName, String JavaDoc[] parameterTypes, String JavaDoc returnType, String JavaDoc featureName) throws IOException JavaDoc {
298         HashMap JavaDoc item = (HashMap JavaDoc) dispatchMethods.get("_invokeOperation"); //NOI18N
299
item.put(featureName, new MethodDescHolder(methodName, parameterTypes, returnType));
300         return null;
301     }
302 }
303
Popular Tags