KickJava   Java API By Example, From Geeks To Geeks.

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


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.Comparator JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Modifier JavaDoc;
29 import com.mchange.v1.lang.ClassUtils;
30 import com.mchange.v2.codegen.CodegenUtils;
31 import com.mchange.v2.codegen.IndentedWriter;
32
33 public final class BeangenUtils
34 {
35     public final static Comparator JavaDoc PROPERTY_COMPARATOR = new Comparator JavaDoc()
36     {
37     public int compare(Object JavaDoc a, Object JavaDoc b)
38     {
39         Property aa = (Property) a;
40         Property bb = (Property) b;
41
42         return String.CASE_INSENSITIVE_ORDER.compare(aa.getName(), bb.getName() );
43     }
44      };
45
46     public static String JavaDoc capitalize( String JavaDoc propName )
47     {
48     char c = propName.charAt( 0 );
49     return Character.toUpperCase(c) + propName.substring(1);
50     }
51
52 // public static Class[] attemptResolveTypes(ClassInfo info, Property[] props)
53
// {
54
// String[] gen = info.getGeneralImports();
55
// String[] spc = info.getSpecificImports();
56

57 // Class[] out = new Class[ props.length ];
58
// for ( int i = 0, len = props.length; i < len; ++i )
59
// {
60
// String name = props[i].getSimpleTypeName();
61
// try
62
// { out[i] = ClassUtils.forName( name , gen, spc ); }
63
// catch ( Exception e )
64
// {
65
// e.printStackTrace();
66
// System.err.println("WARNING: " + this.getClass().getName() + " could not resolve " +
67
// "property type '" + name + "'.");
68
// out[i] = null;
69
// }
70
// }
71
// }
72

73     public static void writeExplicitDefaultConstructor( int ctor_modifiers, ClassInfo info, IndentedWriter iw) throws IOException JavaDoc
74     {
75     iw.print( CodegenUtils.getModifierString( ctor_modifiers ) );
76     iw.println(' ' + info.getClassName() + "()");
77     iw.println("{}");
78     }
79
80
81     public static void writeArgList(Property[] props, boolean declare_types, IndentedWriter iw ) throws IOException JavaDoc
82     {
83     for (int i = 0, len = props.length; i < len; ++i)
84         {
85         if (i != 0)
86             iw.print(", ");
87         if (declare_types)
88             iw.print(props[i].getSimpleTypeName() + ' ');
89         iw.print( props[i].getName() );
90         }
91     }
92
93     /**
94      * @deprecated use writePropertyVariable
95      */

96     public static void writePropertyMember( Property prop, IndentedWriter iw ) throws IOException JavaDoc
97     { writePropertyVariable( prop, iw ); }
98
99     public static void writePropertyVariable( Property prop, IndentedWriter iw ) throws IOException JavaDoc
100     { writePropertyVariable( prop, prop.getDefaultValueExpression(), iw ); }
101
102     /**
103      * @deprecated use writePropertyVariable
104      */

105     public static void writePropertyMember( Property prop, String JavaDoc defaultValueExpression, IndentedWriter iw ) throws IOException JavaDoc
106     { writePropertyVariable( prop, defaultValueExpression, iw ); }
107
108     public static void writePropertyVariable( Property prop, String JavaDoc defaultValueExpression, IndentedWriter iw ) throws IOException JavaDoc
109     {
110     iw.print( CodegenUtils.getModifierString( prop.getVariableModifiers() ) );
111     iw.print( ' ' + prop.getSimpleTypeName() + ' ' + prop.getName());
112     String JavaDoc dflt = defaultValueExpression;
113     if (dflt != null)
114         iw.print( " = " + dflt );
115     iw.println(';');
116     }
117
118     public static void writePropertyGetter( Property prop, IndentedWriter iw ) throws IOException JavaDoc
119     { writePropertyGetter( prop, prop.getDefensiveCopyExpression(), iw ); }
120
121     public static void writePropertyGetter( Property prop, String JavaDoc defensiveCopyExpression, IndentedWriter iw ) throws IOException JavaDoc
122     {
123     String JavaDoc pfx = ("boolean".equals( prop.getSimpleTypeName() ) ? "is" : "get" );
124     iw.print( CodegenUtils.getModifierString( prop.getGetterModifiers() ) );
125     iw.println(' ' + prop.getSimpleTypeName() + ' ' + pfx + BeangenUtils.capitalize( prop.getName() ) + "()");
126     String JavaDoc retVal = defensiveCopyExpression;
127     if (retVal == null) retVal = prop.getName();
128     iw.println("{ return " + retVal + "; }");
129     }
130
131     public static void writePropertySetter( Property prop, IndentedWriter iw )
132     throws IOException JavaDoc
133     { writePropertySetter( prop, prop.getDefensiveCopyExpression(), iw ); }
134
135     public static void writePropertySetter( Property prop, String JavaDoc setterDefensiveCopyExpression, IndentedWriter iw )
136     throws IOException JavaDoc
137     {
138     String JavaDoc setVal = setterDefensiveCopyExpression;
139     if (setVal == null) setVal = prop.getName();
140     String JavaDoc usualGetExpression = ("this." + prop.getName());
141     String JavaDoc usualSetStatement = ("this." + prop.getName() + " = " + setVal + ';');
142     writePropertySetterWithGetExpressionSetStatement(prop, usualGetExpression, usualSetStatement, iw);
143     }
144
145     public static void writePropertySetterWithGetExpressionSetStatement( Property prop, String JavaDoc getExpression, String JavaDoc setStatement, IndentedWriter iw )
146     throws IOException JavaDoc
147     {
148     iw.print( CodegenUtils.getModifierString( prop.getSetterModifiers() ) );
149     iw.print(" void set" + BeangenUtils.capitalize( prop.getName() ) + "( " + prop.getSimpleTypeName() + ' ' + prop.getName() + " )");
150     if ( prop.isConstrained() )
151         iw.println(" throws PropertyVetoException");
152     else
153         iw.println();
154     iw.println('{');
155     iw.upIndent();
156
157     if ( changeMarked( prop ) )
158         {
159         iw.println( prop.getSimpleTypeName() + " oldVal = " + getExpression + ';');
160
161         String JavaDoc oldValExpr = "oldVal";
162         String JavaDoc newValExpr = prop.getName();
163         String JavaDoc changeCheck;
164
165         String JavaDoc simpleTypeName = prop.getSimpleTypeName();
166         if ( ClassUtils.isPrimitive( simpleTypeName ) )
167             {
168             Class JavaDoc propType = ClassUtils.classForPrimitive( simpleTypeName );
169
170             // PropertyChangeSupport already has overloads
171
// for boolean and int
172
if (propType == byte.class)
173                 {
174                 oldValExpr = "new Byte( "+ oldValExpr +" )";
175                 newValExpr = "new Byte( "+ newValExpr +" )";
176                 }
177             else if (propType == char.class)
178                 {
179                 oldValExpr = "new Character( "+ oldValExpr +" )";
180                 newValExpr = "new Character( "+ newValExpr +" )";
181                 }
182             else if (propType == short.class)
183                 {
184                 oldValExpr = "new Short( "+ oldValExpr +" )";
185                 newValExpr = "new Short( "+ newValExpr +" )";
186                 }
187             else if (propType == float.class)
188                 {
189                 oldValExpr = "new Float( "+ oldValExpr +" )";
190                 newValExpr = "new Float( "+ newValExpr +" )";
191                 }
192             else if (propType == double.class)
193                 {
194                 oldValExpr = "new Double( "+ oldValExpr +" )";
195                 newValExpr = "new Double( "+ newValExpr +" )";
196                 }
197
198             changeCheck = "oldVal != " + prop.getName();
199             }
200         else
201             changeCheck = "! eqOrBothNull( oldVal, " + prop.getName() + " )";
202             
203         if ( prop.isConstrained() )
204             {
205             iw.println("if ( " + changeCheck + " )");
206             iw.upIndent();
207             iw.println("vcs.fireVetoableChange( \"" + prop.getName() + "\", " + oldValExpr + ", " + newValExpr + " );");
208             iw.downIndent();
209             }
210
211         iw.println( setStatement );
212                 
213         if ( prop.isBound() )
214             {
215             iw.println("if ( " + changeCheck + " )");
216             iw.upIndent();
217             iw.println("pcs.firePropertyChange( \"" + prop.getName() + "\", " + oldValExpr + ", " + newValExpr + " );");
218             iw.downIndent();
219             }
220         }
221     else
222         iw.println( setStatement );
223
224     iw.downIndent();
225     iw.println('}');
226     }
227
228     public static boolean hasBoundProperties(Property[] props)
229     {
230     for (int i = 0, len = props.length; i < len; ++i)
231         if (props[i].isBound()) return true;
232     return false;
233     }
234
235     public static boolean hasConstrainedProperties(Property[] props)
236     {
237     for (int i = 0, len = props.length; i < len; ++i)
238         if (props[i].isConstrained()) return true;
239     return false;
240     }
241
242     private static boolean changeMarked( Property prop )
243     { return prop.isBound() || prop.isConstrained(); }
244
245     private BeangenUtils()
246     {}
247 }
248
Popular Tags