1 15 package org.apache.tapestry.enhance; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.service.ClassFabUtils; 23 import org.apache.hivemind.service.MethodSignature; 24 import org.apache.hivemind.util.Defense; 25 import org.apache.tapestry.IBinding; 26 import org.apache.tapestry.IRequestCycle; 27 import org.apache.tapestry.engine.IPageLoader; 28 import org.apache.tapestry.event.PageEvent; 29 import org.apache.tapestry.spec.IComponentSpecification; 30 31 37 public class EnhanceUtils 38 { 39 public static final MethodSignature FINISH_LOAD_SIGNATURE = new MethodSignature(void.class, 40 "finishLoad", new Class [] 41 { IRequestCycle.class, IPageLoader.class, IComponentSpecification.class }, null); 42 43 public static final MethodSignature PAGE_DETACHED_SIGNATURE = new MethodSignature(void.class, 44 "pageDetached", new Class [] 45 { PageEvent.class }, null); 46 47 public static final MethodSignature CLEANUP_AFTER_RENDER_SIGNATURE = new MethodSignature( 48 void.class, "cleanupAfterRender", new Class [] 49 { IRequestCycle.class }, null); 50 51 public static String createMutatorMethodName(String propertyName) 52 { 53 return "set" + upcase(propertyName); 54 } 55 56 public static String createAccessorMethodName(String propertyName) 57 { 58 return "get" + upcase(propertyName); 59 } 60 61 private static String upcase(String name) 62 { 63 return name.substring(0, 1).toUpperCase() + name.substring(1); 64 } 65 66 public static void createSimpleAccessor(EnhancementOperation op, String fieldName, 67 String propertyName, Class propertyType) 68 { 69 String methodName = op.getAccessorMethodName(propertyName); 70 71 op.addMethod( 72 Modifier.PUBLIC, 73 new MethodSignature(propertyType, methodName, null, null), 74 "return " + fieldName + ";"); 75 } 76 77 public static void createSimpleMutator(EnhancementOperation op, String fieldName, 78 String propertyName, Class propertyType) 79 { 80 String methodName = createMutatorMethodName(propertyName); 81 82 op.addMethod(Modifier.PUBLIC, new MethodSignature(void.class, methodName, new Class [] 83 { propertyType }, null), fieldName + " = $1;"); 84 } 85 86 102 103 public static Class extractPropertyType(EnhancementOperation op, String propertyName, 104 String definedTypeName) 105 { 106 Defense.notNull(op, "op"); 107 Defense.notNull(propertyName, "propertyName"); 108 109 if (definedTypeName != null) 110 { 111 Class propertyType = op.convertTypeName(definedTypeName); 112 113 op.validateProperty(propertyName, propertyType); 114 115 return propertyType; 116 } 117 118 Class propertyType = op.getPropertyType(propertyName); 119 120 return propertyType == null ? Object .class : propertyType; 121 } 122 123 126 public static boolean toBoolean(IBinding binding) 127 { 128 Boolean wrapped = (Boolean ) binding.getObject(Boolean .class); 129 130 return wrapped.booleanValue(); 131 } 132 133 public static byte toByte(IBinding binding) 134 { 135 Byte wrapped = (Byte ) binding.getObject(Byte .class); 136 137 return wrapped.byteValue(); 138 } 139 140 public static char toChar(IBinding binding) 141 { 142 Character wrapped = (Character ) binding.getObject(Character .class); 143 144 return wrapped.charValue(); 145 } 146 147 public static short toShort(IBinding binding) 148 { 149 Short wrapped = (Short ) binding.getObject(Short .class); 150 151 return wrapped.shortValue(); 152 } 153 154 public static int toInt(IBinding binding) 155 { 156 Integer wrapped = (Integer ) binding.getObject(Integer .class); 157 158 return wrapped.intValue(); 159 } 160 161 public static long toLong(IBinding binding) 162 { 163 Long wrapped = (Long ) binding.getObject(Long .class); 164 165 return wrapped.longValue(); 166 } 167 168 public static float toFloat(IBinding binding) 169 { 170 Float wrapped = (Float ) binding.getObject(Float .class); 171 172 return wrapped.floatValue(); 173 } 174 175 public static double toDouble(IBinding binding) 176 { 177 Double wrapped = (Double ) binding.getObject(Double .class); 178 179 return wrapped.doubleValue(); 180 } 181 182 187 188 private static Map _unwrappers = new HashMap (); 189 190 static 191 { 192 _unwrappers.put(boolean.class, "toBoolean"); 193 _unwrappers.put(byte.class, "toByte"); 194 _unwrappers.put(char.class, "toChar"); 195 _unwrappers.put(short.class, "toShort"); 196 _unwrappers.put(int.class, "toInt"); 197 _unwrappers.put(long.class, "toLong"); 198 _unwrappers.put(float.class, "toFloat"); 199 _unwrappers.put(double.class, "toDouble"); 200 } 201 202 206 207 public static String getUnwrapperMethodName(Class type) 208 { 209 Defense.notNull(type, "type"); 210 211 return (String ) _unwrappers.get(type); 212 } 213 214 226 227 public static String createUnwrapExpression(EnhancementOperation op, String bindingName, 228 Class valueType) 229 { 230 Defense.notNull(op, "op"); 231 Defense.notNull(bindingName, "bindingName"); 232 Defense.notNull(valueType, "valueType"); 233 234 StringBuffer buffer = new StringBuffer (); 235 236 String unwrapper = getUnwrapperMethodName(valueType); 237 238 if (unwrapper == null) 239 { 240 String propertyTypeRef = op.getClassReference(valueType); 241 242 buffer.append("("); 243 buffer.append(ClassFabUtils.getJavaClassName(valueType)); 244 buffer.append(") "); 245 buffer.append(bindingName); 246 buffer.append(".getObject("); 247 buffer.append(propertyTypeRef); 248 buffer.append(")"); 249 } 250 else 251 { 252 buffer.append(EnhanceUtils.class.getName()); 253 buffer.append("."); 254 buffer.append(unwrapper); 255 buffer.append("("); 256 buffer.append(bindingName); 257 buffer.append(")"); 258 } 259 260 return buffer.toString(); 261 } 262 263 274 public static Class verifyPropertyType(EnhancementOperation op, String propertyName, 275 Class requiredType) 276 { 277 Defense.notNull(op, "op"); 278 Defense.notNull(propertyName, "propertyName"); 279 Defense.notNull(requiredType, "requiredType"); 280 281 Class propertyType = op.getPropertyType(propertyName); 282 283 if (propertyType == null) 285 return Object .class; 286 287 290 if (!propertyType.isAssignableFrom(requiredType)) 291 throw new ApplicationRuntimeException(EnhanceMessages.wrongTypeForProperty( 292 propertyName, 293 propertyType, 294 requiredType)); 295 296 return propertyType; 297 } 298 } | Popular Tags |