001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Rodrigo Westrupp
028: */
029:
030: package com.caucho.amber.gen;
031:
032: import com.caucho.amber.field.*;
033: import com.caucho.amber.table.Column;
034: import com.caucho.amber.table.Table;
035: import com.caucho.amber.type.EmbeddableType;
036: import com.caucho.amber.type.Type;
037: import com.caucho.bytecode.*;
038: import com.caucho.java.JavaWriter;
039: import com.caucho.java.gen.ClassComponent;
040: import com.caucho.loader.Environment;
041: import com.caucho.util.L10N;
042:
043: import java.io.IOException;
044: import java.util.ArrayList;
045: import java.util.HashSet;
046:
047: /**
048: * Generates the Java code for the wrapped object.
049: */
050: public class EmbeddableComponent extends ClassComponent {
051: private static final L10N L = new L10N(EmbeddableComponent.class);
052:
053: private String _baseClassName;
054: private String _extClassName;
055:
056: private EmbeddableType _embeddableType;
057:
058: public EmbeddableComponent() {
059: }
060:
061: /**
062: * Sets the bean info for the generator
063: */
064: public void setEmbeddableType(EmbeddableType embeddableType) {
065: _embeddableType = embeddableType;
066: }
067:
068: /**
069: * Sets the base class name
070: */
071: public void setBaseClassName(String baseClassName) {
072: _baseClassName = baseClassName;
073: }
074:
075: /**
076: * Gets the base class name
077: */
078: public String getBaseClassName() {
079: return _baseClassName;
080: }
081:
082: /**
083: * Sets the ext class name
084: */
085: public void setExtClassName(String extClassName) {
086: _extClassName = extClassName;
087: }
088:
089: /**
090: * Sets the ext class name
091: */
092: public String getClassName() {
093: return _extClassName;
094: }
095:
096: /**
097: * Get bean class name.
098: */
099: public String getBeanClassName() {
100: return _baseClassName;
101: }
102:
103: /**
104: * Starts generation of the Java code
105: */
106: public void generate(JavaWriter out) throws IOException {
107: try {
108: generateHeader(out);
109:
110: generateInit(out);
111:
112: generateMake(out);
113:
114: generateMakeFromLoad(out);
115:
116: // jpa/0u21
117: generateGetField(out);
118:
119: // generateFields(out);
120:
121: generateLoad(out);
122: } catch (IOException e) {
123: throw e;
124: }
125: }
126:
127: /**
128: * Generates the class header for the generated code.
129: */
130: private void generateHeader(JavaWriter out) throws IOException {
131: out.println("/*");
132: out.println(" * Generated by Resin Amber");
133: out.println(" * " + com.caucho.Version.VERSION);
134: out.println(" */");
135: out
136: .println("private static final java.util.logging.Logger __caucho_log");
137: out.println(" = java.util.logging.Logger.getLogger(\""
138: + getBeanClassName() + "\");");
139: }
140:
141: /**
142: * Generates the initialization.
143: */
144: private void generateInit(JavaWriter out) throws IOException {
145: String className = getClassName();
146: int p = className.lastIndexOf('.');
147: if (p > 0)
148: className = className.substring(p + 1);
149:
150: ArrayList<AmberField> fields = _embeddableType.getFields();
151:
152: for (JMethod ctor : _embeddableType.getBeanClass()
153: .getConstructors()) {
154: out.println();
155: // XXX: s/b actual access type?
156: out.print("public ");
157:
158: out.print(className);
159: out.print("(");
160:
161: JClass[] args = ctor.getParameterTypes();
162: for (int i = 0; i < args.length; i++) {
163: if (i != 0)
164: out.print(", ");
165:
166: out.print(args[i].getPrintName());
167: out.print(" a" + i);
168: }
169: out.println(")");
170: out.println("{");
171: out.pushDepth();
172:
173: out.print("super(");
174: for (int i = 0; i < args.length; i++) {
175: if (i != 0)
176: out.print(", ");
177:
178: out.print("a" + i);
179: }
180: out.println(");");
181:
182: for (AmberField field : fields) {
183: field.generatePostConstructor(out);
184: }
185:
186: out.popDepth();
187: out.println("}");
188: }
189: }
190:
191: /**
192: * Generates the fields.
193: */
194: /*
195: private void generateFields(JavaWriter out)
196: throws IOException
197: {
198: // jpa/0u22
199: if (_embeddableType.isIdClass() && ! _embeddableType.isFieldAccess())
200: return;
201:
202: ArrayList<AmberField> fields = _embeddableType.getFields();
203:
204: for (int i = 0; i < fields.size(); i++) {
205: AmberField prop = fields.get(i);
206:
207: prop.generatePrologue(out, null);
208:
209: prop.generateSuperGetter(out);
210: prop.generateGetProperty(out);
211:
212: prop.generateSuperSetter(out);
213: prop.generateSetProperty(out);
214: }
215: }
216: */
217:
218: /**
219: * Generates the make method.
220: */
221: private void generateMake(JavaWriter out) throws IOException {
222: ArrayList<AmberField> fields = _embeddableType.getFields();
223:
224: out.println();
225: out.println("public static " + getClassName()
226: + " __caucho_make(");
227:
228: for (int i = 0; i < fields.size(); i++) {
229: AmberField prop = fields.get(i);
230:
231: out.print(" " + prop.getJavaTypeName() + " a" + i);
232:
233: if (i + 1 < fields.size())
234: out.println(",");
235: }
236:
237: out.println(")");
238:
239: out.println("{");
240: out.pushDepth();
241:
242: out.println(getClassName() + " bean = new " + getClassName()
243: + "();");
244:
245: for (int i = 0; i < fields.size(); i++) {
246: AmberField prop = fields.get(i);
247:
248: out
249: .println(prop.generateSuperSetter("bean", "a" + i)
250: + ";");
251: }
252:
253: out.println("return bean;");
254:
255: out.popDepth();
256: out.println("}");
257: }
258:
259: /**
260: * Generates the make method.
261: */
262: private void generateMakeFromLoad(JavaWriter out)
263: throws IOException {
264: ArrayList<AmberField> fields = _embeddableType.getFields();
265:
266: out.println();
267: out.println("public static " + getClassName()
268: + " __caucho_make(");
269: out
270: .println(" com.caucho.amber.manager.AmberConnection aConn,");
271: out.println(" java.sql.ResultSet rs,");
272: out.println(" int index)");
273: out.println(" throws java.sql.SQLException");
274:
275: out.println("{");
276: out.pushDepth();
277:
278: out.println(getClassName() + " bean = new " + getClassName()
279: + "();");
280:
281: out.println("bean.__caucho_load(aConn, rs, index);");
282:
283: out.println("return bean;");
284:
285: out.popDepth();
286: out.println("}");
287: }
288:
289: /**
290: * Generates the get field method.
291: */
292: private void generateGetField(JavaWriter out) throws IOException {
293: out.println();
294: out.println("public Object __caucho_get_field(int index)");
295: out.println("{");
296: out.pushDepth();
297:
298: out.println("switch (index) {");
299: out.pushDepth();
300:
301: ArrayList<AmberField> fields = _embeddableType.getFields();
302:
303: for (int i = 0; i < fields.size(); i++) {
304: AmberField prop = fields.get(i);
305:
306: if (!(prop instanceof PropertyField))
307: break;
308:
309: out.println("case " + i + ":");
310:
311: out.println(" return " + prop.generateSuperGetter() + ";");
312:
313: out.println();
314: }
315:
316: out.println("default:");
317: out
318: .println(" throw new IllegalStateException(\"invalid index: \" + index);");
319:
320: out.popDepth();
321: out.println("}");
322:
323: out.popDepth();
324: out.println("}");
325: }
326:
327: /**
328: * Generates the load.
329: */
330: private void generateLoad(JavaWriter out) throws IOException {
331: out.println();
332: out
333: .println("public int __caucho_load(com.caucho.amber.manager.AmberConnection aConn,");
334: out.println(" java.sql.ResultSet rs,");
335: out.println(" int index)");
336: out.println(" throws java.sql.SQLException");
337: out.println("{");
338: out.pushDepth();
339:
340: if (_embeddableType.isIdClass()) {
341: out.println("return 0;");
342: } else {
343: _embeddableType.generateLoad(out, "rs", "index", 0, -1,
344: null);
345:
346: out.println("return index;");
347: }
348:
349: out.popDepth();
350: out.println("}");
351: }
352: }
|