1*cdf0e10cSrcweir package com.sun.star.uno; 2*cdf0e10cSrcweir 3*cdf0e10cSrcweir /** This class provides static methods which aim at exploring the contents of an 4*cdf0e10cSrcweir * Any and extracting its value. All public methods take an Object argument that 5*cdf0e10cSrcweir * either is the immediate object, such as Boolean, Type, interface implementation, 6*cdf0e10cSrcweir * or an Any that contains an object. <br>The methods which extract the value do a 7*cdf0e10cSrcweir * widening conversion. See the method comments for the respective conversions. 8*cdf0e10cSrcweir */ 9*cdf0e10cSrcweir public class AnyConverter 10*cdf0e10cSrcweir { 11*cdf0e10cSrcweir /** Determines the type of an any object. 12*cdf0e10cSrcweir 13*cdf0e10cSrcweir @param object any object 14*cdf0e10cSrcweir @return type object 15*cdf0e10cSrcweir */ 16*cdf0e10cSrcweir static public Type getType( Object object ) 17*cdf0e10cSrcweir { 18*cdf0e10cSrcweir Type t; 19*cdf0e10cSrcweir if (null == object) 20*cdf0e10cSrcweir { 21*cdf0e10cSrcweir t = m_XInterface_type; 22*cdf0e10cSrcweir } 23*cdf0e10cSrcweir else if (object instanceof Any) 24*cdf0e10cSrcweir { 25*cdf0e10cSrcweir t = ((Any)object).getType(); 26*cdf0e10cSrcweir // nested any 27*cdf0e10cSrcweir if (TypeClass.ANY_value == t.getTypeClass().getValue()) 28*cdf0e10cSrcweir return getType( ((Any)object).getObject() ); 29*cdf0e10cSrcweir } 30*cdf0e10cSrcweir else 31*cdf0e10cSrcweir { 32*cdf0e10cSrcweir t = new Type( object.getClass() ); 33*cdf0e10cSrcweir } 34*cdf0e10cSrcweir return t; 35*cdf0e10cSrcweir } 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir /** checks if the any contains the idl type <code>void</code>. 38*cdf0e10cSrcweir @param object the object to check 39*cdf0e10cSrcweir @return true when the any is void, false otherwise 40*cdf0e10cSrcweir */ 41*cdf0e10cSrcweir static public boolean isVoid(Object object){ 42*cdf0e10cSrcweir return containsType(TypeClass.VOID, object); 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>char</code>. 46*cdf0e10cSrcweir @param object the object to check 47*cdf0e10cSrcweir @return true when the any contains a char, false otherwise. 48*cdf0e10cSrcweir */ 49*cdf0e10cSrcweir static public boolean isChar(Object object){ 50*cdf0e10cSrcweir return containsType(TypeClass.CHAR, object); 51*cdf0e10cSrcweir } 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>boolean</code>. 54*cdf0e10cSrcweir @param object the object to check 55*cdf0e10cSrcweir @return true when the any contains a boolean, false otherwise. 56*cdf0e10cSrcweir */ 57*cdf0e10cSrcweir static public boolean isBoolean(Object object){ 58*cdf0e10cSrcweir return containsType(TypeClass.BOOLEAN, object); 59*cdf0e10cSrcweir } 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>byte</code>. 62*cdf0e10cSrcweir @param object the object to check 63*cdf0e10cSrcweir @return true when the any contains a byte, false otherwise. 64*cdf0e10cSrcweir */ 65*cdf0e10cSrcweir static public boolean isByte(Object object){ 66*cdf0e10cSrcweir return containsType(TypeClass.BYTE, object); 67*cdf0e10cSrcweir } 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>short</code>. 70*cdf0e10cSrcweir @param object the object to check 71*cdf0e10cSrcweir @return true when the any contains a short, false otherwise. 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir static public boolean isShort(Object object){ 74*cdf0e10cSrcweir return containsType(TypeClass.SHORT, object); 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>long</code> (which maps to a java-int). 78*cdf0e10cSrcweir @param object the object to check 79*cdf0e10cSrcweir @return true when the any contains a int, false otherwise. 80*cdf0e10cSrcweir */ 81*cdf0e10cSrcweir static public boolean isInt(Object object){ 82*cdf0e10cSrcweir return containsType(TypeClass.LONG, object); 83*cdf0e10cSrcweir } 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>hyper</code> (which maps to a java-long). 86*cdf0e10cSrcweir @param object the object to check 87*cdf0e10cSrcweir @return true when the any contains a long, false otherwise. 88*cdf0e10cSrcweir */ 89*cdf0e10cSrcweir static public boolean isLong(Object object){ 90*cdf0e10cSrcweir return containsType(TypeClass.HYPER, object); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>float</code>. 94*cdf0e10cSrcweir @param object the object to check 95*cdf0e10cSrcweir @return true when the any contains a float, false otherwise. 96*cdf0e10cSrcweir */ 97*cdf0e10cSrcweir static public boolean isFloat(Object object){ 98*cdf0e10cSrcweir return containsType(TypeClass.FLOAT, object); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>double</code>. 102*cdf0e10cSrcweir @param object the object to check 103*cdf0e10cSrcweir @return true when the any contains a double, false otherwise. 104*cdf0e10cSrcweir */ 105*cdf0e10cSrcweir static public boolean isDouble(Object object){ 106*cdf0e10cSrcweir return containsType(TypeClass.DOUBLE, object); 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>string</code>. 110*cdf0e10cSrcweir @param object the object to check 111*cdf0e10cSrcweir @return true when the any contains a string, false otherwise. 112*cdf0e10cSrcweir */ 113*cdf0e10cSrcweir static public boolean isString(Object object){ 114*cdf0e10cSrcweir return containsType(TypeClass.STRING, object); 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>enum</code>. 118*cdf0e10cSrcweir @param object the object to check 119*cdf0e10cSrcweir @return true if the any contains an enum, false otherwise 120*cdf0e10cSrcweir */ 121*cdf0e10cSrcweir static public boolean isEnum(Object object) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir return containsType(TypeClass.ENUM, object); 124*cdf0e10cSrcweir } 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir /** checks if the any contains a value of the idl type <code>type</code>. 127*cdf0e10cSrcweir @param object the object to check 128*cdf0e10cSrcweir @return true when the any contains a type, false otherwise. 129*cdf0e10cSrcweir */ 130*cdf0e10cSrcweir static public boolean isType(Object object){ 131*cdf0e10cSrcweir return containsType(TypeClass.TYPE, object); 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir /** checks if the any contains an interface, struct, exception, sequence or enum. 135*cdf0e10cSrcweir If <em>object</em> is an any with an interface type, then true is also returned if 136*cdf0e10cSrcweir the any contains a null reference. This is because interfaces are allowed to have 137*cdf0e10cSrcweir a null value contrary to other UNO types. 138*cdf0e10cSrcweir @param object the object to check 139*cdf0e10cSrcweir @return true if the any contains an object 140*cdf0e10cSrcweir */ 141*cdf0e10cSrcweir static public boolean isObject(Object object) 142*cdf0e10cSrcweir { 143*cdf0e10cSrcweir int tc = getType(object).getTypeClass().getValue(); 144*cdf0e10cSrcweir return (TypeClass.INTERFACE_value == tc || 145*cdf0e10cSrcweir TypeClass.STRUCT_value == tc || 146*cdf0e10cSrcweir TypeClass.EXCEPTION_value == tc || 147*cdf0e10cSrcweir TypeClass.SEQUENCE_value == tc || 148*cdf0e10cSrcweir TypeClass.ENUM_value == tc); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir /** checks if the any contains UNO idl sequence value (meaning a java array 152*cdf0e10cSrcweir containing elements which are values of UNO idl types). 153*cdf0e10cSrcweir @param object the object to check 154*cdf0e10cSrcweir @return true when the any contains an object which implements interfaces, false otherwise. 155*cdf0e10cSrcweir */ 156*cdf0e10cSrcweir static public boolean isArray(Object object){ 157*cdf0e10cSrcweir return containsType(TypeClass.SEQUENCE, object); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir /** converts an Char object or an Any object containing a Char object into a simple char. 161*cdf0e10cSrcweir @param object the object to convert 162*cdf0e10cSrcweir @return the char contained within the object 163*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no char is contained within object 164*cdf0e10cSrcweir @see #isChar 165*cdf0e10cSrcweir */ 166*cdf0e10cSrcweir static public char toChar(Object object) throws com.sun.star.lang.IllegalArgumentException{ 167*cdf0e10cSrcweir Character ret= (Character)convertSimple(TypeClass.CHAR, null, object); 168*cdf0e10cSrcweir return ret.charValue(); 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir /** converts an Boolean object or an Any object containing a Boolean object into a simple boolean. 172*cdf0e10cSrcweir @param object the object to convert 173*cdf0e10cSrcweir @return the boolean contained within the object 174*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no boolean is contained within object 175*cdf0e10cSrcweir @see #isBoolean 176*cdf0e10cSrcweir */ 177*cdf0e10cSrcweir static public boolean toBoolean(Object object) throws com.sun.star.lang.IllegalArgumentException{ 178*cdf0e10cSrcweir Boolean ret= (Boolean)convertSimple(TypeClass.BOOLEAN, null, object); 179*cdf0e10cSrcweir return ret.booleanValue(); 180*cdf0e10cSrcweir } 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir /** converts an Byte object or an Any object containing a Byte object into a simple byte. 183*cdf0e10cSrcweir @param object the object to convert 184*cdf0e10cSrcweir @return the boolean contained within the object 185*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no byte is contained within object 186*cdf0e10cSrcweir @see #isBoolean 187*cdf0e10cSrcweir */ 188*cdf0e10cSrcweir static public byte toByte(Object object) throws com.sun.star.lang.IllegalArgumentException{ 189*cdf0e10cSrcweir Byte ret= (Byte)convertSimple(TypeClass.BYTE, null, object); 190*cdf0e10cSrcweir return ret.byteValue(); 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir /** converts a number object into a simple short and allows widening conversions. 194*cdf0e10cSrcweir Allowed argument types are Byte, Short or Any containing these types. 195*cdf0e10cSrcweir @param object the object to convert 196*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no short or byte is contained within object 197*cdf0e10cSrcweir @return the short contained within the object 198*cdf0e10cSrcweir */ 199*cdf0e10cSrcweir static public short toShort(Object object) throws com.sun.star.lang.IllegalArgumentException{ 200*cdf0e10cSrcweir Short ret= (Short)convertSimple(TypeClass.SHORT, null, object); 201*cdf0e10cSrcweir return ret.shortValue(); 202*cdf0e10cSrcweir } 203*cdf0e10cSrcweir /** converts a number object into an idl unsigned short and allows widening conversions. 204*cdf0e10cSrcweir Allowed argument types are Anies containing idl unsigned short values. 205*cdf0e10cSrcweir @param object the object to convert 206*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException 207*cdf0e10cSrcweir in case no idl unsigned short is contained within Any 208*cdf0e10cSrcweir @return an (unsigned) short 209*cdf0e10cSrcweir */ 210*cdf0e10cSrcweir static public short toUnsignedShort(Object object) 211*cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir Short ret= (Short)convertSimple(TypeClass.UNSIGNED_SHORT, null, object); 214*cdf0e10cSrcweir return ret.shortValue(); 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir /** converts a number object into a simple int and allows widening conversions. 218*cdf0e10cSrcweir Allowed argument types are Byte, Short, Integer or Any containing these types. 219*cdf0e10cSrcweir @param object the object to convert 220*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no short, byte or int is contained within object. 221*cdf0e10cSrcweir @return the int contained within the object 222*cdf0e10cSrcweir */ 223*cdf0e10cSrcweir static public int toInt(Object object) throws com.sun.star.lang.IllegalArgumentException{ 224*cdf0e10cSrcweir Integer ret= (Integer) convertSimple( TypeClass.LONG, null, object); 225*cdf0e10cSrcweir return ret.intValue(); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir /** converts a number object into an idl unsigned long and allows widening conversions. 228*cdf0e10cSrcweir Allowed argument types are Anies containing idl unsigned short or unsigned long values. 229*cdf0e10cSrcweir @param object the object to convert 230*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException 231*cdf0e10cSrcweir in case no idl unsigned short nor unsigned long is contained within Any 232*cdf0e10cSrcweir @return an (unsigned) int 233*cdf0e10cSrcweir */ 234*cdf0e10cSrcweir static public int toUnsignedInt(Object object) 235*cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException 236*cdf0e10cSrcweir { 237*cdf0e10cSrcweir Integer ret = (Integer)convertSimple(TypeClass.UNSIGNED_LONG, null, object); 238*cdf0e10cSrcweir return ret.intValue(); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir /** converts a number object into a simple long and allows widening conversions. 242*cdf0e10cSrcweir Allowed argument types are Byte, Short, Integer, Long or Any containing these types. 243*cdf0e10cSrcweir @param object the object to convert 244*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no short, byte, int or long 245*cdf0e10cSrcweir is contained within object. 246*cdf0e10cSrcweir @return the long contained within the object 247*cdf0e10cSrcweir */ 248*cdf0e10cSrcweir static public long toLong(Object object) throws com.sun.star.lang.IllegalArgumentException{ 249*cdf0e10cSrcweir Long ret= (Long) convertSimple( TypeClass.HYPER, null, object); 250*cdf0e10cSrcweir return ret.longValue(); 251*cdf0e10cSrcweir } 252*cdf0e10cSrcweir /** converts a number object into an idl unsigned hyper and allows widening conversions. 253*cdf0e10cSrcweir Allowed argument types are Anies containing idl unsigned short, unsigned long or 254*cdf0e10cSrcweir unsigned hyper values. 255*cdf0e10cSrcweir @param object the object to convert 256*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException 257*cdf0e10cSrcweir in case no idl unsigned short, nor unsigned long nor unsigned hyper 258*cdf0e10cSrcweir is contained within object. 259*cdf0e10cSrcweir @return an (unsigned) long 260*cdf0e10cSrcweir */ 261*cdf0e10cSrcweir static public long toUnsignedLong(Object object) 262*cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException 263*cdf0e10cSrcweir { 264*cdf0e10cSrcweir Long ret = (Long)convertSimple(TypeClass.UNSIGNED_HYPER, null, object); 265*cdf0e10cSrcweir return ret.longValue(); 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir /** converts a number object into a simple float and allows widening conversions. 269*cdf0e10cSrcweir Allowed argument types are Byte, Short, Float or Any containing these types. 270*cdf0e10cSrcweir @param object the object to convert 271*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no byte, short or float 272*cdf0e10cSrcweir is contained within object. 273*cdf0e10cSrcweir @return the float contained within the object 274*cdf0e10cSrcweir */ 275*cdf0e10cSrcweir static public float toFloat(Object object) throws com.sun.star.lang.IllegalArgumentException{ 276*cdf0e10cSrcweir Float ret= (Float) convertSimple( TypeClass.FLOAT,null, object); 277*cdf0e10cSrcweir return ret.floatValue(); 278*cdf0e10cSrcweir } 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir /** converts a number object into a simple double and allows widening conversions. 281*cdf0e10cSrcweir Allowed argument types are Byte, Short, Int, Float, Double or Any containing these types. 282*cdf0e10cSrcweir @param object the object to convert 283*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no byte, short, int, float 284*cdf0e10cSrcweir or double is contained within object. 285*cdf0e10cSrcweir @return the double contained within the object 286*cdf0e10cSrcweir */ 287*cdf0e10cSrcweir static public double toDouble(Object object) throws com.sun.star.lang.IllegalArgumentException { 288*cdf0e10cSrcweir Double ret= (Double) convertSimple( TypeClass.DOUBLE, null, object); 289*cdf0e10cSrcweir return ret.doubleValue(); 290*cdf0e10cSrcweir } 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir /** converts a string or an any containing a string into a string. 293*cdf0e10cSrcweir @param object the object to convert 294*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no string is contained within object. 295*cdf0e10cSrcweir @return the string contained within the object 296*cdf0e10cSrcweir */ 297*cdf0e10cSrcweir static public String toString(Object object) throws com.sun.star.lang.IllegalArgumentException { 298*cdf0e10cSrcweir return (String) convertSimple( TypeClass.STRING, null, object); 299*cdf0e10cSrcweir } 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir /** converts a Type or an any containing a Type into a Type. 302*cdf0e10cSrcweir @param object the object to convert 303*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no type is contained within object. 304*cdf0e10cSrcweir @return the type contained within the object 305*cdf0e10cSrcweir */ 306*cdf0e10cSrcweir static public Type toType(Object object) throws com.sun.star.lang.IllegalArgumentException { 307*cdf0e10cSrcweir return (Type) convertSimple( TypeClass.TYPE, null, object); 308*cdf0e10cSrcweir } 309*cdf0e10cSrcweir 310*cdf0e10cSrcweir /** converts a UNO object (struct, exception, sequence, enum or interface) or an Any containing 311*cdf0e10cSrcweir * these types into an UNO object of a specified destination type. 312*cdf0e10cSrcweir * For interfaces, the argument <em>object</em> is queried for the interface specified 313*cdf0e10cSrcweir * by the <em>type</em> argument. That query (UnoRuntime.queryInterface) might return null, 314*cdf0e10cSrcweir * if the interface is not implemented or a null-ref or a VOID any is given. 315*cdf0e10cSrcweir * 316*cdf0e10cSrcweir * @param type type of the returned value 317*cdf0e10cSrcweir * @param object the object that is to be converted 318*cdf0e10cSrcweir * @return destination object 319*cdf0e10cSrcweir * @throws com.sun.star.lang.IllegalArgumentException 320*cdf0e10cSrcweir * in case conversion is not possible 321*cdf0e10cSrcweir */ 322*cdf0e10cSrcweir static public Object toObject(Type type, Object object) 323*cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir return convertSimple( type.getTypeClass(), type, object ); 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir /** converts a UNO object (struct, exception, sequence, enum or interface) or an Any containing 328*cdf0e10cSrcweir * these types into an UNO object of a specified destination type. 329*cdf0e10cSrcweir * For interfaces, the argument <em>object</em> is queried for the interface specified 330*cdf0e10cSrcweir * by the <em>type</em> argument. That query (UnoRuntime.queryInterface) might return null, 331*cdf0e10cSrcweir * if the interface is not implemented or a null-ref or a VOID any is given. 332*cdf0e10cSrcweir * 333*cdf0e10cSrcweir * @param clazz class of the returned value 334*cdf0e10cSrcweir * @param object the object that is to be converted 335*cdf0e10cSrcweir * @return destination object 336*cdf0e10cSrcweir * @throws com.sun.star.lang.IllegalArgumentException 337*cdf0e10cSrcweir * in case conversion is not possible 338*cdf0e10cSrcweir */ 339*cdf0e10cSrcweir static public Object toObject(Class clazz, Object object) 340*cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException 341*cdf0e10cSrcweir { 342*cdf0e10cSrcweir return toObject( new Type( clazz ), object ); 343*cdf0e10cSrcweir } 344*cdf0e10cSrcweir 345*cdf0e10cSrcweir /** converts an array or an any containing an array into an array. 346*cdf0e10cSrcweir @param object the object to convert 347*cdf0e10cSrcweir @throws com.sun.star.lang.IllegalArgumentException in case no array is contained within object. 348*cdf0e10cSrcweir @return the array contained within the object 349*cdf0e10cSrcweir */ 350*cdf0e10cSrcweir static public Object toArray( Object object) throws com.sun.star.lang.IllegalArgumentException { 351*cdf0e10cSrcweir return convertSimple( TypeClass.SEQUENCE, null, object); 352*cdf0e10cSrcweir } 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir /** 355*cdf0e10cSrcweir Examines the argument <em>object</em> if is correspond to the type in argument <em>what</em>. 356*cdf0e10cSrcweir <em>object</em> is either matched directly against the type or if it is an any then the 357*cdf0e10cSrcweir contained object is matched against the type. 358*cdf0e10cSrcweir */ 359*cdf0e10cSrcweir static private boolean containsType( TypeClass what, Object object){ 360*cdf0e10cSrcweir return (getType(object).getTypeClass().getValue() == what.getValue()); 361*cdf0e10cSrcweir } 362*cdf0e10cSrcweir 363*cdf0e10cSrcweir static private final Type m_XInterface_type = new Type( XInterface.class ); 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir static private Object convertSimple( TypeClass destTClass, Type destType, Object object_ ) 366*cdf0e10cSrcweir throws com.sun.star.lang.IllegalArgumentException 367*cdf0e10cSrcweir { 368*cdf0e10cSrcweir Object object; 369*cdf0e10cSrcweir Type type; 370*cdf0e10cSrcweir if (object_ instanceof Any) 371*cdf0e10cSrcweir { 372*cdf0e10cSrcweir // unbox 373*cdf0e10cSrcweir Any a = (Any)object_; 374*cdf0e10cSrcweir object = a.getObject(); 375*cdf0e10cSrcweir type = a.getType(); 376*cdf0e10cSrcweir // nested any 377*cdf0e10cSrcweir if (TypeClass.ANY_value == type.getTypeClass().getValue()) 378*cdf0e10cSrcweir return convertSimple( destTClass, destType, object ); 379*cdf0e10cSrcweir } 380*cdf0e10cSrcweir else 381*cdf0e10cSrcweir { 382*cdf0e10cSrcweir object = object_; 383*cdf0e10cSrcweir type = (null == object ? m_XInterface_type : new Type( object.getClass() )); 384*cdf0e10cSrcweir } 385*cdf0e10cSrcweir 386*cdf0e10cSrcweir int tc = type.getTypeClass().getValue(); 387*cdf0e10cSrcweir int dest_tc = destTClass.getValue(); 388*cdf0e10cSrcweir 389*cdf0e10cSrcweir if (null == object) 390*cdf0e10cSrcweir { 391*cdf0e10cSrcweir // special for interfaces 392*cdf0e10cSrcweir if (TypeClass.INTERFACE_value == tc && dest_tc == tc) 393*cdf0e10cSrcweir return null; 394*cdf0e10cSrcweir } 395*cdf0e10cSrcweir else 396*cdf0e10cSrcweir { 397*cdf0e10cSrcweir switch (dest_tc) 398*cdf0e10cSrcweir { 399*cdf0e10cSrcweir case TypeClass.CHAR_value: 400*cdf0e10cSrcweir if (tc == TypeClass.CHAR_value) 401*cdf0e10cSrcweir return object; 402*cdf0e10cSrcweir break; 403*cdf0e10cSrcweir case TypeClass.BOOLEAN_value: 404*cdf0e10cSrcweir if (tc == TypeClass.BOOLEAN_value) 405*cdf0e10cSrcweir return object; 406*cdf0e10cSrcweir break; 407*cdf0e10cSrcweir case TypeClass.BYTE_value: 408*cdf0e10cSrcweir if (tc == TypeClass.BYTE_value) 409*cdf0e10cSrcweir return object; 410*cdf0e10cSrcweir break; 411*cdf0e10cSrcweir case TypeClass.SHORT_value: 412*cdf0e10cSrcweir switch (tc) 413*cdf0e10cSrcweir { 414*cdf0e10cSrcweir case TypeClass.BYTE_value: 415*cdf0e10cSrcweir return new Short( ((Byte)object).byteValue() ); 416*cdf0e10cSrcweir case TypeClass.SHORT_value: 417*cdf0e10cSrcweir return object; 418*cdf0e10cSrcweir } 419*cdf0e10cSrcweir break; 420*cdf0e10cSrcweir case TypeClass.UNSIGNED_SHORT_value: 421*cdf0e10cSrcweir switch (tc) 422*cdf0e10cSrcweir { 423*cdf0e10cSrcweir case TypeClass.UNSIGNED_SHORT_value: 424*cdf0e10cSrcweir return object; 425*cdf0e10cSrcweir } 426*cdf0e10cSrcweir break; 427*cdf0e10cSrcweir case TypeClass.LONG_value: 428*cdf0e10cSrcweir switch (tc) 429*cdf0e10cSrcweir { 430*cdf0e10cSrcweir case TypeClass.BYTE_value: 431*cdf0e10cSrcweir return new Integer( ((Byte)object).byteValue() ); 432*cdf0e10cSrcweir case TypeClass.SHORT_value: 433*cdf0e10cSrcweir case TypeClass.UNSIGNED_SHORT_value: 434*cdf0e10cSrcweir return new Integer( ((Short)object).shortValue() ); 435*cdf0e10cSrcweir case TypeClass.LONG_value: 436*cdf0e10cSrcweir return object; 437*cdf0e10cSrcweir } 438*cdf0e10cSrcweir break; 439*cdf0e10cSrcweir case TypeClass.UNSIGNED_LONG_value: 440*cdf0e10cSrcweir switch (tc) 441*cdf0e10cSrcweir { 442*cdf0e10cSrcweir case TypeClass.UNSIGNED_SHORT_value: 443*cdf0e10cSrcweir return new Integer( ((Short)object).shortValue() ); 444*cdf0e10cSrcweir case TypeClass.UNSIGNED_LONG_value: 445*cdf0e10cSrcweir return object; 446*cdf0e10cSrcweir } 447*cdf0e10cSrcweir break; 448*cdf0e10cSrcweir case TypeClass.HYPER_value: 449*cdf0e10cSrcweir switch (tc) 450*cdf0e10cSrcweir { 451*cdf0e10cSrcweir case TypeClass.BYTE_value: 452*cdf0e10cSrcweir return new Long( ((Byte)object).byteValue() ); 453*cdf0e10cSrcweir case TypeClass.SHORT_value: 454*cdf0e10cSrcweir case TypeClass.UNSIGNED_SHORT_value: 455*cdf0e10cSrcweir return new Long( ((Short)object).shortValue() ); 456*cdf0e10cSrcweir case TypeClass.LONG_value: 457*cdf0e10cSrcweir case TypeClass.UNSIGNED_LONG_value: 458*cdf0e10cSrcweir return new Long( ((Integer)object).intValue() ); 459*cdf0e10cSrcweir case TypeClass.HYPER_value: 460*cdf0e10cSrcweir return object; 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir break; 463*cdf0e10cSrcweir case TypeClass.UNSIGNED_HYPER_value: 464*cdf0e10cSrcweir switch (tc) 465*cdf0e10cSrcweir { 466*cdf0e10cSrcweir case TypeClass.UNSIGNED_SHORT_value: 467*cdf0e10cSrcweir return new Long( ((Short)object).shortValue() ); 468*cdf0e10cSrcweir case TypeClass.UNSIGNED_LONG_value: 469*cdf0e10cSrcweir return new Long( ((Integer)object).intValue() ); 470*cdf0e10cSrcweir case TypeClass.UNSIGNED_HYPER_value: 471*cdf0e10cSrcweir return object; 472*cdf0e10cSrcweir } 473*cdf0e10cSrcweir break; 474*cdf0e10cSrcweir case TypeClass.FLOAT_value: 475*cdf0e10cSrcweir switch (tc) 476*cdf0e10cSrcweir { 477*cdf0e10cSrcweir case TypeClass.BYTE_value: 478*cdf0e10cSrcweir return new Float( ((Byte)object).byteValue() ); 479*cdf0e10cSrcweir case TypeClass.SHORT_value: 480*cdf0e10cSrcweir return new Float( ((Short)object).shortValue() ); 481*cdf0e10cSrcweir case TypeClass.FLOAT_value: 482*cdf0e10cSrcweir return object; 483*cdf0e10cSrcweir } 484*cdf0e10cSrcweir break; 485*cdf0e10cSrcweir case TypeClass.DOUBLE_value: 486*cdf0e10cSrcweir switch (tc) 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir case TypeClass.BYTE_value: 489*cdf0e10cSrcweir return new Double( ((Byte)object).byteValue() ); 490*cdf0e10cSrcweir case TypeClass.SHORT_value: 491*cdf0e10cSrcweir return new Double( ((Short)object).shortValue() ); 492*cdf0e10cSrcweir case TypeClass.LONG_value: 493*cdf0e10cSrcweir return new Double( ((Integer)object).intValue() ); 494*cdf0e10cSrcweir case TypeClass.FLOAT_value: 495*cdf0e10cSrcweir return new Double( ((Float)object).floatValue() ); 496*cdf0e10cSrcweir case TypeClass.DOUBLE_value: 497*cdf0e10cSrcweir return object; 498*cdf0e10cSrcweir } 499*cdf0e10cSrcweir break; 500*cdf0e10cSrcweir case TypeClass.ENUM_value: 501*cdf0e10cSrcweir if (tc == TypeClass.ENUM_value && 502*cdf0e10cSrcweir (null == destTClass || destType.equals( type ) /* optional destType */)) 503*cdf0e10cSrcweir { 504*cdf0e10cSrcweir return object; 505*cdf0e10cSrcweir } 506*cdf0e10cSrcweir break; 507*cdf0e10cSrcweir case TypeClass.STRING_value: 508*cdf0e10cSrcweir if (tc == TypeClass.STRING_value) 509*cdf0e10cSrcweir return object; 510*cdf0e10cSrcweir break; 511*cdf0e10cSrcweir case TypeClass.TYPE_value: 512*cdf0e10cSrcweir if (tc == TypeClass.TYPE_value) 513*cdf0e10cSrcweir return object; 514*cdf0e10cSrcweir break; 515*cdf0e10cSrcweir case TypeClass.INTERFACE_value: 516*cdf0e10cSrcweir // Because object is a class, not an interface, it is 517*cdf0e10cSrcweir // controversial what kind of Type "new Type(object.class)" 518*cdf0e10cSrcweir // above should return (UNKNOWN or INTERFACE), so that we should 519*cdf0e10cSrcweir // not check here for "tc == TypeClass.INTERFACE_value". 520*cdf0e10cSrcweir // Instead, we check whether object (indirectly) derives from 521*cdf0e10cSrcweir // XInterface: 522*cdf0e10cSrcweir if (object instanceof XInterface) 523*cdf0e10cSrcweir return UnoRuntime.queryInterface( destType, object ); 524*cdf0e10cSrcweir break; 525*cdf0e10cSrcweir case TypeClass.STRUCT_value: 526*cdf0e10cSrcweir case TypeClass.EXCEPTION_value: 527*cdf0e10cSrcweir if (destType.isSupertypeOf(type)) { 528*cdf0e10cSrcweir return object; 529*cdf0e10cSrcweir } 530*cdf0e10cSrcweir break; 531*cdf0e10cSrcweir case TypeClass.SEQUENCE_value: 532*cdf0e10cSrcweir if (tc == TypeClass.SEQUENCE_value && 533*cdf0e10cSrcweir (null == destType || destType.equals( type ) /* optional destType */)) 534*cdf0e10cSrcweir { 535*cdf0e10cSrcweir return object; 536*cdf0e10cSrcweir } 537*cdf0e10cSrcweir break; 538*cdf0e10cSrcweir } 539*cdf0e10cSrcweir } 540*cdf0e10cSrcweir throw new com.sun.star.lang.IllegalArgumentException( 541*cdf0e10cSrcweir "The Argument did not hold the proper type"); 542*cdf0e10cSrcweir } 543*cdf0e10cSrcweir } 544