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