bump product version to 4.1.6.2
[LibreOffice.git] / bridges / source / jni_uno / jni_info.h
blob63ce45dcfd241deeced5357dce41f931bb1990c5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_JNI_INFO_H
21 #define INCLUDED_JNI_INFO_H
23 #include <boost/unordered_map.hpp>
25 #include "jni_base.h"
27 #include "osl/mutex.hxx"
28 #include "rtl/ref.hxx"
29 #include "rtl/ustring.hxx"
30 #include "rtl/strbuf.hxx"
32 #include "uno/environment.h"
33 #include "typelib/typedescription.hxx"
35 #include "com/sun/star/uno/Type.hxx"
37 namespace jvmaccess { class UnoVirtualMachine; }
39 namespace jni_uno
42 //------------------------------------------------------------------------------
43 inline bool type_equals(
44 typelib_TypeDescriptionReference * type1,
45 typelib_TypeDescriptionReference * type2 )
47 if (type1 == type2)
48 return true;
49 OUString const & name1 =
50 OUString::unacquired( &type1->pTypeName );
51 OUString const & name2 =
52 OUString::unacquired( &type2->pTypeName );
53 return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
56 //------------------------------------------------------------------------------
57 inline bool is_XInterface( typelib_TypeDescriptionReference * type )
59 return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
60 OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
63 //==============================================================================
64 struct JNI_type_info
66 ::com::sun::star::uno::TypeDescription m_td;
67 jclass m_class;
69 virtual void destroy( JNIEnv * jni_env ) = 0;
70 protected:
71 inline void destruct( JNIEnv * jni_env )
72 { jni_env->DeleteGlobalRef( m_class ); }
73 virtual inline ~JNI_type_info() {}
74 explicit JNI_type_info(
75 JNI_context const & jni, typelib_TypeDescription * td );
78 //==============================================================================
79 struct JNI_interface_type_info : public JNI_type_info
81 jobject m_proxy_ctor; // proxy ctor
82 jobject m_type;
83 // sorted via typelib function index
84 jmethodID * m_methods;
86 virtual void destroy( JNIEnv * jni_env );
87 explicit JNI_interface_type_info(
88 JNI_context const & jni, typelib_TypeDescription * td );
91 //==============================================================================
92 struct JNI_compound_type_info : public JNI_type_info
94 JNI_type_info const * m_base;
95 // ctor( msg ) for exceptions
96 jmethodID m_exc_ctor;
97 // sorted via typelib member index
98 jfieldID * m_fields;
100 virtual void destroy( JNIEnv * jni_env );
101 explicit JNI_compound_type_info(
102 JNI_context const & jni, typelib_TypeDescription * td );
105 //==============================================================================
106 struct JNI_type_info_holder
108 JNI_type_info * m_info;
109 inline JNI_type_info_holder()
110 : m_info( 0 )
114 typedef ::boost::unordered_map<
115 OUString, JNI_type_info_holder, OUStringHash > t_str2type;
117 //==============================================================================
118 class JNI_info
120 mutable ::osl::Mutex m_mutex;
121 mutable t_str2type m_type_map;
123 public:
124 // These two are needed very early by find_class from within the ctor:
125 jclass m_class_Class;
126 jmethodID m_method_Class_forName;
129 jobject m_object_java_env;
130 jobject m_object_Any_VOID;
131 jobject m_object_Type_UNSIGNED_SHORT;
132 jobject m_object_Type_UNSIGNED_LONG;
133 jobject m_object_Type_UNSIGNED_HYPER;
136 jclass m_class_Object;
137 jclass m_class_Character;
138 jclass m_class_Boolean;
139 jclass m_class_Byte;
140 jclass m_class_Short;
141 jclass m_class_Integer;
142 jclass m_class_Long;
143 jclass m_class_Float;
144 jclass m_class_Double;
145 jclass m_class_String;
147 jclass m_class_UnoRuntime;
148 jclass m_class_RuntimeException;
149 jclass m_class_Any;
150 jclass m_class_Type;
151 jclass m_class_TypeClass;
152 jclass m_class_JNI_proxy;
155 jmethodID m_method_Object_toString;
156 jmethodID m_method_Class_getName;
157 jmethodID m_method_Throwable_getMessage;
158 jmethodID m_ctor_Character_with_char;
159 jmethodID m_ctor_Boolean_with_boolean;
160 jmethodID m_ctor_Byte_with_byte;
161 jmethodID m_ctor_Short_with_short;
162 jmethodID m_ctor_Integer_with_int;
163 jmethodID m_ctor_Long_with_long;
164 jmethodID m_ctor_Float_with_float;
165 jmethodID m_ctor_Double_with_double;
166 jmethodID m_method_Boolean_booleanValue;
167 jmethodID m_method_Byte_byteValue;
168 jmethodID m_method_Character_charValue;
169 jmethodID m_method_Double_doubleValue;
170 jmethodID m_method_Float_floatValue;
171 jmethodID m_method_Integer_intValue;
172 jmethodID m_method_Long_longValue;
173 jmethodID m_method_Short_shortValue;
176 jmethodID m_method_IEnvironment_getRegisteredInterface;
177 jmethodID m_method_IEnvironment_registerInterface;
178 jmethodID m_method_UnoRuntime_generateOid;
179 jmethodID m_method_UnoRuntime_queryInterface;
180 jmethodID m_ctor_Any_with_Type_Object;
181 jfieldID m_field_Any__type;
182 jfieldID m_field_Any__object;
183 jmethodID m_ctor_Type_with_Class;
184 jmethodID m_ctor_Type_with_Name_TypeClass;
185 jfieldID m_field_Type__typeName;
186 jmethodID m_method_TypeClass_fromInt;
187 jfieldID m_field_Enum_m_value;
190 jmethodID m_method_JNI_proxy_get_proxy_ctor;
191 jmethodID m_method_JNI_proxy_create;
192 jfieldID m_field_JNI_proxy_m_receiver_handle;
193 jfieldID m_field_JNI_proxy_m_td_handle;
194 jfieldID m_field_JNI_proxy_m_type;
195 jfieldID m_field_JNI_proxy_m_oid;
198 ::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td;
199 ::com::sun::star::uno::Type const & m_Exception_type;
200 ::com::sun::star::uno::Type const & m_RuntimeException_type;
201 ::com::sun::star::uno::Type const & m_void_type;
203 JNI_interface_type_info const * m_XInterface_type_info;
206 JNI_type_info const * get_type_info(
207 JNI_context const & jni,
208 typelib_TypeDescription * type ) const;
209 JNI_type_info const * get_type_info(
210 JNI_context const & jni,
211 typelib_TypeDescriptionReference * type ) const;
212 JNI_type_info const * get_type_info(
213 JNI_context const & jni,
214 OUString const & uno_name ) const;
216 inline static void append_sig(
217 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
218 bool use_Object_for_type_XInterface = true, bool use_slashes = true );
220 // get this
221 static JNI_info const * get_jni_info(
222 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
223 inline void destroy( JNIEnv * jni_env );
225 private:
226 JNI_type_info const * create_type_info(
227 JNI_context const & jni, typelib_TypeDescription * td ) const;
229 void destruct( JNIEnv * jni_env );
231 JNI_info( JNIEnv * jni_env, jobject class_loader,
232 jclass classClass, jmethodID methodForName );
233 inline ~JNI_info() {}
236 //______________________________________________________________________________
237 inline void JNI_info::destroy( JNIEnv * jni_env )
239 destruct( jni_env );
240 delete this;
243 //______________________________________________________________________________
244 inline void JNI_info::append_sig(
245 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
246 bool use_Object_for_type_XInterface, bool use_slashes )
248 switch (type->eTypeClass)
250 case typelib_TypeClass_VOID:
251 buf->append( 'V' );
252 break;
253 case typelib_TypeClass_CHAR:
254 buf->append( 'C' );
255 break;
256 case typelib_TypeClass_BOOLEAN:
257 buf->append( 'Z' );
258 break;
259 case typelib_TypeClass_BYTE:
260 buf->append( 'B' );
261 break;
262 case typelib_TypeClass_SHORT:
263 case typelib_TypeClass_UNSIGNED_SHORT:
264 buf->append( 'S' );
265 break;
266 case typelib_TypeClass_LONG:
267 case typelib_TypeClass_UNSIGNED_LONG:
268 buf->append( 'I' );
269 break;
270 case typelib_TypeClass_HYPER:
271 case typelib_TypeClass_UNSIGNED_HYPER:
272 buf->append( 'J' );
273 break;
274 case typelib_TypeClass_FLOAT:
275 buf->append( 'F' );
276 break;
277 case typelib_TypeClass_DOUBLE:
278 buf->append( 'D' );
279 break;
280 case typelib_TypeClass_STRING:
281 if ( use_slashes ) {
282 buf->append( "Ljava/lang/String;" );
283 } else {
284 buf->append( "Ljava.lang.String;" );
286 break;
287 case typelib_TypeClass_TYPE:
288 if ( use_slashes ) {
289 buf->append( "Lcom/sun/star/uno/Type;" );
290 } else {
291 buf->append( "Lcom.sun.star.uno.Type;" );
293 break;
294 case typelib_TypeClass_ANY:
295 if ( use_slashes ) {
296 buf->append( "Ljava/lang/Object;" );
297 } else {
298 buf->append( "Ljava.lang.Object;" );
300 break;
301 case typelib_TypeClass_ENUM:
302 case typelib_TypeClass_STRUCT:
303 case typelib_TypeClass_EXCEPTION:
305 OUString const & uno_name =
306 OUString::unacquired( &type->pTypeName );
307 buf->append( 'L' );
308 // Erase type arguments of instantiated polymorphic struct types:
309 sal_Int32 i = uno_name.indexOf( '<' );
310 if ( i < 0 ) {
311 buf->append(
312 OUStringToOString(
313 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
314 RTL_TEXTENCODING_JAVA_UTF8 ) );
315 } else {
316 OUString s( uno_name.copy( 0, i ) );
317 buf->append(
318 OUStringToOString(
319 use_slashes ? s.replace( '.', '/' ) : s,
320 RTL_TEXTENCODING_JAVA_UTF8 ) );
322 buf->append( ';' );
323 break;
325 case typelib_TypeClass_SEQUENCE:
327 buf->append( '[' );
328 TypeDescr td( type );
329 append_sig(
330 buf, ((typelib_IndirectTypeDescription *)td.get())->pType,
331 use_Object_for_type_XInterface, use_slashes );
332 break;
334 case typelib_TypeClass_INTERFACE:
335 if (use_Object_for_type_XInterface && is_XInterface( type ))
337 if ( use_slashes ) {
338 buf->append( "Ljava/lang/Object;" );
339 } else {
340 buf->append( "Ljava.lang.Object;" );
343 else
345 OUString const & uno_name =
346 OUString::unacquired( &type->pTypeName );
347 buf->append( 'L' );
348 buf->append(
349 OUStringToOString(
350 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
351 RTL_TEXTENCODING_JAVA_UTF8 ) );
352 buf->append( ';' );
354 break;
355 default:
356 throw BridgeRuntimeError(
357 "unsupported type: " +
358 OUString::unacquired( &type->pTypeName ) );
364 #endif
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */