1 package com4j; 2 3 import java.lang.reflect.ParameterizedType ; 4 import java.lang.reflect.Type ; 5 import java.nio.Buffer ; 6 import java.nio.ByteBuffer ; 7 import java.util.Calendar ; 8 import java.util.Iterator ; 9 import java.util.GregorianCalendar ; 10 import java.util.TimeZone ; 11 12 import static com4j.Const.BYREF; 13 14 19 public enum NativeType { 20 28 BSTR(1) { 29 public NativeType byRef() { 30 return BSTR_ByRef; 31 } 32 }, 33 34 42 BSTR_ByRef(1|BYREF), 43 44 45 51 Unicode(2), 52 66 CSTR(3), 67 68 76 Int8(100) { 77 public NativeType byRef() { 78 return Int8_ByRef; 79 } 80 }, 81 Int8_ByRef(100|BYREF), 82 90 Int16(101) { 91 public NativeType byRef() { 92 return Int16_ByRef; 93 } 94 }, 95 Int16_ByRef(101|BYREF), 96 97 109 Int32(102) { 110 Object massage(Object param) { 112 Class <?> clazz = param.getClass(); 113 114 if( Enum .class.isAssignableFrom(clazz) ) { 115 return EnumDictionary.get((Class <? extends Enum >)clazz).value((Enum )param); 117 } 118 return param; 119 } 120 121 Object unmassage(Class <?> type, Type genericSignature, Object param) { 122 if( Enum .class.isAssignableFrom(type) ) { 123 return EnumDictionary.get((Class <? extends Enum >)type).constant((Integer )param); 124 } 125 126 return param; 127 } 128 129 public NativeType byRef() { 130 return Int32_ByRef; 131 } 132 }, 133 Int32_ByRef(102|BYREF), 134 135 144 Bool(103), 145 146 155 VariantBool(104), 156 157 165 Float(120), 166 167 175 Double(121), 176 177 181 HRESULT(200), 182 183 188 Default(201), 189 190 197 ComObject(300) { 198 Object massage(Object param) { 200 return COM4J.unwrap((Com4jObject)param).getPtr(); 201 } 202 203 Object unmassage(Class <?> type, Type genericSignature, Object param) { 204 if(param==null) return null; 205 if(type==Iterator .class) { 206 Class itemType = Object .class; 207 if(genericSignature instanceof ParameterizedType ) { 208 ParameterizedType pt = (ParameterizedType ) genericSignature; 209 Type it = pt.getActualTypeArguments()[0]; 210 if(it instanceof Class ) 211 itemType = (Class )it; 212 } 213 return new ComCollection(itemType,Wrapper.create(IEnumVARIANT.class, (Integer )param )); 214 } 215 return Wrapper.create( (Class <? extends Com4jObject>)type, (Integer )param ); 216 } 217 218 public NativeType byRef() { 219 return ComObject_ByRef; 220 } 221 }, 222 223 230 ComObject_ByRef(300|BYREF) { 231 Object massage(Object param) { 233 Holder h = (Holder)param; 234 h.value = ComObject.massage(h.value); 235 return h; 236 } 237 Object unmassage(Class <?> type, Type genericSignature, Object param) { 238 Holder h = (Holder)param; 239 h.value = ComObject.massage(h.value); 240 return h; 241 } 242 }, 243 244 255 GUID(301) { 256 Object massage(Object param) { 258 GUID g = (GUID)param; 259 return g.v; 260 } 261 Object unmassage(Class <?> signature, Type genericSignature, Object param) { 262 if(param==null) return null; 263 return new GUID( (long[])param ); 264 } 265 }, 266 267 312 VARIANT_ByRef(302|BYREF), 313 314 321 Dispatch(303) { 322 Object massage(Object param) { 324 int ptr = COM4J.unwrap((Com4jObject)param).getPtr(); 325 int disp = COM4J.queryInterface( ptr, COM4J.IID_IDispatch ); 326 return disp; 327 } 328 329 Object unmassage(Class <?> type, Type genericSignature, Object param) { 330 if(param==null) return null; 331 int disp = (Integer )param; 332 if(disp==0) return null; 333 Native.release( disp ); 334 return Wrapper.create( (Class <? extends Com4jObject>)type, (Integer )param ); 335 } 336 }, 337 338 350 PVOID(304) { 351 public NativeType byRef() { 352 return PVOID_ByRef; 353 } 354 }, 355 356 357 368 PVOID_ByRef(304|BYREF), 369 370 379 Date(400) { 380 Object massage(Object param) { 382 java.util.Date dt; 383 if( param instanceof Calendar ) { 384 dt = ((Calendar )param).getTime(); 385 } else { 386 dt = (java.util.Date )param; 387 } 388 389 long t = dt.getTime(); 391 t -= dt.getTimezoneOffset()*60*1000; 393 394 t += 2209161600000L; 396 397 if(t<0) { 399 long offset = -(t%MSPD); t = t-MSPD+offset; 402 } 403 double d = ((double)t)/MSPD; 404 return d; 405 } 406 407 Object unmassage(Class <?> signature, Type genericSignature, Object param) { 408 double d = (Double )param; 409 long t = (long)(d*MSPD); 410 t -= 2209161600000L; 411 t -= defaultTimeZone.getOffset(t); java.util.Date dt = new java.util.Date (t); 413 if(Calendar .class.isAssignableFrom(signature)) { 414 GregorianCalendar cal = new GregorianCalendar (); 415 cal.setTime(dt); 416 return cal; 417 } else { 418 return dt; 419 } 420 } 421 }, 422 423 424 448 SafeArray(500), 449 450 ; 451 452 453 454 455 456 460 final int code; 461 462 NativeType( int code ) { 463 this.code = code; 464 } 465 466 475 Object massage(Object param) { 476 return param; 477 } 478 490 Object unmassage(Class <?> signature, Type genericSignature, Object param) { 491 return param; 492 } 493 494 498 public NativeType byRef() { 499 return null; 500 } 501 502 503 private static final long MSPD = 24*60*60*1000; 504 private static final TimeZone defaultTimeZone = TimeZone.getDefault(); 505 506 } 507 | Popular Tags |