Line data Source code
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 : #ifndef INCLUDED_JNI_INFO_H
21 : #define INCLUDED_JNI_INFO_H
22 :
23 : #include <boost/unordered_map.hpp>
24 :
25 : #include "jni_base.h"
26 :
27 : #include "osl/mutex.hxx"
28 : #include "rtl/ref.hxx"
29 : #include "rtl/ustring.hxx"
30 : #include "rtl/strbuf.hxx"
31 :
32 : #include "uno/environment.h"
33 : #include "typelib/typedescription.hxx"
34 :
35 : #include "com/sun/star/uno/Type.hxx"
36 :
37 : namespace jvmaccess { class UnoVirtualMachine; }
38 :
39 : namespace jni_uno
40 : {
41 :
42 : //------------------------------------------------------------------------------
43 0 : inline bool type_equals(
44 : typelib_TypeDescriptionReference * type1,
45 : typelib_TypeDescriptionReference * type2 )
46 : {
47 0 : if (type1 == type2)
48 0 : return true;
49 : ::rtl::OUString const & name1 =
50 0 : ::rtl::OUString::unacquired( &type1->pTypeName );
51 : ::rtl::OUString const & name2 =
52 0 : ::rtl::OUString::unacquired( &type2->pTypeName );
53 0 : return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
54 : }
55 :
56 : //------------------------------------------------------------------------------
57 0 : inline bool is_XInterface( typelib_TypeDescriptionReference * type )
58 : {
59 : return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
60 0 : ::rtl::OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
61 : }
62 :
63 : //==============================================================================
64 : struct JNI_type_info
65 : {
66 : ::com::sun::star::uno::TypeDescription m_td;
67 : jclass m_class;
68 :
69 : virtual void destroy( JNIEnv * jni_env ) = 0;
70 : protected:
71 0 : inline void destruct( JNIEnv * jni_env )
72 0 : { jni_env->DeleteGlobalRef( m_class ); }
73 0 : virtual inline ~JNI_type_info() {}
74 : explicit JNI_type_info(
75 : JNI_context const & jni, typelib_TypeDescription * td );
76 : };
77 :
78 : //==============================================================================
79 0 : struct JNI_interface_type_info : public JNI_type_info
80 : {
81 : jobject m_proxy_ctor; // proxy ctor
82 : jobject m_type;
83 : // sorted via typelib function index
84 : jmethodID * m_methods;
85 :
86 : virtual void destroy( JNIEnv * jni_env );
87 : explicit JNI_interface_type_info(
88 : JNI_context const & jni, typelib_TypeDescription * td );
89 : };
90 :
91 : //==============================================================================
92 0 : struct JNI_compound_type_info : public JNI_type_info
93 : {
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;
99 :
100 : virtual void destroy( JNIEnv * jni_env );
101 : explicit JNI_compound_type_info(
102 : JNI_context const & jni, typelib_TypeDescription * td );
103 : };
104 :
105 : //==============================================================================
106 : struct JNI_type_info_holder
107 : {
108 : JNI_type_info * m_info;
109 0 : inline JNI_type_info_holder()
110 0 : : m_info( 0 )
111 0 : {}
112 : };
113 :
114 : typedef ::boost::unordered_map<
115 : ::rtl::OUString, JNI_type_info_holder, ::rtl::OUStringHash > t_str2type;
116 :
117 : //==============================================================================
118 : class JNI_info
119 : {
120 : mutable ::osl::Mutex m_mutex;
121 : mutable t_str2type m_type_map;
122 :
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;
127 :
128 : //
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;
134 :
135 : //
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;
146 :
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;
153 :
154 : //
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;
174 :
175 : //
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;
188 :
189 : //
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;
196 :
197 : //
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;
202 : //
203 : JNI_interface_type_info const * m_XInterface_type_info;
204 :
205 : //
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 : ::rtl::OUString const & uno_name ) const;
215 : //
216 : inline static void append_sig(
217 : ::rtl::OStringBuffer * buf, typelib_TypeDescriptionReference * type,
218 : bool use_Object_for_type_XInterface = true, bool use_slashes = true );
219 :
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 );
224 :
225 : private:
226 : JNI_type_info const * create_type_info(
227 : JNI_context const & jni, typelib_TypeDescription * td ) const;
228 :
229 : void destruct( JNIEnv * jni_env );
230 :
231 : JNI_info( JNIEnv * jni_env, jobject class_loader,
232 : jclass classClass, jmethodID methodForName );
233 0 : inline ~JNI_info() {}
234 : };
235 :
236 : //______________________________________________________________________________
237 0 : inline void JNI_info::destroy( JNIEnv * jni_env )
238 : {
239 0 : destruct( jni_env );
240 0 : delete this;
241 0 : }
242 :
243 : //______________________________________________________________________________
244 0 : inline void JNI_info::append_sig(
245 : ::rtl::OStringBuffer * buf, typelib_TypeDescriptionReference * type,
246 : bool use_Object_for_type_XInterface, bool use_slashes )
247 : {
248 0 : switch (type->eTypeClass)
249 : {
250 : case typelib_TypeClass_VOID:
251 0 : buf->append( 'V' );
252 0 : break;
253 : case typelib_TypeClass_CHAR:
254 0 : buf->append( 'C' );
255 0 : break;
256 : case typelib_TypeClass_BOOLEAN:
257 0 : buf->append( 'Z' );
258 0 : break;
259 : case typelib_TypeClass_BYTE:
260 0 : buf->append( 'B' );
261 0 : break;
262 : case typelib_TypeClass_SHORT:
263 : case typelib_TypeClass_UNSIGNED_SHORT:
264 0 : buf->append( 'S' );
265 0 : break;
266 : case typelib_TypeClass_LONG:
267 : case typelib_TypeClass_UNSIGNED_LONG:
268 0 : buf->append( 'I' );
269 0 : break;
270 : case typelib_TypeClass_HYPER:
271 : case typelib_TypeClass_UNSIGNED_HYPER:
272 0 : buf->append( 'J' );
273 0 : break;
274 : case typelib_TypeClass_FLOAT:
275 0 : buf->append( 'F' );
276 0 : break;
277 : case typelib_TypeClass_DOUBLE:
278 0 : buf->append( 'D' );
279 0 : break;
280 : case typelib_TypeClass_STRING:
281 0 : if ( use_slashes ) {
282 0 : buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/String;") );
283 : } else {
284 0 : buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.String;") );
285 : }
286 0 : break;
287 : case typelib_TypeClass_TYPE:
288 0 : if ( use_slashes ) {
289 : buf->append(
290 0 : RTL_CONSTASCII_STRINGPARAM("Lcom/sun/star/uno/Type;") );
291 : } else {
292 : buf->append(
293 0 : RTL_CONSTASCII_STRINGPARAM("Lcom.sun.star.uno.Type;") );
294 : }
295 0 : break;
296 : case typelib_TypeClass_ANY:
297 0 : if ( use_slashes ) {
298 0 : buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;") );
299 : } else {
300 0 : buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.Object;") );
301 : }
302 0 : break;
303 : case typelib_TypeClass_ENUM:
304 : case typelib_TypeClass_STRUCT:
305 : case typelib_TypeClass_EXCEPTION:
306 : {
307 : ::rtl::OUString const & uno_name =
308 0 : ::rtl::OUString::unacquired( &type->pTypeName );
309 0 : buf->append( 'L' );
310 : // Erase type arguments of instantiated polymorphic struct types:
311 0 : sal_Int32 i = uno_name.indexOf( '<' );
312 0 : if ( i < 0 ) {
313 : buf->append(
314 : ::rtl::OUStringToOString(
315 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
316 0 : RTL_TEXTENCODING_JAVA_UTF8 ) );
317 : } else {
318 0 : rtl::OUString s( uno_name.copy( 0, i ) );
319 : buf->append(
320 : ::rtl::OUStringToOString(
321 : use_slashes ? s.replace( '.', '/' ) : s,
322 0 : RTL_TEXTENCODING_JAVA_UTF8 ) );
323 : }
324 0 : buf->append( ';' );
325 0 : break;
326 : }
327 : case typelib_TypeClass_SEQUENCE:
328 : {
329 0 : buf->append( '[' );
330 0 : TypeDescr td( type );
331 : append_sig(
332 0 : buf, ((typelib_IndirectTypeDescription *)td.get())->pType,
333 0 : use_Object_for_type_XInterface, use_slashes );
334 0 : break;
335 : }
336 : case typelib_TypeClass_INTERFACE:
337 0 : if (use_Object_for_type_XInterface && is_XInterface( type ))
338 : {
339 0 : if ( use_slashes ) {
340 0 : buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;") );
341 : } else {
342 0 : buf->append( RTL_CONSTASCII_STRINGPARAM("Ljava.lang.Object;") );
343 : }
344 : }
345 : else
346 : {
347 : ::rtl::OUString const & uno_name =
348 0 : ::rtl::OUString::unacquired( &type->pTypeName );
349 0 : buf->append( 'L' );
350 : buf->append(
351 : ::rtl::OUStringToOString(
352 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
353 0 : RTL_TEXTENCODING_JAVA_UTF8 ) );
354 0 : buf->append( ';' );
355 : }
356 0 : break;
357 : default:
358 : throw BridgeRuntimeError(
359 : OUSTR("unsupported type: ") +
360 0 : ::rtl::OUString::unacquired( &type->pTypeName ) );
361 : }
362 0 : }
363 :
364 : }
365 :
366 : #endif
367 :
368 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|