1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_INFO_H
21 #define INCLUDED_BRIDGES_SOURCE_JNI_UNO_JNI_INFO_H
23 #include <sal/config.h>
25 #include <boost/noncopyable.hpp>
26 #include <boost/unordered_map.hpp>
30 #include "osl/mutex.hxx"
31 #include "rtl/ref.hxx"
32 #include "rtl/ustring.hxx"
33 #include "rtl/strbuf.hxx"
35 #include "uno/environment.h"
36 #include "typelib/typedescription.hxx"
38 #include "com/sun/star/uno/Type.hxx"
40 namespace jvmaccess
{ class UnoVirtualMachine
; }
45 inline bool type_equals(
46 typelib_TypeDescriptionReference
* type1
,
47 typelib_TypeDescriptionReference
* type2
)
51 OUString
const & name1
=
52 OUString::unacquired( &type1
->pTypeName
);
53 OUString
const & name2
=
54 OUString::unacquired( &type2
->pTypeName
);
55 return ((type1
->eTypeClass
== type2
->eTypeClass
) && name1
.equals( name2
));
58 inline bool is_XInterface( typelib_TypeDescriptionReference
* type
)
60 return ((typelib_TypeClass_INTERFACE
== type
->eTypeClass
) &&
61 OUString::unacquired( &type
->pTypeName
) == "com.sun.star.uno.XInterface");
64 struct JNI_type_info
: private boost::noncopyable
66 ::com::sun::star::uno::TypeDescription m_td
;
69 virtual void destroy( JNIEnv
* jni_env
) = 0;
71 inline void destruct( JNIEnv
* jni_env
)
72 { jni_env
->DeleteGlobalRef( m_class
); }
73 virtual inline ~JNI_type_info() {}
74 explicit JNI_type_info(
75 JNI_context
const & jni
, typelib_TypeDescription
* td
);
78 struct JNI_interface_type_info
: public JNI_type_info
80 jobject m_proxy_ctor
; // proxy ctor
82 // sorted via typelib function index
83 jmethodID
* m_methods
;
85 virtual void destroy( JNIEnv
* jni_env
) SAL_OVERRIDE
;
86 explicit JNI_interface_type_info(
87 JNI_context
const & jni
, typelib_TypeDescription
* td
);
90 virtual ~JNI_interface_type_info() {}
93 struct JNI_compound_type_info
: public JNI_type_info
95 JNI_type_info
const * m_base
;
96 // ctor( msg ) for exceptions
98 // sorted via typelib member index
101 virtual void destroy( JNIEnv
* jni_env
) SAL_OVERRIDE
;
102 explicit JNI_compound_type_info(
103 JNI_context
const & jni
, typelib_TypeDescription
* td
);
106 virtual ~JNI_compound_type_info() {}
109 struct JNI_type_info_holder
: private boost::noncopyable
111 JNI_type_info
* m_info
;
112 inline JNI_type_info_holder()
117 typedef ::boost::unordered_map
<
118 OUString
, JNI_type_info_holder
, OUStringHash
> t_str2type
;
120 class JNI_info
: private boost::noncopyable
122 mutable ::osl::Mutex m_mutex
;
123 mutable t_str2type m_type_map
;
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
;
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
;
136 jclass m_class_Object
;
137 jclass m_class_Character
;
138 jclass m_class_Boolean
;
140 jclass m_class_Short
;
141 jclass m_class_Integer
;
143 jclass m_class_Float
;
144 jclass m_class_Double
;
145 jclass m_class_String
;
147 jclass m_class_UnoRuntime
;
148 jclass m_class_RuntimeException
;
151 jclass m_class_TypeClass
;
152 jclass m_class_JNI_proxy
;
153 jclass m_class_AsynchronousFinalizer
;
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
;
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
;
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
;
195 jmethodID m_ctor_AsynchronousFinalizer
;
196 jmethodID m_method_AsynchronousFinalizer_drain
;
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
;
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 );
218 static JNI_info
const * get_jni_info(
219 rtl::Reference
< jvmaccess::UnoVirtualMachine
> const & uno_vm
);
220 inline void destroy( JNIEnv
* jni_env
);
223 JNI_type_info
const * create_type_info(
224 JNI_context
const & jni
, typelib_TypeDescription
* td
) const;
226 void destruct( JNIEnv
* jni_env
);
228 JNI_info( JNIEnv
* jni_env
, jobject class_loader
,
229 jclass classClass
, jmethodID methodForName
);
230 inline ~JNI_info() {}
233 inline void JNI_info::destroy( JNIEnv
* jni_env
)
239 inline void JNI_info::append_sig(
240 OStringBuffer
* buf
, typelib_TypeDescriptionReference
* type
,
241 bool use_Object_for_type_XInterface
, bool use_slashes
)
243 switch (type
->eTypeClass
)
245 case typelib_TypeClass_VOID
:
248 case typelib_TypeClass_CHAR
:
251 case typelib_TypeClass_BOOLEAN
:
254 case typelib_TypeClass_BYTE
:
257 case typelib_TypeClass_SHORT
:
258 case typelib_TypeClass_UNSIGNED_SHORT
:
261 case typelib_TypeClass_LONG
:
262 case typelib_TypeClass_UNSIGNED_LONG
:
265 case typelib_TypeClass_HYPER
:
266 case typelib_TypeClass_UNSIGNED_HYPER
:
269 case typelib_TypeClass_FLOAT
:
272 case typelib_TypeClass_DOUBLE
:
275 case typelib_TypeClass_STRING
:
277 buf
->append( "Ljava/lang/String;" );
279 buf
->append( "Ljava.lang.String;" );
282 case typelib_TypeClass_TYPE
:
284 buf
->append( "Lcom/sun/star/uno/Type;" );
286 buf
->append( "Lcom.sun.star.uno.Type;" );
289 case typelib_TypeClass_ANY
:
291 buf
->append( "Ljava/lang/Object;" );
293 buf
->append( "Ljava.lang.Object;" );
296 case typelib_TypeClass_ENUM
:
297 case typelib_TypeClass_STRUCT
:
298 case typelib_TypeClass_EXCEPTION
:
300 OUString
const & uno_name
=
301 OUString::unacquired( &type
->pTypeName
);
303 // Erase type arguments of instantiated polymorphic struct types:
304 sal_Int32 i
= uno_name
.indexOf( '<' );
308 use_slashes
? uno_name
.replace( '.', '/' ) : uno_name
,
309 RTL_TEXTENCODING_JAVA_UTF8
) );
311 OUString
s( uno_name
.copy( 0, i
) );
314 use_slashes
? s
.replace( '.', '/' ) : s
,
315 RTL_TEXTENCODING_JAVA_UTF8
) );
320 case typelib_TypeClass_SEQUENCE
:
323 TypeDescr
td( type
);
325 buf
, reinterpret_cast<typelib_IndirectTypeDescription
*>(td
.get())->pType
,
326 use_Object_for_type_XInterface
, use_slashes
);
329 case typelib_TypeClass_INTERFACE
:
330 if (use_Object_for_type_XInterface
&& is_XInterface( type
))
333 buf
->append( "Ljava/lang/Object;" );
335 buf
->append( "Ljava.lang.Object;" );
340 OUString
const & uno_name
=
341 OUString::unacquired( &type
->pTypeName
);
345 use_slashes
? uno_name
.replace( '.', '/' ) : uno_name
,
346 RTL_TEXTENCODING_JAVA_UTF8
) );
351 throw BridgeRuntimeError(
352 "unsupported type: " +
353 OUString::unacquired( &type
->pTypeName
) );
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */