Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / bridges / source / jni_uno / jni_info.h
blob7a18e553e4004ff96e63918d03200ca083cd4c60
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 #pragma once
22 #include <sal/config.h>
24 #include <unordered_map>
26 #include "jni_base.h"
28 #include <mutex>
29 #include <rtl/ref.hxx>
30 #include <rtl/ustring.hxx>
31 #include <rtl/strbuf.hxx>
33 #include <uno/environment.h>
34 #include <typelib/typedescription.hxx>
36 #include <com/sun/star/uno/Type.hxx>
38 namespace jvmaccess { class UnoVirtualMachine; }
40 namespace jni_uno
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 == name2);
56 inline bool is_XInterface( typelib_TypeDescriptionReference * type )
58 return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
59 OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
62 struct JNI_type_info
64 JNI_type_info(const JNI_type_info&) = delete;
65 const JNI_type_info& operator=(const JNI_type_info&) = delete;
67 ::com::sun::star::uno::TypeDescription m_td;
68 jclass m_class;
70 virtual void destroy( JNIEnv * jni_env ) = 0;
71 protected:
72 void destruct( JNIEnv * jni_env )
73 { jni_env->DeleteGlobalRef( m_class ); }
74 virtual ~JNI_type_info() {}
75 explicit JNI_type_info(
76 JNI_context const & jni, typelib_TypeDescription * td );
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 std::unique_ptr<jmethodID[]> m_methods;
86 virtual void destroy( JNIEnv * jni_env ) override;
87 explicit JNI_interface_type_info(
88 JNI_context const & jni, typelib_TypeDescription * td );
90 private:
91 virtual ~JNI_interface_type_info() override {}
94 struct JNI_compound_type_info : public JNI_type_info
96 JNI_type_info const * m_base;
97 // ctor( msg ) for exceptions
98 jmethodID m_exc_ctor;
99 // sorted via typelib member index
100 std::unique_ptr<jfieldID[]> m_fields;
102 virtual void destroy( JNIEnv * jni_env ) override;
103 explicit JNI_compound_type_info(
104 JNI_context const & jni, typelib_TypeDescription * td );
106 private:
107 virtual ~JNI_compound_type_info() override {}
110 struct JNI_type_info_holder
112 JNI_type_info * m_info;
114 JNI_type_info_holder(const JNI_type_info_holder&) = delete;
115 const JNI_type_info_holder& operator=(const JNI_type_info_holder&) = delete;
117 JNI_type_info_holder() : m_info( nullptr ) {}
120 typedef std::unordered_map<
121 OUString, JNI_type_info_holder > t_str2type;
123 class JNI_info
125 mutable std::mutex m_mutex;
126 mutable t_str2type m_type_map;
128 public:
129 // These two are needed very early by find_class from within the ctor:
130 jclass m_class_Class;
131 jmethodID m_method_Class_forName;
133 jobject m_object_java_env;
134 jobject m_object_Any_VOID;
135 jobject m_object_Type_UNSIGNED_SHORT;
136 jobject m_object_Type_UNSIGNED_LONG;
137 jobject m_object_Type_UNSIGNED_HYPER;
139 jclass m_class_Object;
140 jclass m_class_Character;
141 jclass m_class_Boolean;
142 jclass m_class_Byte;
143 jclass m_class_Short;
144 jclass m_class_Integer;
145 jclass m_class_Long;
146 jclass m_class_Float;
147 jclass m_class_Double;
148 jclass m_class_String;
150 jclass m_class_UnoRuntime;
151 jclass m_class_RuntimeException;
152 jclass m_class_Any;
153 jclass m_class_Type;
154 jclass m_class_TypeClass;
155 jclass m_class_JNI_proxy;
156 jclass m_class_AsynchronousFinalizer;
158 jmethodID m_method_Object_toString;
159 jmethodID m_method_Class_getName;
160 jmethodID m_method_Throwable_getMessage;
161 jmethodID m_ctor_Character_with_char;
162 jmethodID m_ctor_Boolean_with_boolean;
163 jmethodID m_ctor_Byte_with_byte;
164 jmethodID m_ctor_Short_with_short;
165 jmethodID m_ctor_Integer_with_int;
166 jmethodID m_ctor_Long_with_long;
167 jmethodID m_ctor_Float_with_float;
168 jmethodID m_ctor_Double_with_double;
169 jmethodID m_method_Boolean_booleanValue;
170 jmethodID m_method_Byte_byteValue;
171 jmethodID m_method_Character_charValue;
172 jmethodID m_method_Double_doubleValue;
173 jmethodID m_method_Float_floatValue;
174 jmethodID m_method_Integer_intValue;
175 jmethodID m_method_Long_longValue;
176 jmethodID m_method_Short_shortValue;
178 jmethodID m_method_IEnvironment_getRegisteredInterface;
179 jmethodID m_method_IEnvironment_registerInterface;
180 jmethodID m_method_UnoRuntime_generateOid;
181 jmethodID m_method_UnoRuntime_queryInterface;
182 jmethodID m_ctor_Any_with_Type_Object;
183 jfieldID m_field_Any_type;
184 jfieldID m_field_Any_object;
185 jmethodID m_ctor_Type_with_Class;
186 jmethodID m_ctor_Type_with_Name_TypeClass;
187 jfieldID m_field_Type_typeName;
188 jmethodID m_method_TypeClass_fromInt;
189 jfieldID m_field_Enum_m_value;
191 jmethodID m_method_JNI_proxy_get_proxy_ctor;
192 jmethodID m_method_JNI_proxy_create;
193 jfieldID m_field_JNI_proxy_m_receiver_handle;
194 jfieldID m_field_JNI_proxy_m_td_handle;
195 jfieldID m_field_JNI_proxy_m_type;
196 jfieldID m_field_JNI_proxy_m_oid;
198 jmethodID m_ctor_AsynchronousFinalizer;
199 jmethodID m_method_AsynchronousFinalizer_drain;
201 ::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td;
202 ::com::sun::star::uno::Type const & m_Exception_type;
203 ::com::sun::star::uno::Type const & m_RuntimeException_type;
204 ::com::sun::star::uno::Type const & m_void_type;
205 JNI_interface_type_info const * m_XInterface_type_info;
207 // noncopyable
208 JNI_info(const JNI_info&) = delete;
209 const JNI_info& operator=(const JNI_info&) = delete;
211 JNI_type_info const * get_type_info(
212 JNI_context const & jni,
213 typelib_TypeDescription * type ) const;
214 JNI_type_info const * get_type_info(
215 JNI_context const & jni,
216 typelib_TypeDescriptionReference * type ) const;
217 JNI_type_info const * get_type_info(
218 JNI_context const & jni,
219 OUString const & uno_name ) const;
220 inline static void append_sig(
221 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
222 bool use_Object_for_type_XInterface = true, bool use_slashes = true );
224 // get this
225 static JNI_info const * get_jni_info(
226 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
227 inline void destroy( JNIEnv * jni_env );
229 private:
230 JNI_type_info const * create_type_info(
231 JNI_context const & jni, typelib_TypeDescription * td ) const;
233 void destruct( JNIEnv * jni_env );
235 JNI_info( JNIEnv * jni_env, jobject class_loader,
236 jclass classClass, jmethodID methodForName );
237 ~JNI_info() {}
240 inline void JNI_info::destroy( JNIEnv * jni_env )
242 destruct( jni_env );
243 delete this;
246 inline void JNI_info::append_sig(
247 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
248 bool use_Object_for_type_XInterface, bool use_slashes )
250 switch (type->eTypeClass)
252 case typelib_TypeClass_VOID:
253 buf->append( 'V' );
254 break;
255 case typelib_TypeClass_CHAR:
256 buf->append( 'C' );
257 break;
258 case typelib_TypeClass_BOOLEAN:
259 buf->append( 'Z' );
260 break;
261 case typelib_TypeClass_BYTE:
262 buf->append( 'B' );
263 break;
264 case typelib_TypeClass_SHORT:
265 case typelib_TypeClass_UNSIGNED_SHORT:
266 buf->append( 'S' );
267 break;
268 case typelib_TypeClass_LONG:
269 case typelib_TypeClass_UNSIGNED_LONG:
270 buf->append( 'I' );
271 break;
272 case typelib_TypeClass_HYPER:
273 case typelib_TypeClass_UNSIGNED_HYPER:
274 buf->append( 'J' );
275 break;
276 case typelib_TypeClass_FLOAT:
277 buf->append( 'F' );
278 break;
279 case typelib_TypeClass_DOUBLE:
280 buf->append( 'D' );
281 break;
282 case typelib_TypeClass_STRING:
283 if ( use_slashes ) {
284 buf->append( "Ljava/lang/String;" );
285 } else {
286 buf->append( "Ljava.lang.String;" );
288 break;
289 case typelib_TypeClass_TYPE:
290 if ( use_slashes ) {
291 buf->append( "Lcom/sun/star/uno/Type;" );
292 } else {
293 buf->append( "Lcom.sun.star.uno.Type;" );
295 break;
296 case typelib_TypeClass_ANY:
297 if ( use_slashes ) {
298 buf->append( "Ljava/lang/Object;" );
299 } else {
300 buf->append( "Ljava.lang.Object;" );
302 break;
303 case typelib_TypeClass_ENUM:
304 case typelib_TypeClass_STRUCT:
305 case typelib_TypeClass_EXCEPTION:
307 OUString const & uno_name =
308 OUString::unacquired( &type->pTypeName );
309 buf->append( 'L' );
310 // Erase type arguments of instantiated polymorphic struct types:
311 sal_Int32 i = uno_name.indexOf( '<' );
312 if ( i < 0 ) {
313 buf->append(
314 OUStringToOString(
315 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
316 RTL_TEXTENCODING_JAVA_UTF8 ) );
317 } else {
318 OUString s( uno_name.copy( 0, i ) );
319 buf->append(
320 OUStringToOString(
321 use_slashes ? s.replace( '.', '/' ) : s,
322 RTL_TEXTENCODING_JAVA_UTF8 ) );
324 buf->append( ';' );
325 break;
327 case typelib_TypeClass_SEQUENCE:
329 buf->append( '[' );
330 TypeDescr td( type );
331 append_sig(
332 buf, reinterpret_cast<typelib_IndirectTypeDescription *>(td.get())->pType,
333 use_Object_for_type_XInterface, use_slashes );
334 break;
336 case typelib_TypeClass_INTERFACE:
337 if (use_Object_for_type_XInterface && is_XInterface( type ))
339 if ( use_slashes ) {
340 buf->append( "Ljava/lang/Object;" );
341 } else {
342 buf->append( "Ljava.lang.Object;" );
345 else
347 OUString const & uno_name =
348 OUString::unacquired( &type->pTypeName );
349 buf->append( 'L' );
350 buf->append(
351 OUStringToOString(
352 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
353 RTL_TEXTENCODING_JAVA_UTF8 ) );
354 buf->append( ';' );
356 break;
357 default:
358 throw BridgeRuntimeError(
359 "unsupported type: " +
360 OUString::unacquired( &type->pTypeName ) );
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */