1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/config.h>
24 #include <unordered_map>
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
; }
43 inline bool type_equals(
44 typelib_TypeDescriptionReference
* type1
,
45 typelib_TypeDescriptionReference
* type2
)
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");
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
;
70 virtual void destroy( JNIEnv
* jni_env
) = 0;
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
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
);
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
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
);
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
;
125 mutable std::mutex m_mutex
;
126 mutable t_str2type m_type_map
;
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
;
143 jclass m_class_Short
;
144 jclass m_class_Integer
;
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
;
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
;
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 );
225 static JNI_info
const * get_jni_info(
226 rtl::Reference
< jvmaccess::UnoVirtualMachine
> const & uno_vm
);
227 inline void destroy( JNIEnv
* jni_env
);
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
);
240 inline void JNI_info::destroy( JNIEnv
* jni_env
)
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
:
255 case typelib_TypeClass_CHAR
:
258 case typelib_TypeClass_BOOLEAN
:
261 case typelib_TypeClass_BYTE
:
264 case typelib_TypeClass_SHORT
:
265 case typelib_TypeClass_UNSIGNED_SHORT
:
268 case typelib_TypeClass_LONG
:
269 case typelib_TypeClass_UNSIGNED_LONG
:
272 case typelib_TypeClass_HYPER
:
273 case typelib_TypeClass_UNSIGNED_HYPER
:
276 case typelib_TypeClass_FLOAT
:
279 case typelib_TypeClass_DOUBLE
:
282 case typelib_TypeClass_STRING
:
284 buf
->append( "Ljava/lang/String;" );
286 buf
->append( "Ljava.lang.String;" );
289 case typelib_TypeClass_TYPE
:
291 buf
->append( "Lcom/sun/star/uno/Type;" );
293 buf
->append( "Lcom.sun.star.uno.Type;" );
296 case typelib_TypeClass_ANY
:
298 buf
->append( "Ljava/lang/Object;" );
300 buf
->append( "Ljava.lang.Object;" );
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
);
310 // Erase type arguments of instantiated polymorphic struct types:
311 sal_Int32 i
= uno_name
.indexOf( '<' );
315 use_slashes
? uno_name
.replace( '.', '/' ) : uno_name
,
316 RTL_TEXTENCODING_JAVA_UTF8
) );
318 OUString
s( uno_name
.copy( 0, i
) );
321 use_slashes
? s
.replace( '.', '/' ) : s
,
322 RTL_TEXTENCODING_JAVA_UTF8
) );
327 case typelib_TypeClass_SEQUENCE
:
330 TypeDescr
td( type
);
332 buf
, reinterpret_cast<typelib_IndirectTypeDescription
*>(td
.get())->pType
,
333 use_Object_for_type_XInterface
, use_slashes
);
336 case typelib_TypeClass_INTERFACE
:
337 if (use_Object_for_type_XInterface
&& is_XInterface( type
))
340 buf
->append( "Ljava/lang/Object;" );
342 buf
->append( "Ljava.lang.Object;" );
347 OUString
const & uno_name
=
348 OUString::unacquired( &type
->pTypeName
);
352 use_slashes
? uno_name
.replace( '.', '/' ) : uno_name
,
353 RTL_TEXTENCODING_JAVA_UTF8
) );
358 throw BridgeRuntimeError(
359 "unsupported type: " +
360 OUString::unacquired( &type
->pTypeName
) );
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */