KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > tools > generator > ProxyGenerator


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.tools.generator;
25
26 import java.io.*;
27 import java.util.*;
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.Modifier JavaDoc;
30
31 import org.myoodb.*;
32 import org.myoodb.core.*;
33
34 public class ProxyGenerator implements AbstractCodeGenerator
35 {
36     public final static String JavaDoc IMPL_NAME_SUFFIX = "@implExtension@";
37     public final static String JavaDoc PROXY_NAME_SUFFIX = "@proxyExtension@";
38
39     private String JavaDoc m_file;
40     private PrintWriter m_out;
41     private Class JavaDoc m_myoodbImplClass;
42     private Method JavaDoc m_sortedMethods[];
43     private ArrayList m_remoteInterfaces;
44
45     public ProxyGenerator()
46     {
47         m_out = null;
48         m_file = null;
49     m_sortedMethods = null;
50         m_myoodbImplClass = null;
51         m_remoteInterfaces = null;
52     }
53
54     private Class JavaDoc getClassForType(String JavaDoc type) throws ClassNotFoundException JavaDoc
55     {
56         Class JavaDoc classType = Helper.classForPrimitive(type);
57         if (classType == null)
58         {
59             return Class.forName(type);
60         }
61         else
62         {
63             return classType;
64         }
65     }
66
67     private String JavaDoc getSourceCodeClassName(String JavaDoc className)
68     {
69         return className.replace('$', '.');
70     }
71
72     private String JavaDoc[] getClassNames(Class JavaDoc types[])
73     {
74         String JavaDoc classNames[] = new String JavaDoc[types.length];
75         for (int i = 0; i < types.length; ++i)
76         {
77             classNames[i] = getSourceCodeClassName(types[i].getName());
78         }
79         return classNames;
80     }
81
82     private String JavaDoc getTypecode(Class JavaDoc classType)
83     {
84         String JavaDoc typeCode;
85
86         if (classType.isArray())
87         {
88             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
89             while (classType.isArray())
90             {
91                 sb.append("[]");
92                 classType = classType.getComponentType();
93             }
94             sb.insert(0, getSourceCodeClassName(classType.getName()));
95             typeCode = sb.toString();
96         }
97         else
98         {
99             typeCode = getSourceCodeClassName(classType.getName());
100         }
101         return typeCode;
102     }
103
104     private Class JavaDoc[] parametersToClasses(AbstractCodeGenerator.Parameter parameters[]) throws ClassNotFoundException JavaDoc
105     {
106         Class JavaDoc params[] = new Class JavaDoc[parameters.length];
107         for (int i = 0; i < parameters.length; i++)
108         {
109             AbstractCodeGenerator.Parameter parameter = parameters[i];
110             params[i] = getClassForType(parameter.getFullName());
111         }
112         return params;
113     }
114
115     private AbstractCodeGenerator.Parameter[] getParameters(Class JavaDoc parameterTypes[])
116     {
117         AbstractCodeGenerator.Parameter parameters[] = new AbstractCodeGenerator.Parameter[parameterTypes.length];
118         for (int i = 0; i < parameters.length; ++i)
119         {
120             parameters[i] = new AbstractCodeGenerator.Parameter(getTypecode(parameterTypes[i]), "arg" + i, parameterTypes[i].getName());
121         }
122         return parameters;
123     }
124
125     private int getMethodIndex(String JavaDoc methodName, AbstractCodeGenerator.Parameter parameters[]) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc
126     {
127         Method JavaDoc method = m_myoodbImplClass.getMethod(methodName, parametersToClasses(parameters));
128         return MethodHelper.getMethodIndex(m_sortedMethods, method);
129     }
130
131     private void makeConstructor(String JavaDoc proxyClassName)
132     {
133         proxyClassName += PROXY_NAME_SUFFIX;
134
135         m_out.print(" public " + Helper.simpleClassName(proxyClassName) + "()\n");
136         m_out.print(" {\n");
137         m_out.print(" }\n");
138         m_out.print(" public " + Helper.simpleClassName(proxyClassName) + "(org.myoodb.core.Identifier id, org.myoodb.core.AbstractDatabase db)\n");
139         m_out.print(" {\n");
140         m_out.print(" super(id, db);\n");
141         m_out.print(" }\n");
142     }
143
144     private void makePackage(String JavaDoc implementationClass)
145     {
146         m_out.print("// Proxy class generated by MyOODB-@version@ (DO NOT EDIT!)\n");
147         m_out.print("\n");
148         if (Helper.packageName(implementationClass).equals("") == false)
149         {
150             m_out.print("package " + Helper.packageName(implementationClass) + ";\n");
151             m_out.print("\n");
152         }
153     }
154
155     private void makeInhertance(String JavaDoc proxyClassName)
156     {
157         proxyClassName += PROXY_NAME_SUFFIX;
158
159         /*
160         String parentProxyClassName = m_myoodbImplClass.getSuperclass().getName() + PROXY_NAME_SUFFIX;
161         if (parentProxyClassName.equals("org.myoodb.MyOodbObject" + PROXY_NAME_SUFFIX) == false)
162         {
163             m_out.print("public class " + Helper.simpleClassName(proxyClassName) + " extends " + parentProxyClassName);
164         }
165         else
166         {
167         */

168             m_out.print("public class " + Helper.simpleClassName(proxyClassName) + " extends org.myoodb.MyOodbProxy");
169         /*
170         }
171         */

172
173         boolean first = true;
174         Iterator iter = m_remoteInterfaces.iterator();
175         while (iter.hasNext())
176         {
177             Class JavaDoc classType = (Class JavaDoc) iter.next();
178             if (first)
179             {
180                 first = false;
181                 m_out.print(" implements ");
182             }
183             else
184             {
185                 m_out.print(", ");
186             }
187             m_out.print(classType.getName());
188         }
189         m_out.print("\n");
190         m_out.print("{\n");
191     }
192
193     public void beginClass(String JavaDoc sourcePath, String JavaDoc className) throws GeneratorException
194     {
195         m_file = Helper.classToFileName(className) + PROXY_NAME_SUFFIX + ".java";
196         System.out.println(" BEGIN - Generating proxy for: " + m_file);
197
198         try
199         {
200             String JavaDoc fileName = sourcePath + "/" + m_file;
201             m_out = new PrintWriter(fileName);
202         }
203         catch (IOException e)
204         {
205             throw new GeneratorException(e);
206         }
207
208         try
209         {
210             Class JavaDoc interfaceType = Class.forName(className);
211             m_myoodbImplClass = Class.forName(className + IMPL_NAME_SUFFIX);
212             m_sortedMethods = MethodHelper.getMethods(m_myoodbImplClass, interfaceType);
213             m_remoteInterfaces = Helper.findInterfaces(m_myoodbImplClass, MyOodbRemote.class);
214         }
215         catch (ClassNotFoundException JavaDoc e)
216         {
217             throw new GeneratorException(e);
218         }
219
220         makePackage(className);
221         makeInhertance(className);
222         makeConstructor(className);
223     }
224
225     public void makeMethod(String JavaDoc name, int accessLevel) throws GeneratorException
226     {
227         for (int methodIndex = 0; methodIndex < m_sortedMethods.length; methodIndex++)
228         {
229             Method JavaDoc method = m_sortedMethods[methodIndex];
230
231             if ((Modifier.isFinal(method.getModifiers()) == true) || (method.getName().equals(name) == false))
232             {
233                 continue;
234             }
235
236             AbstractCodeGenerator.Parameter parameters[] = getParameters(method.getParameterTypes());
237             Class JavaDoc returnType = method.getReturnType();
238             Class JavaDoc exceptions[] = method.getExceptionTypes();
239             
240             // signature of this method
241
StringBuilder JavaDoc signaturBuf = new StringBuilder JavaDoc();
242             signaturBuf.append(" public " + getTypecode(returnType) + " " + name + "(");
243
244             for (int i = 0; i < parameters.length; i++)
245             {
246                 if (i != 0)
247                 {
248                     signaturBuf.append(", ");
249                 }
250                 signaturBuf.append(parameters[i].getType() + " " + parameters[i].getName());
251             }
252             signaturBuf.append(")");
253             String JavaDoc signaturStr = signaturBuf.toString();
254
255             m_out.print(signaturStr);
256
257             //exceptions in declaration
258
for (int i = 0; i < exceptions.length; i++)
259             {
260                 m_out.print(i == 0 ? " throws " : ", ");
261                 m_out.print(exceptions[i].getName());
262             }
263             m_out.print("\n");
264             m_out.print(" {\n");
265
266             //array of arguments
267
boolean update = (accessLevel != Lock.ACCESS_READ);
268
269             m_out.print(" try\n");
270             m_out.print(" {\n");
271             if (parameters.length != 0)
272             {
273                 m_out.print(" Object[] args = {");
274                 for (int i = 0; i < parameters.length; i++)
275                 {
276                     m_out.print(i > 0 ? ", " : "");
277                     if (Helper.isPrimitive(parameters[i].getType()))
278                     {
279                         m_out.print("new " + Helper.wrappercodeForPrimitive(parameters[i].getType()) + "(" + parameters[i].getName() + ")");
280                     }
281                     else
282                     {
283                         m_out.print("arg" + i);
284                     }
285                 }
286                 m_out.print("};\n");
287                 if (returnType.getName().equals("void") == false)
288                 {
289                     m_out.print(" Object result = ");
290                 }
291                 else
292                 {
293                     m_out.print(" ");
294                 }
295                 try
296                 {
297                     m_out.print("m_database.invokeMethod(this, " +
298                                 getMethodIndex(name, parameters) +
299                                 ", args, " + (update ? "org.myoodb.core.Lock.ACCESS_WRITE" : "org.myoodb.core.Lock.ACCESS_READ") + ", m_timeout);\n");
300
301                 }
302                 catch (ClassNotFoundException JavaDoc e)
303                 {
304                     throw new GeneratorException(e);
305                 }
306                 catch (NoSuchMethodException JavaDoc e)
307                 {
308                     throw new GeneratorException(e);
309                 }
310             }
311             else
312             {
313                 if (returnType.getName().equals("void") == false)
314                 {
315                     m_out.print(" Object result = ");
316                 }
317                 else
318                 {
319                     m_out.print(" ");
320                 }
321                 try
322                 {
323                     m_out.print("m_database.invokeMethod(this, " +
324                                 getMethodIndex(name,parameters) +
325                                 ", null, " + (update ? "org.myoodb.core.Lock.ACCESS_WRITE" : "org.myoodb.core.Lock.ACCESS_READ") + ", m_timeout);\n");
326
327                 }
328                 catch (ClassNotFoundException JavaDoc e)
329                 {
330                     throw new GeneratorException(e);
331                 }
332                 catch (NoSuchMethodException JavaDoc e)
333                 {
334                     throw new GeneratorException(e);
335                 }
336             }
337
338             // return value
339
if (returnType.getName().equals("void") == false)
340             {
341                 if (Helper.isPrimitive(returnType.getName()))
342                 {
343                     m_out.print(" return " + Helper.returncodeForPrimitive(returnType.getName(), "result") + ";\n");
344                 }
345                 else
346                 {
347                     m_out.print(" return (" + getTypecode(returnType) + ") result;\n");
348                 }
349             }
350
351             // user defined exceptions
352
m_out.print(" }\n");
353             m_out.print(" catch (org.myoodb.exception.ObjectException oe)\n");
354             m_out.print(" {\n");
355             if (exceptions.length != 0)
356             {
357                 m_out.print(" Throwable ee = oe.getCause();\n");
358                 for (int i = 0; i < exceptions.length; i++)
359                 {
360                     m_out.print(" if (ee instanceof " + exceptions[i].getName() + ")\n");
361                     m_out.print(" {\n");
362                     m_out.print(" throw (" + exceptions[i].getName() + ") ee;\n");
363                     m_out.print(" }\n");
364                 }
365                 m_out.print(" throw (RuntimeException) ee;\n");
366             }
367             else
368             {
369                 m_out.print(" throw (RuntimeException) oe.getCause();\n");
370             }
371
372             m_out.print(" }\n");
373             m_out.print(" }\n");
374         }
375     }
376
377     public void endClass() throws GeneratorException
378     {
379         m_out.println("}");
380         m_out.close();
381
382         System.out.println(" END - Generating proxy for: " + m_file);
383     }
384 }
385
Popular Tags