001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.tools.generator;
025:
026: import java.io.*;
027: import java.util.*;
028: import java.lang.reflect.*;
029:
030: import org.myoodb.*;
031: import org.myoodb.core.*;
032:
033: public class BeanGenerator extends AbstractCodeGeneratorImpl implements
034: AbstractCodeGenerator {
035: public final static String IMPL_NAME_SUFFIX = "@implExtension@";
036: public final static String BEAN_NAME_SUFFIX = "@beanExtension@";
037:
038: private String m_file;
039: private PrintWriter m_out;
040: private Class m_myoodbImplClass;
041: private Method[] m_sortedMethods;
042:
043: public BeanGenerator() {
044: m_out = null;
045: m_file = null;
046: m_sortedMethods = null;
047: m_myoodbImplClass = null;
048: }
049:
050: private void makeConstructor(String beanClassName) {
051: beanClassName += BEAN_NAME_SUFFIX;
052:
053: m_out.print(" public "
054: + Helper.simpleClassName(beanClassName) + "()\n");
055: m_out.print(" {\n");
056: m_out.print(" }\n");
057: m_out.print(" public "
058: + Helper.simpleClassName(beanClassName)
059: + "(org.myoodb.core.Identifier id)\n");
060: m_out.print(" {\n");
061: m_out.print(" super(id);\n");
062: m_out.print(" }\n");
063: }
064:
065: private void makePackage(String implementationClass) {
066: m_out
067: .print("// JavaBean class generated by MyOODB-@version@ (DO NOT EDIT!)\n");
068: m_out.print("\n");
069: if (Helper.packageName(implementationClass).equals("") == false) {
070: m_out.print("package "
071: + Helper.packageName(implementationClass) + ";\n");
072: m_out.print("\n");
073: }
074: }
075:
076: private void makeInhertance(String beanClassName) {
077: beanClassName += BEAN_NAME_SUFFIX;
078:
079: m_out.print("public class "
080: + Helper.simpleClassName(beanClassName)
081: + " extends org.myoodb.MyOodbBean\n");
082: m_out.print("{\n");
083: }
084:
085: private void makeJavaBeanSetGetProperty(String name,
086: Method setMethod, Method getMethod) {
087: AbstractCodeGenerator.Parameter parameters[] = getParameters(setMethod
088: .getParameterTypes());
089: Class returnType = getMethod.getReturnType();
090:
091: //
092: // Set Method
093: //
094:
095: m_out.print(" private volatile " + parameters[0].getType()
096: + " " + name + ";\n");
097: StringBuilder signaturBuf = new StringBuilder();
098: signaturBuf.append(" public void set" + name + "(");
099: signaturBuf.append(parameters[0].getType() + " "
100: + parameters[0].getName());
101: signaturBuf.append(")\n");
102: m_out.print(signaturBuf.toString());
103: m_out.print(" {\n");
104: m_out.print(" this." + name + " = "
105: + parameters[0].getName() + ";\n");
106: m_out.print(" }\n");
107:
108: //
109: // Get Method
110: //
111:
112: signaturBuf = new StringBuilder();
113: signaturBuf.append(" public " + getTypecode(returnType)
114: + " get" + name + "()\n");
115: m_out.print(signaturBuf.toString());
116: m_out.print(" {\n");
117: m_out.print(" return this." + name + ";\n");
118: m_out.print(" }\n");
119: }
120:
121: private void makeJavaBeanAddRemoveGetsProperty(String name,
122: Method addMethod) {
123: AbstractCodeGenerator.Parameter parameters[] = getParameters(addMethod
124: .getParameterTypes());
125:
126: //
127: // Add Method
128: //
129:
130: m_out.print(" private volatile java.util.ArrayList<"
131: + parameters[0].getType() + "> " + name
132: + "List = new java.util.ArrayList<"
133: + parameters[0].getType() + ">();\n");
134: StringBuilder signaturBuf = new StringBuilder();
135: signaturBuf.append(" public void add" + name + "(");
136: signaturBuf.append(parameters[0].getType() + " "
137: + parameters[0].getName());
138: signaturBuf.append(")\n");
139: m_out.print(signaturBuf.toString());
140: m_out.print(" {\n");
141: m_out.print(" this." + name + "List.add("
142: + parameters[0].getName() + ");\n");
143: m_out.print(" }\n");
144:
145: //
146: // Remove Method
147: //
148:
149: signaturBuf = new StringBuilder();
150: signaturBuf.append(" public boolean remove" + name + "(");
151: signaturBuf.append(parameters[0].getType() + " "
152: + parameters[0].getName());
153: signaturBuf.append(")\n");
154: m_out.print(signaturBuf.toString());
155: m_out.print(" {\n");
156: m_out.print(" return this." + name + "List.remove("
157: + parameters[0].getName() + ");\n");
158: m_out.print(" }\n");
159:
160: //
161: // Gets Method
162: //
163:
164: signaturBuf = new StringBuilder();
165: signaturBuf.append(" public java.util.ArrayList<"
166: + parameters[0].getType() + "> get" + name + "s()\n");
167: m_out.print(signaturBuf.toString());
168: m_out.print(" {\n");
169: m_out.print(" return this." + name + "List;\n");
170: m_out.print(" }\n");
171: }
172:
173: public void beginClass(String sourcePath, String className)
174: throws GeneratorException {
175: m_file = Helper.classToFileName(className) + BEAN_NAME_SUFFIX
176: + ".java";
177: System.out.println(" BEGIN - Generating bean for: " + m_file);
178:
179: try {
180: String fileName = sourcePath + "/" + m_file;
181: m_out = new PrintWriter(fileName);
182: } catch (IOException e) {
183: throw new GeneratorException(e);
184: }
185:
186: try {
187: Class interfaceType = Class.forName(className);
188: m_myoodbImplClass = Class.forName(className
189: + IMPL_NAME_SUFFIX);
190: m_sortedMethods = MethodHelper.getMethods(
191: m_myoodbImplClass, interfaceType);
192: } catch (ClassNotFoundException e) {
193: throw new GeneratorException(e);
194: }
195:
196: makePackage(className);
197: makeInhertance(className);
198: makeConstructor(className);
199: }
200:
201: public void makeMethod(String name, int accessLevel)
202: throws GeneratorException {
203: for (int methodIndex = 0; methodIndex < m_sortedMethods.length; methodIndex++) {
204: Method method = m_sortedMethods[methodIndex];
205:
206: if (method.getName().equals(name) == false) {
207: continue;
208: }
209:
210: Method getMethod = MethodHelper.getAssociatedBeanGetMethod(
211: method, m_sortedMethods);
212:
213: if (getMethod != null) {
214: makeJavaBeanSetGetProperty(name.substring(3), method,
215: getMethod);
216: continue;
217: }
218:
219: Method getsMethod = MethodHelper
220: .getAssociatedBeanGetsMethod(method,
221: m_sortedMethods);
222: Method removeMethod = MethodHelper
223: .getAssociatedBeanRemoveMethod(method,
224: m_sortedMethods);
225:
226: if ((getsMethod != null) && (removeMethod != null)) {
227: makeJavaBeanAddRemoveGetsProperty(name.substring(3),
228: method);
229: }
230: }
231: }
232:
233: public void endClass() throws GeneratorException {
234: m_out.print("}\n");
235: m_out.close();
236:
237: System.out.println(" END - Generating bean for: " + m_file);
238: }
239: }
|