1 22 package org.jboss.xb.binding.sunday.unmarshalling.impl.runtime; 23 24 import java.lang.reflect.Array ; 25 import java.lang.reflect.Method ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 29 import javax.xml.namespace.NamespaceContext ; 30 import javax.xml.namespace.QName ; 31 import org.jboss.util.Classes; 32 import org.jboss.xb.binding.JBossXBRuntimeException; 33 import org.jboss.xb.binding.Util; 34 import org.jboss.xb.binding.introspection.FieldInfo; 35 import org.jboss.xb.binding.metadata.ValueMetaData; 36 import org.jboss.xb.binding.sunday.unmarshalling.ValueAdapter; 37 38 42 public class RtUtil 43 { 44 private RtUtil() 45 { 46 } 47 48 public static void add(Object o, 49 Object value, 50 String prop, 51 String colType, 52 boolean ignoreNotFoundField, 53 ValueAdapter valueAdapter) 54 { 55 FieldInfo fieldInfo = FieldInfo.getFieldInfo(o.getClass(), prop, !ignoreNotFoundField); 56 if(fieldInfo == null) 57 { 58 return; 59 } 60 61 Class fieldType = fieldInfo.getType(); 62 boolean arrType; 63 if(fieldType.isArray()) 64 { 65 arrType = true; 66 } 67 else if(Collection .class.isAssignableFrom(fieldType)) 68 { 69 arrType = false; 70 } 71 else 72 { 73 throw new JBossXBRuntimeException( 74 "Expected type for " + prop + " in " + o.getClass() + " is an array or java.util.Collection but was " + fieldType 75 ); 76 } 77 78 79 if(valueAdapter != null) 80 { 81 value = valueAdapter.cast(value, fieldType); 82 } 83 84 if(!arrType || colType != null) 85 { 86 Collection col = (Collection )fieldInfo.getValue(o); 87 if(col == null) 88 { 89 if(colType == null) 90 { 91 col = new ArrayList (); 92 } 93 else 94 { 95 Class colCls; 96 try 97 { 98 colCls = Thread.currentThread().getContextClassLoader().loadClass(colType); 99 } 100 catch(ClassNotFoundException e) 101 { 102 throw new JBossXBRuntimeException("Failed to load collection type: " + colType); 103 } 104 105 try 106 { 107 col = (Collection )colCls.newInstance(); 108 } 109 catch(Exception e) 110 { 111 throw new JBossXBRuntimeException("Failed to create an instance of " + colCls); 112 } 113 } 114 115 fieldInfo.setValue(o, col); 116 } 117 118 col.add(value); 119 } 120 else 121 { 122 Object arr = fieldInfo.getValue(o); 123 int length = 0; 124 if(arr == null) 125 { 126 arr = Array.newInstance(fieldType.getComponentType(), 1); 127 } 128 else 129 { 130 Object tmp = arr; 131 length = Array.getLength(arr); 132 arr = Array.newInstance(fieldType.getComponentType(), length + 1); 133 System.arraycopy(tmp, 0, arr, 0, length); 134 } 136 Array.set(arr, length, value); 137 fieldInfo.setValue(o, arr); 138 } 139 } 140 141 public static void set(Object o, 142 Object value, 143 String prop, 144 String colType, 145 boolean ignoreNotFoundField, 146 ValueAdapter valueAdapter) 147 { 148 FieldInfo fieldInfo = FieldInfo.getFieldInfo(o.getClass(), prop, !ignoreNotFoundField); 149 if(fieldInfo == null) 150 { 151 return; 152 } 153 154 Class fieldType = fieldInfo.getType(); 155 156 if(valueAdapter != null) 157 { 158 value = valueAdapter.cast(value, fieldType); 159 } 160 161 if(Collection .class.isAssignableFrom(fieldType) && 162 !Collection .class.isAssignableFrom(value.getClass())) 163 { 164 Collection col = (Collection )fieldInfo.getValue(o); 165 if(col == null) 166 { 167 if(colType == null) 168 { 169 col = new ArrayList (); 170 } 171 else 172 { 173 Class colCls; 174 try 175 { 176 colCls = Thread.currentThread().getContextClassLoader().loadClass(colType); 177 } 178 catch(ClassNotFoundException e) 179 { 180 throw new JBossXBRuntimeException("Failed to load collection type: " + colType); 181 } 182 183 try 184 { 185 col = (Collection )colCls.newInstance(); 186 } 187 catch(Exception e) 188 { 189 throw new JBossXBRuntimeException("Failed to create an instance of " + colCls); 190 } 191 } 192 193 fieldInfo.setValue(o, col); 194 } 195 196 col.add(value); 198 } 199 225 else 226 { 227 Class valueClass = value == null ? null : value.getClass(); 229 if (valueClass != null && fieldType.isArray() && Collection .class.isAssignableFrom(valueClass)) 230 { 231 Collection col = (Collection ) value; 232 Class compType = fieldType.getComponentType(); 233 value = Array.newInstance(compType, col.size()); 234 if (compType.isPrimitive()) 235 { 236 int i = 0; 237 for (Iterator iter = col.iterator(); iter.hasNext();) 238 { 239 Array.set(value, i++, iter.next()); 240 } 241 } 242 else 243 { 244 value = col.toArray((Object []) value); 245 } 246 } 247 248 fieldInfo.setValue(o, value); 249 } 250 } 251 252 public static void set(Object o, QName elementName, Object value, boolean ignoreLowLine) 253 { 254 if(o instanceof Collection ) 255 { 256 ((Collection )o).add(value); 257 } 258 else 259 { 260 String fieldName = Util.xmlNameToFieldName(elementName.getLocalPart(), ignoreLowLine); 261 FieldInfo fieldInfo = FieldInfo.getFieldInfo(o.getClass(), fieldName, true); 262 fieldInfo.setValue(o, value); 263 } 264 } 265 266 public static Class loadClass(String clsName, boolean failIfNotFound) 267 { 268 Class cls = null; 269 try 270 { 271 cls = Classes.loadClass(clsName); 272 } 273 catch(ClassNotFoundException e) 274 { 275 if(failIfNotFound) 276 { 277 throw new JBossXBRuntimeException("Failed to load class " + clsName); 278 } 279 } 280 return cls; 281 } 282 283 public static Method getUnmarshalMethod(QName qName, ValueMetaData valueMetaData) 284 { 285 String unmarshalMethod = valueMetaData.getUnmarshalMethod(); 286 if(unmarshalMethod == null) 287 { 288 throw new JBossXBRuntimeException( 289 "javaType annotation is specified for " + qName + " but does not contain parseMethod attribute" 290 ); 291 } 292 293 int lastDot = unmarshalMethod.lastIndexOf('.'); 294 String clsName = unmarshalMethod.substring(0, lastDot); 295 String methodName = unmarshalMethod.substring(lastDot + 1); 296 297 Class cls = RtUtil.loadClass(clsName, true); 298 299 try 300 { 301 return cls.getMethod(methodName, new Class []{String .class}); 302 } 303 catch(NoSuchMethodException e) 304 { 305 try 306 { 307 return cls.getMethod(methodName, new Class []{String .class, NamespaceContext .class}); 308 } 309 catch(NoSuchMethodException e1) 310 { 311 throw new JBossXBRuntimeException("Neither " + 312 methodName + 313 "(" + 314 String .class.getName() + 315 " p) nor " + 316 methodName + 317 "(" + 318 String .class.getName() + 319 " p1, " + 320 NamespaceContext .class.getName() + 321 " p2) were found in " + cls 322 ); 323 } 324 } 325 } 326 327 public static Object invokeUnmarshalMethod(Class cls, 328 String methodName, 329 Object value, 330 Class valueType, 331 NamespaceContext nsCtx, 332 QName qName) 333 { 334 Method method; 335 Object [] args; 336 try 337 { 338 method = cls.getMethod(methodName, new Class []{valueType}); 339 args = new Object []{value}; 340 } 341 catch(NoSuchMethodException e) 342 { 343 try 344 { 345 method = cls.getMethod(methodName, new Class []{valueType, NamespaceContext .class}); 346 args = new Object []{value, nsCtx}; 347 } 348 catch(NoSuchMethodException e1) 349 { 350 throw new JBossXBRuntimeException("Neither " + 351 methodName + 352 "(" + 353 valueType.getName() + 354 " p) nor " + 355 methodName + 356 "(" + 357 valueType.getName() + 358 " p1, " + 359 NamespaceContext .class.getName() + 360 " p2) were found in " + cls 361 ); 362 } 363 } 364 365 return invokeUnmarshalMethod(method, args, qName); 366 } 367 368 public static Object invokeUnmarshalMethod(Method method, Object [] args, QName qName) 369 { 370 Object unmarshalled; 371 try 372 { 373 unmarshalled = method.invoke(null, args); 374 } 375 catch(Exception e) 376 { 377 throw new JBossXBRuntimeException("Failed to invoke unmarshalMethod " + 378 method.getDeclaringClass().getName() + 379 "." + 380 method.getName() + 381 " for element " + 382 qName + 383 " and value " + 384 args[0] + 385 ": " + 386 e.getMessage(), 387 e 388 ); 389 } 390 return unmarshalled; 391 } 392 } 393 | Popular Tags |