01: /*
02: * Distributed as part of c3p0 v.0.9.1.2
03: *
04: * Copyright (C) 2005 Machinery For Change, Inc.
05: *
06: * Author: Steve Waldman <swaldman@mchange.com>
07: *
08: * This library is free software; you can redistribute it and/or modify
09: * it under the terms of the GNU Lesser General Public License version 2.1, as
10: * published by the Free Software Foundation.
11: *
12: * This software 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 Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public License
18: * along with this software; see the file LICENSE. If not, write to the
19: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20: * Boston, MA 02111-1307, USA.
21: */
22:
23: package com.mchange.v2.codegen.bean;
24:
25: import java.util.*;
26:
27: import java.lang.reflect.Modifier;
28: import java.io.IOException;
29: import com.mchange.v2.codegen.CodegenUtils;
30: import com.mchange.v2.codegen.IndentedWriter;
31:
32: public class PropertyMapConstructorGeneratorExtension implements
33: GeneratorExtension {
34: int ctor_modifiers = Modifier.PUBLIC;
35:
36: public Collection extraGeneralImports() {
37: return Collections.EMPTY_SET;
38: }
39:
40: public Collection extraSpecificImports() {
41: Set set = new HashSet();
42: set.add("java.util.Map");
43: return set;
44: }
45:
46: public Collection extraInterfaceNames() {
47: return Collections.EMPTY_SET;
48: }
49:
50: public void generate(ClassInfo info, Class super classType,
51: Property[] props, Class[] propTypes, IndentedWriter iw)
52: throws IOException {
53: iw.print(CodegenUtils.getModifierString(ctor_modifiers));
54: iw.print(' ' + info.getClassName() + "( Map map )");
55: iw.println("{");
56: iw.upIndent();
57:
58: iw.println("Object raw;");
59: for (int i = 0, len = props.length; i < len; ++i) {
60: Property prop = props[i];
61: String propName = prop.getName();
62: Class propType = propTypes[i];
63: iw.println("raw = map.get( \"" + propName + "\" );");
64: iw.println("if (raw != null)");
65: iw.println("{");
66: iw.upIndent();
67:
68: iw.print("this." + propName + " = ");
69: if (propType == boolean.class)
70: iw.println("((Boolean) raw ).booleanValue();");
71: else if (propType == byte.class)
72: iw.println("((Byte) raw ).byteValue();");
73: else if (propType == char.class)
74: iw.println("((Character) raw ).charValue();");
75: else if (propType == short.class)
76: iw.println("((Short) raw ).shortValue();");
77: else if (propType == int.class)
78: iw.println("((Integer) raw ).intValue();");
79: else if (propType == long.class)
80: iw.println("((Long) raw ).longValue();");
81: else if (propType == float.class)
82: iw.println("((Float) raw ).floatValue();");
83: else if (propType == double.class)
84: iw.println("((Double) raw ).doubleValue();");
85: iw.println("raw = null;");
86:
87: iw.downIndent();
88: iw.println("}");
89: }
90:
91: iw.downIndent();
92: iw.println("}");
93: }
94: }
|