LibreOffice Module bridges (master) 1
jni_info.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include <sal/config.h>
23
24#include <unordered_map>
25
26#include "jni_base.h"
27
28#include <mutex>
29#include <rtl/ref.hxx>
30#include <rtl/ustring.hxx>
31#include <rtl/strbuf.hxx>
32
33#include <uno/environment.h>
34#include <typelib/typedescription.hxx>
35
36#include <com/sun/star/uno/Type.hxx>
37
38namespace jvmaccess { class UnoVirtualMachine; }
39
40namespace jni_uno
41{
42
43inline bool type_equals(
44 typelib_TypeDescriptionReference * type1,
45 typelib_TypeDescriptionReference * type2 )
46{
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);
54}
55
56inline bool is_XInterface( typelib_TypeDescriptionReference * type )
57{
58 return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
59 OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
60}
61
63{
64 JNI_type_info(const JNI_type_info&) = delete;
65 const JNI_type_info& operator=(const JNI_type_info&) = delete;
66
67 ::com::sun::star::uno::TypeDescription m_td;
68 jclass m_class;
69
70 virtual void destroy( JNIEnv * jni_env ) = 0;
71protected:
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 );
77};
78
80{
81 jobject m_proxy_ctor; // proxy ctor
82 jobject m_type;
83 // sorted via typelib function index
84 std::unique_ptr<jmethodID[]> m_methods;
85
86 virtual void destroy( JNIEnv * jni_env ) override;
88 JNI_context const & jni, typelib_TypeDescription * td );
89
90private:
91 virtual ~JNI_interface_type_info() override {}
92};
93
95{
97 // ctor( msg ) for exceptions
98 jmethodID m_exc_ctor;
99 // sorted via typelib member index
100 std::unique_ptr<jfieldID[]> m_fields;
101
102 virtual void destroy( JNIEnv * jni_env ) override;
103 explicit JNI_compound_type_info(
104 JNI_context const & jni, typelib_TypeDescription * td );
105
106private:
107 virtual ~JNI_compound_type_info() override {}
108};
109
111{
113
116
117 JNI_type_info_holder() : m_info( nullptr ) {}
118};
119
120typedef std::unordered_map<
122
124{
125 mutable std::mutex m_mutex;
127
128public:
129 // These two are needed very early by find_class from within the ctor:
132
138
149
157
177
190
197
200
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;
206
207 // noncopyable
208 JNI_info(const JNI_info&) = delete;
209 const JNI_info& operator=(const JNI_info&) = delete;
210
212 JNI_context const & jni,
213 typelib_TypeDescription * type ) const;
215 JNI_context const & jni,
216 typelib_TypeDescriptionReference * type ) const;
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 );
223
224 // get this
225 static JNI_info const * get_jni_info(
227 inline void destroy( JNIEnv * jni_env );
228
229private:
231 JNI_context const & jni, typelib_TypeDescription * td ) const;
232
233 void destruct( JNIEnv * jni_env );
234
235 JNI_info( JNIEnv * jni_env, jobject class_loader,
236 jclass classClass, jmethodID methodForName );
238};
239
240inline void JNI_info::destroy( JNIEnv * jni_env )
241{
242 destruct( jni_env );
243 delete this;
244}
245
247 OStringBuffer * buf, typelib_TypeDescriptionReference * type,
248 bool use_Object_for_type_XInterface, bool use_slashes )
249{
250 switch (type->eTypeClass)
251 {
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;" );
287 }
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;" );
294 }
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;" );
301 }
302 break;
303 case typelib_TypeClass_ENUM:
304 case typelib_TypeClass_STRUCT:
305 case typelib_TypeClass_EXCEPTION:
306 {
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(
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(
321 use_slashes ? s.replace( '.', '/' ) : s,
322 RTL_TEXTENCODING_JAVA_UTF8 ) );
323 }
324 buf->append( ';' );
325 break;
326 }
327 case typelib_TypeClass_SEQUENCE:
328 {
329 buf->append( '[' );
330 TypeDescr td( type );
332 buf, reinterpret_cast<typelib_IndirectTypeDescription *>(td.get())->pType,
333 use_Object_for_type_XInterface, use_slashes );
334 break;
335 }
336 case typelib_TypeClass_INTERFACE:
337 if (use_Object_for_type_XInterface && is_XInterface( type ))
338 {
339 if ( use_slashes ) {
340 buf->append( "Ljava/lang/Object;" );
341 } else {
342 buf->append( "Ljava.lang.Object;" );
343 }
344 }
345 else
346 {
347 OUString const & uno_name =
348 OUString::unacquired( &type->pTypeName );
349 buf->append( 'L' );
350 buf->append(
352 use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
353 RTL_TEXTENCODING_JAVA_UTF8 ) );
354 buf->append( ';' );
355 }
356 break;
357 default:
358 throw BridgeRuntimeError(
359 "unsupported type: " +
360 OUString::unacquired( &type->pTypeName ) );
361 }
362}
363
364}
365
366/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
jmethodID m_method_IEnvironment_getRegisteredInterface
Definition: jni_info.h:178
jfieldID m_field_JNI_proxy_m_td_handle
Definition: jni_info.h:194
jmethodID m_ctor_Short_with_short
Definition: jni_info.h:164
jmethodID m_method_Integer_intValue
Definition: jni_info.h:174
jmethodID m_method_Boolean_booleanValue
Definition: jni_info.h:169
jmethodID m_ctor_Character_with_char
Definition: jni_info.h:161
jmethodID m_method_Short_shortValue
Definition: jni_info.h:176
jmethodID m_ctor_Double_with_double
Definition: jni_info.h:168
void destruct(JNIEnv *jni_env)
Definition: jni_info.cxx:865
jmethodID m_method_Long_longValue
Definition: jni_info.h:175
jclass m_class_Boolean
Definition: jni_info.h:141
jclass m_class_Any
Definition: jni_info.h:152
jclass m_class_UnoRuntime
Definition: jni_info.h:150
jmethodID m_method_Class_getName
Definition: jni_info.h:159
::com::sun::star::uno::Type const & m_RuntimeException_type
Definition: jni_info.h:203
jclass m_class_TypeClass
Definition: jni_info.h:154
static JNI_info const * get_jni_info(rtl::Reference< jvmaccess::UnoVirtualMachine > const &uno_vm)
Definition: jni_info.cxx:906
jfieldID m_field_JNI_proxy_m_oid
Definition: jni_info.h:196
jmethodID m_ctor_Float_with_float
Definition: jni_info.h:167
jfieldID m_field_Type_typeName
Definition: jni_info.h:187
jmethodID m_ctor_Any_with_Type_Object
Definition: jni_info.h:182
jclass m_class_RuntimeException
Definition: jni_info.h:151
jmethodID m_method_JNI_proxy_get_proxy_ctor
Definition: jni_info.h:191
jmethodID m_method_Double_doubleValue
Definition: jni_info.h:172
jfieldID m_field_JNI_proxy_m_type
Definition: jni_info.h:195
jobject m_object_Type_UNSIGNED_LONG
Definition: jni_info.h:136
jmethodID m_method_IEnvironment_registerInterface
Definition: jni_info.h:179
::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td
Definition: jni_info.h:201
jmethodID m_method_Class_forName
Definition: jni_info.h:131
jfieldID m_field_Enum_m_value
Definition: jni_info.h:189
t_str2type m_type_map
Definition: jni_info.h:126
::com::sun::star::uno::Type const & m_Exception_type
Definition: jni_info.h:202
jmethodID m_method_TypeClass_fromInt
Definition: jni_info.h:188
jclass m_class_String
Definition: jni_info.h:148
jobject m_object_Type_UNSIGNED_HYPER
Definition: jni_info.h:137
jmethodID m_method_Float_floatValue
Definition: jni_info.h:173
JNI_info(const JNI_info &)=delete
jmethodID m_ctor_Byte_with_byte
Definition: jni_info.h:163
jmethodID m_ctor_Type_with_Name_TypeClass
Definition: jni_info.h:186
jclass m_class_Type
Definition: jni_info.h:153
jclass m_class_Class
Definition: jni_info.h:130
::com::sun::star::uno::Type const & m_void_type
Definition: jni_info.h:204
jmethodID m_ctor_Boolean_with_boolean
Definition: jni_info.h:162
jfieldID m_field_Any_object
Definition: jni_info.h:184
std::mutex m_mutex
Definition: jni_info.h:125
jclass m_class_Object
Definition: jni_info.h:139
const JNI_info & operator=(const JNI_info &)=delete
jclass m_class_Double
Definition: jni_info.h:147
jobject m_object_java_env
Definition: jni_info.h:133
jclass m_class_JNI_proxy
Definition: jni_info.h:155
jmethodID m_ctor_Long_with_long
Definition: jni_info.h:166
JNI_type_info const * get_type_info(JNI_context const &jni, typelib_TypeDescription *type) const
Definition: jni_info.cxx:381
JNI_interface_type_info const * m_XInterface_type_info
Definition: jni_info.h:205
jfieldID m_field_Any_type
Definition: jni_info.h:183
jclass m_class_Float
Definition: jni_info.h:146
jclass m_class_Short
Definition: jni_info.h:143
jclass m_class_Integer
Definition: jni_info.h:144
jclass m_class_Character
Definition: jni_info.h:140
jmethodID m_method_Byte_byteValue
Definition: jni_info.h:170
jmethodID m_method_JNI_proxy_create
Definition: jni_info.h:192
void destroy(JNIEnv *jni_env)
Definition: jni_info.h:240
static void append_sig(OStringBuffer *buf, typelib_TypeDescriptionReference *type, bool use_Object_for_type_XInterface=true, bool use_slashes=true)
Definition: jni_info.h:246
jmethodID m_ctor_AsynchronousFinalizer
Definition: jni_info.h:198
jmethodID m_ctor_Integer_with_int
Definition: jni_info.h:165
jclass m_class_Long
Definition: jni_info.h:145
jmethodID m_method_UnoRuntime_generateOid
Definition: jni_info.h:180
jmethodID m_ctor_Type_with_Class
Definition: jni_info.h:185
jfieldID m_field_JNI_proxy_m_receiver_handle
Definition: jni_info.h:193
jmethodID m_method_AsynchronousFinalizer_drain
Definition: jni_info.h:199
jclass m_class_Byte
Definition: jni_info.h:142
jobject m_object_Type_UNSIGNED_SHORT
Definition: jni_info.h:135
jmethodID m_method_UnoRuntime_queryInterface
Definition: jni_info.h:181
jobject m_object_Any_VOID
Definition: jni_info.h:134
JNI_type_info const * create_type_info(JNI_context const &jni, typelib_TypeDescription *td) const
Definition: jni_info.cxx:335
jmethodID m_method_Character_charValue
Definition: jni_info.h:171
jclass m_class_AsynchronousFinalizer
Definition: jni_info.h:156
jmethodID m_method_Object_toString
Definition: jni_info.h:158
jmethodID m_method_Throwable_getMessage
Definition: jni_info.h:160
typelib_TypeDescription * get() const
Definition: jni_base.h:240
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:52
int i
bool type_equals(typelib_TypeDescriptionReference *type1, typelib_TypeDescriptionReference *type2)
Definition: jni_info.h:43
std::unordered_map< OUString, JNI_type_info_holder > t_str2type
Definition: jni_info.h:121
bool is_XInterface(typelib_TypeDescriptionReference *type)
Definition: jni_info.h:56
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
std::unique_ptr< jfieldID[]> m_fields
Definition: jni_info.h:100
virtual void destroy(JNIEnv *jni_env) override
Definition: jni_info.cxx:215
virtual ~JNI_compound_type_info() override
Definition: jni_info.h:107
JNI_compound_type_info(JNI_context const &jni, typelib_TypeDescription *td)
Definition: jni_info.cxx:223
JNI_type_info const * m_base
Definition: jni_info.h:96
virtual ~JNI_interface_type_info() override
Definition: jni_info.h:91
JNI_interface_type_info(JNI_context const &jni, typelib_TypeDescription *td)
Definition: jni_info.cxx:66
virtual void destroy(JNIEnv *jni_env) override
Definition: jni_info.cxx:56
std::unique_ptr< jmethodID[]> m_methods
Definition: jni_info.h:84
JNI_type_info * m_info
Definition: jni_info.h:112
const JNI_type_info_holder & operator=(const JNI_type_info_holder &)=delete
JNI_type_info_holder(const JNI_type_info_holder &)=delete
virtual ~JNI_type_info()
Definition: jni_info.h:74
void destruct(JNIEnv *jni_env)
Definition: jni_info.h:72
::com::sun::star::uno::TypeDescription m_td
Definition: jni_info.h:67
const JNI_type_info & operator=(const JNI_type_info &)=delete
JNI_type_info(const JNI_type_info &)=delete
virtual void destroy(JNIEnv *jni_env)=0
ResultType type