From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- ridljar/com/sun/star/uno/Any.java | 151 +++++ ridljar/com/sun/star/uno/AnyConverter.java | 668 +++++++++++++++++++++++ ridljar/com/sun/star/uno/Ascii.java | 43 ++ ridljar/com/sun/star/uno/AsciiString.java | 44 ++ ridljar/com/sun/star/uno/Enum.java | 50 ++ ridljar/com/sun/star/uno/IBridge.java | 94 ++++ ridljar/com/sun/star/uno/IEnvironment.java | 144 +++++ ridljar/com/sun/star/uno/IMapping.java | 41 ++ ridljar/com/sun/star/uno/IQueryInterface.java | 60 ++ ridljar/com/sun/star/uno/MappingException.java | 65 +++ ridljar/com/sun/star/uno/Type.java | 699 ++++++++++++++++++++++++ ridljar/com/sun/star/uno/UnoRuntime.java | 726 +++++++++++++++++++++++++ ridljar/com/sun/star/uno/WeakReference.java | 154 ++++++ 13 files changed, 2939 insertions(+) create mode 100644 ridljar/com/sun/star/uno/Any.java create mode 100644 ridljar/com/sun/star/uno/AnyConverter.java create mode 100644 ridljar/com/sun/star/uno/Ascii.java create mode 100644 ridljar/com/sun/star/uno/AsciiString.java create mode 100644 ridljar/com/sun/star/uno/Enum.java create mode 100644 ridljar/com/sun/star/uno/IBridge.java create mode 100644 ridljar/com/sun/star/uno/IEnvironment.java create mode 100644 ridljar/com/sun/star/uno/IMapping.java create mode 100644 ridljar/com/sun/star/uno/IQueryInterface.java create mode 100644 ridljar/com/sun/star/uno/MappingException.java create mode 100644 ridljar/com/sun/star/uno/Type.java create mode 100644 ridljar/com/sun/star/uno/UnoRuntime.java create mode 100644 ridljar/com/sun/star/uno/WeakReference.java (limited to 'ridljar/com/sun/star/uno') 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 java.lang.Object. + *

+ * 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). + *

+ */ +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 ANY (make sure it is wrapped up as an + * Any instance). + * + * @param any a Java value representing a UNO ANY value. + * @return a complete Java value (that is, an Any instance) + * representing the same UNO ANY 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 true if this object is the same as the obj argument; + * false 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. + * + *

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.

+ * + *

The methods which extract the value do a widening conversion. See the + * method comments for the respective conversions.

+ */ +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 void. + * + * @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 char. + * + * @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 boolean. + * + * @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 byte. + * + * @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 short. + * + * @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 long + * (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 hyper + * (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 float. + * + * @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 double. + * + * @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 string. + * + * @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 enum. + * + * @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 type. + * + * @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. + * + *

If object 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.

+ * + * @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. + * + *

Allowed argument types are Byte, Short or Any containing these types.

+ * + * @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. + * + *

Allowed argument types are Anies containing idl unsigned short values.

+ * + * @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. + * + *

Allowed argument types are Byte, Short, Integer or Any containing these + * types.

+ * + * @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. + * + *

Allowed argument types are Anies containing idl unsigned short or + * unsigned long values.

+ * + * @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. + * + *

Allowed argument types are Byte, Short, Integer, Long or Any containing + * these types.

+ * + * @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. + * + *

Allowed argument types are Anies containing idl unsigned short, unsigned + * long or unsigned hyper values.

+ * + * @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. + * + *

Allowed argument types are Byte, Short, Float or Any containing these + * types.

+ * + * @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. + * + *

Allowed argument types are Byte, Short, Int, Float, Double or Any + * containing these types.

+ * + * @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. + * + *

For interfaces, the argument object is queried for the interface + * specified by the type argument.

+ * + *

That query (UnoRuntime.queryInterface) might return null, if the interface + * is not implemented or a null-ref or a VOID any is given.

+ * + * @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. + * + *

For interfaces, the argument object is queried for the interface + * specified by the type argument. That query (UnoRuntime.queryInterface) + * might return null, if the interface is not implemented or a null-ref or a + * VOID any is given.

+ * + * @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 object if is correspond to the type in + * argument what. + * + *

object is either matched directly against the type or if it is + * an any then the contained object is matched against the type.

+ */ + 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 ascii. + * + * @deprecated do not use. + */ +@Deprecated +public final class Ascii { + public final char ascii; + + /** + * Constructs a new Ascii. + * + * @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 asciistring. + * + * @deprecated do not use. + */ +@Deprecated +public final class AsciiString { + public final String asciistring; + + /** + * Constructs a new AsciiString. + * + * @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. + *

+ * 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. + *

+ */ +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. + * + *

Bridges are able to map one object from one UNO environment to another and + * vice versa.

+ * + * @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. + * + *

If the life count drops to zero, the bridge disposes itself.

+ */ + void release(); + + /** + * Disposes the bridge. + * + *

Sends involved threads an InterruptedException. Releases + * mapped objects.

+ * + * @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. + * + *

With this interface, objects can be registered at and revoked from an + * environment.

+ * + * @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. + * + *

Such an object will typically be one of three things: + *

+ * + *

The object actually registered may differ from the specified + * object that is passed as an argument. This enables an + * environment to work in a multi-threaded scenario, where two threads can + * call registerInterface for the same combination of + * oid and type at the same time; the race + * condition is solved by letting one of the calls register its argument + * object, ignoring the argument object of the + * other call, and letting both calls return the same + * object.

+ * + *

The registered object is held only weakly by the environment. After a + * call to registerInterface, a call to + * getRegisteredInterface only succeeds as long as the + * registered object is still strongly reachable, and the registered object + * has not been explicitly revoked by calling + * revokeInterface.

+ * + * @param object the object to register; must be non-null + * @param oid in-out parameter containing the OID of object. + * 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 + * object should implement this type. + * @return the registered object (may differ from the object + * passed in); will never be null + */ + Object registerInterface(Object object, String[] oid, Type type); + + /** + * Explicitly revokes a UNO interface facet. + * + *

Calls to registerInterface and + * revokeInterface 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 revokeInterface (and calls to + * revokeInterface would no longer be necessary if the calling + * code does not want to control the temporal extent of the + * registration).

+ * + *

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.

+ * + * @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 object; will never be null + */ + String getRegisteredObjectIdentifier(Object object); + + /** + * Lists the registered objects to System.out. + * + *

This is for debug purposes.

+ */ + 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. + * + *

This interface exists for compatibility with the binary UNO API.

+ * + * @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. + * + *

Calls are delegated through the UnoRuntime to this + * interface. Implement this interface in case you want to customize the + * behaviour of UnoRuntime.queryInterface.

+ * + * @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 Type + * object representing a UNO interface type + * @return a reference to the requested UNO interface type if available, + * otherwise null + * @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 null + * @return true if and only if object is not + * null 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. + * + *

The exception is replaced by the com.sun.star.lang.DisposedException.

+ * + * @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 MappingException. + */ + public MappingException() { + super(); + } + + /** + * Constructs an MappingException with a detail message. + * + * @param message the detail message. + */ + public MappingException(String message) { + super(message); + } + + /** + * Constructs an MappingException 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 TYPE. + * + *

The UNO type is not directly mapped to java.lang.Class for at + * least two reasons. For one, some UNO types (like UNSIGNED + * SHORT) 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.)

+ * + *

A Type is uniquely determined by its type class (a + * TypeClass) and its type name (a String); these two + * will never be null. A Type may have an additional + * "z class" (a java.lang.Class), giving a Java class type that + * corresponds to the UNO type. Also, a Type can cache a type + * description (a com.sun.star.uno.typedesc.TypeDescription), which can be + * computed and set by TypeDescription.getTypeDescription. + */ +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, TypeInfo> __javaClassToTypeClass = + new HashMap, 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 Type which defaults to VOID. + */ + public Type() { + init(null, void.class, false, false, false); + } + + /** + * Constructs a new Type with the given type class and type + * name. + * + * @param typeName the type name. Must not be null. + * @param typeClass the type class. Must not be null, and must + * match the typeName (for example, it is illegal to + * combine a typeName of "void" with a + * typeClass of BOOLEAN). + */ + public Type(String typeName, TypeClass typeClass) { + _typeClass = typeClass; + _typeName = typeName; + } + + /** + * Constructs a new Type from the given + * java.lang.Class. + * + *

This is equivalent to Type(zClass, false).

+ * + * @param zClass the Java class of this type. Must not be + * null. + */ + public Type(Class zClass) { + init(null, zClass, false, false, false); + } + + /** + * Constructs a new Type from the given + * java.lang.Class, handling ambiguous cases. + * + *

In certain cases, one Java class corresponds to two UNO types (e.g., + * the Java class short[].class corresponds to both a sequence + * of SHORT and a sequence of UNSIGNED SHORT in + * UNO). In such ambiguous cases, the parameter alternative + * controls which UNO type is chosen: + *

+ *

In all other cases, the value of alternative is + * ignored.

+ * + *

This constructor cannot be used to create Type instances + * that represent (sequences of) instantiated polymorphic struct types.

+ * + * @param zClass the Java class of this type; must not be null + * @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 Type from the given type description. + * + * For internal URE use only. Not to be used by client code. + * + * @param typeDescription a type description. Must not be + * null. + */ + public Type(TypeDescription typeDescription) { + _typeName = typeDescription.getTypeName(); + _typeClass = typeDescription.getTypeClass(); + _iTypeDescription = typeDescription; + } + + /** + * Constructs a new Type with the given type name. + * + * @param typeName the name of this type; must not be null. + */ + 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 Type with the given type class. + * + * @param typeClass the type class of this type; must not be + * null. Only type classes for simple types are allowed + * here. + * + * @throws IllegalArgumentException if the given typeClass 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 null, but might be + * UNKNOWN. + */ + public TypeClass getTypeClass() { + return _typeClass; + } + + /** + * Gets the type name. + * + * @return the type name; will never be null + */ + public String getTypeName() { + return _typeName; + } + + /** + * Gets the Java class. + * + * @return the type name; may be null in extreme situations + * (inconsistent TypeClass, error loading a class) + */ + public Class getZClass() { + synchronized (this) { + if (_class == null) { + _class = determineClass(); + } + } + return _class; + } + + /** + * Gives the type description of this type. + * + * For internal URE use only. Not to be used by client code. + * + * @return the type description; may be null + */ + public TypeDescription getTypeDescription() { + return _iTypeDescription; + } + + /** + * Sets the type description for this type. + * + * For internal URE use only. Not to be used by client code. + * + * @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 true if this object is the same as the obj argument; + * false 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. + * + *

The methods queryInterface and areSame delegate + * calls to the implementing objects and are used instead of casts, + * instanceof, ==, and equals.

+ * + *

For historic reasons, this class is not final, and has a + * public 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.

+ * + * @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 public + * constructor for this class, which only has static members. + * Also, this class might be changed to become final in a + * future version. + */ + @Deprecated + public UnoRuntime() {} + + /** + * Generates a worldwide unique identifier string. + * + *

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.

+ * + * @return a unique String + */ + 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. + * + *

It is guaranteed that subsequent calls to this method with the same + * Java object will give the same ID.

+ * + *

This method is generally of little use for client code. It should be + * considered a mistake that this method is published at all.

+ * + * @param object any object for which an OID shall be generated; must not be + * null + * @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. + * + *

This method returns null in case the given UNO object + * does not support the given UNO interface type (or is itself + * null). 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.

+ * + * @param type the requested UNO interface type; must be a Type + * object representing a UNO interface type + * @param object a reference to any Java object representing (a facet of) a + * UNO object; may be null + * @return a reference to the requested UNO interface type if available, + * otherwise null + * @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 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 null + * @return a reference to the requested UNO interface type if available, + * otherwise null + * @see #queryInterface(Type, Object) + */ + @SuppressWarnings("unchecked") + public static T queryInterface(Class zInterface, Object object) { + return (T) queryInterface(new Type(zInterface), object); + } + + /** + Tests two UNO ANY values for equality. + +

Two UNO values are equal if and only if they are of the + same UNO type t, and they meet the following condition, + depending on t:

+
    +
  • If t is a primitive type, then both values must denote + the same element of the set of values of t.
  • + +
  • If t is a structured type, then both values must + recursively contain corresponding values that are equal.
  • + +
  • If t is an interface type, then the two values must be + either both null references, or both references to the same UNO + object.
  • +
+ + @param any1 a Java value representing a UNO ANY value. + + @param any2 a Java value representing a UNO ANY value. + + @return true 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 null + value). + +

This is useful for members of parameterized type of instantiated + polymorphic struct types, as null is a valid value there + (and only there, for all types except ANY and interface + types).

+ + @param type a non-void, non-exception UNO type. + + @param value a Java value representing a UNO value of the given UNO type, + or null. + + @return the given value, or the neutral value of the given type, if the + given value was an invalid null 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 null if + * no context has been set for the current thread. + * + *

The current context is thread local, which means that this method + * returns the context that was last set for this thread.

+ * + * @return the current context of the current thread, or null + * 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. + * + *

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 finally + * block).

+ * + * @param context the context to be set; if null, 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 name with context + * context. + * + *

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 + * com.sun.star.lib.uno.environments.name.name_environment.

+ * + * @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 from to environment + * to. + * + *

Creates a new bridge, if the requested bridge does not yet exist, and + * hands the arguments to the bridge.

+ * + *

If the requested bridge does not exist, it is searched for in package + * com.sun.star.lib.uno.bridges.from_to; + * and the root classpath as + * from_to_bridge.

+ * + * @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 from to environment + * to. + * + *

Creates a new bridge, if the requested bridge does not yet exist, and + * hands the arguments to the bridge.

+ * + *

If the requested bridge does not exist, it is searched for in package + * com.sun.star.lib.uno.bridges.from_to; + * and the root classpath as + * from_to_bridge. The used environments + * are retrieved through getEnvironment.

+ * + * @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 IBridge 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 l = new ArrayList(); + synchronized (bridges) { + for (Iterator> 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 from to environment + * to. + * + *

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.

+ * + * @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 from to environment + * to. + * + *

The used environments are retrieved through + * getEnvironment.

+ * + * @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 UnoRuntime to its initial state. + * + *

Releases all references to bridges and environments.

+ * + * @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> 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 oidMap = new WeakHashMap(); + 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 currentContext = new ThreadLocal(); + + private static final WeakMap environments = new WeakMap(); + private static final WeakMap bridges = new WeakMap(); +} 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. + * + *

It actually holds a reference to a com.sun.star.XAdapter + * 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. + * + *

It keeps the XAdapter implementation and registers always with it. + * Deregistering occurs on notification by the adapter and the adapter is + * released.

+ */ +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 com.sun.star.uno.XReference. + * + *

When called, it deregisters this object with the adapter and releases + * the reference to it.

+ */ + 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: */ -- cgit v1.2.3