LibreOffice Module bridges (master) 1
jni_info.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21
22#include <cassert>
23
24#include "jni_bridge.h"
25
26#include <com/sun/star/uno/RuntimeException.hpp>
27
29#include <rtl/string.hxx>
30#include <rtl/strbuf.hxx>
31#include <rtl/ustrbuf.hxx>
32
33#include <uno/lbnames.h>
34
35
36namespace jni_uno
37{
38
39
41 JNI_context const & jni, typelib_TypeDescription * td )
42 : m_td( td ),
43 m_class( nullptr )
44{
45 m_td.makeComplete();
46 if (! m_td.get()->bComplete)
47 {
49 "cannot make type complete: "
50 + OUString::unacquired( &m_td.get()->pTypeName )
51 + jni.get_stack_trace() );
52 }
53}
54
55
56void JNI_interface_type_info::destroy( JNIEnv * jni_env )
57{
58 JNI_type_info::destruct( jni_env );
59 jni_env->DeleteGlobalRef( m_proxy_ctor );
60 jni_env->DeleteGlobalRef( m_type );
61 m_methods.reset();
62 delete this;
63}
64
65
67 JNI_context const & jni, typelib_TypeDescription * td_ )
68 : JNI_type_info( jni, td_ )
69{
70 assert( m_td.get()->eTypeClass == typelib_TypeClass_INTERFACE );
71
72 OUString const & uno_name = OUString::unacquired( &m_td.get()->pTypeName );
73 JNI_info const * jni_info = jni.get_info();
74
75 JLocalAutoRef jo_class(
76 jni,
78 jni,
79 ( OUStringToOString( uno_name, RTL_TEXTENCODING_JAVA_UTF8 ).
80 getStr() ) ) );
81 JLocalAutoRef jo_type( jni, create_type( jni, static_cast<jclass>(jo_class.get()) ) );
82
83 // get proxy ctor
84 jvalue arg;
85 arg.l = jo_class.get();
86 JLocalAutoRef jo_proxy_ctor(
87 jni, jni->CallStaticObjectMethodA(
88 jni_info->m_class_JNI_proxy,
89 jni_info->m_method_JNI_proxy_get_proxy_ctor, &arg ) );
90
91 if (is_XInterface( m_td.get()->pWeakRef ))
92 {
93 m_methods = nullptr; // no methods
94 }
95 else
96 {
97 // retrieve method ids for all direct members
98 try
99 {
100 typelib_InterfaceTypeDescription * td =
101 reinterpret_cast< typelib_InterfaceTypeDescription * >(
102 m_td.get() );
103 // coverity[ctor_dtor_leak] - on purpose
104 m_methods.reset(new jmethodID[ td->nMapFunctionIndexToMemberIndex ]);
105 sal_Int32 nMethodIndex = 0;
106 typelib_TypeDescriptionReference ** ppMembers = td->ppMembers;
107 sal_Int32 nMembers = td->nMembers;
108
109 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
110 {
111 TypeDescr member_td( ppMembers[ nPos ] );
112
113 OStringBuffer sig_buf( 64 );
114
115 if (member_td.get()->eTypeClass ==
116 typelib_TypeClass_INTERFACE_METHOD) // method
117 {
118 typelib_InterfaceMethodTypeDescription * method_td =
119 reinterpret_cast<
120 typelib_InterfaceMethodTypeDescription * >(
121 member_td.get() );
122
123 sig_buf.append( '(' );
124 for ( sal_Int32 i = 0; i < method_td->nParams; ++i )
125 {
126 typelib_MethodParameter const & param =
127 method_td->pParams[ i ];
128 if (param.bOut)
129 sig_buf.append( '[' );
130 JNI_info::append_sig( &sig_buf, param.pTypeRef );
131 }
132 sig_buf.append( ')' );
133 JNI_info::append_sig( &sig_buf, method_td->pReturnTypeRef );
134
135 OString method_signature( sig_buf.makeStringAndClear() );
136 OString method_name(
137 OUStringToOString( OUString::unacquired(
138 &method_td->aBase.pMemberName ),
139 RTL_TEXTENCODING_JAVA_UTF8 ) );
140
141 m_methods[ nMethodIndex ] = jni->GetMethodID(
142 static_cast<jclass>(jo_class.get()), method_name.getStr(),
143 method_signature.getStr() );
145 assert( m_methods[ nMethodIndex ] != nullptr );
146 ++nMethodIndex;
147 }
148 else // attribute
149 {
150 assert(
151 member_td.get()->eTypeClass ==
152 typelib_TypeClass_INTERFACE_ATTRIBUTE );
153 typelib_InterfaceAttributeTypeDescription * attribute_td =
154 reinterpret_cast<
155 typelib_InterfaceAttributeTypeDescription * >(
156 member_td.get() );
157
158 // type sig
160 &sig_buf, attribute_td->pAttributeTypeRef );
161 OString type_sig( sig_buf.makeStringAndClear() );
162 sig_buf.ensureCapacity( 64 );
163 // member name
164 OUString const & member_name =
165 OUString::unacquired(
166 &attribute_td->aBase.pMemberName );
167
168 // getter
169 sig_buf.append( "()" + type_sig );
170 OString method_signature( sig_buf.makeStringAndClear() );
171 OString method_name(
173 rtl::Concat2View("get" + member_name),
174 RTL_TEXTENCODING_JAVA_UTF8 ) );
175 m_methods[ nMethodIndex ] = jni->GetMethodID(
176 static_cast<jclass>(jo_class.get()), method_name.getStr(),
177 method_signature.getStr() );
179 assert( m_methods[ nMethodIndex ] != nullptr );
180 ++nMethodIndex;
181 if (! attribute_td->bReadOnly)
182 {
183 // setter
184 method_signature = "(" + type_sig + ")V";
185 method_name = OUStringToOString(
186 rtl::Concat2View("set" + member_name),
187 RTL_TEXTENCODING_JAVA_UTF8 );
188 m_methods[ nMethodIndex ] = jni->GetMethodID(
189 static_cast<jclass>(jo_class.get()), method_name.getStr(),
190 method_signature.getStr() );
192 assert( m_methods[ nMethodIndex ] != nullptr );
193 ++nMethodIndex;
194 }
195 }
196 }
197 }
198 catch (...)
199 {
200 m_methods.reset();
201 throw;
202 }
203 }
204 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
205 m_type = jni->NewGlobalRef( jo_type.get() );
206 m_proxy_ctor = jni->NewGlobalRef( jo_proxy_ctor.get() );
207}
208
209
210void JNI_compound_type_info::destroy( JNIEnv * jni_env )
211{
212 JNI_type_info::destruct( jni_env );
213 m_fields.reset();
214 delete this;
215}
216
217
219 JNI_context const & jni, typelib_TypeDescription * td_ )
220 : JNI_type_info( jni, td_ ),
221 m_exc_ctor( nullptr )
222{
223 assert( m_td.get()->eTypeClass == typelib_TypeClass_STRUCT ||
224 m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION );
225 typelib_CompoundTypeDescription * td =
226 reinterpret_cast< typelib_CompoundTypeDescription * >( m_td.get() );
227
228 OUString const & uno_name =
229 OUString::unacquired( &td->aBase.pTypeName );
230
231 // Erase type arguments of instantiated polymorphic struct types:
232 std::u16string_view nucleus;
233 sal_Int32 i = uno_name.indexOf( '<' );
234 if ( i < 0 ) {
235 nucleus = uno_name;
236 } else {
237 nucleus = uno_name.subView( 0, i );
238 }
239 JLocalAutoRef jo_class(
240 jni,
242 jni,
244 nucleus, RTL_TEXTENCODING_JAVA_UTF8 ).getStr() ) );
245
246 JNI_info const * jni_info = jni.get_info();
247
248 if (m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION)
249 {
250 // retrieve exc ctor( msg )
251 m_exc_ctor = jni->GetMethodID(
252 static_cast<jclass>(jo_class.get()), "<init>", "(Ljava/lang/String;)V" );
254 assert( m_exc_ctor != nullptr );
255 }
256
257 // retrieve info for base type
258 typelib_TypeDescription * base_td =
260 td->aBase.pWeakRef,
261 jni_info->m_RuntimeException_type.getTypeLibType())
262 ? nullptr
263 : reinterpret_cast< typelib_TypeDescription * >(
264 td->pBaseTypeDescription );
265 m_base = (base_td == nullptr ? nullptr : jni_info->get_type_info( jni, base_td ));
266
267 try
268 {
269 if (type_equals(
270 td->aBase.pWeakRef,
271 jni_info->m_Exception_type.getTypeLibType() ) ||
273 td->aBase.pWeakRef,
274 jni_info->m_RuntimeException_type.getTypeLibType() ))
275 {
276 // coverity[ctor_dtor_leak] - on purpose
277 m_fields.reset(new jfieldID[ 2 ]);
278 m_fields[ 0 ] = nullptr; // special Throwable.getMessage()
279 // field Context
280 m_fields[ 1 ] = jni->GetFieldID(
281 static_cast<jclass>(jo_class.get()), "Context", "Ljava/lang/Object;" );
283 assert( m_fields[ 1 ] != nullptr );
284 }
285 else
286 {
287 // retrieve field ids for all direct members
288 sal_Int32 nMembers = td->nMembers;
289 m_fields.reset(new jfieldID[ nMembers ]);
290
291 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
292 {
293 OString sig;
294 if (td->aBase.eTypeClass == typelib_TypeClass_STRUCT
295 && reinterpret_cast< typelib_StructTypeDescription * >(
296 td)->pParameterizedTypes != nullptr
297 && reinterpret_cast< typelib_StructTypeDescription * >(
298 td)->pParameterizedTypes[nPos])
299 {
300 sig = OString( "Ljava/lang/Object;" );
301 } else {
302 OStringBuffer sig_buf( 32 );
303 JNI_info::append_sig( &sig_buf, td->ppTypeRefs[ nPos ] );
304 sig = sig_buf.makeStringAndClear();
305 }
306
307 OString member_name(
309 OUString::unacquired( &td->ppMemberNames[ nPos ] ),
310 RTL_TEXTENCODING_JAVA_UTF8 ) );
311
312 m_fields[ nPos ] = jni->GetFieldID(
313 static_cast<jclass>(jo_class.get()), member_name.getStr(),
314 sig.getStr() );
316 assert( m_fields[ nPos ] != nullptr );
317 }
318 }
319 }
320 catch (...)
321 {
322 m_fields.reset();
323 throw;
324 }
325
326 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
327}
328
329
331 JNI_context const & jni, typelib_TypeDescription * td ) const
332{
333 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
334
335 JNI_type_info * new_info;
336 switch (td->eTypeClass)
337 {
338 case typelib_TypeClass_STRUCT:
339 case typelib_TypeClass_EXCEPTION:
340 {
341 new_info = new JNI_compound_type_info( jni, td );
342 break;
343 }
344 case typelib_TypeClass_INTERFACE:
345 {
346 new_info = new JNI_interface_type_info( jni, td );
347 break;
348 }
349 default:
350 {
351 throw BridgeRuntimeError(
352 "type info not supported for " + uno_name + jni.get_stack_trace() );
353 }
354 }
355
356 // look up
357 JNI_type_info * info;
358 std::unique_lock guard( m_mutex );
359 JNI_type_info_holder & holder = m_type_map[ uno_name ];
360 if (holder.m_info == nullptr) // new insertion
361 {
362 holder.m_info = new_info;
363 guard.unlock();
364 info = new_info;
365 }
366 else // inserted in the meantime
367 {
368 info = holder.m_info;
369 guard.unlock();
370 new_info->destroy( jni.get_jni_env() );
371 }
372 return info;
373}
374
375
377 JNI_context const & jni, typelib_TypeDescription * td ) const
378{
379 if (is_XInterface( td->pWeakRef ))
380 {
382 }
383
384 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
385 JNI_type_info const * info;
386 std::unique_lock guard( m_mutex );
387
388 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
389 if (iFind == m_type_map.end())
390 {
391 guard.unlock();
392 info = create_type_info( jni, td );
393 }
394 else
395 {
396 info = iFind->second.m_info;
397 }
398
399 return info;
400}
401
402
404 JNI_context const & jni, typelib_TypeDescriptionReference * type ) const
405{
406 if (is_XInterface( type ))
407 {
409 }
410
411 OUString const & uno_name = OUString::unacquired( &type->pTypeName );
412 JNI_type_info const * info;
413 std::unique_lock guard( m_mutex );
414 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
415 if (iFind == m_type_map.end())
416 {
417 guard.unlock();
418 TypeDescr td( type );
419 info = create_type_info( jni, td.get() );
420 }
421 else
422 {
423 info = iFind->second.m_info;
424 }
425
426 return info;
427}
428
429
431 JNI_context const & jni, OUString const & uno_name ) const
432{
433 if ( uno_name == "com.sun.star.uno.XInterface" )
434 {
436 }
437
438 JNI_type_info const * info;
439 std::unique_lock guard( m_mutex );
440 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
441 if (iFind == m_type_map.end())
442 {
443 guard.unlock();
444 css::uno::TypeDescription td( uno_name );
445 if (! td.is())
446 {
447 throw BridgeRuntimeError(
448 "UNO type not found: " + uno_name + jni.get_stack_trace() );
449 }
450 info = create_type_info( jni, td.get() );
451 }
452 else
453 {
454 info = iFind->second.m_info;
455 }
456
457 return info;
458}
459
460
462 JNIEnv * jni_env, jobject class_loader, jclass classClass,
463 jmethodID methodForName )
464 : m_class_Class( classClass ),
465 m_method_Class_forName( methodForName ),
466 m_class_JNI_proxy( nullptr ),
467 m_XInterface_queryInterface_td(
468 (reinterpret_cast< typelib_InterfaceTypeDescription * >(
469 css::uno::TypeDescription(
470 cppu::UnoType<css::uno::XInterface>::get())
471 .get())->ppMembers[ 0 ] ) ),
472 m_Exception_type(cppu::UnoType<css::uno::Exception>::get()),
473 m_RuntimeException_type(cppu::UnoType<css::uno::RuntimeException>::get()),
474 m_void_type(cppu::UnoType<void>::get()),
475 m_XInterface_type_info( nullptr )
476{
477 JNI_context jni( this, jni_env, class_loader ); // !no proper jni_info!
478
479 // class lookup
480 JLocalAutoRef jo_Object(
481 jni, find_class( jni, "java.lang.Object" ) );
482 JLocalAutoRef jo_Class(
483 jni, find_class( jni, "java.lang.Class" ) );
484 JLocalAutoRef jo_Throwable(
485 jni, find_class( jni, "java.lang.Throwable" ) );
486 JLocalAutoRef jo_Character(
487 jni, find_class( jni, "java.lang.Character" ) );
488 JLocalAutoRef jo_Boolean(
489 jni, find_class( jni, "java.lang.Boolean" ) );
490 JLocalAutoRef jo_Byte(
491 jni, find_class( jni, "java.lang.Byte" ) );
492 JLocalAutoRef jo_Short(
493 jni, find_class( jni, "java.lang.Short" ) );
494 JLocalAutoRef jo_Integer(
495 jni, find_class( jni, "java.lang.Integer" ) );
496 JLocalAutoRef jo_Long(
497 jni, find_class( jni, "java.lang.Long" ) );
498 JLocalAutoRef jo_Float(
499 jni, find_class( jni, "java.lang.Float" ) );
500 JLocalAutoRef jo_Double(
501 jni, find_class( jni, "java.lang.Double" ) );
502 JLocalAutoRef jo_String(
503 jni, find_class( jni, "java.lang.String" ) );
504 JLocalAutoRef jo_RuntimeException(
505 jni, find_class( jni, "com.sun.star.uno.RuntimeException" ) );
506 JLocalAutoRef jo_UnoRuntime(
507 jni, find_class( jni, "com.sun.star.uno.UnoRuntime" ) );
508 JLocalAutoRef jo_Any(
509 jni, find_class( jni, "com.sun.star.uno.Any" ) );
510 JLocalAutoRef jo_Enum(
511 jni, find_class( jni, "com.sun.star.uno.Enum" ) );
512 JLocalAutoRef jo_Type(
513 jni, find_class( jni, "com.sun.star.uno.Type" ) );
514 JLocalAutoRef jo_TypeClass(
515 jni, find_class( jni, "com.sun.star.uno.TypeClass" ) );
516 JLocalAutoRef jo_IEnvironment(
517 jni, find_class( jni, "com.sun.star.uno.IEnvironment" ) );
518 JLocalAutoRef jo_JNI_proxy(
519 jni, find_class( jni, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
520 JLocalAutoRef jo_AsynchronousFinalizer(
521 jni, find_class( jni, "com.sun.star.lib.util.AsynchronousFinalizer" ) );
522
523 // method Object.toString()
524 m_method_Object_toString = jni->GetMethodID(
525 static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
527 assert( m_method_Object_toString != nullptr );
528 // method Class.getName()
529 m_method_Class_getName = jni->GetMethodID(
530 static_cast<jclass>(jo_Class.get()), "getName", "()Ljava/lang/String;" );
532 assert( m_method_Class_getName != nullptr );
533
534 // method Throwable.getMessage()
535 m_method_Throwable_getMessage = jni->GetMethodID(
536 static_cast<jclass>(jo_Throwable.get()), "getMessage", "()Ljava/lang/String;" );
538 assert( m_method_Throwable_getMessage != nullptr );
539
540 // method Character.charValue()
541 m_method_Character_charValue = jni->GetMethodID(
542 static_cast<jclass>(jo_Character.get()), "charValue", "()C" );
544 assert( m_method_Character_charValue != nullptr );
545 // method Boolean.booleanValue()
546 m_method_Boolean_booleanValue = jni->GetMethodID(
547 static_cast<jclass>(jo_Boolean.get()), "booleanValue", "()Z" );
549 assert( m_method_Boolean_booleanValue != nullptr );
550 // method Byte.byteValue()
551 m_method_Byte_byteValue = jni->GetMethodID(
552 static_cast<jclass>(jo_Byte.get()), "byteValue", "()B" );
554 assert( m_method_Byte_byteValue != nullptr );
555 // method Short.shortValue()
556 m_method_Short_shortValue = jni->GetMethodID(
557 static_cast<jclass>(jo_Short.get()), "shortValue", "()S" );
559 assert( m_method_Short_shortValue != nullptr );
560 // method Integer.intValue()
561 m_method_Integer_intValue = jni->GetMethodID(
562 static_cast<jclass>(jo_Integer.get()), "intValue", "()I" );
564 assert( m_method_Integer_intValue != nullptr );
565 // method Long.longValue()
566 m_method_Long_longValue = jni->GetMethodID(
567 static_cast<jclass>(jo_Long.get()), "longValue", "()J" );
569 assert( m_method_Long_longValue != nullptr );
570 // method Float.floatValue()
571 m_method_Float_floatValue = jni->GetMethodID(
572 static_cast<jclass>(jo_Float.get()), "floatValue", "()F" );
574 assert( m_method_Float_floatValue != nullptr );
575 // method Double.doubleValue()
576 m_method_Double_doubleValue = jni->GetMethodID(
577 static_cast<jclass>(jo_Double.get()), "doubleValue", "()D" );
579 assert( m_method_Double_doubleValue != nullptr );
580
581 // ctor Character( char )
582 m_ctor_Character_with_char = jni->GetMethodID(
583 static_cast<jclass>(jo_Character.get()), "<init>", "(C)V" );
585 assert( m_ctor_Character_with_char != nullptr );
586 // ctor Boolean( boolean )
587 m_ctor_Boolean_with_boolean = jni->GetMethodID(
588 static_cast<jclass>(jo_Boolean.get()), "<init>", "(Z)V" );
590 assert( m_ctor_Boolean_with_boolean != nullptr );
591 // ctor Byte( byte )
592 m_ctor_Byte_with_byte = jni->GetMethodID(
593 static_cast<jclass>(jo_Byte.get()), "<init>", "(B)V" );
595 assert( m_ctor_Byte_with_byte != nullptr );
596 // ctor Short( short )
597 m_ctor_Short_with_short = jni->GetMethodID(
598 static_cast<jclass>(jo_Short.get()), "<init>", "(S)V" );
600 assert( m_ctor_Short_with_short != nullptr );
601 // ctor Integer( int )
602 m_ctor_Integer_with_int = jni->GetMethodID(
603 static_cast<jclass>(jo_Integer.get()), "<init>", "(I)V" );
605 assert( m_ctor_Integer_with_int != nullptr );
606 // ctor Long( long )
607 m_ctor_Long_with_long = jni->GetMethodID(
608 static_cast<jclass>(jo_Long.get()), "<init>", "(J)V" );
610 assert( m_ctor_Long_with_long != nullptr );
611 // ctor Float( float )
612 m_ctor_Float_with_float = jni->GetMethodID(
613 static_cast<jclass>(jo_Float.get()), "<init>", "(F)V" );
615 assert( m_ctor_Float_with_float != nullptr );
616 // ctor Double( double )
617 m_ctor_Double_with_double = jni->GetMethodID(
618 static_cast<jclass>(jo_Double.get()), "<init>", "(D)V" );
620 assert( m_ctor_Double_with_double != nullptr );
621
622 // static method UnoRuntime.generateOid()
623 m_method_UnoRuntime_generateOid = jni->GetStaticMethodID(
624 static_cast<jclass>(jo_UnoRuntime.get()),
625 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
627 assert( m_method_UnoRuntime_generateOid != nullptr );
628 // static method UnoRuntime.queryInterface()
629 m_method_UnoRuntime_queryInterface = jni->GetStaticMethodID(
630 static_cast<jclass>(jo_UnoRuntime.get()),
631 "queryInterface",
632 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
634 assert( m_method_UnoRuntime_queryInterface != nullptr );
635
636 // field Enum.m_value
637 m_field_Enum_m_value = jni->GetFieldID(
638 static_cast<jclass>(jo_Enum.get()), "m_value", "I" );
640 assert( m_field_Enum_m_value != nullptr );
641
642 // static method TypeClass.fromInt()
643 m_method_TypeClass_fromInt = jni->GetStaticMethodID(
644 static_cast<jclass>(jo_TypeClass.get()),
645 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
647 assert( m_method_TypeClass_fromInt != nullptr );
648
649 // ctor Type( Class )
650 m_ctor_Type_with_Class = jni->GetMethodID(
651 static_cast<jclass>(jo_Type.get()), "<init>", "(Ljava/lang/Class;)V" );
653 assert( m_ctor_Type_with_Class != nullptr );
654 // ctor Type( String, TypeClass )
655 m_ctor_Type_with_Name_TypeClass = jni->GetMethodID(
656 static_cast<jclass>(jo_Type.get()),
657 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
659 assert( m_ctor_Type_with_Name_TypeClass != nullptr );
660 // field Type._typeName
661 m_field_Type_typeName = jni->GetFieldID(
662 static_cast<jclass>(jo_Type.get()), "_typeName", "Ljava/lang/String;" );
664 assert( m_field_Type_typeName != nullptr );
665
666 // ctor Any( Type, Object )
667 m_ctor_Any_with_Type_Object = jni->GetMethodID(
668 static_cast<jclass>(jo_Any.get()),
669 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
671 assert( m_ctor_Any_with_Type_Object != nullptr );
672
673 // field Any._type
674 m_field_Any_type = jni->GetFieldID(
675 static_cast<jclass>(jo_Any.get()), "_type", "Lcom/sun/star/uno/Type;" );
677 assert( m_field_Any_type != nullptr );
678 // field Any._object
679 m_field_Any_object = jni->GetFieldID(
680 static_cast<jclass>(jo_Any.get()), "_object", "Ljava/lang/Object;" );
682 assert( m_field_Any_object != nullptr );
683
684 // method IEnvironment.getRegisteredInterface()
686 static_cast<jclass>(jo_IEnvironment.get()),
687 "getRegisteredInterface",
688 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
691 // method IEnvironment.registerInterface()
693 static_cast<jclass>(jo_IEnvironment.get()), "registerInterface",
694 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
695 "Ljava/lang/Object;" );
697 assert( m_method_IEnvironment_registerInterface != nullptr );
698
699 // static method JNI_proxy.get_proxy_ctor()
700 m_method_JNI_proxy_get_proxy_ctor = jni->GetStaticMethodID(
701 static_cast<jclass>(jo_JNI_proxy.get()), "get_proxy_ctor",
702 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
704 assert( m_method_JNI_proxy_get_proxy_ctor != nullptr );
705 // static method JNI_proxy.create()
706 m_method_JNI_proxy_create = jni->GetStaticMethodID(
707 static_cast<jclass>(jo_JNI_proxy.get()), "create",
708 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
709 "/String;Ljava/lang/reflect/Constructor;"
710 "Lcom/sun/star/lib/util/AsynchronousFinalizer;)Ljava/lang/Object;" );
712 assert( m_method_JNI_proxy_create != nullptr );
713 // field JNI_proxy.m_receiver_handle
714 m_field_JNI_proxy_m_receiver_handle = jni->GetFieldID(
715 static_cast<jclass>(jo_JNI_proxy.get()), "m_receiver_handle", "J" );
717 assert( m_field_JNI_proxy_m_receiver_handle != nullptr );
718 // field JNI_proxy.m_td_handle
719 m_field_JNI_proxy_m_td_handle = jni->GetFieldID(
720 static_cast<jclass>(jo_JNI_proxy.get()), "m_td_handle", "J" );
722 assert( m_field_JNI_proxy_m_td_handle != nullptr );
723 // field JNI_proxy.m_type
724 m_field_JNI_proxy_m_type = jni->GetFieldID(
725 static_cast<jclass>(jo_JNI_proxy.get()), "m_type", "Lcom/sun/star/uno/Type;" );
727 assert( m_field_JNI_proxy_m_type != nullptr );
728 // field JNI_proxy.m_oid
729 m_field_JNI_proxy_m_oid = jni->GetFieldID(
730 static_cast<jclass>(jo_JNI_proxy.get()), "m_oid", "Ljava/lang/String;" );
732 assert( m_field_JNI_proxy_m_oid != nullptr );
733
734 // ctor AsynchronousFinalizer
735 m_ctor_AsynchronousFinalizer = jni->GetMethodID(
736 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "<init>", "()V" );
738 assert( m_ctor_AsynchronousFinalizer != nullptr );
739 // method AsynchronousFinalizer.drain()
740 m_method_AsynchronousFinalizer_drain = jni->GetMethodID(
741 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "drain", "()V" );
743 assert( m_method_AsynchronousFinalizer_drain != nullptr );
744
745 // get java env
746 OUString java_env_type_name( UNO_LB_JAVA );
747 JLocalAutoRef jo_java(
748 jni, ustring_to_jstring( jni, java_env_type_name.pData ) );
749 jvalue args[ 2 ];
750 args[ 0 ].l = jo_java.get();
751 args[ 1 ].l = nullptr;
752 jmethodID method_getEnvironment = jni->GetStaticMethodID(
753 static_cast<jclass>(jo_UnoRuntime.get()), "getEnvironment",
754 "(Ljava/lang/String;Ljava/lang/Object;)"
755 "Lcom/sun/star/uno/IEnvironment;" );
757 assert( method_getEnvironment != nullptr );
758 JLocalAutoRef jo_java_env(
759 jni, jni->CallStaticObjectMethodA(
760 static_cast<jclass>(jo_UnoRuntime.get()), method_getEnvironment, args ) );
761
762 // get com.sun.star.uno.Any.VOID
763 jfieldID field_Any_VOID = jni->GetStaticFieldID(
764 static_cast<jclass>(jo_Any.get()), "VOID", "Lcom/sun/star/uno/Any;" );
766 assert( field_Any_VOID != nullptr );
767 JLocalAutoRef jo_Any_VOID(
768 jni, jni->GetStaticObjectField(
769 static_cast<jclass>(jo_Any.get()), field_Any_VOID ) );
770 // get com.sun.star.uno.Type.UNSIGNED_SHORT
771 jfieldID field_Type_UNSIGNED_SHORT = jni->GetStaticFieldID(
772 static_cast<jclass>(jo_Type.get()), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
774 assert( field_Type_UNSIGNED_SHORT != nullptr );
775 JLocalAutoRef jo_Type_UNSIGNED_SHORT(
776 jni, jni->GetStaticObjectField(
777 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_SHORT ) );
778 // get com.sun.star.uno.Type.UNSIGNED_LONG
779 jfieldID field_Type_UNSIGNED_LONG = jni->GetStaticFieldID(
780 static_cast<jclass>(jo_Type.get()), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
782 assert( field_Type_UNSIGNED_LONG != nullptr );
783 JLocalAutoRef jo_Type_UNSIGNED_LONG(
784 jni, jni->GetStaticObjectField(
785 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_LONG ) );
786 // get com.sun.star.uno.Type.UNSIGNED_HYPER
787 jfieldID field_Type_UNSIGNED_HYPER = jni->GetStaticFieldID(
788 static_cast<jclass>(jo_Type.get()), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
790 assert( field_Type_UNSIGNED_HYPER != nullptr );
791 JLocalAutoRef jo_Type_UNSIGNED_HYPER(
792 jni, jni->GetStaticObjectField(
793 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_HYPER ) );
794
795 // make global refs
797 static_cast<jclass>(jni->NewGlobalRef( jo_UnoRuntime.get() ));
799 static_cast<jclass>(jni->NewGlobalRef( jo_RuntimeException.get() ));
801 static_cast<jclass>(jni->NewGlobalRef( jo_Any.get() ));
803 static_cast<jclass>(jni->NewGlobalRef( jo_Type.get() ));
805 static_cast<jclass>(jni->NewGlobalRef( jo_TypeClass.get() ));
807 static_cast<jclass>(jni->NewGlobalRef( jo_JNI_proxy.get() ));
809 static_cast<jclass>(jni->NewGlobalRef( jo_AsynchronousFinalizer.get() ));
810
812 static_cast<jclass>(jni->NewGlobalRef( jo_Character.get() ));
814 static_cast<jclass>(jni->NewGlobalRef( jo_Boolean.get() ));
816 static_cast<jclass>(jni->NewGlobalRef( jo_Byte.get() ));
818 static_cast<jclass>(jni->NewGlobalRef( jo_Short.get() ));
820 static_cast<jclass>(jni->NewGlobalRef( jo_Integer.get() ));
822 static_cast<jclass>(jni->NewGlobalRef( jo_Long.get() ));
824 static_cast<jclass>(jni->NewGlobalRef( jo_Float.get() ));
826 static_cast<jclass>(jni->NewGlobalRef( jo_Double.get() ));
828 static_cast<jclass>(jni->NewGlobalRef( jo_String.get() ));
830 static_cast<jclass>(jni->NewGlobalRef( jo_Object.get() ));
832 static_cast<jclass>(jni->NewGlobalRef( m_class_Class ));
833
835 jni->NewGlobalRef( jo_Any_VOID.get() );
837 jni->NewGlobalRef( jo_Type_UNSIGNED_SHORT.get() );
839 jni->NewGlobalRef( jo_Type_UNSIGNED_LONG.get() );
841 jni->NewGlobalRef( jo_Type_UNSIGNED_HYPER.get() );
842 m_object_java_env = jni->NewGlobalRef( jo_java_env.get() );
843
844 try
845 {
846 css::uno::TypeDescription XInterface_td(
848 // coverity[ctor_dtor_leak] - on purpose
850 new JNI_interface_type_info( jni, XInterface_td.get() );
851 }
852 catch (...)
853 {
854 destruct( jni_env );
855 throw;
856 }
857}
858
859
860void JNI_info::destruct( JNIEnv * jni_env )
861{
862 for (auto & i: m_type_map)
863 {
864 i.second.m_info->destroy( jni_env );
865 }
866 if (m_XInterface_type_info != nullptr)
867 {
868 const_cast< JNI_interface_type_info * >(
869 m_XInterface_type_info )->destroy( jni_env );
870 }
871
872 // free global refs
873 jni_env->DeleteGlobalRef( m_object_java_env );
874 jni_env->DeleteGlobalRef( m_object_Any_VOID );
875 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT );
876 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG );
877 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER );
878
879 jni_env->DeleteGlobalRef( m_class_Class );
880 jni_env->DeleteGlobalRef( m_class_Object );
881 jni_env->DeleteGlobalRef( m_class_String );
882 jni_env->DeleteGlobalRef( m_class_Double );
883 jni_env->DeleteGlobalRef( m_class_Float );
884 jni_env->DeleteGlobalRef( m_class_Long );
885 jni_env->DeleteGlobalRef( m_class_Integer );
886 jni_env->DeleteGlobalRef( m_class_Short );
887 jni_env->DeleteGlobalRef( m_class_Byte );
888 jni_env->DeleteGlobalRef( m_class_Boolean );
889 jni_env->DeleteGlobalRef( m_class_Character );
890
891 jni_env->DeleteGlobalRef( m_class_AsynchronousFinalizer );
892 jni_env->DeleteGlobalRef( m_class_JNI_proxy );
893 jni_env->DeleteGlobalRef( m_class_RuntimeException );
894 jni_env->DeleteGlobalRef( m_class_UnoRuntime );
895 jni_env->DeleteGlobalRef( m_class_TypeClass );
896 jni_env->DeleteGlobalRef( m_class_Type );
897 jni_env->DeleteGlobalRef( m_class_Any );
898}
899
900
903{
904 // !!!no JNI_info available at JNI_context!!!
906 uno_vm->getVirtualMachine() );
907 JNIEnv * jni_env = guard.getEnvironment();
908 JNI_context jni(
909 nullptr, jni_env, static_cast< jobject >(uno_vm->getClassLoader()) );
910
911 jclass jo_class;
912 jmethodID jo_forName;
913 jni.getClassForName( &jo_class, &jo_forName );
915 JLocalAutoRef jo_JNI_info_holder(
916 jni,
917 jni.findClass(
918 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class,
919 jo_forName, false ) );
920 // field JNI_info_holder.m_jni_info_handle
921 jfieldID field_s_jni_info_handle =
922 jni->GetStaticFieldID(
923 static_cast<jclass>(jo_JNI_info_holder.get()), "s_jni_info_handle", "J" );
925 assert( field_s_jni_info_handle != nullptr );
926
927 JNI_info const * jni_info =
928 reinterpret_cast< JNI_info const * >(
929 jni->GetStaticLongField(
930 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle ) );
931 if (jni_info == nullptr) // un-initialized?
932 {
933 JNI_info * new_info = new JNI_info(
934 jni_env, static_cast< jobject >(uno_vm->getClassLoader()), jo_class,
935 jo_forName );
936
937 osl::ClearableMutexGuard g( osl::Mutex::getGlobalMutex() );
938 jni_info =
939 reinterpret_cast< JNI_info const * >(
940 jni->GetStaticLongField(
941 static_cast<jclass>(jo_JNI_info_holder.get()),
942 field_s_jni_info_handle ) );
943 if (jni_info == nullptr) // still un-initialized?
944 {
945 jni->SetStaticLongField(
946 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle,
947 reinterpret_cast< jlong >( new_info ) );
948 jni_info = new_info;
949 }
950 else
951 {
952 g.clear();
953 new_info->destroy( jni_env );
954 }
955 }
956
957 return jni_info;
958}
959
960}
961
962extern "C"
963{
964
965
966SAL_JNI_EXPORT void
968 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle )
970{
971 ::jni_uno::JNI_info * jni_info =
972 reinterpret_cast< ::jni_uno::JNI_info * >( jni_info_handle );
973 jni_info->destroy( jni_env );
974}
975
976}
977
978/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
jobject get() const
Definition: jni_base.h:156
jclass findClass(char const *name, jclass classClass, jmethodID methodForName, bool inException) const
Definition: jni_bridge.cxx:355
OUString get_stack_trace(jobject jo_exc=nullptr) const
Definition: jni_bridge.cxx:376
JNI_info const * get_info() const
Definition: jni_base.h:73
void ensure_no_exception() const
Definition: jni_base.h:97
JNIEnv * get_jni_env() const
Definition: jni_base.h:78
void getClassForName(jclass *classClass, jmethodID *methodForName) const
Definition: jni_bridge.cxx:342
jmethodID m_method_IEnvironment_getRegisteredInterface
Definition: jni_info.h:178
jfieldID m_field_JNI_proxy_m_td_handle
Definition: jni_info.h:194
jmethodID m_ctor_Short_with_short
Definition: jni_info.h:164
jmethodID m_method_Integer_intValue
Definition: jni_info.h:174
jmethodID m_method_Boolean_booleanValue
Definition: jni_info.h:169
jmethodID m_ctor_Character_with_char
Definition: jni_info.h:161
jmethodID m_method_Short_shortValue
Definition: jni_info.h:176
jmethodID m_ctor_Double_with_double
Definition: jni_info.h:168
void destruct(JNIEnv *jni_env)
Definition: jni_info.cxx:860
jmethodID m_method_Long_longValue
Definition: jni_info.h:175
jclass m_class_Boolean
Definition: jni_info.h:141
jclass m_class_Any
Definition: jni_info.h:152
jclass m_class_UnoRuntime
Definition: jni_info.h:150
jmethodID m_method_Class_getName
Definition: jni_info.h:159
::com::sun::star::uno::Type const & m_RuntimeException_type
Definition: jni_info.h:203
jclass m_class_TypeClass
Definition: jni_info.h:154
static JNI_info const * get_jni_info(rtl::Reference< jvmaccess::UnoVirtualMachine > const &uno_vm)
Definition: jni_info.cxx:901
jfieldID m_field_JNI_proxy_m_oid
Definition: jni_info.h:196
jmethodID m_ctor_Float_with_float
Definition: jni_info.h:167
jfieldID m_field_Type_typeName
Definition: jni_info.h:187
jmethodID m_ctor_Any_with_Type_Object
Definition: jni_info.h:182
jclass m_class_RuntimeException
Definition: jni_info.h:151
jmethodID m_method_JNI_proxy_get_proxy_ctor
Definition: jni_info.h:191
jmethodID m_method_Double_doubleValue
Definition: jni_info.h:172
jfieldID m_field_JNI_proxy_m_type
Definition: jni_info.h:195
jobject m_object_Type_UNSIGNED_LONG
Definition: jni_info.h:136
jmethodID m_method_IEnvironment_registerInterface
Definition: jni_info.h:179
jfieldID m_field_Enum_m_value
Definition: jni_info.h:189
t_str2type m_type_map
Definition: jni_info.h:126
::com::sun::star::uno::Type const & m_Exception_type
Definition: jni_info.h:202
jmethodID m_method_TypeClass_fromInt
Definition: jni_info.h:188
jclass m_class_String
Definition: jni_info.h:148
jobject m_object_Type_UNSIGNED_HYPER
Definition: jni_info.h:137
jmethodID m_method_Float_floatValue
Definition: jni_info.h:173
JNI_info(const JNI_info &)=delete
jmethodID m_ctor_Byte_with_byte
Definition: jni_info.h:163
jmethodID m_ctor_Type_with_Name_TypeClass
Definition: jni_info.h:186
jclass m_class_Type
Definition: jni_info.h:153
jclass m_class_Class
Definition: jni_info.h:130
jmethodID m_ctor_Boolean_with_boolean
Definition: jni_info.h:162
jfieldID m_field_Any_object
Definition: jni_info.h:184
std::mutex m_mutex
Definition: jni_info.h:125
jclass m_class_Object
Definition: jni_info.h:139
jclass m_class_Double
Definition: jni_info.h:147
jobject m_object_java_env
Definition: jni_info.h:133
jclass m_class_JNI_proxy
Definition: jni_info.h:155
jmethodID m_ctor_Long_with_long
Definition: jni_info.h:166
JNI_type_info const * get_type_info(JNI_context const &jni, typelib_TypeDescription *type) const
Definition: jni_info.cxx:376
JNI_interface_type_info const * m_XInterface_type_info
Definition: jni_info.h:205
jfieldID m_field_Any_type
Definition: jni_info.h:183
jclass m_class_Float
Definition: jni_info.h:146
jclass m_class_Short
Definition: jni_info.h:143
jclass m_class_Integer
Definition: jni_info.h:144
jclass m_class_Character
Definition: jni_info.h:140
jmethodID m_method_Byte_byteValue
Definition: jni_info.h:170
jmethodID m_method_JNI_proxy_create
Definition: jni_info.h:192
void destroy(JNIEnv *jni_env)
Definition: jni_info.h:240
static void append_sig(OStringBuffer *buf, typelib_TypeDescriptionReference *type, bool use_Object_for_type_XInterface=true, bool use_slashes=true)
Definition: jni_info.h:246
jmethodID m_ctor_AsynchronousFinalizer
Definition: jni_info.h:198
jmethodID m_ctor_Integer_with_int
Definition: jni_info.h:165
jclass m_class_Long
Definition: jni_info.h:145
jmethodID m_method_UnoRuntime_generateOid
Definition: jni_info.h:180
jmethodID m_ctor_Type_with_Class
Definition: jni_info.h:185
jfieldID m_field_JNI_proxy_m_receiver_handle
Definition: jni_info.h:193
jmethodID m_method_AsynchronousFinalizer_drain
Definition: jni_info.h:199
jclass m_class_Byte
Definition: jni_info.h:142
jobject m_object_Type_UNSIGNED_SHORT
Definition: jni_info.h:135
jmethodID m_method_UnoRuntime_queryInterface
Definition: jni_info.h:181
jobject m_object_Any_VOID
Definition: jni_info.h:134
JNI_type_info const * create_type_info(JNI_context const &jni, typelib_TypeDescription *td) const
Definition: jni_info.cxx:330
jmethodID m_method_Character_charValue
Definition: jni_info.h:171
jclass m_class_AsynchronousFinalizer
Definition: jni_info.h:156
jmethodID m_method_Object_toString
Definition: jni_info.h:158
jmethodID m_method_Throwable_getMessage
Definition: jni_info.h:160
typelib_TypeDescription * get() const
Definition: jni_base.h:240
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(JNIEnv *jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle) SAL_THROW_EXTERN_C()
Definition: jni_info.cxx:967
sal_uInt16 nPos
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
@ Exception
int i
bool type_equals(typelib_TypeDescriptionReference *type1, typelib_TypeDescriptionReference *type2)
Definition: jni_info.h:43
jstring ustring_to_jstring(JNI_context const &jni, rtl_uString const *ustr)
Definition: jni_helper.h:68
jclass find_class(JNI_context const &jni, char const *class_name, bool inException=false)
Definition: jni_helper.h:79
jobject create_type(JNI_context const &jni, jclass clazz)
Definition: jni_helper.h:102
bool is_XInterface(typelib_TypeDescriptionReference *type)
Definition: jni_info.h:56
args
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
std::unique_ptr< jfieldID[]> m_fields
Definition: jni_info.h:100
virtual void destroy(JNIEnv *jni_env) override
Definition: jni_info.cxx:210
JNI_compound_type_info(JNI_context const &jni, typelib_TypeDescription *td)
Definition: jni_info.cxx:218
JNI_type_info const * m_base
Definition: jni_info.h:96
JNI_interface_type_info(JNI_context const &jni, typelib_TypeDescription *td)
Definition: jni_info.cxx:66
virtual void destroy(JNIEnv *jni_env) override
Definition: jni_info.cxx:56
std::unique_ptr< jmethodID[]> m_methods
Definition: jni_info.h:84
JNI_type_info * m_info
Definition: jni_info.h:112
void destruct(JNIEnv *jni_env)
Definition: jni_info.h:72
::com::sun::star::uno::TypeDescription m_td
Definition: jni_info.h:67
JNI_type_info(const JNI_type_info &)=delete
virtual void destroy(JNIEnv *jni_env)=0
#define SAL_THROW_EXTERN_C()
ResultType type