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_BRIDGES_SOURCE_JNI_UNO_JNI_INFO_H
21 : #define INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_INFO_H
22 :
23 : #include <sal/config.h>
24 :
25 : #include <boost/noncopyable.hpp>
26 : #include <boost/unordered_map.hpp>
27 :
28 : #include "jni_base.h"
29 :
30 : #include "osl/mutex.hxx"
31 : #include "rtl/ref.hxx"
32 : #include "rtl/ustring.hxx"
33 : #include "rtl/strbuf.hxx"
34 :
35 : #include "uno/environment.h"
36 : #include "typelib/typedescription.hxx"
37 :
38 : #include "com/sun/star/uno/Type.hxx"
39 :
40 : namespace jvmaccess { class UnoVirtualMachine; }
41 :
42 : namespace jni_uno
43 : {
44 :
45 5 : inline bool type_equals(
46 : typelib_TypeDescriptionReference * type1,
47 : typelib_TypeDescriptionReference * type2 )
48 : {
49 5 : if (type1 == type2)
50 1 : return true;
51 : OUString const & name1 =
52 4 : OUString::unacquired( &type1->pTypeName );
53 : OUString const & name2 =
54 4 : OUString::unacquired( &type2->pTypeName );
55 4 : return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
56 : }
57 :
58 216 : inline bool is_XInterface( typelib_TypeDescriptionReference * type )
59 : {
60 430 : return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
61 430 : OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
62 : }
63 :
64 : struct JNI_type_info: private boost::noncopyable
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 : struct JNI_interface_type_info : public JNI_type_info
79 : {
80 : jobject m_proxy_ctor; // proxy ctor
81 : jobject m_type;
82 : // sorted via typelib function index
83 : jmethodID * m_methods;
84 :
85 : virtual void destroy( JNIEnv * jni_env ) SAL_OVERRIDE;
86 : explicit JNI_interface_type_info(
87 : JNI_context const & jni, typelib_TypeDescription * td );
88 :
89 : private:
90 0 : virtual ~JNI_interface_type_info() {}
91 : };
92 :
93 : struct JNI_compound_type_info : public JNI_type_info
94 : {
95 : JNI_type_info const * m_base;
96 : // ctor( msg ) for exceptions
97 : jmethodID m_exc_ctor;
98 : // sorted via typelib member index
99 : jfieldID * m_fields;
100 :
101 : virtual void destroy( JNIEnv * jni_env ) SAL_OVERRIDE;
102 : explicit JNI_compound_type_info(
103 : JNI_context const & jni, typelib_TypeDescription * td );
104 :
105 : private:
106 0 : virtual ~JNI_compound_type_info() {}
107 : };
108 :
109 : struct JNI_type_info_holder: private boost::noncopyable
110 : {
111 : JNI_type_info * m_info;
112 40 : inline JNI_type_info_holder()
113 40 : : m_info( 0 )
114 40 : {}
115 : };
116 :
117 : typedef ::boost::unordered_map<
118 : OUString, JNI_type_info_holder, OUStringHash > t_str2type;
119 :
120 : class JNI_info: private boost::noncopyable
121 : {
122 : mutable ::osl::Mutex m_mutex;
123 : mutable t_str2type m_type_map;
124 :
125 : public:
126 : // These two are needed very early by find_class from within the ctor:
127 : jclass m_class_Class;
128 : jmethodID m_method_Class_forName;
129 :
130 : jobject m_object_java_env;
131 : jobject m_object_Any_VOID;
132 : jobject m_object_Type_UNSIGNED_SHORT;
133 : jobject m_object_Type_UNSIGNED_LONG;
134 : jobject m_object_Type_UNSIGNED_HYPER;
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 : jclass m_class_AsynchronousFinalizer;
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 : jmethodID m_method_IEnvironment_getRegisteredInterface;
176 : jmethodID m_method_IEnvironment_registerInterface;
177 : jmethodID m_method_UnoRuntime_generateOid;
178 : jmethodID m_method_UnoRuntime_queryInterface;
179 : jmethodID m_ctor_Any_with_Type_Object;
180 : jfieldID m_field_Any__type;
181 : jfieldID m_field_Any__object;
182 : jmethodID m_ctor_Type_with_Class;
183 : jmethodID m_ctor_Type_with_Name_TypeClass;
184 : jfieldID m_field_Type__typeName;
185 : jmethodID m_method_TypeClass_fromInt;
186 : jfieldID m_field_Enum_m_value;
187 :
188 : jmethodID m_method_JNI_proxy_get_proxy_ctor;
189 : jmethodID m_method_JNI_proxy_create;
190 : jfieldID m_field_JNI_proxy_m_receiver_handle;
191 : jfieldID m_field_JNI_proxy_m_td_handle;
192 : jfieldID m_field_JNI_proxy_m_type;
193 : jfieldID m_field_JNI_proxy_m_oid;
194 :
195 : jmethodID m_ctor_AsynchronousFinalizer;
196 : jmethodID m_method_AsynchronousFinalizer_drain;
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 : JNI_interface_type_info const * m_XInterface_type_info;
203 :
204 : JNI_type_info const * get_type_info(
205 : JNI_context const & jni,
206 : typelib_TypeDescription * type ) const;
207 : JNI_type_info const * get_type_info(
208 : JNI_context const & jni,
209 : typelib_TypeDescriptionReference * type ) const;
210 : JNI_type_info const * get_type_info(
211 : JNI_context const & jni,
212 : OUString const & uno_name ) const;
213 : inline static void append_sig(
214 : OStringBuffer * buf, typelib_TypeDescriptionReference * type,
215 : bool use_Object_for_type_XInterface = true, bool use_slashes = true );
216 :
217 : // get this
218 : static JNI_info const * get_jni_info(
219 : rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
220 : inline void destroy( JNIEnv * jni_env );
221 :
222 : private:
223 : JNI_type_info const * create_type_info(
224 : JNI_context const & jni, typelib_TypeDescription * td ) const;
225 :
226 : void destruct( JNIEnv * jni_env );
227 :
228 : JNI_info( JNIEnv * jni_env, jobject class_loader,
229 : jclass classClass, jmethodID methodForName );
230 0 : inline ~JNI_info() {}
231 : };
232 :
233 0 : inline void JNI_info::destroy( JNIEnv * jni_env )
234 : {
235 0 : destruct( jni_env );
236 0 : delete this;
237 0 : }
238 :
239 292 : inline void JNI_info::append_sig(
240 : OStringBuffer * buf, typelib_TypeDescriptionReference * type,
241 : bool use_Object_for_type_XInterface, bool use_slashes )
242 : {
243 292 : switch (type->eTypeClass)
244 : {
245 : case typelib_TypeClass_VOID:
246 37 : buf->append( 'V' );
247 37 : break;
248 : case typelib_TypeClass_CHAR:
249 0 : buf->append( 'C' );
250 0 : break;
251 : case typelib_TypeClass_BOOLEAN:
252 12 : buf->append( 'Z' );
253 12 : break;
254 : case typelib_TypeClass_BYTE:
255 4 : buf->append( 'B' );
256 4 : break;
257 : case typelib_TypeClass_SHORT:
258 : case typelib_TypeClass_UNSIGNED_SHORT:
259 2 : buf->append( 'S' );
260 2 : break;
261 : case typelib_TypeClass_LONG:
262 : case typelib_TypeClass_UNSIGNED_LONG:
263 4 : buf->append( 'I' );
264 4 : break;
265 : case typelib_TypeClass_HYPER:
266 : case typelib_TypeClass_UNSIGNED_HYPER:
267 0 : buf->append( 'J' );
268 0 : break;
269 : case typelib_TypeClass_FLOAT:
270 0 : buf->append( 'F' );
271 0 : break;
272 : case typelib_TypeClass_DOUBLE:
273 0 : buf->append( 'D' );
274 0 : break;
275 : case typelib_TypeClass_STRING:
276 90 : if ( use_slashes ) {
277 90 : buf->append( "Ljava/lang/String;" );
278 : } else {
279 0 : buf->append( "Ljava.lang.String;" );
280 : }
281 90 : break;
282 : case typelib_TypeClass_TYPE:
283 2 : if ( use_slashes ) {
284 2 : buf->append( "Lcom/sun/star/uno/Type;" );
285 : } else {
286 0 : buf->append( "Lcom.sun.star.uno.Type;" );
287 : }
288 2 : break;
289 : case typelib_TypeClass_ANY:
290 32 : if ( use_slashes ) {
291 32 : buf->append( "Ljava/lang/Object;" );
292 : } else {
293 0 : buf->append( "Ljava.lang.Object;" );
294 : }
295 32 : break;
296 : case typelib_TypeClass_ENUM:
297 : case typelib_TypeClass_STRUCT:
298 : case typelib_TypeClass_EXCEPTION:
299 : {
300 : OUString const & uno_name =
301 3 : OUString::unacquired( &type->pTypeName );
302 3 : buf->append( 'L' );
303 : // Erase type arguments of instantiated polymorphic struct types:
304 3 : sal_Int32 i = uno_name.indexOf( '<' );
305 3 : if ( i < 0 ) {
306 : buf->append(
307 : OUStringToOString(
308 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
309 3 : RTL_TEXTENCODING_JAVA_UTF8 ) );
310 : } else {
311 0 : OUString s( uno_name.copy( 0, i ) );
312 : buf->append(
313 : OUStringToOString(
314 : use_slashes ? s.replace( '.', '/' ) : s,
315 0 : RTL_TEXTENCODING_JAVA_UTF8 ) );
316 : }
317 3 : buf->append( ';' );
318 3 : break;
319 : }
320 : case typelib_TypeClass_SEQUENCE:
321 : {
322 41 : buf->append( '[' );
323 41 : TypeDescr td( type );
324 : append_sig(
325 41 : buf, reinterpret_cast<typelib_IndirectTypeDescription *>(td.get())->pType,
326 82 : use_Object_for_type_XInterface, use_slashes );
327 41 : break;
328 : }
329 : case typelib_TypeClass_INTERFACE:
330 65 : if (use_Object_for_type_XInterface && is_XInterface( type ))
331 : {
332 21 : if ( use_slashes ) {
333 21 : buf->append( "Ljava/lang/Object;" );
334 : } else {
335 0 : buf->append( "Ljava.lang.Object;" );
336 : }
337 : }
338 : else
339 : {
340 : OUString const & uno_name =
341 44 : OUString::unacquired( &type->pTypeName );
342 44 : buf->append( 'L' );
343 : buf->append(
344 : OUStringToOString(
345 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
346 44 : RTL_TEXTENCODING_JAVA_UTF8 ) );
347 44 : buf->append( ';' );
348 : }
349 65 : break;
350 : default:
351 : throw BridgeRuntimeError(
352 0 : "unsupported type: " +
353 0 : OUString::unacquired( &type->pTypeName ) );
354 : }
355 292 : }
356 :
357 : }
358 :
359 : #endif
360 :
361 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|