Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / bridges / source / jni_uno / jni_info.cxx
blob0195ed9d13005fe7fe0aed6215a9ad65286fd7ef
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <sal/config.h>
22 #include <cassert>
24 #include "jni_bridge.h"
26 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <jvmaccess/unovirtualmachine.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/strbuf.hxx>
31 #include <rtl/ustrbuf.hxx>
33 #include <uno/lbnames.h>
36 namespace jni_uno
40 JNI_type_info::JNI_type_info(
41 JNI_context const & jni, typelib_TypeDescription * td )
42 : m_td( td ),
43 m_class( nullptr )
45 m_td.makeComplete();
46 if (! m_td.get()->bComplete)
48 throw BridgeRuntimeError(
49 "cannot make type complete: "
50 + OUString::unacquired( &m_td.get()->pTypeName )
51 + jni.get_stack_trace() );
56 void JNI_interface_type_info::destroy( JNIEnv * jni_env )
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;
66 JNI_interface_type_info::JNI_interface_type_info(
67 JNI_context const & jni, typelib_TypeDescription * td_ )
68 : JNI_type_info( jni, td_ )
70 assert( m_td.get()->eTypeClass == typelib_TypeClass_INTERFACE );
72 OUString const & uno_name = OUString::unacquired( &m_td.get()->pTypeName );
73 JNI_info const * jni_info = jni.get_info();
75 JLocalAutoRef jo_class(
76 jni,
77 find_class(
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()) ) );
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 ) );
91 if (is_XInterface( m_td.get()->pWeakRef ))
93 m_methods = nullptr; // no methods
95 else
97 // retrieve method ids for all direct members
98 try
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;
109 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
111 TypeDescr member_td( ppMembers[ nPos ] );
113 OStringBuffer sig_buf( 64 );
115 if (member_td.get()->eTypeClass ==
116 typelib_TypeClass_INTERFACE_METHOD) // method
118 typelib_InterfaceMethodTypeDescription * method_td =
119 reinterpret_cast<
120 typelib_InterfaceMethodTypeDescription * >(
121 member_td.get() );
123 sig_buf.append( '(' );
124 for ( sal_Int32 i = 0; i < method_td->nParams; ++i )
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 );
132 sig_buf.append( ')' );
133 JNI_info::append_sig( &sig_buf, method_td->pReturnTypeRef );
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 ) );
141 m_methods[ nMethodIndex ] = jni->GetMethodID(
142 static_cast<jclass>(jo_class.get()), method_name.getStr(),
143 method_signature.getStr() );
144 jni.ensure_no_exception();
145 assert( m_methods[ nMethodIndex ] != nullptr );
146 ++nMethodIndex;
148 else // attribute
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() );
158 // type sig
159 JNI_info::append_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 );
168 // getter
169 sig_buf.append( "()" + type_sig );
170 OString method_signature( sig_buf.makeStringAndClear() );
171 OString method_name(
172 OUStringToOString(
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() );
178 jni.ensure_no_exception();
179 assert( m_methods[ nMethodIndex ] != nullptr );
180 ++nMethodIndex;
181 if (! attribute_td->bReadOnly)
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() );
191 jni.ensure_no_exception();
192 assert( m_methods[ nMethodIndex ] != nullptr );
193 ++nMethodIndex;
198 catch (...)
200 m_methods.reset();
201 throw;
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() );
210 void JNI_compound_type_info::destroy( JNIEnv * jni_env )
212 JNI_type_info::destruct( jni_env );
213 m_fields.reset();
214 delete this;
218 JNI_compound_type_info::JNI_compound_type_info(
219 JNI_context const & jni, typelib_TypeDescription * td_ )
220 : JNI_type_info( jni, td_ ),
221 m_exc_ctor( nullptr )
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() );
228 OUString const & uno_name =
229 OUString::unacquired( &td->aBase.pTypeName );
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 );
239 JLocalAutoRef jo_class(
240 jni,
241 find_class(
242 jni,
243 OUStringToOString(
244 nucleus, RTL_TEXTENCODING_JAVA_UTF8 ).getStr() ) );
246 JNI_info const * jni_info = jni.get_info();
248 if (m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION)
250 // retrieve exc ctor( msg )
251 m_exc_ctor = jni->GetMethodID(
252 static_cast<jclass>(jo_class.get()), "<init>", "(Ljava/lang/String;)V" );
253 jni.ensure_no_exception();
254 assert( m_exc_ctor != nullptr );
257 // retrieve info for base type
258 typelib_TypeDescription * base_td =
259 type_equals(
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 ));
269 if (type_equals(
270 td->aBase.pWeakRef,
271 jni_info->m_Exception_type.getTypeLibType() ) ||
272 type_equals(
273 td->aBase.pWeakRef,
274 jni_info->m_RuntimeException_type.getTypeLibType() ))
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;" );
282 jni.ensure_no_exception();
283 assert( m_fields[ 1 ] != nullptr );
285 else
287 // retrieve field ids for all direct members
288 sal_Int32 nMembers = td->nMembers;
289 m_fields.reset(new jfieldID[ nMembers ]);
291 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
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])
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();
307 OString member_name(
308 OUStringToOString(
309 OUString::unacquired( &td->ppMemberNames[ nPos ] ),
310 RTL_TEXTENCODING_JAVA_UTF8 ) );
312 m_fields[ nPos ] = jni->GetFieldID(
313 static_cast<jclass>(jo_class.get()), member_name.getStr(),
314 sig.getStr() );
315 jni.ensure_no_exception();
316 assert( m_fields[ nPos ] != nullptr );
320 catch (...)
322 m_fields.reset();
323 throw;
326 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
330 JNI_type_info const * JNI_info::create_type_info(
331 JNI_context const & jni, typelib_TypeDescription * td ) const
333 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
335 JNI_type_info * new_info;
336 switch (td->eTypeClass)
338 case typelib_TypeClass_STRUCT:
339 case typelib_TypeClass_EXCEPTION:
341 new_info = new JNI_compound_type_info( jni, td );
342 break;
344 case typelib_TypeClass_INTERFACE:
346 new_info = new JNI_interface_type_info( jni, td );
347 break;
349 default:
351 throw BridgeRuntimeError(
352 "type info not supported for " + uno_name + jni.get_stack_trace() );
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
362 holder.m_info = new_info;
363 guard.unlock();
364 info = new_info;
366 else // inserted in the meantime
368 info = holder.m_info;
369 guard.unlock();
370 new_info->destroy( jni.get_jni_env() );
372 return info;
376 JNI_type_info const * JNI_info::get_type_info(
377 JNI_context const & jni, typelib_TypeDescription * td ) const
379 if (is_XInterface( td->pWeakRef ))
381 return m_XInterface_type_info;
384 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
385 JNI_type_info const * info;
386 std::unique_lock guard( m_mutex );
388 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
389 if (iFind == m_type_map.end())
391 guard.unlock();
392 info = create_type_info( jni, td );
394 else
396 info = iFind->second.m_info;
399 return info;
403 JNI_type_info const * JNI_info::get_type_info(
404 JNI_context const & jni, typelib_TypeDescriptionReference * type ) const
406 if (is_XInterface( type ))
408 return m_XInterface_type_info;
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())
417 guard.unlock();
418 TypeDescr td( type );
419 info = create_type_info( jni, td.get() );
421 else
423 info = iFind->second.m_info;
426 return info;
430 JNI_type_info const * JNI_info::get_type_info(
431 JNI_context const & jni, OUString const & uno_name ) const
433 if ( uno_name == "com.sun.star.uno.XInterface" )
435 return m_XInterface_type_info;
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())
443 guard.unlock();
444 css::uno::TypeDescription td( uno_name );
445 if (! td.is())
447 throw BridgeRuntimeError(
448 "UNO type not found: " + uno_name + jni.get_stack_trace() );
450 info = create_type_info( jni, td.get() );
452 else
454 info = iFind->second.m_info;
457 return info;
461 JNI_info::JNI_info(
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 )
477 JNI_context jni( this, jni_env, class_loader ); // !no proper jni_info!
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" ) );
523 // method Object.toString()
524 m_method_Object_toString = jni->GetMethodID(
525 static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
526 jni.ensure_no_exception();
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;" );
531 jni.ensure_no_exception();
532 assert( m_method_Class_getName != nullptr );
534 // method Throwable.getMessage()
535 m_method_Throwable_getMessage = jni->GetMethodID(
536 static_cast<jclass>(jo_Throwable.get()), "getMessage", "()Ljava/lang/String;" );
537 jni.ensure_no_exception();
538 assert( m_method_Throwable_getMessage != nullptr );
540 // method Character.charValue()
541 m_method_Character_charValue = jni->GetMethodID(
542 static_cast<jclass>(jo_Character.get()), "charValue", "()C" );
543 jni.ensure_no_exception();
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" );
548 jni.ensure_no_exception();
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" );
553 jni.ensure_no_exception();
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" );
558 jni.ensure_no_exception();
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" );
563 jni.ensure_no_exception();
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" );
568 jni.ensure_no_exception();
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" );
573 jni.ensure_no_exception();
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" );
578 jni.ensure_no_exception();
579 assert( m_method_Double_doubleValue != nullptr );
581 // ctor Character( char )
582 m_ctor_Character_with_char = jni->GetMethodID(
583 static_cast<jclass>(jo_Character.get()), "<init>", "(C)V" );
584 jni.ensure_no_exception();
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" );
589 jni.ensure_no_exception();
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" );
594 jni.ensure_no_exception();
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" );
599 jni.ensure_no_exception();
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" );
604 jni.ensure_no_exception();
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" );
609 jni.ensure_no_exception();
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" );
614 jni.ensure_no_exception();
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" );
619 jni.ensure_no_exception();
620 assert( m_ctor_Double_with_double != nullptr );
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;" );
626 jni.ensure_no_exception();
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;" );
633 jni.ensure_no_exception();
634 assert( m_method_UnoRuntime_queryInterface != nullptr );
636 // field Enum.m_value
637 m_field_Enum_m_value = jni->GetFieldID(
638 static_cast<jclass>(jo_Enum.get()), "m_value", "I" );
639 jni.ensure_no_exception();
640 assert( m_field_Enum_m_value != nullptr );
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;" );
646 jni.ensure_no_exception();
647 assert( m_method_TypeClass_fromInt != nullptr );
649 // ctor Type( Class )
650 m_ctor_Type_with_Class = jni->GetMethodID(
651 static_cast<jclass>(jo_Type.get()), "<init>", "(Ljava/lang/Class;)V" );
652 jni.ensure_no_exception();
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" );
658 jni.ensure_no_exception();
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;" );
663 jni.ensure_no_exception();
664 assert( m_field_Type_typeName != nullptr );
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" );
670 jni.ensure_no_exception();
671 assert( m_ctor_Any_with_Type_Object != nullptr );
673 // field Any._type
674 m_field_Any_type = jni->GetFieldID(
675 static_cast<jclass>(jo_Any.get()), "_type", "Lcom/sun/star/uno/Type;" );
676 jni.ensure_no_exception();
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;" );
681 jni.ensure_no_exception();
682 assert( m_field_Any_object != nullptr );
684 // method IEnvironment.getRegisteredInterface()
685 m_method_IEnvironment_getRegisteredInterface = jni->GetMethodID(
686 static_cast<jclass>(jo_IEnvironment.get()),
687 "getRegisteredInterface",
688 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
689 jni.ensure_no_exception();
690 assert( m_method_IEnvironment_getRegisteredInterface != nullptr );
691 // method IEnvironment.registerInterface()
692 m_method_IEnvironment_registerInterface = jni->GetMethodID(
693 static_cast<jclass>(jo_IEnvironment.get()), "registerInterface",
694 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
695 "Ljava/lang/Object;" );
696 jni.ensure_no_exception();
697 assert( m_method_IEnvironment_registerInterface != nullptr );
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;" );
703 jni.ensure_no_exception();
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;" );
711 jni.ensure_no_exception();
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" );
716 jni.ensure_no_exception();
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" );
721 jni.ensure_no_exception();
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;" );
726 jni.ensure_no_exception();
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;" );
731 jni.ensure_no_exception();
732 assert( m_field_JNI_proxy_m_oid != nullptr );
734 // ctor AsynchronousFinalizer
735 m_ctor_AsynchronousFinalizer = jni->GetMethodID(
736 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "<init>", "()V" );
737 jni.ensure_no_exception();
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" );
742 jni.ensure_no_exception();
743 assert( m_method_AsynchronousFinalizer_drain != nullptr );
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;" );
756 jni.ensure_no_exception();
757 assert( method_getEnvironment != nullptr );
758 JLocalAutoRef jo_java_env(
759 jni, jni->CallStaticObjectMethodA(
760 static_cast<jclass>(jo_UnoRuntime.get()), method_getEnvironment, args ) );
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;" );
765 jni.ensure_no_exception();
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;" );
773 jni.ensure_no_exception();
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;" );
781 jni.ensure_no_exception();
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;" );
789 jni.ensure_no_exception();
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 ) );
795 // make global refs
796 m_class_UnoRuntime =
797 static_cast<jclass>(jni->NewGlobalRef( jo_UnoRuntime.get() ));
798 m_class_RuntimeException =
799 static_cast<jclass>(jni->NewGlobalRef( jo_RuntimeException.get() ));
800 m_class_Any =
801 static_cast<jclass>(jni->NewGlobalRef( jo_Any.get() ));
802 m_class_Type =
803 static_cast<jclass>(jni->NewGlobalRef( jo_Type.get() ));
804 m_class_TypeClass =
805 static_cast<jclass>(jni->NewGlobalRef( jo_TypeClass.get() ));
806 m_class_JNI_proxy =
807 static_cast<jclass>(jni->NewGlobalRef( jo_JNI_proxy.get() ));
808 m_class_AsynchronousFinalizer =
809 static_cast<jclass>(jni->NewGlobalRef( jo_AsynchronousFinalizer.get() ));
811 m_class_Character =
812 static_cast<jclass>(jni->NewGlobalRef( jo_Character.get() ));
813 m_class_Boolean =
814 static_cast<jclass>(jni->NewGlobalRef( jo_Boolean.get() ));
815 m_class_Byte =
816 static_cast<jclass>(jni->NewGlobalRef( jo_Byte.get() ));
817 m_class_Short =
818 static_cast<jclass>(jni->NewGlobalRef( jo_Short.get() ));
819 m_class_Integer =
820 static_cast<jclass>(jni->NewGlobalRef( jo_Integer.get() ));
821 m_class_Long =
822 static_cast<jclass>(jni->NewGlobalRef( jo_Long.get() ));
823 m_class_Float =
824 static_cast<jclass>(jni->NewGlobalRef( jo_Float.get() ));
825 m_class_Double =
826 static_cast<jclass>(jni->NewGlobalRef( jo_Double.get() ));
827 m_class_String =
828 static_cast<jclass>(jni->NewGlobalRef( jo_String.get() ));
829 m_class_Object =
830 static_cast<jclass>(jni->NewGlobalRef( jo_Object.get() ));
831 m_class_Class =
832 static_cast<jclass>(jni->NewGlobalRef( m_class_Class ));
834 m_object_Any_VOID =
835 jni->NewGlobalRef( jo_Any_VOID.get() );
836 m_object_Type_UNSIGNED_SHORT =
837 jni->NewGlobalRef( jo_Type_UNSIGNED_SHORT.get() );
838 m_object_Type_UNSIGNED_LONG =
839 jni->NewGlobalRef( jo_Type_UNSIGNED_LONG.get() );
840 m_object_Type_UNSIGNED_HYPER =
841 jni->NewGlobalRef( jo_Type_UNSIGNED_HYPER.get() );
842 m_object_java_env = jni->NewGlobalRef( jo_java_env.get() );
846 css::uno::TypeDescription XInterface_td(
847 cppu::UnoType<css::uno::XInterface>::get());
848 // coverity[ctor_dtor_leak] - on purpose
849 m_XInterface_type_info =
850 new JNI_interface_type_info( jni, XInterface_td.get() );
852 catch (...)
854 destruct( jni_env );
855 throw;
860 void JNI_info::destruct( JNIEnv * jni_env )
862 for (auto & i: m_type_map)
864 i.second.m_info->destroy( jni_env );
866 if (m_XInterface_type_info != nullptr)
868 const_cast< JNI_interface_type_info * >(
869 m_XInterface_type_info )->destroy( jni_env );
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 );
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 );
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 );
901 JNI_info const * JNI_info::get_jni_info(
902 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm )
904 // !!!no JNI_info available at JNI_context!!!
905 ::jvmaccess::VirtualMachine::AttachGuard guard(
906 uno_vm->getVirtualMachine() );
907 JNIEnv * jni_env = guard.getEnvironment();
908 JNI_context jni(
909 nullptr, jni_env, static_cast< jobject >(uno_vm->getClassLoader()) );
911 jclass jo_class;
912 jmethodID jo_forName;
913 jni.getClassForName( &jo_class, &jo_forName );
914 jni.ensure_no_exception();
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" );
924 jni.ensure_no_exception();
925 assert( field_s_jni_info_handle != nullptr );
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?
933 JNI_info * new_info = new JNI_info(
934 jni_env, static_cast< jobject >(uno_vm->getClassLoader()), jo_class,
935 jo_forName );
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?
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;
950 else
952 g.clear();
953 new_info->destroy( jni_env );
957 return jni_info;
962 extern "C"
966 SAL_JNI_EXPORT void
967 JNICALL Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
968 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle )
969 SAL_THROW_EXTERN_C()
971 ::jni_uno::JNI_info * jni_info =
972 reinterpret_cast< ::jni_uno::JNI_info * >( jni_info_handle );
973 jni_info->destroy( jni_env );
978 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */