001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software 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 Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen.bean;
024:
025: import java.util.*;
026: import java.lang.reflect.Modifier;
027: import java.io.IOException;
028: import com.mchange.v2.codegen.CodegenUtils;
029: import com.mchange.v2.codegen.IndentedWriter;
030:
031: public class BeanExtractingGeneratorExtension implements
032: GeneratorExtension {
033: int ctor_modifiers = Modifier.PUBLIC;
034: int method_modifiers = Modifier.PRIVATE;
035:
036: public void setConstructorModifiers(int ctor_modifiers) {
037: this .ctor_modifiers = ctor_modifiers;
038: }
039:
040: public int getConstructorModifiers() {
041: return ctor_modifiers;
042: }
043:
044: public void setExtractMethodModifiers(int method_modifiers) {
045: this .method_modifiers = method_modifiers;
046: }
047:
048: public int getExtractMethodModifiers() {
049: return method_modifiers;
050: }
051:
052: public Collection extraGeneralImports() {
053: return Collections.EMPTY_SET;
054: }
055:
056: public Collection extraSpecificImports() {
057: Set set = new HashSet();
058: set.add("java.beans.BeanInfo");
059: set.add("java.beans.PropertyDescriptor");
060: set.add("java.beans.Introspector");
061: set.add("java.beans.IntrospectionException");
062: set.add("java.lang.reflect.InvocationTargetException");
063: return set;
064: }
065:
066: public Collection extraInterfaceNames() {
067: return Collections.EMPTY_SET;
068: }
069:
070: public void generate(ClassInfo info, Class super classType,
071: Property[] props, Class[] propTypes, IndentedWriter iw)
072: throws IOException {
073: iw.println("private static Class[] NOARGS = new Class[0];");
074: iw.println();
075: iw.print(CodegenUtils.getModifierString(method_modifiers));
076: iw
077: .print(" void extractPropertiesFromBean( Object bean ) throws InvocationTargetException, IllegalAccessException, IntrospectionException");
078: iw.println("{");
079: iw.upIndent();
080:
081: iw
082: .println("BeanInfo bi = Introspector.getBeanInfo( bean.getClass() );");
083: iw
084: .println("PropertyDescriptor[] pds = bi.getPropertyDescriptors();");
085: iw.println("for (int i = 0, len = pds.length; i < len; ++i)");
086: iw.println("{");
087: iw.upIndent();
088:
089: for (int i = 0, len = props.length; i < len; ++i) {
090: iw.println("if (\"" + props[i].getName()
091: + "\".equals( pds[i].getName() ) )");
092: iw.upIndent();
093: iw.println("this." + props[i].getName() + " = "
094: + extractorExpr(props[i], propTypes[i]) + ';');
095: iw.downIndent();
096: }
097: iw.println("}"); //for loop
098:
099: iw.downIndent();
100: iw.println("}"); //method
101: iw.println();
102: iw.print(CodegenUtils.getModifierString(ctor_modifiers));
103: iw
104: .println(' '
105: + info.getClassName()
106: + "( Object bean ) throws InvocationTargetException, IllegalAccessException, IntrospectionException");
107: iw.println("{");
108: iw.upIndent();
109: iw.println("extractPropertiesFromBean( bean );");
110: iw.downIndent();
111: iw.println("}");
112: }
113:
114: private String extractorExpr(Property prop, Class propType) {
115: if (propType.isPrimitive()) {
116: String castType = BeangenUtils.capitalize(prop
117: .getSimpleTypeName());
118: String valueMethod = prop.getSimpleTypeName() + "Value()";
119:
120: if (propType == char.class)
121: castType = "Character";
122: else if (propType == int.class)
123: castType = "Integer";
124:
125: return "(("
126: + castType
127: + ") pds[i].getReadMethod().invoke( bean, NOARGS ))."
128: + valueMethod;
129: } else
130: return "(" + prop.getSimpleTypeName()
131: + ") pds[i].getReadMethod().invoke( bean, NOARGS )";
132: }
133: }
|