1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.util.Comparator ; 27 import java.io.IOException ; 28 import java.lang.reflect.Modifier ; 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 PROPERTY_COMPARATOR = new Comparator () 36 { 37 public int compare(Object a, Object 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 capitalize( String propName ) 47 { 48 char c = propName.charAt( 0 ); 49 return Character.toUpperCase(c) + propName.substring(1); 50 } 51 52 57 73 public static void writeExplicitDefaultConstructor( int ctor_modifiers, ClassInfo info, IndentedWriter iw) throws IOException 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 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 96 public static void writePropertyMember( Property prop, IndentedWriter iw ) throws IOException 97 { writePropertyVariable( prop, iw ); } 98 99 public static void writePropertyVariable( Property prop, IndentedWriter iw ) throws IOException 100 { writePropertyVariable( prop, prop.getDefaultValueExpression(), iw ); } 101 102 105 public static void writePropertyMember( Property prop, String defaultValueExpression, IndentedWriter iw ) throws IOException 106 { writePropertyVariable( prop, defaultValueExpression, iw ); } 107 108 public static void writePropertyVariable( Property prop, String defaultValueExpression, IndentedWriter iw ) throws IOException 109 { 110 iw.print( CodegenUtils.getModifierString( prop.getVariableModifiers() ) ); 111 iw.print( ' ' + prop.getSimpleTypeName() + ' ' + prop.getName()); 112 String 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 119 { writePropertyGetter( prop, prop.getDefensiveCopyExpression(), iw ); } 120 121 public static void writePropertyGetter( Property prop, String defensiveCopyExpression, IndentedWriter iw ) throws IOException 122 { 123 String 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 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 133 { writePropertySetter( prop, prop.getDefensiveCopyExpression(), iw ); } 134 135 public static void writePropertySetter( Property prop, String setterDefensiveCopyExpression, IndentedWriter iw ) 136 throws IOException 137 { 138 String setVal = setterDefensiveCopyExpression; 139 if (setVal == null) setVal = prop.getName(); 140 String usualGetExpression = ("this." + prop.getName()); 141 String usualSetStatement = ("this." + prop.getName() + " = " + setVal + ';'); 142 writePropertySetterWithGetExpressionSetStatement(prop, usualGetExpression, usualSetStatement, iw); 143 } 144 145 public static void writePropertySetterWithGetExpressionSetStatement( Property prop, String getExpression, String setStatement, IndentedWriter iw ) 146 throws IOException 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 oldValExpr = "oldVal"; 162 String newValExpr = prop.getName(); 163 String changeCheck; 164 165 String simpleTypeName = prop.getSimpleTypeName(); 166 if ( ClassUtils.isPrimitive( simpleTypeName ) ) 167 { 168 Class propType = ClassUtils.classForPrimitive( simpleTypeName ); 169 170 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 |