update dev300-m58
[ooovba.git] / bridges / source / jni_uno / jni_info.h
blob2be866a00da529676cb3881f3d04cf35fee82529
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jni_info.h,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #if ! defined INCLUDED_JNI_INFO_H
32 #define INCLUDED_JNI_INFO_H
34 #include <hash_map>
36 #include "jni_base.h"
38 #include "osl/mutex.hxx"
39 #include "rtl/ref.hxx"
40 #include "rtl/ustring.hxx"
41 #include "rtl/strbuf.hxx"
43 #include "uno/environment.h"
44 #include "typelib/typedescription.hxx"
46 #include "com/sun/star/uno/Type.hxx"
48 namespace jvmaccess { class UnoVirtualMachine; }
50 namespace jni_uno
53 //------------------------------------------------------------------------------
54 inline bool type_equals(
55 typelib_TypeDescriptionReference * type1,
56 typelib_TypeDescriptionReference * type2 )
58 if (type1 == type2)
59 return true;
60 ::rtl::OUString const & name1 =
61 ::rtl::OUString::unacquired( &type1->pTypeName );
62 ::rtl::OUString const & name2 =
63 ::rtl::OUString::unacquired( &type2->pTypeName );
64 return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
67 //------------------------------------------------------------------------------
68 inline bool is_XInterface( typelib_TypeDescriptionReference * type )
70 return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
71 ::rtl::OUString::unacquired( &type->pTypeName ).equalsAsciiL(
72 RTL_CONSTASCII_STRINGPARAM("com.sun.star.uno.XInterface") ));
75 //==============================================================================
76 struct JNI_type_info
78 ::com::sun::star::uno::TypeDescription m_td;
79 jclass m_class;
81 virtual void destroy( JNIEnv * jni_env ) = 0;
82 protected:
83 inline void destruct( JNIEnv * jni_env )
84 { jni_env->DeleteGlobalRef( m_class ); }
85 virtual inline ~JNI_type_info() {}
86 explicit JNI_type_info(
87 JNI_context const & jni, typelib_TypeDescription * td );
90 //==============================================================================
91 struct JNI_interface_type_info : public JNI_type_info
93 jobject m_proxy_ctor; // proxy ctor
94 jobject m_type;
95 // sorted via typelib function index
96 jmethodID * m_methods;
98 virtual void destroy( JNIEnv * jni_env );
99 explicit JNI_interface_type_info(
100 JNI_context const & jni, typelib_TypeDescription * td );
103 //==============================================================================
104 struct JNI_compound_type_info : public JNI_type_info
106 JNI_type_info const * m_base;
107 // ctor( msg ) for exceptions
108 jmethodID m_exc_ctor;
109 // sorted via typelib member index
110 jfieldID * m_fields;
112 virtual void destroy( JNIEnv * jni_env );
113 explicit JNI_compound_type_info(
114 JNI_context const & jni, typelib_TypeDescription * td );
117 //==============================================================================
118 struct JNI_type_info_holder
120 JNI_type_info * m_info;
121 inline JNI_type_info_holder()
122 : m_info( 0 )
126 typedef ::std::hash_map<
127 ::rtl::OUString, JNI_type_info_holder, ::rtl::OUStringHash > t_str2type;
129 //==============================================================================
130 class JNI_info
132 mutable ::osl::Mutex m_mutex;
133 mutable t_str2type m_type_map;
135 public:
136 // These two are needed very early by find_class from within the ctor:
137 jclass m_class_Class;
138 jmethodID m_method_Class_forName;
141 jobject m_object_java_env;
142 jobject m_object_Any_VOID;
143 jobject m_object_Type_UNSIGNED_SHORT;
144 jobject m_object_Type_UNSIGNED_LONG;
145 jobject m_object_Type_UNSIGNED_HYPER;
148 jclass m_class_Object;
149 jclass m_class_Character;
150 jclass m_class_Boolean;
151 jclass m_class_Byte;
152 jclass m_class_Short;
153 jclass m_class_Integer;
154 jclass m_class_Long;
155 jclass m_class_Float;
156 jclass m_class_Double;
157 jclass m_class_String;
159 jclass m_class_UnoRuntime;
160 jclass m_class_RuntimeException;
161 jclass m_class_Any;
162 jclass m_class_Type;
163 jclass m_class_TypeClass;
164 jclass m_class_JNI_proxy;
167 jmethodID m_method_Object_toString;
168 jmethodID m_method_Class_getName;
169 jmethodID m_method_Throwable_getMessage;
170 jmethodID m_ctor_Character_with_char;
171 jmethodID m_ctor_Boolean_with_boolean;
172 jmethodID m_ctor_Byte_with_byte;
173 jmethodID m_ctor_Short_with_short;
174 jmethodID m_ctor_Integer_with_int;
175 jmethodID m_ctor_Long_with_long;
176 jmethodID m_ctor_Float_with_float;
177 jmethodID m_ctor_Double_with_double;
178 jmethodID m_method_Boolean_booleanValue;
179 jmethodID m_method_Byte_byteValue;
180 jmethodID m_method_Character_charValue;
181 jmethodID m_method_Double_doubleValue;
182 jmethodID m_method_Float_floatValue;
183 jmethodID m_method_Integer_intValue;
184 jmethodID m_method_Long_longValue;
185 jmethodID m_method_Short_shortValue;
188 jmethodID m_method_IEnvironment_getRegisteredInterface;
189 jmethodID m_method_IEnvironment_registerInterface;
190 jmethodID m_method_UnoRuntime_generateOid;
191 jmethodID m_method_UnoRuntime_queryInterface;
192 jmethodID m_ctor_Any_with_Type_Object;
193 jfieldID m_field_Any__type;
194 jfieldID m_field_Any__object;
195 jmethodID m_ctor_Type_with_Class;
196 jmethodID m_ctor_Type_with_Name_TypeClass;
197 jfieldID m_field_Type__typeName;
198 jmethodID m_method_TypeClass_fromInt;
199 jfieldID m_field_Enum_m_value;
202 jmethodID m_method_JNI_proxy_get_proxy_ctor;
203 jmethodID m_method_JNI_proxy_create;
204 jfieldID m_field_JNI_proxy_m_receiver_handle;
205 jfieldID m_field_JNI_proxy_m_td_handle;
206 jfieldID m_field_JNI_proxy_m_type;
207 jfieldID m_field_JNI_proxy_m_oid;
210 ::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td;
211 ::com::sun::star::uno::Type const & m_Exception_type;
212 ::com::sun::star::uno::Type const & m_RuntimeException_type;
213 ::com::sun::star::uno::Type const & m_void_type;
215 JNI_interface_type_info const * m_XInterface_type_info;
218 JNI_type_info const * get_type_info(
219 JNI_context const & jni,
220 typelib_TypeDescription * type ) const;
221 JNI_type_info const * get_type_info(
222 JNI_context const & jni,
223 typelib_TypeDescriptionReference * type ) const;
224 JNI_type_info const * get_type_info(
225 JNI_context const & jni,
226 ::rtl::OUString const & uno_name ) const;
228 inline static void append_sig(
229 ::rtl::OStringBuffer * buf, typelib_TypeDescriptionReference * type,
230 bool use_Object_for_type_XInterface = true, bool use_slashes = true );
232 // get this
233 static JNI_info const * get_jni_info(
234 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
235 inline void destroy( JNIEnv * jni_env );
237 private:
238 JNI_type_info const * create_type_info(
239 JNI_context const & jni, typelib_TypeDescription * td ) const;
241 void destruct( JNIEnv * jni_env );
243 JNI_info( JNIEnv * jni_env, jobject class_loader,
244 jclass classClass, jmethodID methodForName );
245 inline ~JNI_info() {}
248 //______________________________________________________________________________
249 inline void JNI_info::destroy( JNIEnv * jni_env )
251 destruct( jni_env );
252 delete this;
255 //______________________________________________________________________________
256 inline void JNI_info::append_sig(
257 ::rtl::OStringBuffer * buf, typelib_TypeDescriptionReference * type,
258 bool use_Object_for_type_XInterface, bool use_slashes )
260 switch (type->eTypeClass)
262 case typelib_TypeClass_VOID:
263 buf->append( 'V' );
264 break;
265 case typelib_TypeClass_CHAR:
266 buf->append( 'C' );
267 break;
268 case typelib_TypeClass_BOOLEAN:
269 buf->append( 'Z' );
270 break;
271 case typelib_TypeClass_BYTE:
272 buf->append( 'B' );
273 break;
274 case typelib_TypeClass_SHORT:
275 case typelib_TypeClass_UNSIGNED_SHORT:
276 buf->append( 'S' );
277 break;
278 case typelib_TypeClass_LONG:
279 case typelib_TypeClass_UNSIGNED_LONG:
280 buf->append( 'I' );
281 break;
282 case typelib_TypeClass_HYPER:
283 case typelib_TypeClass_UNSIGNED_HYPER:
284 buf->append( 'J' );
285 break;
286 case typelib_TypeClass_FLOAT:
287 buf->append( 'F' );
288 break;
289 case typelib_TypeClass_DOUBLE:
290 buf->append( 'D' );
291 break;
292 case typelib_TypeClass_STRING:
293 if ( use_slashes ) {
294 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/String;") );
295 } else {
296 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.String;") );
298 break;
299 case typelib_TypeClass_TYPE:
300 if ( use_slashes ) {
301 buf->append(
302 RTL_CONSTASCII_STRINGPARAM("Lcom/sun/star/uno/Type;") );
303 } else {
304 buf->append(
305 RTL_CONSTASCII_STRINGPARAM("Lcom.sun.star.uno.Type;") );
307 break;
308 case typelib_TypeClass_ANY:
309 if ( use_slashes ) {
310 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;") );
311 } else {
312 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.Object;") );
314 break;
315 case typelib_TypeClass_ENUM:
316 case typelib_TypeClass_STRUCT:
317 case typelib_TypeClass_EXCEPTION:
319 ::rtl::OUString const & uno_name =
320 ::rtl::OUString::unacquired( &type->pTypeName );
321 buf->append( 'L' );
322 // Erase type arguments of instantiated polymorphic struct types:
323 sal_Int32 i = uno_name.indexOf( '<' );
324 if ( i < 0 ) {
325 buf->append(
326 ::rtl::OUStringToOString(
327 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
328 RTL_TEXTENCODING_JAVA_UTF8 ) );
329 } else {
330 rtl::OUString s( uno_name.copy( 0, i ) );
331 buf->append(
332 ::rtl::OUStringToOString(
333 use_slashes ? s.replace( '.', '/' ) : s,
334 RTL_TEXTENCODING_JAVA_UTF8 ) );
336 buf->append( ';' );
337 break;
339 case typelib_TypeClass_SEQUENCE:
341 buf->append( '[' );
342 TypeDescr td( type );
343 append_sig(
344 buf, ((typelib_IndirectTypeDescription *)td.get())->pType,
345 use_Object_for_type_XInterface, use_slashes );
346 break;
348 case typelib_TypeClass_INTERFACE:
349 if (use_Object_for_type_XInterface && is_XInterface( type ))
351 if ( use_slashes ) {
352 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;") );
353 } else {
354 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.Object;") );
357 else
359 ::rtl::OUString const & uno_name =
360 ::rtl::OUString::unacquired( &type->pTypeName );
361 buf->append( 'L' );
362 buf->append(
363 ::rtl::OUStringToOString(
364 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
365 RTL_TEXTENCODING_JAVA_UTF8 ) );
366 buf->append( ';' );
368 break;
369 default:
370 throw BridgeRuntimeError(
371 OUSTR("unsupported type: ") +
372 ::rtl::OUString::unacquired( &type->pTypeName ) );
378 #endif