KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
29
30 import org.myoodb.*;
31 import org.myoodb.core.*;
32
33 public class BeanGenerator implements AbstractCodeGenerator
34 {
35     public final static String JavaDoc IMPL_NAME_SUFFIX = "@implExtension@";
36     public final static String JavaDoc BEAN_NAME_SUFFIX = "@beanExtension@";
37
38     private String JavaDoc m_file;
39     private PrintWriter m_out;
40     private Class JavaDoc m_myoodbImplClass;
41     private Method m_sortedMethods[];
42
43     public BeanGenerator()
44     {
45         m_out = null;
46         m_file = null;
47     m_sortedMethods = null;
48         m_myoodbImplClass = null;
49     }
50
51     private Class JavaDoc getClassForType(String JavaDoc type) throws ClassNotFoundException JavaDoc
52     {
53         Class JavaDoc classType = Helper.classForPrimitive(type);
54         if (classType == null)
55         {
56             return Class.forName(type);
57         }
58         else
59         {
60             return classType;
61         }
62     }
63
64     private String JavaDoc getSourceCodeClassName(String JavaDoc className)
65     {
66         return className.replace('$', '.');
67     }
68
69     private String JavaDoc[] getClassNames(Class JavaDoc types[])
70     {
71         String JavaDoc classNames[] = new String JavaDoc[types.length];
72         for (int i = 0; i < types.length; ++i)
73         {
74             classNames[i] = getSourceCodeClassName(types[i].getName());
75         }
76         return classNames;
77     }
78
79     private String JavaDoc getTypecode(Class JavaDoc classType)
80     {
81         String JavaDoc typeCode;
82
83         if (classType.isArray())
84         {
85             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
86             while (classType.isArray())
87             {
88                 sb.append("[]");
89                 classType = classType.getComponentType();
90             }
91             sb.insert(0, getSourceCodeClassName(classType.getName()));
92             typeCode = sb.toString();
93         }
94         else
95         {
96             typeCode = getSourceCodeClassName(classType.getName());
97         }
98         return typeCode;
99     }
100
101     private Class JavaDoc[] parametersToClasses(AbstractCodeGenerator.Parameter parameters[]) throws ClassNotFoundException JavaDoc
102     {
103         Class JavaDoc params[] = new Class JavaDoc[parameters.length];
104         for (int i = 0; i < parameters.length; i++)
105         {
106             AbstractCodeGenerator.Parameter parameter = parameters[i];
107             params[i] = getClassForType(parameter.getFullName());
108         }
109         return params;
110     }
111
112     private AbstractCodeGenerator.Parameter[] getParameters(Class JavaDoc parameterTypes[])
113     {
114         AbstractCodeGenerator.Parameter parameters[] = new AbstractCodeGenerator.Parameter[parameterTypes.length];
115         for (int i = 0; i < parameters.length; ++i)
116         {
117             parameters[i] = new AbstractCodeGenerator.Parameter(getTypecode(parameterTypes[i]), "arg" + i, parameterTypes[i].getName());
118         }
119         return parameters;
120     }
121
122     private int getMethodIndex(String JavaDoc methodName, AbstractCodeGenerator.Parameter parameters[]) throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc
123     {
124         Method method = m_myoodbImplClass.getMethod(methodName, parametersToClasses(parameters));
125         return MethodHelper.getMethodIndex(m_sortedMethods, method);
126     }
127
128     private void makeConstructor(String JavaDoc beanClassName)
129     {
130         beanClassName += BEAN_NAME_SUFFIX;
131
132         m_out.print(" public " + Helper.simpleClassName(beanClassName) + "()\n");
133         m_out.print(" {\n");
134         m_out.print(" }\n");
135         m_out.print(" public " + Helper.simpleClassName(beanClassName) + "(org.myoodb.core.Identifier id)\n");
136         m_out.print(" {\n");
137         m_out.print(" super(id);\n");
138         m_out.print(" }\n");
139     }
140
141     private void makePackage(String JavaDoc implementationClass)
142     {
143         m_out.print("// JavaBean class generated by MyOODB-@version@ (DO NOT EDIT!)\n");
144         m_out.print("\n");
145         if (Helper.packageName(implementationClass).equals("") == false)
146         {
147             m_out.print("package " + Helper.packageName(implementationClass) + ";\n");
148             m_out.print("\n");
149         }
150     }
151
152     private void makeInhertance(String JavaDoc beanClassName)
153     {
154         beanClassName += BEAN_NAME_SUFFIX;
155
156         m_out.print("public class " + Helper.simpleClassName(beanClassName) + " extends org.myoodb.MyOodbBean\n");
157         m_out.print("{\n");
158     }
159
160     private void makeJavaBeanProperty(String JavaDoc name, Method setMethod, Method getMethod)
161     {
162         AbstractCodeGenerator.Parameter parameters[] = getParameters(setMethod.getParameterTypes());
163         Class JavaDoc returnType = getMethod.getReturnType();
164
165         //
166
// Set Method
167
//
168

169         m_out.print(" private " + parameters[0].getType() + " m_" + name + ";\n");
170         StringBuilder JavaDoc signaturBuf = new StringBuilder JavaDoc();
171         signaturBuf.append(" public void set" + name + "(");
172         signaturBuf.append(parameters[0].getType() + " " + parameters[0].getName());
173         signaturBuf.append(")\n");
174         m_out.print(signaturBuf.toString());
175         m_out.print(" {\n");
176         m_out.print(" m_" + name + " = " + parameters[0].getName() + ";\n");
177         m_out.print(" }\n");
178
179         //
180
// Get Method
181
//
182

183         signaturBuf = new StringBuilder JavaDoc();
184         signaturBuf.append(" public " + getTypecode(returnType) + " get" + name + "()\n");
185         m_out.print(signaturBuf.toString());
186         m_out.print(" {\n");
187         m_out.print(" return m_" + name + ";\n");
188         m_out.print(" }\n");
189     }
190
191     public void beginClass(String JavaDoc sourcePath, String JavaDoc className) throws GeneratorException
192     {
193         m_file = Helper.classToFileName(className) + BEAN_NAME_SUFFIX + ".java";
194         System.out.println(" BEGIN - Generating bean for: " + m_file);
195
196         try
197         {
198             String JavaDoc fileName = sourcePath + "/" + m_file;
199             m_out = new PrintWriter(fileName);
200         }
201         catch (IOException e)
202         {
203             throw new GeneratorException(e);
204         }
205
206         try
207         {
208             Class JavaDoc interfaceType = Class.forName(className);
209             m_myoodbImplClass = Class.forName(className + IMPL_NAME_SUFFIX);
210             m_sortedMethods = MethodHelper.getMethods(m_myoodbImplClass, interfaceType);
211         }
212         catch (ClassNotFoundException JavaDoc e)
213         {
214             throw new GeneratorException(e);
215         }
216
217         makePackage(className);
218         makeInhertance(className);
219         makeConstructor(className);
220     }
221
222     public void makeMethod(String JavaDoc name, int accessLevel) throws GeneratorException
223     {
224         for (int methodIndex = 0; methodIndex < m_sortedMethods.length; methodIndex++)
225         {
226             Method setMethod = m_sortedMethods[methodIndex];
227
228             if (setMethod.getName().equals(name) == false)
229             {
230                 continue;
231             }
232
233             Method getMethod = AbstractObjectContainer.ifThisIsAJavaBeanSetMethodThenReturnItsGetMethod(setMethod, m_sortedMethods);
234
235             if (getMethod == null)
236             {
237                 continue;
238             }
239
240             makeJavaBeanProperty(name.substring(3), setMethod, getMethod);
241         }
242     }
243
244     public void endClass() throws GeneratorException
245     {
246         m_out.println("}");
247         m_out.close();
248
249         System.out.println(" END - Generating bean for: " + m_file);
250     }
251 }
252
Popular Tags