Bump for 3.6-28
[LibreOffice.git] / bridges / source / jni_uno / jni_info.h
blobf890e49f898db54b8af06a5050fb14f73f09f71e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #if ! defined INCLUDED_JNI_INFO_H
30 #define INCLUDED_JNI_INFO_H
32 #include <boost/unordered_map.hpp>
34 #include "jni_base.h"
36 #include "osl/mutex.hxx"
37 #include "rtl/ref.hxx"
38 #include "rtl/ustring.hxx"
39 #include "rtl/strbuf.hxx"
41 #include "uno/environment.h"
42 #include "typelib/typedescription.hxx"
44 #include "com/sun/star/uno/Type.hxx"
46 namespace jvmaccess { class UnoVirtualMachine; }
48 namespace jni_uno
51 //------------------------------------------------------------------------------
52 inline bool type_equals(
53 typelib_TypeDescriptionReference * type1,
54 typelib_TypeDescriptionReference * type2 )
56 if (type1 == type2)
57 return true;
58 ::rtl::OUString const & name1 =
59 ::rtl::OUString::unacquired( &type1->pTypeName );
60 ::rtl::OUString const & name2 =
61 ::rtl::OUString::unacquired( &type2->pTypeName );
62 return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
65 //------------------------------------------------------------------------------
66 inline bool is_XInterface( typelib_TypeDescriptionReference * type )
68 return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
69 ::rtl::OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
72 //==============================================================================
73 struct JNI_type_info
75 ::com::sun::star::uno::TypeDescription m_td;
76 jclass m_class;
78 virtual void destroy( JNIEnv * jni_env ) = 0;
79 protected:
80 inline void destruct( JNIEnv * jni_env )
81 { jni_env->DeleteGlobalRef( m_class ); }
82 virtual inline ~JNI_type_info() {}
83 explicit JNI_type_info(
84 JNI_context const & jni, typelib_TypeDescription * td );
87 //==============================================================================
88 struct JNI_interface_type_info : public JNI_type_info
90 jobject m_proxy_ctor; // proxy ctor
91 jobject m_type;
92 // sorted via typelib function index
93 jmethodID * m_methods;
95 virtual void destroy( JNIEnv * jni_env );
96 explicit JNI_interface_type_info(
97 JNI_context const & jni, typelib_TypeDescription * td );
100 //==============================================================================
101 struct JNI_compound_type_info : public JNI_type_info
103 JNI_type_info const * m_base;
104 // ctor( msg ) for exceptions
105 jmethodID m_exc_ctor;
106 // sorted via typelib member index
107 jfieldID * m_fields;
109 virtual void destroy( JNIEnv * jni_env );
110 explicit JNI_compound_type_info(
111 JNI_context const & jni, typelib_TypeDescription * td );
114 //==============================================================================
115 struct JNI_type_info_holder
117 JNI_type_info * m_info;
118 inline JNI_type_info_holder()
119 : m_info( 0 )
123 typedef ::boost::unordered_map<
124 ::rtl::OUString, JNI_type_info_holder, ::rtl::OUStringHash > t_str2type;
126 //==============================================================================
127 class JNI_info
129 mutable ::osl::Mutex m_mutex;
130 mutable t_str2type m_type_map;
132 public:
133 // These two are needed very early by find_class from within the ctor:
134 jclass m_class_Class;
135 jmethodID m_method_Class_forName;
138 jobject m_object_java_env;
139 jobject m_object_Any_VOID;
140 jobject m_object_Type_UNSIGNED_SHORT;
141 jobject m_object_Type_UNSIGNED_LONG;
142 jobject m_object_Type_UNSIGNED_HYPER;
145 jclass m_class_Object;
146 jclass m_class_Character;
147 jclass m_class_Boolean;
148 jclass m_class_Byte;
149 jclass m_class_Short;
150 jclass m_class_Integer;
151 jclass m_class_Long;
152 jclass m_class_Float;
153 jclass m_class_Double;
154 jclass m_class_String;
156 jclass m_class_UnoRuntime;
157 jclass m_class_RuntimeException;
158 jclass m_class_Any;
159 jclass m_class_Type;
160 jclass m_class_TypeClass;
161 jclass m_class_JNI_proxy;
164 jmethodID m_method_Object_toString;
165 jmethodID m_method_Class_getName;
166 jmethodID m_method_Throwable_getMessage;
167 jmethodID m_ctor_Character_with_char;
168 jmethodID m_ctor_Boolean_with_boolean;
169 jmethodID m_ctor_Byte_with_byte;
170 jmethodID m_ctor_Short_with_short;
171 jmethodID m_ctor_Integer_with_int;
172 jmethodID m_ctor_Long_with_long;
173 jmethodID m_ctor_Float_with_float;
174 jmethodID m_ctor_Double_with_double;
175 jmethodID m_method_Boolean_booleanValue;
176 jmethodID m_method_Byte_byteValue;
177 jmethodID m_method_Character_charValue;
178 jmethodID m_method_Double_doubleValue;
179 jmethodID m_method_Float_floatValue;
180 jmethodID m_method_Integer_intValue;
181 jmethodID m_method_Long_longValue;
182 jmethodID m_method_Short_shortValue;
185 jmethodID m_method_IEnvironment_getRegisteredInterface;
186 jmethodID m_method_IEnvironment_registerInterface;
187 jmethodID m_method_UnoRuntime_generateOid;
188 jmethodID m_method_UnoRuntime_queryInterface;
189 jmethodID m_ctor_Any_with_Type_Object;
190 jfieldID m_field_Any__type;
191 jfieldID m_field_Any__object;
192 jmethodID m_ctor_Type_with_Class;
193 jmethodID m_ctor_Type_with_Name_TypeClass;
194 jfieldID m_field_Type__typeName;
195 jmethodID m_method_TypeClass_fromInt;
196 jfieldID m_field_Enum_m_value;
199 jmethodID m_method_JNI_proxy_get_proxy_ctor;
200 jmethodID m_method_JNI_proxy_create;
201 jfieldID m_field_JNI_proxy_m_receiver_handle;
202 jfieldID m_field_JNI_proxy_m_td_handle;
203 jfieldID m_field_JNI_proxy_m_type;
204 jfieldID m_field_JNI_proxy_m_oid;
207 ::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td;
208 ::com::sun::star::uno::Type const & m_Exception_type;
209 ::com::sun::star::uno::Type const & m_RuntimeException_type;
210 ::com::sun::star::uno::Type const & m_void_type;
212 JNI_interface_type_info const * m_XInterface_type_info;
215 JNI_type_info const * get_type_info(
216 JNI_context const & jni,
217 typelib_TypeDescription * type ) const;
218 JNI_type_info const * get_type_info(
219 JNI_context const & jni,
220 typelib_TypeDescriptionReference * type ) const;
221 JNI_type_info const * get_type_info(
222 JNI_context const & jni,
223 ::rtl::OUString const & uno_name ) const;
225 inline static void append_sig(
226 ::rtl::OStringBuffer * buf, typelib_TypeDescriptionReference * type,
227 bool use_Object_for_type_XInterface = true, bool use_slashes = true );
229 // get this
230 static JNI_info const * get_jni_info(
231 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
232 inline void destroy( JNIEnv * jni_env );
234 private:
235 JNI_type_info const * create_type_info(
236 JNI_context const & jni, typelib_TypeDescription * td ) const;
238 void destruct( JNIEnv * jni_env );
240 JNI_info( JNIEnv * jni_env, jobject class_loader,
241 jclass classClass, jmethodID methodForName );
242 inline ~JNI_info() {}
245 //______________________________________________________________________________
246 inline void JNI_info::destroy( JNIEnv * jni_env )
248 destruct( jni_env );
249 delete this;
252 //______________________________________________________________________________
253 inline void JNI_info::append_sig(
254 ::rtl::OStringBuffer * buf, typelib_TypeDescriptionReference * type,
255 bool use_Object_for_type_XInterface, bool use_slashes )
257 switch (type->eTypeClass)
259 case typelib_TypeClass_VOID:
260 buf->append( 'V' );
261 break;
262 case typelib_TypeClass_CHAR:
263 buf->append( 'C' );
264 break;
265 case typelib_TypeClass_BOOLEAN:
266 buf->append( 'Z' );
267 break;
268 case typelib_TypeClass_BYTE:
269 buf->append( 'B' );
270 break;
271 case typelib_TypeClass_SHORT:
272 case typelib_TypeClass_UNSIGNED_SHORT:
273 buf->append( 'S' );
274 break;
275 case typelib_TypeClass_LONG:
276 case typelib_TypeClass_UNSIGNED_LONG:
277 buf->append( 'I' );
278 break;
279 case typelib_TypeClass_HYPER:
280 case typelib_TypeClass_UNSIGNED_HYPER:
281 buf->append( 'J' );
282 break;
283 case typelib_TypeClass_FLOAT:
284 buf->append( 'F' );
285 break;
286 case typelib_TypeClass_DOUBLE:
287 buf->append( 'D' );
288 break;
289 case typelib_TypeClass_STRING:
290 if ( use_slashes ) {
291 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/String;") );
292 } else {
293 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.String;") );
295 break;
296 case typelib_TypeClass_TYPE:
297 if ( use_slashes ) {
298 buf->append(
299 RTL_CONSTASCII_STRINGPARAM("Lcom/sun/star/uno/Type;") );
300 } else {
301 buf->append(
302 RTL_CONSTASCII_STRINGPARAM("Lcom.sun.star.uno.Type;") );
304 break;
305 case typelib_TypeClass_ANY:
306 if ( use_slashes ) {
307 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;") );
308 } else {
309 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.Object;") );
311 break;
312 case typelib_TypeClass_ENUM:
313 case typelib_TypeClass_STRUCT:
314 case typelib_TypeClass_EXCEPTION:
316 ::rtl::OUString const & uno_name =
317 ::rtl::OUString::unacquired( &type->pTypeName );
318 buf->append( 'L' );
319 // Erase type arguments of instantiated polymorphic struct types:
320 sal_Int32 i = uno_name.indexOf( '<' );
321 if ( i < 0 ) {
322 buf->append(
323 ::rtl::OUStringToOString(
324 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
325 RTL_TEXTENCODING_JAVA_UTF8 ) );
326 } else {
327 rtl::OUString s( uno_name.copy( 0, i ) );
328 buf->append(
329 ::rtl::OUStringToOString(
330 use_slashes ? s.replace( '.', '/' ) : s,
331 RTL_TEXTENCODING_JAVA_UTF8 ) );
333 buf->append( ';' );
334 break;
336 case typelib_TypeClass_SEQUENCE:
338 buf->append( '[' );
339 TypeDescr td( type );
340 append_sig(
341 buf, ((typelib_IndirectTypeDescription *)td.get())->pType,
342 use_Object_for_type_XInterface, use_slashes );
343 break;
345 case typelib_TypeClass_INTERFACE:
346 if (use_Object_for_type_XInterface && is_XInterface( type ))
348 if ( use_slashes ) {
349 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;") );
350 } else {
351 buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.Object;") );
354 else
356 ::rtl::OUString const & uno_name =
357 ::rtl::OUString::unacquired( &type->pTypeName );
358 buf->append( 'L' );
359 buf->append(
360 ::rtl::OUStringToOString(
361 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
362 RTL_TEXTENCODING_JAVA_UTF8 ) );
363 buf->append( ';' );
365 break;
366 default:
367 throw BridgeRuntimeError(
368 OUSTR("unsupported type: ") +
369 ::rtl::OUString::unacquired( &type->pTypeName ) );
375 #endif
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */