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 5 : inline bool type_equals(
44 : typelib_TypeDescriptionReference * type1,
45 : typelib_TypeDescriptionReference * type2 )
46 : {
47 5 : if (type1 == type2)
48 1 : return true;
49 : OUString const & name1 =
50 4 : OUString::unacquired( &type1->pTypeName );
51 : OUString const & name2 =
52 4 : OUString::unacquired( &type2->pTypeName );
53 4 : return ((type1->eTypeClass == type2->eTypeClass) && name1.equals( name2 ));
54 : }
55 :
56 : //------------------------------------------------------------------------------
57 165 : inline bool is_XInterface( typelib_TypeDescriptionReference * type )
58 : {
59 328 : return ((typelib_TypeClass_INTERFACE == type->eTypeClass) &&
60 328 : 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 31 : inline JNI_type_info_holder()
110 31 : : m_info( 0 )
111 31 : {}
112 : };
113 :
114 : typedef ::boost::unordered_map<
115 : OUString, JNI_type_info_holder, 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 : OUString const & uno_name ) const;
215 : //
216 : inline static void append_sig(
217 : 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 241 : inline void JNI_info::append_sig(
245 : OStringBuffer * buf, typelib_TypeDescriptionReference * type,
246 : bool use_Object_for_type_XInterface, bool use_slashes )
247 : {
248 241 : switch (type->eTypeClass)
249 : {
250 : case typelib_TypeClass_VOID:
251 33 : buf->append( 'V' );
252 33 : break;
253 : case typelib_TypeClass_CHAR:
254 0 : buf->append( 'C' );
255 0 : break;
256 : case typelib_TypeClass_BOOLEAN:
257 10 : buf->append( 'Z' );
258 10 : break;
259 : case typelib_TypeClass_BYTE:
260 4 : buf->append( 'B' );
261 4 : break;
262 : case typelib_TypeClass_SHORT:
263 : case typelib_TypeClass_UNSIGNED_SHORT:
264 2 : buf->append( 'S' );
265 2 : break;
266 : case typelib_TypeClass_LONG:
267 : case typelib_TypeClass_UNSIGNED_LONG:
268 4 : buf->append( 'I' );
269 4 : 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 73 : if ( use_slashes ) {
282 73 : buf->append( "Ljava/lang/String;" );
283 : } else {
284 0 : buf->append( "Ljava.lang.String;" );
285 : }
286 73 : break;
287 : case typelib_TypeClass_TYPE:
288 2 : if ( use_slashes ) {
289 2 : buf->append( "Lcom/sun/star/uno/Type;" );
290 : } else {
291 0 : buf->append( "Lcom.sun.star.uno.Type;" );
292 : }
293 2 : break;
294 : case typelib_TypeClass_ANY:
295 27 : if ( use_slashes ) {
296 27 : buf->append( "Ljava/lang/Object;" );
297 : } else {
298 0 : buf->append( "Ljava.lang.Object;" );
299 : }
300 27 : break;
301 : case typelib_TypeClass_ENUM:
302 : case typelib_TypeClass_STRUCT:
303 : case typelib_TypeClass_EXCEPTION:
304 : {
305 : OUString const & uno_name =
306 3 : OUString::unacquired( &type->pTypeName );
307 3 : buf->append( 'L' );
308 : // Erase type arguments of instantiated polymorphic struct types:
309 3 : sal_Int32 i = uno_name.indexOf( '<' );
310 3 : if ( i < 0 ) {
311 : buf->append(
312 : OUStringToOString(
313 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
314 3 : RTL_TEXTENCODING_JAVA_UTF8 ) );
315 : } else {
316 0 : OUString s( uno_name.copy( 0, i ) );
317 : buf->append(
318 : OUStringToOString(
319 : use_slashes ? s.replace( '.', '/' ) : s,
320 0 : RTL_TEXTENCODING_JAVA_UTF8 ) );
321 : }
322 3 : buf->append( ';' );
323 3 : break;
324 : }
325 : case typelib_TypeClass_SEQUENCE:
326 : {
327 34 : buf->append( '[' );
328 34 : TypeDescr td( type );
329 : append_sig(
330 34 : buf, ((typelib_IndirectTypeDescription *)td.get())->pType,
331 68 : use_Object_for_type_XInterface, use_slashes );
332 34 : break;
333 : }
334 : case typelib_TypeClass_INTERFACE:
335 49 : if (use_Object_for_type_XInterface && is_XInterface( type ))
336 : {
337 14 : if ( use_slashes ) {
338 14 : buf->append( "Ljava/lang/Object;" );
339 : } else {
340 0 : buf->append( "Ljava.lang.Object;" );
341 : }
342 : }
343 : else
344 : {
345 : OUString const & uno_name =
346 35 : OUString::unacquired( &type->pTypeName );
347 35 : buf->append( 'L' );
348 : buf->append(
349 : OUStringToOString(
350 : use_slashes ? uno_name.replace( '.', '/' ) : uno_name,
351 35 : RTL_TEXTENCODING_JAVA_UTF8 ) );
352 35 : buf->append( ';' );
353 : }
354 49 : break;
355 : default:
356 : throw BridgeRuntimeError(
357 0 : "unsupported type: " +
358 0 : OUString::unacquired( &type->pTypeName ) );
359 : }
360 241 : }
361 :
362 : }
363 :
364 : #endif
365 :
366 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|