1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package com
.sun
.star
.uno
;
23 * This class provides static methods which aim at exploring the contents of an
24 * Any and extracting its value.
26 * <p>All public methods take an Object argument that either is the immediate object,
27 * such as Boolean, Type, interface implementation, or an Any that contains an
30 * <p>The methods which extract the value do a widening conversion. See the
31 * method comments for the respective conversions.</p>
33 public class AnyConverter
36 * Determines the type of an Any object.
38 * @param object any object.
39 * @return type object.
41 public static Type
getType( Object object
)
46 t
= m_XInterface_type
;
48 else if (object
instanceof Any
)
50 t
= ((Any
)object
).getType();
52 if (TypeClass
.ANY_value
== t
.getTypeClass().getValue())
53 return getType( ((Any
)object
).getObject() );
57 t
= new Type( object
.getClass() );
63 * Checks if the any contains the idl type <code>void</code>.
65 * @param object the object to check.
66 * @return true when the any is void, false otherwise.
68 public static boolean isVoid(Object object
){
69 return containsType(TypeClass
.VOID
, object
);
73 * Checks if the any contains a value of the idl type <code>char</code>.
75 * @param object the object to check.
76 * @return true when the any contains a char, false otherwise.
78 public static boolean isChar(Object object
){
79 return containsType(TypeClass
.CHAR
, object
);
83 * Checks if the any contains a value of the idl type <code>boolean</code>.
85 * @param object the object to check.
86 * @return true when the any contains a boolean, false otherwise.
88 public static boolean isBoolean(Object object
){
89 return containsType(TypeClass
.BOOLEAN
, object
);
93 * Checks if the any contains a value of the idl type <code>byte</code>.
95 * @param object the object to check.
96 * @return true when the any contains a byte, false otherwise.
98 public static boolean isByte(Object object
){
99 return containsType(TypeClass
.BYTE
, object
);
103 * Checks if the any contains a value of the idl type <code>short</code>.
105 * @param object the object to check.
106 * @return true when the any contains a short, false otherwise.
108 public static boolean isShort(Object object
){
109 return containsType(TypeClass
.SHORT
, object
);
113 * Checks if the any contains a value of the idl type <code>long</code>
114 * (which maps to a java-int).
116 * @param object the object to check.
117 * @return true when the any contains a int, false otherwise.
119 public static boolean isInt(Object object
){
120 return containsType(TypeClass
.LONG
, object
);
124 * Checks if the any contains a value of the idl type <code>hyper</code>
125 * (which maps to a java-long).
127 * @param object the object to check.
128 * @return true when the any contains a long, false otherwise.
130 public static boolean isLong(Object object
){
131 return containsType(TypeClass
.HYPER
, object
);
135 * Checks if the any contains a value of the idl type <code>float</code>.
137 * @param object the object to check.
138 * @return true when the any contains a float, false otherwise.
140 public static boolean isFloat(Object object
){
141 return containsType(TypeClass
.FLOAT
, object
);
145 * Checks if the any contains a value of the idl type <code>double</code>.
147 * @param object the object to check.
148 * @return true when the any contains a double, false otherwise.
150 public static boolean isDouble(Object object
){
151 return containsType(TypeClass
.DOUBLE
, object
);
155 * Checks if the any contains a value of the idl type <code>string</code>.
157 * @param object the object to check.
158 * @return true when the any contains a string, false otherwise.
160 public static boolean isString(Object object
){
161 return containsType(TypeClass
.STRING
, object
);
165 * Checks if the any contains a value of the idl type <code>enum</code>.
167 * @param object the object to check.
168 * @return true if the any contains an enum, false otherwise.
170 public static boolean isEnum(Object object
)
172 return containsType(TypeClass
.ENUM
, object
);
176 * Checks if the any contains a value of the idl type <code>type</code>.
178 * @param object the object to check.
179 * @return true when the any contains a type, false otherwise.
181 public static boolean isType(Object object
){
182 return containsType(TypeClass
.TYPE
, object
);
186 * Checks if the any contains an interface, struct, exception, sequence or enum.
188 * <p>If <em>object</em> is an any with an interface type, then true is also
189 * returned if the any contains a null reference. This is because interfaces
190 * are allowed to have a null value contrary to other UNO types.</p>
192 * @param object the object to check.
193 * @return true if the any contains an object.
195 public static boolean isObject(Object object
)
197 int tc
= getType(object
).getTypeClass().getValue();
198 return (TypeClass
.INTERFACE_value
== tc
||
199 TypeClass
.STRUCT_value
== tc
||
200 TypeClass
.EXCEPTION_value
== tc
||
201 TypeClass
.SEQUENCE_value
== tc
||
202 TypeClass
.ENUM_value
== tc
);
206 * Checks if the any contains UNO idl sequence value (meaning a java array
207 * containing elements which are values of UNO idl types).
209 * @param object the object to check.
210 * @return true when the any contains an object which implements interfaces,
213 public static boolean isArray(Object object
){
214 return containsType(TypeClass
.SEQUENCE
, object
);
218 * Converts a Char object or an Any object containing a Char object into a
221 * @param object the object to convert.
222 * @return the char contained within the object.
223 * @throws com.sun.star.lang.IllegalArgumentException in case no char is
224 * contained within object.
228 public static char toChar(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
229 Character ret
= (Character
)convertSimple(TypeClass
.CHAR
, null, object
);
230 return ret
.charValue();
234 * Converts a Boolean object or an Any object containing a Boolean object
235 * into a simple boolean.
237 * @param object the object to convert.
238 * @return the boolean contained within the object
239 * @throws com.sun.star.lang.IllegalArgumentException in case no boolean is
240 * contained within object
244 public static boolean toBoolean(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
245 Boolean ret
= (Boolean
)convertSimple(TypeClass
.BOOLEAN
, null, object
);
246 return ret
.booleanValue();
250 * Converts a Byte object or an Any object containing a Byte object into a
253 * @param object the object to convert.
254 * @return the boolean contained within the object.
255 * @throws com.sun.star.lang.IllegalArgumentException in case no byte is
256 * contained within object.
260 public static byte toByte(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
261 Byte ret
= (Byte
)convertSimple(TypeClass
.BYTE
, null, object
);
262 return ret
.byteValue();
266 * Converts a number object into a simple short and allows widening conversions.
268 * <p>Allowed argument types are Byte, Short or Any containing these types.</p>
270 * @param object the object to convert.
271 * @throws com.sun.star.lang.IllegalArgumentException in case no short or
272 * byte is contained within object.
274 * @return the short contained within the object.
276 public static short toShort(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
277 Short ret
= (Short
)convertSimple(TypeClass
.SHORT
, null, object
);
278 return ret
.shortValue();
281 * Converts a number object into an idl unsigned short and allows widening
284 * <p>Allowed argument types are Anies containing idl unsigned short values.</p>
286 * @param object the object to convert.
287 * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned
288 * short is contained within Any.
290 * @return an (unsigned) short.
292 public static short toUnsignedShort(Object object
)
293 throws com
.sun
.star
.lang
.IllegalArgumentException
295 Short ret
= (Short
)convertSimple(TypeClass
.UNSIGNED_SHORT
, null, object
);
296 return ret
.shortValue();
300 * Converts a number object into a simple int and allows widening conversions.
302 * <p>Allowed argument types are Byte, Short, Integer or Any containing these
305 * @param object the object to convert.
306 * @throws com.sun.star.lang.IllegalArgumentException in case no short, byte
307 * or int is contained within object.
309 * @return the int contained within the object.
311 public static int toInt(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
312 Integer ret
= (Integer
) convertSimple( TypeClass
.LONG
, null, object
);
313 return ret
.intValue();
316 * Converts a number object into an idl unsigned long and allows widening
319 * <p>Allowed argument types are Anies containing idl unsigned short or
320 * unsigned long values.</p>
322 * @param object the object to convert.
323 * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned
324 * short nor unsigned long is contained within Any.
326 * @return an (unsigned) int.
328 public static int toUnsignedInt(Object object
)
329 throws com
.sun
.star
.lang
.IllegalArgumentException
331 Integer ret
= (Integer
)convertSimple(TypeClass
.UNSIGNED_LONG
, null, object
);
332 return ret
.intValue();
336 * Converts a number object into a simple long and allows widening conversions.
338 * <p>Allowed argument types are Byte, Short, Integer, Long or Any containing
341 * @param object the object to convert.
342 * @throws com.sun.star.lang.IllegalArgumentException in case no short, byte,
343 * int or long is contained within object.
345 * @return the long contained within the object.
347 public static long toLong(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
348 Long ret
= (Long
) convertSimple( TypeClass
.HYPER
, null, object
);
349 return ret
.longValue();
352 * Converts a number object into an idl unsigned hyper and allows widening
355 * <p>Allowed argument types are Anies containing idl unsigned short, unsigned
356 * long or unsigned hyper values.</p>
358 * @param object the object to convert.
359 * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned
360 * short, nor unsigned long nor unsigned hyper is contained within object.
362 * @return an (unsigned) long.
364 public static long toUnsignedLong(Object object
)
365 throws com
.sun
.star
.lang
.IllegalArgumentException
367 Long ret
= (Long
)convertSimple(TypeClass
.UNSIGNED_HYPER
, null, object
);
368 return ret
.longValue();
372 * Converts a number object into a simple float and allows widening conversions.
374 * <p>Allowed argument types are Byte, Short, Float or Any containing these
377 * @param object the object to convert.
378 * @throws com.sun.star.lang.IllegalArgumentException in case no byte, short
379 * or float is contained within object.
381 * @return the float contained within the object.
383 public static float toFloat(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
384 Float ret
= (Float
) convertSimple( TypeClass
.FLOAT
,null, object
);
385 return ret
.floatValue();
389 * Converts a number object into a simple double and allows widening conversions.
391 * <p>Allowed argument types are Byte, Short, Int, Float, Double or Any
392 * containing these types.</p>
394 * @param object the object to convert.
395 * @throws com.sun.star.lang.IllegalArgumentException in case no byte, short,
396 * int, float or double is contained within object.
398 * @return the double contained within the object.
400 public static double toDouble(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
401 Double ret
= (Double
) convertSimple( TypeClass
.DOUBLE
, null, object
);
402 return ret
.doubleValue();
406 * Converts a string or an any containing a string into a string.
408 * @param object the object to convert.
409 * @throws com.sun.star.lang.IllegalArgumentException in case no string is
410 * contained within object.
412 * @return the string contained within the object.
414 public static String
toString(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
415 return (String
) convertSimple( TypeClass
.STRING
, null, object
);
419 * Converts a Type or an any containing a Type into a Type.
421 * @param object the object to convert.
422 * @throws com.sun.star.lang.IllegalArgumentException in case no type is
423 * contained within object.
425 * @return the type contained within the object.
427 public static Type
toType(Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
428 return (Type
) convertSimple( TypeClass
.TYPE
, null, object
);
432 * Converts a UNO object (struct, exception, sequence, enum or interface) or
433 * an Any containing these types into a UNO object of a specified destination
436 * <p> For interfaces, the argument <em>object</em> is queried for the interface
437 * specified by the <em>type</em> argument.</p>
439 * <p>That query (UnoRuntime.queryInterface) might return null, if the interface
440 * is not implemented or a null-ref or a VOID any is given.</p>
442 * @param type type of the returned value.
443 * @param object the object that is to be converted.
444 * @throws com.sun.star.lang.IllegalArgumentException in case conversion is
447 * @return destination object.
449 public static Object
toObject(Type type
, Object object
)
450 throws com
.sun
.star
.lang
.IllegalArgumentException
452 return convertSimple( type
.getTypeClass(), type
, object
);
455 * Converts a UNO object (struct, exception, sequence, enum or interface) or
456 * an Any containing these types into a UNO object of a specified destination
459 * <p>For interfaces, the argument <em>object</em> is queried for the interface
460 * specified by the <em>type</em> argument. That query (UnoRuntime.queryInterface)
461 * might return null, if the interface is not implemented or a null-ref or a
462 * VOID any is given.</p>
464 * @param clazz class of the returned value.
465 * @param object the object that is to be converted.
466 * @throws com.sun.star.lang.IllegalArgumentException in case conversion is
469 * @return destination object.
471 public static Object
toObject(Class
<?
> clazz
, Object object
)
472 throws com
.sun
.star
.lang
.IllegalArgumentException
474 return toObject( new Type( clazz
), object
);
478 * Converts an array or an any containing an array into an array.
480 * @param object the object to convert.
481 * @throws com.sun.star.lang.IllegalArgumentException in case no array is
482 * contained within object.
484 * @return the array contained within the object.
486 public static Object
toArray( Object object
) throws com
.sun
.star
.lang
.IllegalArgumentException
{
487 return convertSimple( TypeClass
.SEQUENCE
, null, object
);
491 * Examines the argument <em>object</em> if is correspond to the type in
492 * argument <em>what</em>.
494 * <p><em>object</em> is either matched directly against the type or if it is
495 * an any then the contained object is matched against the type.</p>
497 private static boolean containsType( TypeClass what
, Object object
){
498 return (getType(object
).getTypeClass().getValue() == what
.getValue());
501 private static final Type m_XInterface_type
= new Type( XInterface
.class );
503 private static Object
convertSimple( TypeClass destTClass
, Type destType
, Object object_
)
504 throws com
.sun
.star
.lang
.IllegalArgumentException
508 if (object_
instanceof Any
) {
510 Any a
= (Any
)object_
;
511 object
= a
.getObject();
514 if (TypeClass
.ANY_value
== type
.getTypeClass().getValue())
515 return convertSimple( destTClass
, destType
, object
);
518 type
= (null == object ? m_XInterface_type
: new Type( object
.getClass() ));
521 int tc
= type
.getTypeClass().getValue();
522 int dest_tc
= destTClass
.getValue();
524 if (null == object
) {
525 // special for interfaces
526 if (TypeClass
.INTERFACE_value
== tc
&& dest_tc
== tc
)
530 case TypeClass
.CHAR_value
:
531 if (tc
== TypeClass
.CHAR_value
)
534 case TypeClass
.BOOLEAN_value
:
535 if (tc
== TypeClass
.BOOLEAN_value
)
538 case TypeClass
.BYTE_value
:
539 if (tc
== TypeClass
.BYTE_value
)
542 case TypeClass
.SHORT_value
:
544 case TypeClass
.BYTE_value
:
545 return Short
.valueOf( ((Byte
)object
).byteValue() );
546 case TypeClass
.SHORT_value
:
550 case TypeClass
.UNSIGNED_SHORT_value
:
552 case TypeClass
.UNSIGNED_SHORT_value
:
556 case TypeClass
.LONG_value
:
558 case TypeClass
.BYTE_value
:
559 return Integer
.valueOf( ((Byte
)object
).byteValue() );
560 case TypeClass
.SHORT_value
:
561 case TypeClass
.UNSIGNED_SHORT_value
:
562 return Integer
.valueOf( ((Short
)object
).shortValue() );
563 case TypeClass
.LONG_value
:
567 case TypeClass
.UNSIGNED_LONG_value
:
569 case TypeClass
.UNSIGNED_SHORT_value
:
570 return Integer
.valueOf( ((Short
)object
).shortValue() );
571 case TypeClass
.UNSIGNED_LONG_value
:
575 case TypeClass
.HYPER_value
:
577 case TypeClass
.BYTE_value
:
578 return Long
.valueOf( ((Byte
)object
).byteValue() );
579 case TypeClass
.SHORT_value
:
580 case TypeClass
.UNSIGNED_SHORT_value
:
581 return Long
.valueOf( ((Short
)object
).shortValue() );
582 case TypeClass
.LONG_value
:
583 case TypeClass
.UNSIGNED_LONG_value
:
584 return Long
.valueOf( ((Integer
)object
).intValue() );
585 case TypeClass
.HYPER_value
:
589 case TypeClass
.UNSIGNED_HYPER_value
:
591 case TypeClass
.UNSIGNED_SHORT_value
:
592 return Long
.valueOf( ((Short
)object
).shortValue() );
593 case TypeClass
.UNSIGNED_LONG_value
:
594 return Long
.valueOf( ((Integer
)object
).intValue() );
595 case TypeClass
.UNSIGNED_HYPER_value
:
599 case TypeClass
.FLOAT_value
:
601 case TypeClass
.BYTE_value
:
602 return new Float( ((Byte
)object
).byteValue() );
603 case TypeClass
.SHORT_value
:
604 return new Float( ((Short
)object
).shortValue() );
605 case TypeClass
.FLOAT_value
:
609 case TypeClass
.DOUBLE_value
:
611 case TypeClass
.BYTE_value
:
612 return new Double( ((Byte
)object
).byteValue() );
613 case TypeClass
.SHORT_value
:
614 return new Double( ((Short
)object
).shortValue() );
615 case TypeClass
.LONG_value
:
616 return new Double( ((Integer
)object
).intValue() );
617 case TypeClass
.FLOAT_value
:
618 return new Double( ((Float
)object
).floatValue() );
619 case TypeClass
.DOUBLE_value
:
623 case TypeClass
.ENUM_value
:
624 if (tc
== TypeClass
.ENUM_value
&&
625 (null == destType
|| destType
.equals( type
) /* optional destType */))
630 case TypeClass
.STRING_value
:
631 if (tc
== TypeClass
.STRING_value
)
634 case TypeClass
.TYPE_value
:
635 if (tc
== TypeClass
.TYPE_value
)
638 case TypeClass
.INTERFACE_value
:
639 // Because object is a class, not an interface, it is
640 // controversial what kind of Type "new Type(object.class)"
641 // above should return (UNKNOWN or INTERFACE), so that we should
642 // not check here for "tc == TypeClass.INTERFACE_value".
643 // Instead, we check whether object (indirectly) derives from
645 if (object
instanceof XInterface
)
646 return UnoRuntime
.queryInterface( destType
, object
);
648 case TypeClass
.STRUCT_value
:
649 case TypeClass
.EXCEPTION_value
:
650 if (destType
.isSupertypeOf(type
)) {
654 case TypeClass
.SEQUENCE_value
:
655 if (tc
== TypeClass
.SEQUENCE_value
&&
656 (null == destType
|| destType
.equals( type
) /* optional destType */))
663 throw new com
.sun
.star
.lang
.IllegalArgumentException(
664 "The Argument did not hold the proper type");
668 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */