KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > gen > EmbeddableComponent


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Rodrigo Westrupp
28  */

29
30 package com.caucho.amber.gen;
31
32 import com.caucho.amber.field.*;
33 import com.caucho.amber.table.Column;
34 import com.caucho.amber.table.Table;
35 import com.caucho.amber.type.EmbeddableType;
36 import com.caucho.amber.type.Type;
37 import com.caucho.bytecode.*;
38 import com.caucho.java.JavaWriter;
39 import com.caucho.java.gen.ClassComponent;
40 import com.caucho.loader.Environment;
41 import com.caucho.util.L10N;
42
43 import java.io.IOException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashSet JavaDoc;
46
47 /**
48  * Generates the Java code for the wrapped object.
49  */

50 public class EmbeddableComponent extends ClassComponent {
51   private static final L10N L = new L10N(EmbeddableComponent.class);
52
53   private String JavaDoc _baseClassName;
54   private String JavaDoc _extClassName;
55
56   private EmbeddableType _embeddableType;
57
58   public EmbeddableComponent()
59   {
60   }
61
62   /**
63    * Sets the bean info for the generator
64    */

65   public void setEmbeddableType(EmbeddableType embeddableType)
66   {
67     _embeddableType = embeddableType;
68   }
69
70   /**
71    * Sets the base class name
72    */

73   public void setBaseClassName(String JavaDoc baseClassName)
74   {
75     _baseClassName = baseClassName;
76   }
77
78   /**
79    * Gets the base class name
80    */

81   public String JavaDoc getBaseClassName()
82   {
83     return _baseClassName;
84   }
85
86   /**
87    * Sets the ext class name
88    */

89   public void setExtClassName(String JavaDoc extClassName)
90   {
91     _extClassName = extClassName;
92   }
93
94   /**
95    * Sets the ext class name
96    */

97   public String JavaDoc getClassName()
98   {
99     return _extClassName;
100   }
101
102   /**
103    * Get bean class name.
104    */

105   public String JavaDoc getBeanClassName()
106   {
107     return _baseClassName;
108   }
109
110   /**
111    * Starts generation of the Java code
112    */

113   public void generate(JavaWriter out)
114     throws IOException JavaDoc
115   {
116     try {
117       generateHeader(out);
118
119       generateInit(out);
120
121       // jpa/0u21
122
generateGetField(out);
123
124       generateFields(out);
125
126       generateLoad(out);
127
128     } catch (IOException JavaDoc e) {
129       throw e;
130     }
131   }
132
133   /**
134    * Generates the class header for the generated code.
135    */

136   private void generateHeader(JavaWriter out)
137     throws IOException JavaDoc
138   {
139     out.println("/*");
140     out.println(" * Generated by Resin Amber");
141     out.println(" * " + com.caucho.Version.VERSION);
142     out.println(" */");
143     out.print("private static final java.util.logging.Logger __caucho_log = ");
144     out.println("java.util.logging.Logger.getLogger(\"" + getBeanClassName() + "\");");
145   }
146
147   /**
148    * Generates the initialization.
149    */

150   private void generateInit(JavaWriter out)
151     throws IOException JavaDoc
152   {
153     String JavaDoc className = getClassName();
154     int p = className.lastIndexOf('.');
155     if (p > 0)
156       className = className.substring(p + 1);
157
158     ArrayList JavaDoc<AmberField> fields = _embeddableType.getFields();
159
160     for (JMethod ctor : _embeddableType.getBeanClass().getConstructors()) {
161       out.println();
162       // XXX: s/b actual access type?
163
out.print("public ");
164
165       out.print(className);
166       out.print("(");
167
168       JClass []args = ctor.getParameterTypes();
169       for (int i = 0; i < args.length; i++) {
170         if (i != 0)
171           out.print(", ");
172
173         out.print(args[i].getPrintName());
174         out.print(" a" + i);
175       }
176       out.println(")");
177       out.println("{");
178       out.pushDepth();
179
180       out.print("super(");
181       for (int i = 0; i < args.length; i++) {
182         if (i != 0)
183           out.print(", ");
184
185         out.print("a" + i);
186       }
187       out.println(");");
188
189       for (AmberField field : fields) {
190         field.generatePostConstructor(out);
191       }
192
193       out.popDepth();
194       out.println("}");
195     }
196   }
197
198   /**
199    * Generates the fields.
200    */

201   private void generateFields(JavaWriter out)
202     throws IOException JavaDoc
203   {
204     if (_embeddableType.isIdClass())
205       return;
206
207     ArrayList JavaDoc<AmberField> fields = _embeddableType.getFields();
208
209     for (int i = 0; i < fields.size(); i++) {
210       AmberField prop = fields.get(i);
211
212       prop.generatePrologue(out, null);
213
214       prop.generateSuperGetter(out);
215       prop.generateGetProperty(out);
216
217       prop.generateSuperSetter(out);
218       prop.generateSetProperty(out);
219     }
220   }
221
222   /**
223    * Generates the get field method.
224    */

225   private void generateGetField(JavaWriter out)
226     throws IOException JavaDoc
227   {
228     out.println();
229     out.println("public Object __caucho_get_field(int index)");
230     out.println("{");
231     out.pushDepth();
232
233     out.println("switch (index) {");
234     out.pushDepth();
235
236     ArrayList JavaDoc<AmberField> fields = _embeddableType.getFields();
237
238     for (int i = 0; i < fields.size(); i++) {
239       AmberField prop = fields.get(i);
240
241       if (! (prop instanceof PropertyField))
242         break;
243
244       out.println("case " + i + ":");
245
246       if (_embeddableType.isIdClass() || _embeddableType.isFieldAccess())
247         out.println(" return " + prop.getName() + ";");
248       else // jpa/0w00
249
out.println(" return " + prop.generateSuperGetter() + ";");
250
251       out.println();
252     }
253
254     out.println("default:");
255     out.println(" return null;");
256
257     out.popDepth();
258     out.println("}");
259
260     out.popDepth();
261     out.println("}");
262    }
263
264   /**
265    * Generates the load.
266    */

267   private void generateLoad(JavaWriter out)
268     throws IOException JavaDoc
269   {
270     out.println();
271     out.println("public int __caucho_load(com.caucho.amber.manager.AmberConnection aConn,");
272     out.println(" java.sql.ResultSet rs,");
273     out.println(" int index)");
274     out.println(" throws java.sql.SQLException");
275     out.println("{");
276     out.pushDepth();
277
278     if (_embeddableType.isIdClass()) {
279       out.println("return 0;");
280     }
281     else {
282       _embeddableType.generateLoad(out, "rs", "index", 0, -1);
283
284       out.println("return index;");
285     }
286
287     out.popDepth();
288     out.println("}");
289   }
290 }
291
Popular Tags