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.Comparator;
026: import java.io.IOException;
027: import java.lang.reflect.Modifier;
028: import com.mchange.v1.lang.ClassUtils;
029: import com.mchange.v2.codegen.CodegenUtils;
030: import com.mchange.v2.codegen.IndentedWriter;
031:
032: public final class BeangenUtils {
033: public final static Comparator PROPERTY_COMPARATOR = new Comparator() {
034: public int compare(Object a, Object b) {
035: Property aa = (Property) a;
036: Property bb = (Property) b;
037:
038: return String.CASE_INSENSITIVE_ORDER.compare(aa.getName(),
039: bb.getName());
040: }
041: };
042:
043: public static String capitalize(String propName) {
044: char c = propName.charAt(0);
045: return Character.toUpperCase(c) + propName.substring(1);
046: }
047:
048: // public static Class[] attemptResolveTypes(ClassInfo info, Property[] props)
049: // {
050: // String[] gen = info.getGeneralImports();
051: // String[] spc = info.getSpecificImports();
052:
053: // Class[] out = new Class[ props.length ];
054: // for ( int i = 0, len = props.length; i < len; ++i )
055: // {
056: // String name = props[i].getSimpleTypeName();
057: // try
058: // { out[i] = ClassUtils.forName( name , gen, spc ); }
059: // catch ( Exception e )
060: // {
061: // e.printStackTrace();
062: // System.err.println("WARNING: " + this.getClass().getName() + " could not resolve " +
063: // "property type '" + name + "'.");
064: // out[i] = null;
065: // }
066: // }
067: // }
068:
069: public static void writeExplicitDefaultConstructor(
070: int ctor_modifiers, ClassInfo info, IndentedWriter iw)
071: throws IOException {
072: iw.print(CodegenUtils.getModifierString(ctor_modifiers));
073: iw.println(' ' + info.getClassName() + "()");
074: iw.println("{}");
075: }
076:
077: public static void writeArgList(Property[] props,
078: boolean declare_types, IndentedWriter iw)
079: throws IOException {
080: for (int i = 0, len = props.length; i < len; ++i) {
081: if (i != 0)
082: iw.print(", ");
083: if (declare_types)
084: iw.print(props[i].getSimpleTypeName() + ' ');
085: iw.print(props[i].getName());
086: }
087: }
088:
089: /**
090: * @deprecated use writePropertyVariable
091: */
092: public static void writePropertyMember(Property prop,
093: IndentedWriter iw) throws IOException {
094: writePropertyVariable(prop, iw);
095: }
096:
097: public static void writePropertyVariable(Property prop,
098: IndentedWriter iw) throws IOException {
099: writePropertyVariable(prop, prop.getDefaultValueExpression(),
100: iw);
101: }
102:
103: /**
104: * @deprecated use writePropertyVariable
105: */
106: public static void writePropertyMember(Property prop,
107: String defaultValueExpression, IndentedWriter iw)
108: throws IOException {
109: writePropertyVariable(prop, defaultValueExpression, iw);
110: }
111:
112: public static void writePropertyVariable(Property prop,
113: String defaultValueExpression, IndentedWriter iw)
114: throws IOException {
115: iw.print(CodegenUtils.getModifierString(prop
116: .getVariableModifiers()));
117: iw.print(' ' + prop.getSimpleTypeName() + ' ' + prop.getName());
118: String dflt = defaultValueExpression;
119: if (dflt != null)
120: iw.print(" = " + dflt);
121: iw.println(';');
122: }
123:
124: public static void writePropertyGetter(Property prop,
125: IndentedWriter iw) throws IOException {
126: writePropertyGetter(prop, prop.getDefensiveCopyExpression(), iw);
127: }
128:
129: public static void writePropertyGetter(Property prop,
130: String defensiveCopyExpression, IndentedWriter iw)
131: throws IOException {
132: String pfx = ("boolean".equals(prop.getSimpleTypeName()) ? "is"
133: : "get");
134: iw.print(CodegenUtils.getModifierString(prop
135: .getGetterModifiers()));
136: iw.println(' ' + prop.getSimpleTypeName() + ' ' + pfx
137: + BeangenUtils.capitalize(prop.getName()) + "()");
138: String retVal = defensiveCopyExpression;
139: if (retVal == null)
140: retVal = prop.getName();
141: iw.println("{ return " + retVal + "; }");
142: }
143:
144: public static void writePropertySetter(Property prop,
145: IndentedWriter iw) throws IOException {
146: writePropertySetter(prop, prop.getDefensiveCopyExpression(), iw);
147: }
148:
149: public static void writePropertySetter(Property prop,
150: String setterDefensiveCopyExpression, IndentedWriter iw)
151: throws IOException {
152: String setVal = setterDefensiveCopyExpression;
153: if (setVal == null)
154: setVal = prop.getName();
155: String usualGetExpression = ("this." + prop.getName());
156: String usualSetStatement = ("this." + prop.getName() + " = "
157: + setVal + ';');
158: writePropertySetterWithGetExpressionSetStatement(prop,
159: usualGetExpression, usualSetStatement, iw);
160: }
161:
162: public static void writePropertySetterWithGetExpressionSetStatement(
163: Property prop, String getExpression, String setStatement,
164: IndentedWriter iw) throws IOException {
165: iw.print(CodegenUtils.getModifierString(prop
166: .getSetterModifiers()));
167: iw.print(" void set" + BeangenUtils.capitalize(prop.getName())
168: + "( " + prop.getSimpleTypeName() + ' '
169: + prop.getName() + " )");
170: if (prop.isConstrained())
171: iw.println(" throws PropertyVetoException");
172: else
173: iw.println();
174: iw.println('{');
175: iw.upIndent();
176:
177: if (changeMarked(prop)) {
178: iw.println(prop.getSimpleTypeName() + " oldVal = "
179: + getExpression + ';');
180:
181: String oldValExpr = "oldVal";
182: String newValExpr = prop.getName();
183: String changeCheck;
184:
185: String simpleTypeName = prop.getSimpleTypeName();
186: if (ClassUtils.isPrimitive(simpleTypeName)) {
187: Class propType = ClassUtils
188: .classForPrimitive(simpleTypeName);
189:
190: // PropertyChangeSupport already has overloads
191: // for boolean and int
192: if (propType == byte.class) {
193: oldValExpr = "new Byte( " + oldValExpr + " )";
194: newValExpr = "new Byte( " + newValExpr + " )";
195: } else if (propType == char.class) {
196: oldValExpr = "new Character( " + oldValExpr + " )";
197: newValExpr = "new Character( " + newValExpr + " )";
198: } else if (propType == short.class) {
199: oldValExpr = "new Short( " + oldValExpr + " )";
200: newValExpr = "new Short( " + newValExpr + " )";
201: } else if (propType == float.class) {
202: oldValExpr = "new Float( " + oldValExpr + " )";
203: newValExpr = "new Float( " + newValExpr + " )";
204: } else if (propType == double.class) {
205: oldValExpr = "new Double( " + oldValExpr + " )";
206: newValExpr = "new Double( " + newValExpr + " )";
207: }
208:
209: changeCheck = "oldVal != " + prop.getName();
210: } else
211: changeCheck = "! eqOrBothNull( oldVal, "
212: + prop.getName() + " )";
213:
214: if (prop.isConstrained()) {
215: iw.println("if ( " + changeCheck + " )");
216: iw.upIndent();
217: iw.println("vcs.fireVetoableChange( \""
218: + prop.getName() + "\", " + oldValExpr + ", "
219: + newValExpr + " );");
220: iw.downIndent();
221: }
222:
223: iw.println(setStatement);
224:
225: if (prop.isBound()) {
226: iw.println("if ( " + changeCheck + " )");
227: iw.upIndent();
228: iw.println("pcs.firePropertyChange( \""
229: + prop.getName() + "\", " + oldValExpr + ", "
230: + newValExpr + " );");
231: iw.downIndent();
232: }
233: } else
234: iw.println(setStatement);
235:
236: iw.downIndent();
237: iw.println('}');
238: }
239:
240: public static boolean hasBoundProperties(Property[] props) {
241: for (int i = 0, len = props.length; i < len; ++i)
242: if (props[i].isBound())
243: return true;
244: return false;
245: }
246:
247: public static boolean hasConstrainedProperties(Property[] props) {
248: for (int i = 0, len = props.length; i < len; ++i)
249: if (props[i].isConstrained())
250: return true;
251: return false;
252: }
253:
254: private static boolean changeMarked(Property prop) {
255: return prop.isBound() || prop.isConstrained();
256: }
257:
258: private BeangenUtils() {
259: }
260: }
|