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