diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /ridljar/com/sun/star/uno | |
parent | Initial commit. (diff) | |
download | libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ridljar/com/sun/star/uno')
-rw-r--r-- | ridljar/com/sun/star/uno/Any.java | 151 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/AnyConverter.java | 668 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/Ascii.java | 43 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/AsciiString.java | 44 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/Enum.java | 50 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/IBridge.java | 94 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/IEnvironment.java | 144 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/IMapping.java | 41 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/IQueryInterface.java | 60 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/MappingException.java | 65 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/Type.java | 699 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/UnoRuntime.java | 726 | ||||
-rw-r--r-- | ridljar/com/sun/star/uno/WeakReference.java | 154 |
13 files changed, 2939 insertions, 0 deletions
diff --git a/ridljar/com/sun/star/uno/Any.java b/ridljar/com/sun/star/uno/Any.java new file mode 100644 index 000000000..2f4976436 --- /dev/null +++ b/ridljar/com/sun/star/uno/Any.java @@ -0,0 +1,151 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +package com.sun.star.uno; + + +/** + * The UNO IDL type any is mapped to java type <code>java.lang.Object</code>. + * <p> + * In special cases it is necessary to have an explicit any to additionally transport + * an exact type. For instance if you want to pass an object reference via + * an interprocess connection using an any, you should use this class to add + * an explicit interface type, so the remote counterpart doesn't need to invoke + * a queryInterface). + * </p> + */ +public class Any { + /** + * The type of the any. + * + * @see #getType + */ + protected Type _type; + + /** + * The data of the any. + * + * @see #getObject + */ + protected Object _object; + + public static final Any VOID = new Any(new Type("void", TypeClass.VOID), + null); + // do not use Type.VOID here to avoid circular dependencies between + // static members of Any and Type + + /** + * Constructs a new any. + * + * @param zInterface the type of the any. + * @param object the data of the any. + * @deprecated as of UDK 2.0 + */ + @Deprecated + public Any(Class<?> zInterface, Object object) { + this(new Type(zInterface), object); + } + + /** + * Constructs a new any with a given type and value + * + * @param type the UNO type of the any. + * @param object the value of the any. + */ + public Any(Type type, Object object) { + if (type.equals(Type.ANY)) { + throw new IllegalArgumentException("Any cannot contain Any"); + } + _type = type; + _object = object; + } + + /** + * Complete a UNO <code>ANY</code> (make sure it is wrapped up as an + * <code>Any</code> instance). + * + * @param any a Java value representing a UNO <code>ANY</code> value. + * @return a complete Java value (that is, an <code>Any</code> instance) + * representing the same UNO <code>ANY</code> value as the given argument. + * @since UDK 3.2.3 + */ + public static final Any complete(Object any) { + return any instanceof Any + ? (Any) any + : new Any( + new Type(any == null ? XInterface.class : any.getClass()), any); + } + + /** + * Gets the type of the value within the any. + * + * @return the type of the value within the any. + */ + public Type getType() { + return _type; + } + + /** + * Gets the value within the any. + * + * @return gets the value within the any. + */ + public Object getObject() { + return _object; + } + + /** + * Indicates whether some other object is equal to this one. + * + * @param obj the reference object with which to compare. + * @return <code>true</code> if this object is the same as the obj argument; + * <code>false</code> otherwise. + * @see java.lang.Object#equals + */ + @Override + public boolean equals(Object obj) { + return obj instanceof Any && _type.equals(((Any) obj)._type) + && (_object == null + ? ((Any) obj)._object == null + : _object.equals(((Any) obj)._object)); + } + + /** + * Returns a hash code value for the object. + * + * @return a hash code value for this object. + * @see java.lang.Object#hashCode + */ + @Override + public int hashCode() { + return _type.hashCode() * 13 + + (_object == null ? 0 : _object.hashCode()); + } + + /** + * Returns a string representation of the object. + * + * @return a string representation of the object. + * @see java.lang.Object#toString + */ + @Override + public String toString() { + return "Any[" + _type + ", " + _object + "]"; + } +} diff --git a/ridljar/com/sun/star/uno/AnyConverter.java b/ridljar/com/sun/star/uno/AnyConverter.java new file mode 100644 index 000000000..d36542e1a --- /dev/null +++ b/ridljar/com/sun/star/uno/AnyConverter.java @@ -0,0 +1,668 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * This class provides static methods which aim at exploring the contents of an + * Any and extracting its value. + * + * <p>All public methods take an Object argument that either is the immediate object, + * such as Boolean, Type, interface implementation, or an Any that contains an + * object.</p> + * + * <p>The methods which extract the value do a widening conversion. See the + * method comments for the respective conversions.</p> + */ +public class AnyConverter +{ + /** + * Determines the type of an Any object. + * + * @param object any object. + * @return type object. + */ + public static Type getType( Object object ) + { + Type t; + if (null == object) + { + t = m_XInterface_type; + } + else if (object instanceof Any) + { + t = ((Any)object).getType(); + // nested any + if (TypeClass.ANY_value == t.getTypeClass().getValue()) + return getType( ((Any)object).getObject() ); + } + else + { + t = new Type( object.getClass() ); + } + return t; + } + + /** + * Checks if the any contains the idl type <code>void</code>. + * + * @param object the object to check. + * @return true when the any is void, false otherwise. + */ + public static boolean isVoid(Object object){ + return containsType(TypeClass.VOID, object); + } + + /** + * Checks if the any contains a value of the idl type <code>char</code>. + * + * @param object the object to check. + * @return true when the any contains a char, false otherwise. + */ + public static boolean isChar(Object object){ + return containsType(TypeClass.CHAR, object); + } + + /** + * Checks if the any contains a value of the idl type <code>boolean</code>. + * + * @param object the object to check. + * @return true when the any contains a boolean, false otherwise. + */ + public static boolean isBoolean(Object object){ + return containsType(TypeClass.BOOLEAN, object); + } + + /** + * Checks if the any contains a value of the idl type <code>byte</code>. + * + * @param object the object to check. + * @return true when the any contains a byte, false otherwise. + */ + public static boolean isByte(Object object){ + return containsType(TypeClass.BYTE, object); + } + + /** + * Checks if the any contains a value of the idl type <code>short</code>. + * + * @param object the object to check. + * @return true when the any contains a short, false otherwise. + */ + public static boolean isShort(Object object){ + return containsType(TypeClass.SHORT, object); + } + + /** + * Checks if the any contains a value of the idl type <code>long</code> + * (which maps to a java-int). + * + * @param object the object to check. + * @return true when the any contains a int, false otherwise. + */ + public static boolean isInt(Object object){ + return containsType(TypeClass.LONG, object); + } + + /** + * Checks if the any contains a value of the idl type <code>hyper</code> + * (which maps to a java-long). + * + * @param object the object to check. + * @return true when the any contains a long, false otherwise. + */ + public static boolean isLong(Object object){ + return containsType(TypeClass.HYPER, object); + } + + /** + * Checks if the any contains a value of the idl type <code>float</code>. + * + * @param object the object to check. + * @return true when the any contains a float, false otherwise. + */ + public static boolean isFloat(Object object){ + return containsType(TypeClass.FLOAT, object); + } + + /** + * Checks if the any contains a value of the idl type <code>double</code>. + * + * @param object the object to check. + * @return true when the any contains a double, false otherwise. + */ + public static boolean isDouble(Object object){ + return containsType(TypeClass.DOUBLE, object); + } + + /** + * Checks if the any contains a value of the idl type <code>string</code>. + * + * @param object the object to check. + * @return true when the any contains a string, false otherwise. + */ + public static boolean isString(Object object){ + return containsType(TypeClass.STRING, object); + } + + /** + * Checks if the any contains a value of the idl type <code>enum</code>. + * + * @param object the object to check. + * @return true if the any contains an enum, false otherwise. + */ + public static boolean isEnum(Object object) + { + return containsType(TypeClass.ENUM, object); + } + + /** + * Checks if the any contains a value of the idl type <code>type</code>. + * + * @param object the object to check. + * @return true when the any contains a type, false otherwise. + */ + public static boolean isType(Object object){ + return containsType(TypeClass.TYPE, object); + } + + /** + * Checks if the any contains an interface, struct, exception, sequence or enum. + * + * <p>If <em>object</em> is an any with an interface type, then true is also + * returned if the any contains a null reference. This is because interfaces + * are allowed to have a null value contrary to other UNO types.</p> + * + * @param object the object to check. + * @return true if the any contains an object. + */ + public static boolean isObject(Object object) + { + int tc = getType(object).getTypeClass().getValue(); + return (TypeClass.INTERFACE_value == tc || + TypeClass.STRUCT_value == tc || + TypeClass.EXCEPTION_value == tc || + TypeClass.SEQUENCE_value == tc || + TypeClass.ENUM_value == tc); + } + + /** + * Checks if the any contains UNO idl sequence value (meaning a java array + * containing elements which are values of UNO idl types). + * + * @param object the object to check. + * @return true when the any contains an object which implements interfaces, + * false otherwise. + */ + public static boolean isArray(Object object){ + return containsType(TypeClass.SEQUENCE, object); + } + + /** + * Converts a Char object or an Any object containing a Char object into a + * simple char. + * + * @param object the object to convert. + * @return the char contained within the object. + * @throws com.sun.star.lang.IllegalArgumentException in case no char is + * contained within object. + * + * @see #isChar + */ + public static char toChar(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Character ret= (Character)convertSimple(TypeClass.CHAR, null, object); + return ret.charValue(); + } + + /** + * Converts a Boolean object or an Any object containing a Boolean object + * into a simple boolean. + * + * @param object the object to convert. + * @return the boolean contained within the object + * @throws com.sun.star.lang.IllegalArgumentException in case no boolean is + * contained within object + * + * @see #isBoolean + */ + public static boolean toBoolean(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Boolean ret= (Boolean)convertSimple(TypeClass.BOOLEAN, null, object); + return ret.booleanValue(); + } + + /** + * Converts a Byte object or an Any object containing a Byte object into a + * simple byte. + * + * @param object the object to convert. + * @return the boolean contained within the object. + * @throws com.sun.star.lang.IllegalArgumentException in case no byte is + * contained within object. + * + * @see #isBoolean + */ + public static byte toByte(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Byte ret= (Byte)convertSimple(TypeClass.BYTE, null, object); + return ret.byteValue(); + } + + /** + * Converts a number object into a simple short and allows widening conversions. + * + * <p>Allowed argument types are Byte, Short or Any containing these types.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no short or + * byte is contained within object. + * + * @return the short contained within the object. + */ + public static short toShort(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Short ret= (Short)convertSimple(TypeClass.SHORT, null, object); + return ret.shortValue(); + } + /** + * Converts a number object into an idl unsigned short and allows widening + * conversions. + * + * <p>Allowed argument types are Anies containing idl unsigned short values.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned + * short is contained within Any. + * + * @return an (unsigned) short. + */ + public static short toUnsignedShort(Object object) + throws com.sun.star.lang.IllegalArgumentException + { + Short ret= (Short)convertSimple(TypeClass.UNSIGNED_SHORT, null, object); + return ret.shortValue(); + } + + /** + * Converts a number object into a simple int and allows widening conversions. + * + * <p>Allowed argument types are Byte, Short, Integer or Any containing these + * types.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no short, byte + * or int is contained within object. + * + * @return the int contained within the object. + */ + public static int toInt(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Integer ret= (Integer) convertSimple( TypeClass.LONG, null, object); + return ret.intValue(); + } + /** + * Converts a number object into an idl unsigned long and allows widening + * conversions. + * + * <p>Allowed argument types are Anies containing idl unsigned short or + * unsigned long values.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned + * short nor unsigned long is contained within Any. + * + * @return an (unsigned) int. + */ + public static int toUnsignedInt(Object object) + throws com.sun.star.lang.IllegalArgumentException + { + Integer ret = (Integer)convertSimple(TypeClass.UNSIGNED_LONG, null, object); + return ret.intValue(); + } + + /** + * Converts a number object into a simple long and allows widening conversions. + * + * <p>Allowed argument types are Byte, Short, Integer, Long or Any containing + * these types.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no short, byte, + * int or long is contained within object. + * + * @return the long contained within the object. + */ + public static long toLong(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Long ret= (Long) convertSimple( TypeClass.HYPER, null, object); + return ret.longValue(); + } + /** + * Converts a number object into an idl unsigned hyper and allows widening + * conversions. + * + * <p>Allowed argument types are Anies containing idl unsigned short, unsigned + * long or unsigned hyper values.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no idl unsigned + * short, nor unsigned long nor unsigned hyper is contained within object. + * + * @return an (unsigned) long. + */ + public static long toUnsignedLong(Object object) + throws com.sun.star.lang.IllegalArgumentException + { + Long ret = (Long)convertSimple(TypeClass.UNSIGNED_HYPER, null, object); + return ret.longValue(); + } + + /** + * Converts a number object into a simple float and allows widening conversions. + * + * <p>Allowed argument types are Byte, Short, Float or Any containing these + * types.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no byte, short + * or float is contained within object. + * + * @return the float contained within the object. + */ + public static float toFloat(Object object) throws com.sun.star.lang.IllegalArgumentException{ + Float ret= (Float) convertSimple( TypeClass.FLOAT,null, object); + return ret.floatValue(); + } + + /** + * Converts a number object into a simple double and allows widening conversions. + * + * <p>Allowed argument types are Byte, Short, Int, Float, Double or Any + * containing these types.</p> + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no byte, short, + * int, float or double is contained within object. + * + * @return the double contained within the object. + */ + public static double toDouble(Object object) throws com.sun.star.lang.IllegalArgumentException { + Double ret= (Double) convertSimple( TypeClass.DOUBLE, null, object); + return ret.doubleValue(); + } + + /** + * Converts a string or an any containing a string into a string. + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no string is + * contained within object. + * + * @return the string contained within the object. + */ + public static String toString(Object object) throws com.sun.star.lang.IllegalArgumentException { + return (String) convertSimple( TypeClass.STRING, null, object); + } + + /** + * Converts a Type or an any containing a Type into a Type. + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no type is + * contained within object. + * + * @return the type contained within the object. + */ + public static Type toType(Object object) throws com.sun.star.lang.IllegalArgumentException { + return (Type) convertSimple( TypeClass.TYPE, null, object); + } + + /** + * Converts a UNO object (struct, exception, sequence, enum or interface) or + * an Any containing these types into a UNO object of a specified destination + * type. + * + * <p> For interfaces, the argument <em>object</em> is queried for the interface + * specified by the <em>type</em> argument.</p> + * + * <p>That query (UnoRuntime.queryInterface) might return null, if the interface + * is not implemented or a null-ref or a VOID any is given.</p> + * + * @param type type of the returned value. + * @param object the object that is to be converted. + * @throws com.sun.star.lang.IllegalArgumentException in case conversion is + * not possible. + * + * @return destination object. + */ + public static Object toObject(Type type, Object object) + throws com.sun.star.lang.IllegalArgumentException + { + return convertSimple( type.getTypeClass(), type, object ); + } + /** + * Converts a UNO object (struct, exception, sequence, enum or interface) or + * an Any containing these types into a UNO object of a specified destination + * type. + * + * <p>For interfaces, the argument <em>object</em> is queried for the interface + * specified by the <em>type</em> argument. That query (UnoRuntime.queryInterface) + * might return null, if the interface is not implemented or a null-ref or a + * VOID any is given.</p> + * + * @param clazz class of the returned value. + * @param object the object that is to be converted. + * @throws com.sun.star.lang.IllegalArgumentException in case conversion is + * not possible. + * + * @return destination object. + */ + public static Object toObject(Class<?> clazz, Object object) + throws com.sun.star.lang.IllegalArgumentException + { + return toObject( new Type( clazz ), object ); + } + + /** + * Converts an array or an any containing an array into an array. + * + * @param object the object to convert. + * @throws com.sun.star.lang.IllegalArgumentException in case no array is + * contained within object. + * + * @return the array contained within the object. + */ + public static Object toArray( Object object) throws com.sun.star.lang.IllegalArgumentException { + return convertSimple( TypeClass.SEQUENCE, null, object); + } + + /** + * Examines the argument <em>object</em> if is correspond to the type in + * argument <em>what</em>. + * + * <p><em>object</em> is either matched directly against the type or if it is + * an any then the contained object is matched against the type.</p> + */ + private static boolean containsType( TypeClass what, Object object){ + return (getType(object).getTypeClass().getValue() == what.getValue()); + } + + private static final Type m_XInterface_type = new Type( XInterface.class ); + + private static Object convertSimple( TypeClass destTClass, Type destType, Object object_ ) + throws com.sun.star.lang.IllegalArgumentException + { + Object object; + Type type; + if (object_ instanceof Any) { + // unbox + Any a = (Any)object_; + object = a.getObject(); + type = a.getType(); + // nested any + if (TypeClass.ANY_value == type.getTypeClass().getValue()) + return convertSimple( destTClass, destType, object ); + } else { + object = object_; + type = (null == object ? m_XInterface_type : new Type( object.getClass() )); + } + + int tc = type.getTypeClass().getValue(); + int dest_tc = destTClass.getValue(); + + if (null == object) { + // special for interfaces + if (TypeClass.INTERFACE_value == tc && dest_tc == tc) + return null; + } else { + switch (dest_tc) { + case TypeClass.CHAR_value: + if (tc == TypeClass.CHAR_value) + return object; + break; + case TypeClass.BOOLEAN_value: + if (tc == TypeClass.BOOLEAN_value) + return object; + break; + case TypeClass.BYTE_value: + if (tc == TypeClass.BYTE_value) + return object; + break; + case TypeClass.SHORT_value: + switch (tc) { + case TypeClass.BYTE_value: + return Short.valueOf( ((Byte)object).byteValue() ); + case TypeClass.SHORT_value: + return object; + } + break; + case TypeClass.UNSIGNED_SHORT_value: + switch (tc) { + case TypeClass.UNSIGNED_SHORT_value: + return object; + } + break; + case TypeClass.LONG_value: + switch (tc) { + case TypeClass.BYTE_value: + return Integer.valueOf( ((Byte)object).byteValue() ); + case TypeClass.SHORT_value: + case TypeClass.UNSIGNED_SHORT_value: + return Integer.valueOf( ((Short)object).shortValue() ); + case TypeClass.LONG_value: + return object; + } + break; + case TypeClass.UNSIGNED_LONG_value: + switch (tc) { + case TypeClass.UNSIGNED_SHORT_value: + return Integer.valueOf( ((Short)object).shortValue() ); + case TypeClass.UNSIGNED_LONG_value: + return object; + } + break; + case TypeClass.HYPER_value: + switch (tc) { + case TypeClass.BYTE_value: + return Long.valueOf( ((Byte)object).byteValue() ); + case TypeClass.SHORT_value: + case TypeClass.UNSIGNED_SHORT_value: + return Long.valueOf( ((Short)object).shortValue() ); + case TypeClass.LONG_value: + case TypeClass.UNSIGNED_LONG_value: + return Long.valueOf( ((Integer)object).intValue() ); + case TypeClass.HYPER_value: + return object; + } + break; + case TypeClass.UNSIGNED_HYPER_value: + switch (tc) { + case TypeClass.UNSIGNED_SHORT_value: + return Long.valueOf( ((Short)object).shortValue() ); + case TypeClass.UNSIGNED_LONG_value: + return Long.valueOf( ((Integer)object).intValue() ); + case TypeClass.UNSIGNED_HYPER_value: + return object; + } + break; + case TypeClass.FLOAT_value: + switch (tc) { + case TypeClass.BYTE_value: + return new Float( ((Byte)object).byteValue() ); + case TypeClass.SHORT_value: + return new Float( ((Short)object).shortValue() ); + case TypeClass.FLOAT_value: + return object; + } + break; + case TypeClass.DOUBLE_value: + switch (tc) { + case TypeClass.BYTE_value: + return new Double( ((Byte)object).byteValue() ); + case TypeClass.SHORT_value: + return new Double( ((Short)object).shortValue() ); + case TypeClass.LONG_value: + return new Double( ((Integer)object).intValue() ); + case TypeClass.FLOAT_value: + return new Double( ((Float)object).floatValue() ); + case TypeClass.DOUBLE_value: + return object; + } + break; + case TypeClass.ENUM_value: + if (tc == TypeClass.ENUM_value && + (null == destType || destType.equals( type ) /* optional destType */)) + { + return object; + } + break; + case TypeClass.STRING_value: + if (tc == TypeClass.STRING_value) + return object; + break; + case TypeClass.TYPE_value: + if (tc == TypeClass.TYPE_value) + return object; + break; + case TypeClass.INTERFACE_value: + // Because object is a class, not an interface, it is + // controversial what kind of Type "new Type(object.class)" + // above should return (UNKNOWN or INTERFACE), so that we should + // not check here for "tc == TypeClass.INTERFACE_value". + // Instead, we check whether object (indirectly) derives from + // XInterface: + if (object instanceof XInterface) + return UnoRuntime.queryInterface( destType, object ); + break; + case TypeClass.STRUCT_value: + case TypeClass.EXCEPTION_value: + if (destType.isSupertypeOf(type)) { + return object; + } + break; + case TypeClass.SEQUENCE_value: + if (tc == TypeClass.SEQUENCE_value && + (null == destType || destType.equals( type ) /* optional destType */)) + { + return object; + } + break; + } + } + throw new com.sun.star.lang.IllegalArgumentException( + "The Argument did not hold the proper type"); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/uno/Ascii.java b/ridljar/com/sun/star/uno/Ascii.java new file mode 100644 index 000000000..ce5108893 --- /dev/null +++ b/ridljar/com/sun/star/uno/Ascii.java @@ -0,0 +1,43 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * The Ascii class represents the IDL build in type <code>ascii</code>. + * + * @deprecated do not use. + */ +@Deprecated +public final class Ascii { + public final char ascii; + + /** + * Constructs a new <code>Ascii</code>. + * + * @deprecated do not use. + * @param c the char value. + */ + @Deprecated + public Ascii(char c) { + ascii = c; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/uno/AsciiString.java b/ridljar/com/sun/star/uno/AsciiString.java new file mode 100644 index 000000000..8b744c5a2 --- /dev/null +++ b/ridljar/com/sun/star/uno/AsciiString.java @@ -0,0 +1,44 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * The Ascii class represents the IDL build in type <code>asciistring</code>. + * + * @deprecated do not use. + */ +@Deprecated +public final class AsciiString { + public final String asciistring; + + /** + * Constructs a new <code>AsciiString</code>. + * + * @deprecated do not use. + * @param s the String value. + */ + @Deprecated + public AsciiString(String s) { + asciistring = s; + } + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/uno/Enum.java b/ridljar/com/sun/star/uno/Enum.java new file mode 100644 index 000000000..8bd50876d --- /dev/null +++ b/ridljar/com/sun/star/uno/Enum.java @@ -0,0 +1,50 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * The Enum class is the base class for all classes generated + * as java binding for the IDL type enum. + * <p> + * Each java mapped enum class provides static member of this class + * which represents the enum values. + * You cannot create an object of this class or subclass direct, to + * avoid enum values with integer values outside the defined range. + * </p> + */ +public abstract class Enum { + private final int m_value; + + /** + * Constructs an enum value. + * @param value the integer value of this enum value. + */ + protected Enum(int value) { + m_value = value; + } + + /** + * Get the integer value of an enum value. + * @return the integer value. + */ + public final int getValue() { + return m_value; + } +} + diff --git a/ridljar/com/sun/star/uno/IBridge.java b/ridljar/com/sun/star/uno/IBridge.java new file mode 100644 index 000000000..51babcdfc --- /dev/null +++ b/ridljar/com/sun/star/uno/IBridge.java @@ -0,0 +1,94 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import java.io.IOException; + +/** + * This is abstract interface for bridges. + * + * <p>Bridges are able to map one object from one UNO environment to another and + * vice versa.<p> + * + * @see com.sun.star.uno.IBridge + * @see com.sun.star.uno.IQueryInterface + * @see com.sun.star.uno.UnoRuntime + * + * @deprecated As of UDK 3.2, this interface is deprecated, without offering a + * replacement. + */ +@Deprecated +public interface IBridge { + /** + * Maps an object from the source environment to the destination + * environment. + * + * @param object the object to map + * @param type the type of the interface that shall be mapped + * @return the object in the destination environment + */ + Object mapInterfaceTo(Object object, Type type); + + /** + * Maps an object from the destination environment to the source + * environment. + * + * @param object the object to map + * @param type the type of the interface that shall be mapped + * @return the object in the source environment + */ + Object mapInterfaceFrom(Object object, Type type); + + /** + * Returns the source environment. + * + * @return the source environment of this bridge + */ + IEnvironment getSourceEnvironment(); + + /** + * Returns the destination environment. + * + * @return the destination environment of this bridge + */ + IEnvironment getTargetEnvironment(); + + /** + * Increases the life count. + */ + void acquire(); + + /** + * Decreases the life count. + * + * <p>If the life count drops to zero, the bridge disposes itself.</p> + */ + void release(); + + /** + * Disposes the bridge. + * + * <p>Sends involved threads an <code>InterruptedException</code>. Releases + * mapped objects.</p> + * + * @throws InterruptedException it's deprecated so who cares. + * @throws IOException it's deprecated so who cares. + */ + void dispose() throws InterruptedException, IOException; +} diff --git a/ridljar/com/sun/star/uno/IEnvironment.java b/ridljar/com/sun/star/uno/IEnvironment.java new file mode 100644 index 000000000..5d3289305 --- /dev/null +++ b/ridljar/com/sun/star/uno/IEnvironment.java @@ -0,0 +1,144 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * The interface implemented by UNO environments. + * + * <p>With this interface, objects can be registered at and revoked from an + * environment.</p> + * + * @see com.sun.star.uno.IBridge + * @see com.sun.star.uno.IQueryInterface + * @see com.sun.star.uno.UnoRuntime + * + * @deprecated As of UDK 3.2, this interface is deprecated, without offering a + * replacement. + */ +@Deprecated +public interface IEnvironment { + /** + * Gets the context of this environment. + * + * @return the context of this environment + */ + Object getContext(); + + /** + * Gets the name of this environment. + * + * @return the name of this environment + */ + String getName(); + + /** + * Registers one UNO interface facet of an object. + * + * <p>Such an object will typically be one of three things: + * <ul> + * <li>A local Java object, to be mapped out of this environment via a given + * bridge.</li> + * <li>A proxy object, mapped into this environment via some bridge + * <var>B1</var>, and now to be mapped out of this environment via a + * given bridge <var>B2</var>.</li> + * <li>A proxy object, created as a remote object is mapped into this + * environment via a given bridge.</li> + * </ul> + * + * <p>The object actually registered may differ from the specified + * <code>object</code> that is passed as an argument. This enables an + * environment to work in a multi-threaded scenario, where two threads can + * call <code>registerInterface</code> for the same combination of + * <code>oid</code> and <code>type</code> at the same time; the race + * condition is solved by letting one of the calls register its argument + * <code>object</code>, ignoring the argument <code>object</code> of the + * other call, and letting both calls return the same + * <code>object</code>.</p> + * + * <p>The registered object is held only weakly by the environment. After a + * call to <code>registerInterface</code>, a call to + * <code>getRegisteredInterface</code> only succeeds as long as the + * registered object is still strongly reachable, and the registered object + * has not been explicitly revoked by calling + * <code>revokeInterface</code>.</p> + * + * @param object the object to register; must be non-null + * @param oid in-out parameter containing the OID of <code>object</code>. + * This must be a non-null reference to an array of length at least one; + * the zeroth element is used to pass the argument in and out. If the + * zeroth element is null on input, the OID will be computed and passed + * out (that is, the zeroth element will never be null upon normal + * return). + * @param type the UNO interface type to register. This argument must be + * non-null, and must denote a UNO interface type. The given + * <code>object</code> should implement this <code>type</code>. + * @return the registered object (may differ from the <code>object</code> + * passed in); will never be null + */ + Object registerInterface(Object object, String[] oid, Type type); + + /** + * Explicitly revokes a UNO interface facet. + * + * <p>Calls to <code>registerInterface</code> and + * <code>revokeInterface</code> must be paired. A facet is only removed + * from the environment when it has been revoked as often as it has been + * registered. This may change in the future, so that a facet would be + * removed upon the first call to <code>revokeInterface</code> (and calls to + * <code>revokeInterface</code> would no longer be necessary if the calling + * code does not want to control the temporal extent of the + * registration).</p> + * + * <p>It is not an error if the specified facet is not registered at this + * environment (either because no corresponding object has ever been + * registered, or it has been explicitly revoked, or it is no longer + * strongly reachable). In such a case, this method simply does + * nothing.</p> + * + * @param oid the OID of the object to revoke; must be non-null + * @param type the UNO interface type of the object to revoke. This + * argument must be non-null, and must denote a UNO interface type. + */ + void revokeInterface(String oid, Type type); + + /** + * Retrieves a registered object, specified by OID and UNO interface type. + * + * @param oid the OID of the object to retrieve; must be non-null + * @param type the UNO interface type of the object to retrieve. This + * argument must be non-null, and must denote a UNO interface type. + * @return the registered object, or null if none is found + */ + Object getRegisteredInterface(String oid, Type type); + + /** + * Retrieves the OID for a registered object. + * + * @param object a registered object; must be non-null + * @return the OID of the <code>object</code>; will never be null + */ + String getRegisteredObjectIdentifier(Object object); + + /** + * Lists the registered objects to <code>System.out</code>. + * + * <p>This is for debug purposes.</p> + */ + void list(); +} diff --git a/ridljar/com/sun/star/uno/IMapping.java b/ridljar/com/sun/star/uno/IMapping.java new file mode 100644 index 000000000..fdf6d8207 --- /dev/null +++ b/ridljar/com/sun/star/uno/IMapping.java @@ -0,0 +1,41 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * With a mapping objects can be mapped from one environment to another. + * + * <p>This interface exists for compatibility with the binary UNO API.</p> + * + * @see com.sun.star.uno.IBridge + * + * @deprecated As of UDK 3.2, this interface is deprecated, without offering a + * replacement. + */ +@Deprecated +public interface IMapping { + /** + * Maps an interface from one environment to another. + * + * @param object source object that is to be mapped + * @param type description of the interface that is to be mapped + * @return the object mapped to the destination environment + */ + Object mapInterface(Object object, Type type); +} diff --git a/ridljar/com/sun/star/uno/IQueryInterface.java b/ridljar/com/sun/star/uno/IQueryInterface.java new file mode 100644 index 000000000..7857d6205 --- /dev/null +++ b/ridljar/com/sun/star/uno/IQueryInterface.java @@ -0,0 +1,60 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * This is the delegator interface for Java objects implementing interfaces of + * an underlying UNO object. + * + * <p>Calls are delegated through the <code>UnoRuntime</code> to this + * interface. Implement this interface in case you want to customize the + * behaviour of <code>UnoRuntime.queryInterface</code>.</p> + * + * @see com.sun.star.uno.UnoRuntime + */ +public interface IQueryInterface { + /** + * Returns the unique object identifier (OID) of the underlying UNO object. + * + * @return the OID of the underlying object + */ + String getOid(); + + /** + * Returns an object implementing the requested interface type. + * + * @param type the requested UNO interface type; must be a <code>Type</code> + * object representing a UNO interface type + * @return a reference to the requested UNO interface type if available, + * otherwise <code>null</code> + * @see com.sun.star.uno.UnoRuntime + */ + Object queryInterface(Type type); + + /** + * Tests if the given reference represents a facet of the underlying UNO + * object. + * + * @param object a reference to any Java object representing (a facet of) a + * UNO object; may be <code>null</code> + * @return <code>true</code> if and only if <code>object</code> is not + * <code>null</code> and represents the same UNO object as this object + */ + boolean isSame(Object object); +} diff --git a/ridljar/com/sun/star/uno/MappingException.java b/ridljar/com/sun/star/uno/MappingException.java new file mode 100644 index 000000000..7670a790a --- /dev/null +++ b/ridljar/com/sun/star/uno/MappingException.java @@ -0,0 +1,65 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + + +/** + * The mapping Exception. + * + * <p>The exception is replaced by the com.sun.star.lang.DisposedException.</p> + * + * @see com.sun.star.uno.UnoRuntime + * @see com.sun.star.uno.IQueryInterface + * @see com.sun.star.uno.IBridge + * + * @deprecated since UDK 3.0.2 + */ +@Deprecated +public class MappingException extends com.sun.star.uno.RuntimeException { + /** + * Constructs an empty <code>MappingException</code>. + */ + public MappingException() { + super(); + } + + /** + * Constructs an <code>MappingException</code> with a detail message. + * + * @param message the detail message. + */ + public MappingException(String message) { + super(message); + } + + /** + * Constructs an <code>MappingException</code> with a detail message and a + * context. + * + * @param message the detail message. + * @param context the context. + */ + public MappingException(String message, Object context) { + super(message, context); + } +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/com/sun/star/uno/Type.java b/ridljar/com/sun/star/uno/Type.java new file mode 100644 index 000000000..0589b64cb --- /dev/null +++ b/ridljar/com/sun/star/uno/Type.java @@ -0,0 +1,699 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import java.util.HashMap; + +import com.sun.star.lib.uno.typedesc.TypeDescription; + +/** + * Represents the UNO built-in type <code>TYPE</code>. + * + * <p>The UNO type is not directly mapped to <code>java.lang.Class</code> for at + * least two reasons. For one, some UNO types (like <code>UNSIGNED + * SHORT</code>) do not have a matching Java class. For another, it can be + * necessary to describe a type which is unknown to the Java runtime system + * (for example, for delaying the need of a class, so that it is possible to + * generate it on the fly.)</p> + * + * <p>A <code>Type</code> is uniquely determined by its type class (a + * <code>TypeClass</code>) and its type name (a <code>String</code>); these two + * will never be <code>null</code>. A <code>Type</code> may have an additional + * "z class" (a <code>java.lang.Class</code>), giving a Java class type that + * corresponds to the UNO type. Also, a <code>Type</code> can cache a type + * description (a <code>com.sun.star.uno.typedesc.TypeDescription</code>), which can be + * computed and set by <code>TypeDescription.getTypeDescription</code>. + */ +public class Type { + // The following private static members and static initializer must come + // first in the class definition, so that the class can be initialized + // successfully: + + private static final String TYPE_NAME_VOID = "void"; + private static final String TYPE_NAME_BOOLEAN = "boolean"; + private static final String TYPE_NAME_BYTE = "byte"; + private static final String TYPE_NAME_SHORT = "short"; + private static final String TYPE_NAME_UNSIGNED_SHORT = "unsigned short"; + private static final String TYPE_NAME_LONG = "long"; + private static final String TYPE_NAME_UNSIGNED_LONG = "unsigned long"; + private static final String TYPE_NAME_HYPER = "hyper"; + private static final String TYPE_NAME_UNSIGNED_HYPER = "unsigned hyper"; + private static final String TYPE_NAME_FLOAT = "float"; + private static final String TYPE_NAME_DOUBLE = "double"; + private static final String TYPE_NAME_CHAR = "char"; + private static final String TYPE_NAME_STRING = "string"; + private static final String TYPE_NAME_TYPE = "type"; + private static final String TYPE_NAME_ANY = "any"; + + // must be sorted same as TypeClass: + private static final String[] __typeClassToTypeName = new String[] { + TYPE_NAME_VOID, + TYPE_NAME_CHAR, + TYPE_NAME_BOOLEAN, + TYPE_NAME_BYTE, + TYPE_NAME_SHORT, + TYPE_NAME_UNSIGNED_SHORT, + TYPE_NAME_LONG, + TYPE_NAME_UNSIGNED_LONG, + TYPE_NAME_HYPER, + TYPE_NAME_UNSIGNED_HYPER, + TYPE_NAME_FLOAT, + TYPE_NAME_DOUBLE, + TYPE_NAME_STRING, + TYPE_NAME_TYPE, + TYPE_NAME_ANY + }; + + private static final class TypeInfo { + TypeInfo( + TypeClass thePrimary, TypeClass theAlternative, + boolean theSequenceComponentType) + { + primary = thePrimary; + alternative = theAlternative; + sequenceComponentType = theSequenceComponentType; + } + + final TypeClass primary; + final TypeClass alternative; + final boolean sequenceComponentType; + } + + private static final HashMap<Class<?>, TypeInfo> __javaClassToTypeClass = + new HashMap<Class<?>, TypeInfo>(); + static { + __javaClassToTypeClass.put( + void.class, new TypeInfo(TypeClass.VOID, TypeClass.VOID, false)); + __javaClassToTypeClass.put( + Void.class, new TypeInfo(TypeClass.VOID, TypeClass.VOID, false)); + __javaClassToTypeClass.put( + boolean.class, + new TypeInfo(TypeClass.BOOLEAN, TypeClass.BOOLEAN, true)); + __javaClassToTypeClass.put( + Boolean.class, + new TypeInfo(TypeClass.BOOLEAN, TypeClass.BOOLEAN, false)); + __javaClassToTypeClass.put( + byte.class, new TypeInfo(TypeClass.BYTE, TypeClass.BYTE, true)); + __javaClassToTypeClass.put( + Byte.class, new TypeInfo(TypeClass.BYTE, TypeClass.BYTE, false)); + __javaClassToTypeClass.put( + short.class, + new TypeInfo(TypeClass.SHORT, TypeClass.UNSIGNED_SHORT, true)); + __javaClassToTypeClass.put( + Short.class, + new TypeInfo(TypeClass.SHORT, TypeClass.UNSIGNED_SHORT, false)); + __javaClassToTypeClass.put( + int.class, + new TypeInfo(TypeClass.LONG, TypeClass.UNSIGNED_LONG, true)); + __javaClassToTypeClass.put( + Integer.class, + new TypeInfo(TypeClass.LONG, TypeClass.UNSIGNED_LONG, false)); + __javaClassToTypeClass.put( + long.class, + new TypeInfo(TypeClass.HYPER, TypeClass.UNSIGNED_HYPER, true)); + __javaClassToTypeClass.put( + Long.class, + new TypeInfo(TypeClass.HYPER, TypeClass.UNSIGNED_HYPER, false)); + __javaClassToTypeClass.put( + float.class, new TypeInfo(TypeClass.FLOAT, TypeClass.FLOAT, true)); + __javaClassToTypeClass.put( + Float.class, new TypeInfo(TypeClass.FLOAT, TypeClass.FLOAT, false)); + __javaClassToTypeClass.put( + double.class, + new TypeInfo(TypeClass.DOUBLE, TypeClass.DOUBLE, true)); + __javaClassToTypeClass.put( + Double.class, + new TypeInfo(TypeClass.DOUBLE, TypeClass.DOUBLE, false)); + __javaClassToTypeClass.put( + char.class, new TypeInfo(TypeClass.CHAR, TypeClass.CHAR, true)); + __javaClassToTypeClass.put( + Character.class, + new TypeInfo(TypeClass.CHAR, TypeClass.CHAR, false)); + __javaClassToTypeClass.put( + String.class, + new TypeInfo(TypeClass.STRING, TypeClass.STRING, true)); + __javaClassToTypeClass.put( + Type.class, new TypeInfo(TypeClass.TYPE, TypeClass.TYPE, true)); + __javaClassToTypeClass.put( + Any.class, new TypeInfo(TypeClass.ANY, TypeClass.ANY, true)); + __javaClassToTypeClass.put( + Object.class, + new TypeInfo(TypeClass.ANY, TypeClass.INTERFACE, true)); + } + + public static final Type VOID = new Type(void.class); + public static final Type CHAR = new Type(char.class); + public static final Type BOOLEAN = new Type(boolean.class); + public static final Type BYTE = new Type(byte.class); + public static final Type SHORT = new Type(short.class); + public static final Type UNSIGNED_SHORT = new Type( + TYPE_NAME_UNSIGNED_SHORT, TypeClass.UNSIGNED_SHORT); + public static final Type LONG = new Type(int.class); + public static final Type UNSIGNED_LONG = new Type( + TYPE_NAME_UNSIGNED_LONG, TypeClass.UNSIGNED_LONG); + public static final Type HYPER = new Type(long.class); + public static final Type UNSIGNED_HYPER = new Type( + TYPE_NAME_UNSIGNED_HYPER, TypeClass.UNSIGNED_HYPER); + public static final Type FLOAT = new Type(float.class); + public static final Type DOUBLE = new Type(double.class); + public static final Type STRING = new Type(String.class); + public static final Type TYPE = new Type(Type.class); + public static final Type ANY = new Type(Any.class); + + /** + * Constructs a new <code>Type</code> which defaults to <code>VOID</code>. + */ + public Type() { + init(null, void.class, false, false, false); + } + + /** + * Constructs a new <code>Type</code> with the given type class and type + * name. + * + * @param typeName the type name. Must not be <code>null</code>. + * @param typeClass the type class. Must not be <code>null</code>, and must + * match the <code>typeName</code> (for example, it is illegal to + * combine a <code>typeName</code> of <code>"void"</code> with a + * <code>typeClass</code> of <code>BOOLEAN</code>). + */ + public Type(String typeName, TypeClass typeClass) { + _typeClass = typeClass; + _typeName = typeName; + } + + /** + * Constructs a new <code>Type</code> from the given + * <code>java.lang.Class</code>. + * + * <p>This is equivalent to <code>Type(zClass, false)</code>.</p> + * + * @param zClass the Java class of this type. Must not be + * <code>null</code>. + */ + public Type(Class<?> zClass) { + init(null, zClass, false, false, false); + } + + /** + * Constructs a new <code>Type</code> from the given + * <code>java.lang.Class</code>, handling ambiguous cases. + * + * <p>In certain cases, one Java class corresponds to two UNO types (e.g., + * the Java class <code>short[].class</code> corresponds to both a sequence + * of <code>SHORT</code> and a sequence of <code>UNSIGNED SHORT</code> in + * UNO). In such ambiguous cases, the parameter <code>alternative</code> + * controls which UNO type is chosen: + * <ul> + * <li>If the Java type is (an array type with element type) + * <code>short</code> or <code>java.lang.Short</code>: If + * <code>alternative</code> is <code>false</code>, the chosen UNO type is + * (a sequence type with element type) <code>SHORT</code>. If + * <code>alternative</code> is <code>true</code>, the chosen UNO type is + * (a sequence type with element type) <code>UNSIGNED SHORT</code>.</li> + * + * <li>If the Java type is (an array type with element type) + * <code>int</code> or <code>java.lang.Integer</code>: If + * <code>alternative</code> is <code>false</code>, the chosen UNO type is + * (a sequence type with element type) <code>LONG</code>. If + * <code>alternative</code> is <code>true</code>, the chosen UNO type is + * (a sequence type with element type) <code>UNSIGNED LONG</code>.</li> + * + * <li>If the Java type is (an array type with element type) + * <code>long</code> or <code>java.lang.Long</code>: If + * <code>alternative</code> is <code>false</code>, the chosen UNO type is + * (a sequence type with element type) <code>HYPER</code>. If + * <code>alternative</code> is <code>true</code>, the chosen UNO type is + * (a sequence type with element type) <code>UNSIGNED HYPER</code>.</li> + * + * <li>If the Java type is (an array type with element type) + * <code>java.lang.Object</code>: If <code>alternative</code> is + * <code>false</code>, the chosen UNO type is (a sequence type with + * element type) <code>ANY</code>. If <code>alternative</code> is + * <code>true</code>, the chosen UNO type is (a sequence type with element + * type) <code>com.sun.star.uno.XInterface</code>.</li> + * </ul> + * <p>In all other cases, the value of <code>alternative</code> is + * ignored.</p> + * + * <p>This constructor cannot be used to create <code>Type</code> instances + * that represent (sequences of) instantiated polymorphic struct types.</p> + * + * @param zClass the Java class of this type; must not be <code>null</code> + * @param alternative controls which UNO type to choose in case of + * ambiguities + * + * @since UDK 3.2.0 + */ + public Type(Class<?> zClass, boolean alternative) { + init(null, zClass, alternative, false, false); + } + + private Type( + Class<?> zClass, boolean alternative, boolean sequenceComponentType) + { + init(null, zClass, alternative, false, sequenceComponentType); + } + + /** + * Constructs a new <code>Type</code> from the given type description. + * + * <em>For internal URE use only. Not to be used by client code.</em> + * + * @param typeDescription a type description. Must not be + * <code>null</code>. + */ + public Type(TypeDescription typeDescription) { + _typeName = typeDescription.getTypeName(); + _typeClass = typeDescription.getTypeClass(); + _iTypeDescription = typeDescription; + } + + /** + * Constructs a new <code>Type</code> with the given type name. + * + * @param typeName the name of this type; must not be <code>null</code>. + */ + public Type(String typeName) { + if (typeName.startsWith("[]")) { + _typeName = typeName; + _typeClass = TypeClass.SEQUENCE; + return; + } + for (int i = 0; i < __typeClassToTypeName.length; ++i) { + if (__typeClassToTypeName[i].equals(typeName)) { + _typeName = typeName; + _typeClass = TypeClass.fromInt(i); + return; + } + } + int i = typeName.indexOf('<'); + try { + init( + typeName, + Class.forName(i < 0 ? typeName : typeName.substring(0, i)), + false, i >= 0, false); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + /** + * Constructs a new <code>Type</code> with the given type class. + * + * @param typeClass the type class of this type; must not be + * <code>null</code>. Only type classes for simple types are allowed + * here. + * + * @throws IllegalArgumentException if the given <code>typeClass</code> is + * not simple (for example, a struct or an interface type). This + * constructor could not find out the type name in such a case. + */ + public Type(TypeClass typeClass) { + if(__isTypeClassPrimitive(typeClass)) { + _typeClass = typeClass; + _typeName = __typeClassToTypeName[typeClass.getValue()]; + } + else + throw new IllegalArgumentException(typeClass + " is not primitive"); + } + + /** + * Gets the type class. + * + * @return the type class. Will never be <code>null</code>, but might be + * <code>UNKNOWN</code>. + */ + public TypeClass getTypeClass() { + return _typeClass; + } + + /** + * Gets the type name. + * + * @return the type name; will never be <code>null</code> + */ + public String getTypeName() { + return _typeName; + } + + /** + * Gets the Java class. + * + * @return the type name; may be <code>null</code> in extreme situations + * (inconsistent <code>TypeClass</code>, error loading a class) + */ + public Class<?> getZClass() { + synchronized (this) { + if (_class == null) { + _class = determineClass(); + } + } + return _class; + } + + /** + * Gives the type description of this type. + * + * <em>For internal URE use only. Not to be used by client code.</em> + * + * @return the type description; may be <code>null</code> + */ + public TypeDescription getTypeDescription() { + return _iTypeDescription; + } + + /** + * Sets the type description for this type. + * + * <em>For internal URE use only. Not to be used by client code.</em> + * + * @param typeDescription the type description + */ + public void setTypeDescription(TypeDescription typeDescription) { + _iTypeDescription = typeDescription; + } + + /** + * Determines whether this UNO type is a supertype of another UNO type. + * + * UNO only defines the following supertype relations: + * (1) A struct type t1 is a supertype of a struct type t2, if either t1 + * and t2 are the same, or t1 is a direct or indirect parent of t2. + * (2) An exception type t1 is a supertype of an exception type t2, if + * either t1 and t2 are the same, or t1 is a direct or indirect parent + * of t2. + * (3) An interface type t1 is a supertype of an interface type t2, if + * either t1 and t2 are the same, or t1 is a direct or indirect parent + * of t2. + * + * Following the conventions of the Java UNO language binding, + * com.sun.star.uno.Exception is not considered a supertype of + * com.sun.star.uno.RuntimeException or any exception type derived from + * com.sun.star.uno.RuntimeException. + * + * @param type some Type + * @return true if this type is a supertype of the given type + * + * @since UDK 3.2.0 + */ + public boolean isSupertypeOf(Type type) { + if (_typeClass != type._typeClass) { + return false; + } + switch (_typeClass.getValue()) { + case TypeClass.SEQUENCE_value: + case TypeClass.ENUM_value: + return _typeName.equals(type._typeName); + + case TypeClass.STRUCT_value: + // This check exploits the fact that an instantiated polymorphic + // struct type may not be the direct base of a struct type: + if (_typeName.indexOf('<') >= 0 || type._typeName.indexOf('<') >= 0) + { + return _typeName.equals(type._typeName); + } + // fall-through + case TypeClass.EXCEPTION_value: + case TypeClass.INTERFACE_value: + Class<?> c1 = getZClass(); + Class<?> c2 = type.getZClass(); + return c1 != null && c2 != null && c1.isAssignableFrom(c2); + + default: + return true; + } + } + + /** + * Indicates whether some other object is equal to this one. + * + * @param obj the reference object with which to compare. + * @return <code>true</code> if this object is the same as the obj argument; + * <code>false</code> otherwise. + * @see java.lang.Object#equals + */ + @Override + public boolean equals(Object obj) { + return obj instanceof Type + && _typeClass == ((Type) obj)._typeClass + && _typeName.equals(((Type) obj)._typeName); + } + + /** + * Returns a hash code value for the object. + * + * @return a hash code value for this object. + * @see java.lang.Object#hashCode + */ + @Override + public int hashCode() { + return _typeName.hashCode(); + } + + /** + * Returns a string representation of the object. + * + * @return a string representation of the object. + * @see java.lang.Object#toString + */ + @Override + public String toString() { + return "Type[" + _typeName + "]"; + } + + private void init( + String name, Class<?> zClass, boolean alternative, boolean arguments, + boolean sequenceComponentType) + { + TypeInfo info = __javaClassToTypeClass.get(zClass); + if (info != null) { + if (sequenceComponentType && !info.sequenceComponentType) { + throw new IllegalArgumentException( + zClass + " cannot be sequence component type"); + } + // info only contains primitive type classes, except for + // TypeClass.INTERFACE, which stands for XInterface (the alternative + // interpretation of java.lang.Object): + _typeClass = alternative ? info.alternative : info.primary; + _typeName = _typeClass == TypeClass.INTERFACE + ? XInterface.class.getName() + : __typeClassToTypeName[_typeClass.getValue()]; + // do not assign _class from zClass, as _class should always be + // normalized (e.g., boolean.class instead of + // java.lang.Boolean.class); getZClass will later calculate the + // correct class when needed + } else if (zClass.isArray()) { + Type t = new Type(zClass.getComponentType(), alternative, true); + _typeClass = t.getTypeClass() != TypeClass.UNKNOWN + ? TypeClass.SEQUENCE : TypeClass.UNKNOWN; + _typeName = "[]" + t.getTypeName(); + // do not assign _class from zClass, as _class should always be + // normalized (e.g., boolean[].class instead of + // java.lang.Boolean[].class); getZClass will later calculate the + // correct class when needed + } else if (Enum.class.isAssignableFrom(zClass)) { + _typeClass = zClass != Enum.class + ? TypeClass.ENUM : TypeClass.UNKNOWN; + _typeName = zClass.getName(); + _class = zClass; + } else if (Throwable.class.isAssignableFrom(zClass)) { + _typeClass + = com.sun.star.uno.Exception.class.isAssignableFrom(zClass) + || com.sun.star.uno.RuntimeException.class.isAssignableFrom( + zClass) + ? TypeClass.EXCEPTION : TypeClass.UNKNOWN; + _typeName = zClass.getName(); + _class = zClass; + } else if (zClass.isInterface()) { + _typeClass = XInterface.class.isAssignableFrom(zClass) + ? TypeClass.INTERFACE : TypeClass.UNKNOWN; + _typeName = zClass.getName(); + _class = zClass; + } else if (XInterface.class.isAssignableFrom(zClass)) { + // This case is needed by code that uses this constructor to + // calculate the UNO type corresponding to a Java object: + _typeClass = TypeClass.INTERFACE; + _typeName = XInterface.class.getName(); + _class = XInterface.class; + } else { + // assert zClass != Object.class && !zClass.isPrimitive(); + _typeClass = TypeClass.STRUCT; + _typeName = name == null ? zClass.getName() : name; + _class = zClass; + } + if (arguments && _typeClass != TypeClass.STRUCT) { + throw new IllegalArgumentException( + zClass + " cannot have type arguments"); + } + } + + private Class<?> determineClass() { + switch (_typeClass.getValue()) { + case TypeClass.VOID_value: + return _typeName.equals(TYPE_NAME_VOID) ? void.class : null; + + case TypeClass.BOOLEAN_value: + return _typeName.equals(TYPE_NAME_BOOLEAN) ? boolean.class : null; + + case TypeClass.BYTE_value: + return _typeName.equals(TYPE_NAME_BYTE) ? byte.class : null; + + case TypeClass.SHORT_value: + return _typeName.equals(TYPE_NAME_SHORT) ? short.class : null; + + case TypeClass.UNSIGNED_SHORT_value: + return _typeName.equals(TYPE_NAME_UNSIGNED_SHORT) + ? short.class : null; + + case TypeClass.LONG_value: + return _typeName.equals(TYPE_NAME_LONG) ? int.class : null; + + case TypeClass.UNSIGNED_LONG_value: + return _typeName.equals(TYPE_NAME_UNSIGNED_LONG) ? int.class : null; + + case TypeClass.HYPER_value: + return _typeName.equals(TYPE_NAME_HYPER) ? long.class : null; + + case TypeClass.UNSIGNED_HYPER_value: + return _typeName.equals(TYPE_NAME_UNSIGNED_HYPER) + ? long.class : null; + + case TypeClass.FLOAT_value: + return _typeName.equals(TYPE_NAME_FLOAT) ? float.class : null; + + case TypeClass.DOUBLE_value: + return _typeName.equals(TYPE_NAME_DOUBLE) ? double.class : null; + + case TypeClass.CHAR_value: + return _typeName.equals(TYPE_NAME_CHAR) ? char.class : null; + + case TypeClass.STRING_value: + return _typeName.equals(TYPE_NAME_STRING) ? String.class : null; + + case TypeClass.TYPE_value: + return _typeName.equals(TYPE_NAME_TYPE) ? Type.class : null; + + case TypeClass.ANY_value: + return _typeName.equals(TYPE_NAME_ANY) ? Object.class : null; + + case TypeClass.SEQUENCE_value: + StringBuffer buf = new StringBuffer(); + int offset = 0; + for (; _typeName.startsWith("[]", offset); offset += "[]".length()) + { + buf.append('['); + } + if (buf.length() == 0) { + return null; + } + String base = _typeName.substring(offset); + if (base.equals(TYPE_NAME_VOID)) { + buf.append('V'); + } else if (base.equals(TYPE_NAME_BOOLEAN)) { + buf.append('Z'); + } else if (base.equals(TYPE_NAME_BYTE)) { + buf.append('B'); + } else if (base.equals(TYPE_NAME_SHORT) + || base.equals(TYPE_NAME_UNSIGNED_SHORT)) { + buf.append('S'); + } else if (base.equals(TYPE_NAME_LONG) + || base.equals(TYPE_NAME_UNSIGNED_LONG)) { + buf.append('I'); + } else if (base.equals(TYPE_NAME_HYPER) + || base.equals(TYPE_NAME_UNSIGNED_HYPER)) { + buf.append('J'); + } else if (base.equals(TYPE_NAME_FLOAT)) { + buf.append('F'); + } else if (base.equals(TYPE_NAME_DOUBLE)) { + buf.append('D'); + } else if (base.equals(TYPE_NAME_CHAR)) { + buf.append('C'); + } else if (base.equals(TYPE_NAME_STRING)) { + buf.append("Ljava.lang.String;"); + } else if (base.equals(TYPE_NAME_TYPE)) { + buf.append("Lcom.sun.star.uno.Type;"); + } else if (base.equals(TYPE_NAME_ANY)) { + buf.append("Ljava.lang.Object;"); + } else { + int args = base.indexOf('<'); + if (args >= 0) { + base = base.substring(0, args); + } + Class<?> c; + try { + c = Class.forName(base); + } catch (ClassNotFoundException e) { + return null; + } + if (args < 0 && new Type(c).getTypeClass() == TypeClass.UNKNOWN) + { + return null; + } + buf.append('L'); + buf.append(base); + buf.append(';'); + } + try { + return Class.forName(buf.toString()); + } catch (ClassNotFoundException e) { + return null; + } + + case TypeClass.ENUM_value: + case TypeClass.EXCEPTION_value: + case TypeClass.INTERFACE_value: + { + Class<?> c; + try { + c = Class.forName(_typeName); + } catch (ClassNotFoundException e) { + return null; + } + return new Type(c).equals(this) ? c : null; + } + + case TypeClass.STRUCT_value: + { + int args = _typeName.indexOf('<'); + Class<?> c; + try { + c = Class.forName( + args < 0 ? _typeName : _typeName.substring(0, args)); + } catch (ClassNotFoundException e) { + return null; + } + return args >= 0 || new Type(c).equals(this) ? c : null; + } + + default: + return null; + } + } + + private static boolean __isTypeClassPrimitive(TypeClass typeClass) { + return typeClass.getValue() < __typeClassToTypeName.length; + } + + private TypeClass _typeClass; // TODO should be final + private String _typeName; // TODO should be final + + private Class<?> _class; + private TypeDescription _iTypeDescription; +} diff --git a/ridljar/com/sun/star/uno/UnoRuntime.java b/ridljar/com/sun/star/uno/UnoRuntime.java new file mode 100644 index 000000000..1f011aa77 --- /dev/null +++ b/ridljar/com/sun/star/uno/UnoRuntime.java @@ -0,0 +1,726 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import java.io.IOException; +import java.lang.reflect.Array; +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.UUID; +import java.util.WeakHashMap; + +import com.sun.star.lib.uno.typedesc.FieldDescription; +import com.sun.star.lib.uno.typedesc.TypeDescription; +import com.sun.star.lib.util.WeakMap; + +/** + * The central class needed for implementing or using UNO components in Java. + * + * <p>The methods <code>queryInterface</code> and <code>areSame</code> delegate + * calls to the implementing objects and are used instead of casts, + * <code>instanceof</code>, <code>==</code>, and <code>equals</code>.</p> + * + * <p>For historic reasons, this class is not <code>final</code>, and has a + * <code>public</code> constructor. These artifacts are considered mistakes, + * which might be corrected in a future version of this class, so client code + * should not rely on them.</p> + * + * @see com.sun.star.uno.IBridge + * @see com.sun.star.uno.IEnvironment + * @see com.sun.star.uno.IQueryInterface + */ +public class UnoRuntime { + /** + * @deprecated As of UDK 3.2.0, do not create instances of this class. + * It is considered a historic mistake to have a <code>public</code> + * constructor for this class, which only has <code>static</code> members. + * Also, this class might be changed to become <code>final</code> in a + * future version. + */ + @Deprecated + public UnoRuntime() {} + + /** + * Generates a worldwide unique identifier string. + * + * <p>It is guaranteed that every invocation of this method generates a new + * ID, which is unique within the VM. The quality of “worldwide + * unique” will depend on the actual implementation, you should look + * at the source to determine if it meets your requirements.</p> + * + * @return a unique <code>String</code> + */ + public static String getUniqueKey() { + synchronized (uniqueKeyLock) { + if (uniqueKeyCount == Long.MAX_VALUE) { + long time; + for (time = System.currentTimeMillis(); time == uniqueKeyTime;) + { + // Conservatively sleep for 100 millisecond to wait for + // System.currentTimeMillis() to change: + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + uniqueKeyTime = time; + uniqueKeyCount = Long.MIN_VALUE; + } + return uniqueKeyHostPrefix + Long.toString(uniqueKeyTime, 16) + ":" + + Long.toString(uniqueKeyCount++, 16); + } + } + + /** + * Generates a worldwide unique object identifier (OID) for the given + * Java object. + * + * <p>It is guaranteed that subsequent calls to this method with the same + * Java object will give the same ID.</p> + * + * <p>This method is generally of little use for client code. It should be + * considered a mistake that this method is published at all.</p> + * + * @param object any object for which an OID shall be generated; must not be + * <code>null</code> + * @return the generated OID + * @see com.sun.star.uno.IQueryInterface#getOid + */ + public static String generateOid(Object object) { + String oid = null; + if (object instanceof IQueryInterface) { + oid = ((IQueryInterface) object).getOid(); + } + if (oid == null) { + synchronized (oidMap) { + oid = oidMap.get(object); + if (oid == null) { + oid = UUID.randomUUID().toString() + oidSuffix; + oidMap.put(object, oid); + } + } + } + return oid; + } + + /** + * Queries the given UNO object for the given UNO interface type. + * + * <p>This method returns <code>null</code> in case the given UNO object + * does not support the given UNO interface type (or is itself + * <code>null</code>). Otherwise, a reference to a Java object implementing + * the Java interface type corresponding to the given UNO interface is + * returned. In the latter case, it is unspecified whether the returned + * Java object is the same as the given object, or is another facet of that + * UNO object.</p> + * + * @param type the requested UNO interface type; must be a <code>Type</code> + * object representing a UNO interface type + * @param object a reference to any Java object representing (a facet of) a + * UNO object; may be <code>null</code> + * @return a reference to the requested UNO interface type if available, + * otherwise <code>null</code> + * @see com.sun.star.uno.IQueryInterface#queryInterface + */ + public static Object queryInterface(Type type, Object object) { + // Gracefully handle those situations where the passed in UNO object is + // wrapped in an Any. Strictly speaking, such a situation constitutes a + // bug, but it is anticipated that such situations will arise quite + // often in practice (especially since UNO Anys containing an XInterface + // reference are not wrapped in a Java Any, but UNO Anys containing any + // other interface reference are wrapped in a Java Any, which can lead + // to confusion). + if (object instanceof Any) { + Any a = (Any) object; + if (a.getType().getTypeClass() == TypeClass.INTERFACE) { + object = a.getObject(); + } + } + if (object instanceof IQueryInterface) { + object = ((IQueryInterface) object).queryInterface(type); + if (object instanceof Any) { + Any a = (Any) object; + object = a.getType().getTypeClass() == TypeClass.INTERFACE + ? a.getObject() : null; + } + } + // Ensure that the object implements the requested interface type: + Class<?> c = type.getZClass(); + if (c == null || !c.isInstance(object)) { + object = null; + } + return object; + } + + /** + * Queries the given UNO object for the given Java class (which must + * represent a UNO interface type). + * + * @param <T> the requested UNO interface type. + * @param zInterface a Java class representing a UNO interface type + * @param object a reference to any Java object representing (a facet of) a + * UNO object; may be <code>null</code> + * @return a reference to the requested UNO interface type if available, + * otherwise <code>null</code> + * @see #queryInterface(Type, Object) + */ + @SuppressWarnings("unchecked") + public static <T> T queryInterface(Class<T> zInterface, Object object) { + return (T) queryInterface(new Type(zInterface), object); + } + + /** + Tests two UNO <code>ANY</code> values for equality. + + <p>Two UNO values are <dfn>equal</dfn> if and only if they are of the + same UNO type <var>t</var>, and they meet the following condition, + depending on <var>t</var>:</p> + <ul> + <li>If <var>t</var> is a primitive type, then both values must denote + the same element of the set of values of <var>t</var>.</li> + + <li>If <var>t</var> is a structured type, then both values must + recursively contain corresponding values that are equal.</li> + + <li>If <var>t</var> is an interface type, then the two values must be + either both null references, or both references to the same UNO + object.</li> + </ul> + + @param any1 a Java value representing a UNO <code>ANY</code> value. + + @param any2 a Java value representing a UNO <code>ANY</code> value. + + @return <code>true</code> if and only if the two arguments represent + equal UNO values. + */ + public static boolean areSame(Object any1, Object any2) { + Any a1 = Any.complete(any1); + Any a2 = Any.complete(any2); + Type t = a1.getType(); + if (!a2.getType().equals(t)) { + return false; + } + Object v1 = a1.getObject(); + Object v2 = a2.getObject(); + switch (t.getTypeClass().getValue()) { + case TypeClass.VOID_value: + return true; + case TypeClass.BOOLEAN_value: + case TypeClass.BYTE_value: + case TypeClass.SHORT_value: + case TypeClass.UNSIGNED_SHORT_value: + case TypeClass.LONG_value: + case TypeClass.UNSIGNED_LONG_value: + case TypeClass.HYPER_value: + case TypeClass.UNSIGNED_HYPER_value: + case TypeClass.FLOAT_value: + case TypeClass.DOUBLE_value: + case TypeClass.CHAR_value: + case TypeClass.STRING_value: + case TypeClass.TYPE_value: + return v1.equals(v2); + case TypeClass.SEQUENCE_value: + int n = Array.getLength(v1); + if (n != Array.getLength(v2)) { + return false; + } + for (int i = 0; i < n; ++i) { + // Recursively using areSame on Java values that are (boxed) + // elements of Java arrays representing UNO sequence values, + // instead of on Java values that are representations of UNO ANY + // values, works by chance: + if (!areSame(Array.get(v1, i), Array.get(v2, i))) { + return false; + } + } + return true; + case TypeClass.ENUM_value: + return v1 == v2; + case TypeClass.STRUCT_value: + case TypeClass.EXCEPTION_value: + FieldDescription[] fs; + try { + fs = TypeDescription.getTypeDescription(t). + getFieldDescriptions(); + } catch (ClassNotFoundException e) { + throw new java.lang.RuntimeException(e); + } + for (int i = 0; i< fs.length; ++i) { + Type ft = new Type(fs[i].getTypeDescription()); + try { + // Recursively using areSame on Java values that are (boxed) + // fields of Java classes representing UNO struct or + // exception values, instead of on Java values that are + // representations of UNO ANY values, works by chance: + if (!areSame( + completeValue(ft, fs[i].getField().get(v1)), + completeValue(ft, fs[i].getField().get(v2)))) + { + return false; + } + } catch (IllegalAccessException e) { + throw new java.lang.RuntimeException(e); + } + } + return true; + case TypeClass.INTERFACE_value: + return v1 == v2 + || (v1 instanceof IQueryInterface + && ((IQueryInterface) v1).isSame(v2)) + || (v2 instanceof IQueryInterface + && ((IQueryInterface) v2).isSame(v1)); + default: + throw new java.lang.RuntimeException( + "com.sun.star.uno.Any has bad com.sun.star.uno.TypeClass"); + } + } + + /** + Complete a UNO value (make sure it is no invalid <code>null</code> + value). + + <p>This is useful for members of parameterized type of instantiated + polymorphic struct types, as <code>null</code> is a valid value there + (and only there, for all types except <code>ANY</code> and interface + types).</p> + + @param type a non-void, non-exception UNO type. + + @param value a Java value representing a UNO value of the given UNO type, + or <code>null</code>. + + @return the given value, or the neutral value of the given type, if the + given value was an invalid <code>null</code> value. + + @since UDK 3.2.3 + */ + public static final Object completeValue(Type type, Object value) { + if (value != null) { + return value; + } + switch (type.getTypeClass().getValue()) { + case TypeClass.BOOLEAN_value: + return Boolean.FALSE; + case TypeClass.BYTE_value: + return Byte.valueOf((byte) 0); + case TypeClass.SHORT_value: + case TypeClass.UNSIGNED_SHORT_value: + return Short.valueOf((short) 0); + case TypeClass.LONG_value: + case TypeClass.UNSIGNED_LONG_value: + return Integer.valueOf(0); + case TypeClass.HYPER_value: + case TypeClass.UNSIGNED_HYPER_value: + return Long.valueOf(0); + case TypeClass.FLOAT_value: + return new Float(0.0f); + case TypeClass.DOUBLE_value: + return new Double(0.0); + case TypeClass.CHAR_value: + return new Character('\u0000'); + case TypeClass.STRING_value: + return ""; + case TypeClass.TYPE_value: + return Type.VOID; + case TypeClass.ANY_value: + case TypeClass.INTERFACE_value: + return null; + case TypeClass.SEQUENCE_value: + return Array.newInstance(type.getZClass().getComponentType(), 0); + case TypeClass.STRUCT_value: + try { + return type.getZClass().getConstructor((Class[]) null). + newInstance((Object[]) null); + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.lang.Exception e) { + throw new java.lang.RuntimeException(e); + } + case TypeClass.ENUM_value: + try { + return type.getZClass().getMethod("getDefault", (Class[]) null). + invoke(null, (Object[]) null); + } catch (java.lang.RuntimeException e) { + throw e; + } catch (java.lang.Exception e) { + throw new java.lang.RuntimeException(e); + } + default: + throw new IllegalArgumentException( + "com.sun.star.uno.UnoRuntime.completeValue called with bad" + + " com.sun.star.uno.Type"); + } + } + + /** + * Gets the current context of the current thread, or <code>null</code> if + * no context has been set for the current thread. + * + * <p>The current context is thread local, which means that this method + * returns the context that was last set for this thread.</p> + * + * @return the current context of the current thread, or <code>null</code> + * if no context has been set for the current thread + */ + public static XCurrentContext getCurrentContext() { + return currentContext.get(); + } + + /** + * Sets the current context for the current thread. + * + * <p>The current context is thread local. To support a stacking behaviour, + * every function that sets the current context should reset it to the + * original value when exiting (for example, within a <code>finally</code> + * block).</p> + * + * @param context the context to be set; if <code>null</code>, any + * previously set context will be removed + */ + public static void setCurrentContext(XCurrentContext context) { + if (context == null) { + currentContext.remove(); + } else { + currentContext.set(context); + } + } + + /** + * Retrieves an environment of type <code>name</code> with context + * <code>context</code>. + * + * <p>Environments are held weakly by this class. If the requested + * environment already exists, this methods simply returns it. Otherwise, + * this method looks for it under + * <code>com.sun.star.lib.uno.environments.<var>name</var>.<!-- + * --><var>name</var>_environment</code>.</p> + * + * @param name the name of the environment + * @param context the context of the environment + * @throws Exception if something goes awry. + * @return an environment. + * @see com.sun.star.uno.IEnvironment + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static IEnvironment getEnvironment(String name, Object context) + throws java.lang.Exception + { + synchronized (environments) { + IEnvironment env = WeakMap.getValue( + environments.get(name + context)); + if (env == null) { + Class<?> c = Class.forName( + "com.sun.star.lib.uno.environments." + name + "." + name + + "_environment"); + Constructor<?> ctor = c.getConstructor( + new Class[] { Object.class }); + env = (IEnvironment) ctor.newInstance(new Object[] { context }); + environments.put(name + context, env); + } + return env; + } + } + + /** + * Gets a bridge from environment <code>from</code> to environment + * <code>to</code>. + * + * <p>Creates a new bridge, if the requested bridge does not yet exist, and + * hands the arguments to the bridge.</p> + * + * <p>If the requested bridge does not exist, it is searched for in package + * <code>com.sun.star.lib.uno.bridges.<var>from</var>_<var>to</var>;</code> + * and the root classpath as + * <code><var>from</var>_<var>to</var>_bridge</code>.</p> + * + * @param from the source environment + * @param to the target environment + * @param args the initial arguments for the bridge + * @throws Exception if something goes awry. + * @return the requested bridge + * @see #getBridgeByName + * @see com.sun.star.uno.IBridge + * @see com.sun.star.uno.IEnvironment + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static IBridge getBridge( + IEnvironment from, IEnvironment to, Object[] args) + throws java.lang.Exception + { + synchronized (bridges) { + String name = from.getName() + "_" + to.getName(); + String hashName = from.getName() + from.getContext() + "_" + + to.getName() + to.getContext(); + IBridge bridge = WeakMap.getValue(bridges.get(hashName)); + if(bridge == null) { + Class<?> zClass = null; + String className = name + "_bridge"; + try { + zClass = Class.forName(className); + } catch (ClassNotFoundException e) { + className = "com.sun.star.lib.uno.bridges." + name + "." + + className; + zClass = Class.forName(className); + } + Class<?>[] signature = { + IEnvironment.class, IEnvironment.class, args.getClass() }; + Constructor<?> constructor = zClass.getConstructor(signature); + Object[] iargs = { from, to, args }; + bridge = (IBridge) constructor.newInstance(iargs); + bridges.put(hashName, bridge); + } + return bridge; + } + } + + /** + * Gets a bridge from environment <code>from</code> to environment + * <code>to</code>. + * + * <p>Creates a new bridge, if the requested bridge does not yet exist, and + * hands the arguments to the bridge.</p> + * + * <p>If the requested bridge does not exist, it is searched for in package + * <code>com.sun.star.lib.uno.bridges.<var>from</var>_<var>to</var>;</code> + * and the root classpath as + * <code><var>from</var>_<var>to</var>_bridge</code>. The used environments + * are retrieved through <code>getEnvironment</code>.</p> + * + * @param from the name of the source environment + * @param fromContext the context for the source environment + * @param to the name of the target environment + * @param toContext the context for the target environment + * @param args the initial arguments for the bridge + * @throws Exception if something goes awry. + * @return the requested bridge + * @see #getBridge + * @see #getEnvironment + * @see com.sun.star.uno.IBridge + * @see com.sun.star.uno.IEnvironment + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static IBridge getBridgeByName( + String from, Object fromContext, String to, Object toContext, + Object[] args) throws java.lang.Exception + { + return getBridge( + getEnvironment(from, fromContext), getEnvironment(to, toContext), + args); + } + + /** + * Returns an array of all active bridges. + * + * @return an array of <code>IBridge</code> objects + * @see com.sun.star.uno.IBridge + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static IBridge[] getBridges() { + ArrayList<Object> l = new ArrayList<Object>(); + synchronized (bridges) { + for (Iterator<java.lang.ref.WeakReference<IBridge>> i = bridges.values().iterator(); i.hasNext();) { + IBridge o = WeakMap.getValue(i.next()); + if (o != null) { + l.add(o); + } + } + } + return l.toArray(new IBridge[l.size()]); + } + + /** + * Gets a mapping from environment <code>from</code> to environment + * <code>to</code>. + * + * <p>Mappings are like bridges, except that with mappings one can only map + * in one direction. Mappings are here for compatibility with the binary + * UNO API. Mappings are implemented as wrappers around bridges.</p> + * + * @param from the source environment + * @param to the target environment + * @throws Exception if something goes awry. + * @return the requested mapping + * @see com.sun.star.uno.IEnvironment + * @see com.sun.star.uno.IMapping + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static IMapping getMapping(IEnvironment from, IEnvironment to) + throws java.lang.Exception + { + IBridge bridge; + try { + bridge = getBridge(from, to, null); + } + catch (ClassNotFoundException e) { + bridge = new BridgeTurner(getBridge(to, from, null)); + } + return new MappingWrapper(bridge); + } + + /** + * Gets a mapping from environment <code>from</code> to environment + * <code>to</code>. + * + * <p>The used environments are retrieved through + * <code>getEnvironment</code>.</p> + * + * @param from the name of the source environment + * @param to the name of the target environment + * @throws Exception if something goes awry. + * @return the requested mapping + * @see #getEnvironment + * @see #getMapping + * @see com.sun.star.uno.IMapping + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static IMapping getMappingByName(String from, String to) + throws java.lang.Exception + { + return getMapping(getEnvironment(from, null), getEnvironment(to, null)); + } + + /** + * Resets this <code>UnoRuntime</code> to its initial state. + * + * <p>Releases all references to bridges and environments.</p> + * + * @return true if another thread didn't re-insert some bridge or + * environment before the method returns. Why that information + * would be useful is anybody's guess. + * + * @deprecated As of UDK 3.2.0, this method is deprecated, without + * offering a replacement. + */ + @Deprecated + public static boolean reset() { + synchronized (bridges) { + for (Iterator<java.lang.ref.WeakReference<IBridge>> i = bridges.values().iterator(); i.hasNext();) { + IBridge b = WeakMap.getValue(i.next()); + if (b != null) { + // The following call to dispose was originally made to + // com.sun.star.lib.sandbox.Disposable.dispose, which cannot + // throw an InterruptedException or IOException: + try { + b.dispose(); + } catch (InterruptedException e) { + Thread.currentThread(); + Thread.interrupted(); + throw new RuntimeException( + "Unexpected exception in UnoRuntime.reset: " + e); + } catch (IOException e) { + throw new RuntimeException( + "Unexpected exception in UnoRuntime.reset: " + e); + } + } + } + bridges.clear(); + } + environments.clear(); + return bridges.isEmpty() && environments.isEmpty(); + } + + /** + * @deprecated As of UDK 3.2.0, do not use this internal field. + */ + @Deprecated + public static final boolean DEBUG = false; + + private static final class BridgeTurner implements IBridge { + public BridgeTurner(IBridge bridge) { + this.bridge = bridge; + } + + public Object mapInterfaceTo(Object object, Type type) { + return bridge.mapInterfaceFrom(object, type); + } + + public Object mapInterfaceFrom(Object object, Type type) { + return bridge.mapInterfaceTo(object, type); + } + + public IEnvironment getSourceEnvironment() { + return bridge.getTargetEnvironment(); + } + + public IEnvironment getTargetEnvironment() { + return bridge.getSourceEnvironment(); + } + + public void acquire() { + bridge.acquire(); + } + + public void release() { + bridge.release(); + } + + public void dispose() throws InterruptedException, IOException { + bridge.dispose(); + } + + private final IBridge bridge; + } + + private static final class MappingWrapper implements IMapping { + public MappingWrapper(IBridge bridge) { + this.bridge = bridge; + } + + public Object mapInterface(Object object, Type type) { + return bridge.mapInterfaceTo(object, type); + } + + private final IBridge bridge; + } + + private static final WeakHashMap<Object,String> oidMap = new WeakHashMap<Object,String>(); + private static final String uniqueKeyHostPrefix + = Integer.toString(new Object().hashCode(), 16) + ":"; + private static final Object uniqueKeyLock = new Object(); + private static long uniqueKeyTime = System.currentTimeMillis(); + private static long uniqueKeyCount = Long.MIN_VALUE; + + private static final String oidSuffix = ";java[];" + getUniqueKey(); + + private static final ThreadLocal<XCurrentContext> currentContext = new ThreadLocal<XCurrentContext>(); + + private static final WeakMap<String,IEnvironment> environments = new WeakMap<String,IEnvironment>(); + private static final WeakMap<String,IBridge> bridges = new WeakMap<String,IBridge>(); +} diff --git a/ridljar/com/sun/star/uno/WeakReference.java b/ridljar/com/sun/star/uno/WeakReference.java new file mode 100644 index 000000000..a6b171ac2 --- /dev/null +++ b/ridljar/com/sun/star/uno/WeakReference.java @@ -0,0 +1,154 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +/** + * This class holds weak reference to an object. + * + * <p>It actually holds a reference to a <code>com.sun.star.XAdapter</code> + * implementation and obtains a hard reference if necessary. + */ +public class WeakReference +{ + private OWeakRefListener m_listener; + // There is no default constructor. Every instance must register itself with the + // XAdapter interface, which is done in the constructors. Assume we have this code + // WeakReference ref= new WeakReference(); + // ref = someOtherWeakReference; + // + // ref would not be notified (XReference.dispose()) because it did not register + // itself. Therefore the XAdapter would be kept alive although this is not + // necessary. + + /** + * Creates an instance of this class. + * + * @param obj another instance that is to be copied. + */ + public WeakReference(WeakReference obj) + { + if (obj == null) { + return; + } + Object weakImpl = obj.get(); + if (weakImpl == null) { + return; + } + XWeak weak = UnoRuntime.queryInterface(XWeak.class, weakImpl); + if (weak != null) { + XAdapter adapter = weak.queryAdapter(); + if (adapter != null) + m_listener = new OWeakRefListener(adapter); + } + } + + /** + * Creates an instance of this class. + * + * @param obj XWeak implementation. + */ + public WeakReference(Object obj) + { + XWeak weak= UnoRuntime.queryInterface(XWeak.class, obj); + if (weak != null) + { + XAdapter adapter= weak.queryAdapter(); + if (adapter != null) + m_listener= new OWeakRefListener(adapter); + } + } + + /** + * Returns a hard reference to the object that is kept weak by this class. + * + * @return a hard reference to the XWeak implementation. + */ + public Object get() + { + if (m_listener != null) + return m_listener.get(); + return null; + } +} + +/** + * Implementation of com.sun.star.uno.XReference for use with WeakReference. + * + * <p>It keeps the XAdapter implementation and registers always with it. + * Deregistering occurs on notification by the adapter and the adapter is + * released.</p> + */ +class OWeakRefListener implements XReference +{ + private XAdapter m_adapter; + + /** + * The constructor registered this object with adapter. + * + * @param adapter the XAdapter implementation. + */ + OWeakRefListener( XAdapter adapter) + { + m_adapter= adapter; + m_adapter.addReference(this); + } + + /** + * Method of <code>com.sun.star.uno.XReference</code>. + * + * <p>When called, it deregisters this object with the adapter and releases + * the reference to it.</p> + */ + synchronized public void dispose() + { + if (m_adapter != null) + { + m_adapter.removeReference(this); + m_adapter= null; + } + } + + /** + * Obtains a hard reference to the object which is kept weak by the adapter + * and returns it. + * + * @return hard reference to the otherwise weakly kept object. + */ + synchronized Object get() + { + Object retVal= null; + if (m_adapter != null) + { + retVal= m_adapter.queryAdapted(); + if (retVal == null) + { + // If this object registered as listener with XAdapter while it was notifying + // the listeners then this object might not have been notified. If queryAdapted + // returned null then the weak kept object is dead and the listeners have already + // been notified. And we missed it. + m_adapter.removeReference(this); + m_adapter= null; + } + } + return retVal; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |