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 <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 10 : inline bool type_equals(
43 : typelib_TypeDescriptionReference * type1,
44 : typelib_TypeDescriptionReference * type2 )
45 : {
46 10 : if (type1 == type2)
47 2 : return true;
48 : OUString const & name1 =
49 8 : OUString::unacquired( &type1->pTypeName );
50 : OUString const & name2 =
51 8 : OUString::unacquired( &type2->pTypeName );
52 8 : return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
53 : }
54 :
55 398 : inline bool is_XInterface( typelib_TypeDescriptionReference * type )
56 : {
57 792 : return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
58 792 : OUString::unacquired( &type->pTypeName ) == "com.sun.star.uno.XInterface");
59 : }
60 :
61 : struct JNI_type_info
62 : {
63 : ::com::sun::star::uno::TypeDescription m_td;
64 : jclass m_class;
65 :
66 : virtual void destroy( JNIEnv * jni_env ) = 0;
67 : protected:
68 0 : inline void destruct( JNIEnv * jni_env )
69 0 : { jni_env->DeleteGlobalRef( m_class ); }
70 0 : virtual inline ~JNI_type_info() {}
71 : explicit JNI_type_info(
72 : JNI_context const & jni, typelib_TypeDescription * td );
73 : };
74 :
75 : struct JNI_interface_type_info : public JNI_type_info
76 : {
77 : jobject m_proxy_ctor; // proxy ctor
78 : jobject m_type;
79 : // sorted via typelib function index
80 : jmethodID * m_methods;
81 :
82 : virtual void destroy( JNIEnv * jni_env ) SAL_OVERRIDE;
83 : explicit JNI_interface_type_info(
84 : JNI_context const & jni, typelib_TypeDescription * td );
85 :
86 : private:
87 0 : virtual ~JNI_interface_type_info() {}
88 : };
89 :
90 : struct JNI_compound_type_info : public JNI_type_info
91 : {
92 : JNI_type_info const * m_base;
93 : // ctor( msg ) for exceptions
94 : jmethodID m_exc_ctor;
95 : // sorted via typelib member index
96 : jfieldID * m_fields;
97 :
98 : virtual void destroy( JNIEnv * jni_env ) SAL_OVERRIDE;
99 : explicit JNI_compound_type_info(
100 : JNI_context const & jni, typelib_TypeDescription * td );
101 :
102 : private:
103 0 : virtual ~JNI_compound_type_info() {}
104 : };
105 :
106 : struct JNI_type_info_holder
107 : {
108 : JNI_type_info * m_info;
109 76 : inline JNI_type_info_holder()
110 76 : : m_info( 0 )
111 76 : {}
112 : };
113 :
114 : typedef ::boost::unordered_map<
115 : OUString, JNI_type_info_holder, OUStringHash > t_str2type;
116 :
117 : class JNI_info
118 : {
119 : mutable ::osl::Mutex m_mutex;
120 : mutable t_str2type m_type_map;
121 :
122 : public:
123 : // These two are needed very early by find_class from within the ctor:
124 : jclass m_class_Class;
125 : jmethodID m_method_Class_forName;
126 :
127 : jobject m_object_java_env;
128 : jobject m_object_Any_VOID;
129 : jobject m_object_Type_UNSIGNED_SHORT;
130 : jobject m_object_Type_UNSIGNED_LONG;
131 : jobject m_object_Type_UNSIGNED_HYPER;
132 :
133 : jclass m_class_Object;
134 : jclass m_class_Character;
135 : jclass m_class_Boolean;
136 : jclass m_class_Byte;
137 : jclass m_class_Short;
138 : jclass m_class_Integer;
139 : jclass m_class_Long;
140 : jclass m_class_Float;
141 : jclass m_class_Double;
142 : jclass m_class_String;
143 :
144 : jclass m_class_UnoRuntime;
145 : jclass m_class_RuntimeException;
146 : jclass m_class_Any;
147 : jclass m_class_Type;
148 : jclass m_class_TypeClass;
149 : jclass m_class_JNI_proxy;
150 :
151 : jmethodID m_method_Object_toString;
152 : jmethodID m_method_Class_getName;
153 : jmethodID m_method_Throwable_getMessage;
154 : jmethodID m_ctor_Character_with_char;
155 : jmethodID m_ctor_Boolean_with_boolean;
156 : jmethodID m_ctor_Byte_with_byte;
157 : jmethodID m_ctor_Short_with_short;
158 : jmethodID m_ctor_Integer_with_int;
159 : jmethodID m_ctor_Long_with_long;
160 : jmethodID m_ctor_Float_with_float;
161 : jmethodID m_ctor_Double_with_double;
162 : jmethodID m_method_Boolean_booleanValue;
163 : jmethodID m_method_Byte_byteValue;
164 : jmethodID m_method_Character_charValue;
165 : jmethodID m_method_Double_doubleValue;
166 : jmethodID m_method_Float_floatValue;
167 : jmethodID m_method_Integer_intValue;
168 : jmethodID m_method_Long_longValue;
169 : jmethodID m_method_Short_shortValue;
170 :
171 : jmethodID m_method_IEnvironment_getRegisteredInterface;
172 : jmethodID m_method_IEnvironment_registerInterface;
173 : jmethodID m_method_UnoRuntime_generateOid;
174 : jmethodID m_method_UnoRuntime_queryInterface;
175 : jmethodID m_ctor_Any_with_Type_Object;
176 : jfieldID m_field_Any__type;
177 : jfieldID m_field_Any__object;
178 : jmethodID m_ctor_Type_with_Class;
179 : jmethodID m_ctor_Type_with_Name_TypeClass;
180 : jfieldID m_field_Type__typeName;
181 : jmethodID m_method_TypeClass_fromInt;
182 : jfieldID m_field_Enum_m_value;
183 :
184 : jmethodID m_method_JNI_proxy_get_proxy_ctor;
185 : jmethodID m_method_JNI_proxy_create;
186 : jfieldID m_field_JNI_proxy_m_receiver_handle;
187 : jfieldID m_field_JNI_proxy_m_td_handle;
188 : jfieldID m_field_JNI_proxy_m_type;
189 : jfieldID m_field_JNI_proxy_m_oid;
190 :
191 : ::com::sun::star::uno::TypeDescription m_XInterface_queryInterface_td;
192 : ::com::sun::star::uno::Type const & m_Exception_type;
193 : ::com::sun::star::uno::Type const & m_RuntimeException_type;
194 : ::com::sun::star::uno::Type const & m_void_type;
195 : JNI_interface_type_info const * m_XInterface_type_info;
196 :
197 : JNI_type_info const * get_type_info(
198 : JNI_context const & jni,
199 : typelib_TypeDescription * type ) const;
200 : JNI_type_info const * get_type_info(
201 : JNI_context const & jni,
202 : typelib_TypeDescriptionReference * type ) const;
203 : JNI_type_info const * get_type_info(
204 : JNI_context const & jni,
205 : OUString const & uno_name ) const;
206 : inline static void append_sig(
207 : OStringBuffer * buf, typelib_TypeDescriptionReference * type,
208 : bool use_Object_for_type_XInterface = true, bool use_slashes = true );
209 :
210 : // get this
211 : static JNI_info const * get_jni_info(
212 : rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm );
213 : inline void destroy( JNIEnv * jni_env );
214 :
215 : private:
216 : JNI_type_info const * create_type_info(
217 : JNI_context const & jni, typelib_TypeDescription * td ) const;
218 :
219 : void destruct( JNIEnv * jni_env );
220 :
221 : JNI_info( JNIEnv * jni_env, jobject class_loader,
222 : jclass classClass, jmethodID methodForName );
223 0 : inline ~JNI_info() {}
224 : };
225 :
226 0 : inline void JNI_info::destroy( JNIEnv * jni_env )
227 : {
228 0 : destruct( jni_env );
229 0 : delete this;
230 0 : }
231 :
232 570 : inline void JNI_info::append_sig(
233 : OStringBuffer * buf, typelib_TypeDescriptionReference * type,
234 : bool use_Object_for_type_XInterface, bool use_slashes )
235 : {
236 570 : switch (type->eTypeClass)
237 : {
238 : case typelib_TypeClass_VOID:
239 74 : buf->append( 'V' );
240 74 : break;
241 : case typelib_TypeClass_CHAR:
242 0 : buf->append( 'C' );
243 0 : break;
244 : case typelib_TypeClass_BOOLEAN:
245 22 : buf->append( 'Z' );
246 22 : break;
247 : case typelib_TypeClass_BYTE:
248 8 : buf->append( 'B' );
249 8 : break;
250 : case typelib_TypeClass_SHORT:
251 : case typelib_TypeClass_UNSIGNED_SHORT:
252 4 : buf->append( 'S' );
253 4 : break;
254 : case typelib_TypeClass_LONG:
255 : case typelib_TypeClass_UNSIGNED_LONG:
256 8 : buf->append( 'I' );
257 8 : break;
258 : case typelib_TypeClass_HYPER:
259 : case typelib_TypeClass_UNSIGNED_HYPER:
260 0 : buf->append( 'J' );
261 0 : break;
262 : case typelib_TypeClass_FLOAT:
263 0 : buf->append( 'F' );
264 0 : break;
265 : case typelib_TypeClass_DOUBLE:
266 0 : buf->append( 'D' );
267 0 : break;
268 : case typelib_TypeClass_STRING:
269 170 : if ( use_slashes ) {
270 170 : buf->append( "Ljava/lang/String;" );
271 : } else {
272 0 : buf->append( "Ljava.lang.String;" );
273 : }
274 170 : break;
275 : case typelib_TypeClass_TYPE:
276 4 : if ( use_slashes ) {
277 4 : buf->append( "Lcom/sun/star/uno/Type;" );
278 : } else {
279 0 : buf->append( "Lcom.sun.star.uno.Type;" );
280 : }
281 4 : break;
282 : case typelib_TypeClass_ANY:
283 64 : if ( use_slashes ) {
284 64 : buf->append( "Ljava/lang/Object;" );
285 : } else {
286 0 : buf->append( "Ljava.lang.Object;" );
287 : }
288 64 : break;
289 : case typelib_TypeClass_ENUM:
290 : case typelib_TypeClass_STRUCT:
291 : case typelib_TypeClass_EXCEPTION:
292 : {
293 : OUString const & uno_name =
294 6 : OUString::unacquired( &type->pTypeName );
295 6 : buf->append( 'L' );
296 : // Erase type arguments of instantiated polymorphic struct types:
297 6 : sal_Int32 i = uno_name.indexOf( '<' );
298 6 : if ( i < 0 ) {
299 : buf->append(
300 : OUStringToOString(
301 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
302 6 : RTL_TEXTENCODING_JAVA_UTF8 ) );
303 : } else {
304 0 : OUString s( uno_name.copy( 0, i ) );
305 : buf->append(
306 : OUStringToOString(
307 : use_slashes ? s.replace( '.', '/' ) : s,
308 0 : RTL_TEXTENCODING_JAVA_UTF8 ) );
309 : }
310 6 : buf->append( ';' );
311 6 : break;
312 : }
313 : case typelib_TypeClass_SEQUENCE:
314 : {
315 80 : buf->append( '[' );
316 80 : TypeDescr td( type );
317 : append_sig(
318 80 : buf, ((typelib_IndirectTypeDescription *)td.get())->pType,
319 160 : use_Object_for_type_XInterface, use_slashes );
320 80 : break;
321 : }
322 : case typelib_TypeClass_INTERFACE:
323 130 : if (use_Object_for_type_XInterface && is_XInterface( type ))
324 : {
325 42 : if ( use_slashes ) {
326 42 : buf->append( "Ljava/lang/Object;" );
327 : } else {
328 0 : buf->append( "Ljava.lang.Object;" );
329 : }
330 : }
331 : else
332 : {
333 : OUString const & uno_name =
334 88 : OUString::unacquired( &type->pTypeName );
335 88 : buf->append( 'L' );
336 : buf->append(
337 : OUStringToOString(
338 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
339 88 : RTL_TEXTENCODING_JAVA_UTF8 ) );
340 88 : buf->append( ';' );
341 : }
342 130 : break;
343 : default:
344 : throw BridgeRuntimeError(
345 0 : "unsupported type: " +
346 0 : OUString::unacquired( &type->pTypeName ) );
347 : }
348 570 : }
349 :
350 : }
351 :
352 : #endif
353 :
354 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|