KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > BeanExtractingGeneratorExtension


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * 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
24 package com.mchange.v2.codegen.bean;
25
26 import java.util.*;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.io.IOException JavaDoc;
29 import com.mchange.v2.codegen.CodegenUtils;
30 import com.mchange.v2.codegen.IndentedWriter;
31
32 public class BeanExtractingGeneratorExtension implements GeneratorExtension
33 {
34     int ctor_modifiers = Modifier.PUBLIC;
35     int method_modifiers = Modifier.PRIVATE;
36
37     public void setConstructorModifiers( int ctor_modifiers )
38     { this.ctor_modifiers = ctor_modifiers; }
39
40     public int getConstructorModifiers()
41     { return ctor_modifiers; }
42
43     public void setExtractMethodModifiers( int ctor_modifiers )
44     { this.method_modifiers = method_modifiers; }
45
46     public int getExtractMethodModifiers()
47     { return method_modifiers; }
48
49     public Collection extraGeneralImports()
50     { return Collections.EMPTY_SET; }
51
52     public Collection extraSpecificImports()
53     {
54     Set set = new HashSet();
55     set.add("java.beans.BeanInfo");
56     set.add("java.beans.PropertyDescriptor");
57     set.add("java.beans.Introspector");
58     set.add("java.beans.IntrospectionException");
59     set.add("java.lang.reflect.InvocationTargetException");
60     return set;
61     }
62
63     public Collection extraInterfaceNames()
64     { return Collections.EMPTY_SET; }
65
66     public void generate(ClassInfo info, Class JavaDoc superclassType, Property[] props, Class JavaDoc[] propTypes, IndentedWriter iw)
67     throws IOException JavaDoc
68     {
69     iw.println("private static Class[] NOARGS = new Class[0];");
70     iw.println();
71     iw.print( CodegenUtils.getModifierString( method_modifiers ) );
72     iw.print(" void extractPropertiesFromBean( Object bean ) throws InvocationTargetException, IllegalAccessException, IntrospectionException");
73     iw.println("{");
74     iw.upIndent();
75
76     iw.println("BeanInfo bi = Introspector.getBeanInfo( bean.getClass() );");
77     iw.println("PropertyDescriptor[] pds = bi.getPropertyDescriptors();");
78     iw.println("for (int i = 0, len = pds.length; i < len; ++i)");
79     iw.println("{");
80     iw.upIndent();
81
82     for (int i = 0, len = props.length; i < len; ++i)
83         {
84         iw.println("if (\"" + props[i].getName() + "\".equals( pds[i].getName() ) )");
85         iw.upIndent();
86         iw.println("this." + props[i].getName() + " = " + extractorExpr( props[i], propTypes[i] ) + ';');
87         iw.downIndent();
88         }
89     iw.println("}"); //for loop
90

91
92     iw.downIndent();
93     iw.println("}"); //method
94
iw.println();
95     iw.print( CodegenUtils.getModifierString( ctor_modifiers ) );
96     iw.println(' ' + info.getClassName() + "( Object bean ) throws InvocationTargetException, IllegalAccessException, IntrospectionException");
97     iw.println("{");
98     iw.upIndent();
99     iw.println("extractPropertiesFromBean( bean );");
100     iw.downIndent();
101     iw.println("}");
102     }
103
104     private String JavaDoc extractorExpr( Property prop, Class JavaDoc propType )
105     {
106     if ( propType.isPrimitive() )
107         {
108         String JavaDoc castType = BeangenUtils.capitalize( prop.getSimpleTypeName() );
109         String JavaDoc valueMethod = prop.getSimpleTypeName() + "Value()";
110
111         if ( propType == char.class)
112             castType = "Character";
113         else if ( propType == int.class)
114             castType = "Integer";
115
116         return "((" + castType + ") pds[i].getReadMethod().invoke( bean, NOARGS ))." + valueMethod;
117         }
118     else
119         return "(" + prop.getSimpleTypeName() + ") pds[i].getReadMethod().invoke( bean, NOARGS )";
120     }
121 }
122
Popular Tags