bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / jni_uno / jni_info.h
blob98ba7c9e505a68a2b4a05f4ca1e61759520a9854
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_BRIDGES_SOURCE_JNI_UNO_JNI_INFO_H
21 #define INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_INFO_H
23 #include <sal/config.h>
25 #include <unordered_map>
27 #include "jni_base.h"
29 #include <osl/mutex.hxx>
30 #include <rtl/ref.hxx>
31 #include <rtl/ustring.hxx>
32 #include <rtl/strbuf.hxx>
34 #include <uno/environment.h>
35 #include <typelib/typedescription.hxx>
37 #include <com/sun/star/uno/Type.hxx>
39 namespace jvmaccess { class UnoVirtualMachine; }
41 namespace jni_uno
44 inline bool type_equals(
45 typelib_TypeDescriptionReference * type1,
46 typelib_TypeDescriptionReference * type2 )
48 if (type1 == type2)
49 return true;
50 OUString const & name1 =
51 OUString::unacquired( &type1->pTypeName );
52 OUString const & name2 =
53 OUString::unacquired( &type2->pTypeName );
54 return ((type1->eTypeClass == type2->eTypeClass) && name1 == name2);
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 struct JNI_type_info
65 JNI_type_info(const JNI_type_info&) = delete;
66 const JNI_type_info& operator=(const JNI_type_info&) = delete;
68 ::com::sun::star::uno::TypeDescription m_td;
69 jclass m_class;
71 virtual void destroy( JNIEnv * jni_env ) = 0;
72 protected:
73 void destruct( JNIEnv * jni_env )
74 { jni_env->DeleteGlobalRef( m_class ); }
75 virtual ~JNI_type_info() {}
76 explicit JNI_type_info(
77 JNI_context const & jni, typelib_TypeDescription * td );
80 struct JNI_interface_type_info : public JNI_type_info
82 jobject m_proxy_ctor; // proxy ctor
83 jobject m_type;
84 // sorted via typelib function index
85 std::unique_ptr<jmethodID[]> m_methods;
87 virtual void destroy( JNIEnv * jni_env ) override;
88 explicit JNI_interface_type_info(
89 JNI_context const & jni, typelib_TypeDescription * td );
91 private:
92 virtual ~JNI_interface_type_info() override {}
95 struct JNI_compound_type_info : public JNI_type_info
97 JNI_type_info const * m_base;
98 // ctor( msg ) for exceptions
99 jmethodID m_exc_ctor;
100 // sorted via typelib member index
101 std::unique_ptr<jfieldID[]> m_fields;
103 virtual void destroy( JNIEnv * jni_env ) override;
104 explicit JNI_compound_type_info(
105 JNI_context const & jni, typelib_TypeDescription * td );
107 private:
108 virtual ~JNI_compound_type_info() override {}
111 struct JNI_type_info_holder
113 JNI_type_info * m_info;
115 JNI_type_info_holder(const JNI_type_info_holder&) = delete;
116 const JNI_type_info_holder& operator=(const JNI_type_info_holder&) = delete;
118 JNI_type_info_holder() : m_info( nullptr ) {}
121 typedef std::unordered_map<
122 OUString, JNI_type_info_holder > t_str2type;
124 class JNI_info
126 mutable ::osl::Mutex m_mutex;
127 mutable t_str2type m_type_map;
129 public:
130 // These two are needed very early by find_class from within the ctor:
131 jclass m_class_Class;
132 jmethodID m_method_Class_forName;
134 jobject m_object_java_env;
135 jobject m_object_Any_VOID;
136 jobject m_object_Type_UNSIGNED_SHORT;
137 jobject m_object_Type_UNSIGNED_LONG;
138 jobject m_object_Type_UNSIGNED_HYPER;
140 jclass m_class_Object;
141 jclass m_class_Character;
142 jclass m_class_Boolean;
143 jclass m_class_Byte;
144 jclass m_class_Short;
145 jclass m_class_Integer;
146 jclass m_class_Long;
147 jclass m_class_Float;
148 jclass m_class_Double;
149 jclass m_class_String;
151 jclass m_class_UnoRuntime;
152 jclass m_class_RuntimeException;
153 jclass m_class_Any;
154 jclass m_class_Type;
155 jclass m_class_TypeClass;
156 jclass m_class_JNI_proxy;
157 jclass m_class_AsynchronousFinalizer;
159 jmethodID m_method_Object_toString;
160 jmethodID m_method_Class_getName;
161 jmethodID m_method_Throwable_getMessage;
162 jmethodID m_ctor_Character_with_char;
163 jmethodID m_ctor_Boolean_with_boolean;
164 jmethodID m_ctor_Byte_with_byte;
165 jmethodID m_ctor_Short_with_short;
166 jmethodID m_ctor_Integer_with_int;
167 jmethodID m_ctor_Long_with_long;
168 jmethodID m_ctor_Float_with_float;
169 jmethodID m_ctor_Double_with_double;
170 jmethodID m_method_Boolean_booleanValue;
171 jmethodID m_method_Byte_byteValue;
172 jmethodID m_method_Character_charValue;
173 jmethodID m_method_Double_doubleValue;
174 jmethodID m_method_Float_floatValue;
175 jmethodID m_method_Integer_intValue;
176 jmethodID m_method_Long_longValue;
177 jmethodID m_method_Short_shortValue;
179 jmethodID m_method_IEnvironment_getRegisteredInterface;
180 jmethodID m_method_IEnvironment_registerInterface;
181 jmethodID m_method_UnoRuntime_generateOid;
182 jmethodID m_method_UnoRuntime_queryInterface;
183 jmethodID m_ctor_Any_with_Type_Object;
184 jfieldID m_field_Any_type;
185 jfieldID m_field_Any_object;
186 jmethodID m_ctor_Type_with_Class;
187 jmethodID m_ctor_Type_with_Name_TypeClass;
188 jfieldID m_field_Type_typeName;
189 jmethodID m_method_TypeClass_fromInt;
190 jfieldID m_field_Enum_m_value;
192 jmethodID m_method_JNI_proxy_get_proxy_ctor;
193 jmethodID m_method_JNI_proxy_create;
194 jfieldID m_field_JNI_proxy_m_receiver_handle;
195 jfieldID m_field_JNI_proxy_m_td_handle;
196 jfieldID m_field_JNI_proxy_m_type;
197 jfieldID m_field_JNI_proxy_m_oid;
199 jmethodID m_ctor_AsynchronousFinalizer;
200 jmethodID m_method_AsynchronousFinalizer_drain;
202 ::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td;
203 ::com::sun::star::uno::Type const & m_Exception_type;
204 ::com::sun::star::uno::Type const & m_RuntimeException_type;
205 ::com::sun::star::uno::Type const & m_void_type;
206 JNI_interface_type_info const * m_XInterface_type_info;
208 // noncopyable
209 JNI_info(const JNI_info&) = delete;
210 const JNI_info& operator=(const JNI_info&) = delete;
212 JNI_type_info const * get_type_info(
213 JNI_context const & jni,
214 typelib_TypeDescription * type ) const;
215 JNI_type_info const * get_type_info(
216 JNI_context const & jni,
217 typelib_TypeDescriptionReference * type ) const;
218 JNI_type_info const * get_type_info(
219 JNI_context const & jni,
220 OUString const & uno_name ) const;
221 inline static void append_sig(
222 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
223 bool use_Object_for_type_XInterface = true, bool use_slashes = true );
225 // get this
226 static JNI_info const * get_jni_info(
227 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
228 inline void destroy( JNIEnv * jni_env );
230 private:
231 JNI_type_info const * create_type_info(
232 JNI_context const & jni, typelib_TypeDescription * td ) const;
234 void destruct( JNIEnv * jni_env );
236 JNI_info( JNIEnv * jni_env, jobject class_loader,
237 jclass classClass, jmethodID methodForName );
238 ~JNI_info() {}
241 inline void JNI_info::destroy( JNIEnv * jni_env )
243 destruct( jni_env );
244 delete this;
247 inline void JNI_info::append_sig(
248 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
249 bool use_Object_for_type_XInterface, bool use_slashes )
251 switch (type->eTypeClass)
253 case typelib_TypeClass_VOID:
254 buf->append( 'V' );
255 break;
256 case typelib_TypeClass_CHAR:
257 buf->append( 'C' );
258 break;
259 case typelib_TypeClass_BOOLEAN:
260 buf->append( 'Z' );
261 break;
262 case typelib_TypeClass_BYTE:
263 buf->append( 'B' );
264 break;
265 case typelib_TypeClass_SHORT:
266 case typelib_TypeClass_UNSIGNED_SHORT:
267 buf->append( 'S' );
268 break;
269 case typelib_TypeClass_LONG:
270 case typelib_TypeClass_UNSIGNED_LONG:
271 buf->append( 'I' );
272 break;
273 case typelib_TypeClass_HYPER:
274 case typelib_TypeClass_UNSIGNED_HYPER:
275 buf->append( 'J' );
276 break;
277 case typelib_TypeClass_FLOAT:
278 buf->append( 'F' );
279 break;
280 case typelib_TypeClass_DOUBLE:
281 buf->append( 'D' );
282 break;
283 case typelib_TypeClass_STRING:
284 if ( use_slashes ) {
285 buf->append( "Ljava/lang/String;" );
286 } else {
287 buf->append( "Ljava.lang.String;" );
289 break;
290 case typelib_TypeClass_TYPE:
291 if ( use_slashes ) {
292 buf->append( "Lcom/sun/star/uno/Type;" );
293 } else {
294 buf->append( "Lcom.sun.star.uno.Type;" );
296 break;
297 case typelib_TypeClass_ANY:
298 if ( use_slashes ) {
299 buf->append( "Ljava/lang/Object;" );
300 } else {
301 buf->append( "Ljava.lang.Object;" );
303 break;
304 case typelib_TypeClass_ENUM:
305 case typelib_TypeClass_STRUCT:
306 case typelib_TypeClass_EXCEPTION:
308 OUString const & uno_name =
309 OUString::unacquired( &type->pTypeName );
310 buf->append( 'L' );
311 // Erase type arguments of instantiated polymorphic struct types:
312 sal_Int32 i = uno_name.indexOf( '<' );
313 if ( i < 0 ) {
314 buf->append(
315 OUStringToOString(
316 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
317 RTL_TEXTENCODING_JAVA_UTF8 ) );
318 } else {
319 OUString s( uno_name.copy( 0, i ) );
320 buf->append(
321 OUStringToOString(
322 use_slashes ? s.replace( '.', '/' ) : s,
323 RTL_TEXTENCODING_JAVA_UTF8 ) );
325 buf->append( ';' );
326 break;
328 case typelib_TypeClass_SEQUENCE:
330 buf->append( '[' );
331 TypeDescr td( type );
332 append_sig(
333 buf, reinterpret_cast<typelib_IndirectTypeDescription *>(td.get())->pType,
334 use_Object_for_type_XInterface, use_slashes );
335 break;
337 case typelib_TypeClass_INTERFACE:
338 if (use_Object_for_type_XInterface && is_XInterface( type ))
340 if ( use_slashes ) {
341 buf->append( "Ljava/lang/Object;" );
342 } else {
343 buf->append( "Ljava.lang.Object;" );
346 else
348 OUString const & uno_name =
349 OUString::unacquired( &type->pTypeName );
350 buf->append( 'L' );
351 buf->append(
352 OUStringToOString(
353 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
354 RTL_TEXTENCODING_JAVA_UTF8 ) );
355 buf->append( ';' );
357 break;
358 default:
359 throw BridgeRuntimeError(
360 "unsupported type: " +
361 OUString::unacquired( &type->pTypeName ) );
367 #endif
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */