tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / jni_uno / jni_info.cxx
blob5fddcc27819d16ae5343589cb39b8d29357f504f
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_helper.h"
26 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <jvmaccess/unovirtualmachine.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/strbuf.hxx>
32 #include <uno/lbnames.h>
35 namespace jni_uno
39 JNI_type_info::JNI_type_info(
40 JNI_context const & jni, typelib_TypeDescription * td )
41 : m_td( td ),
42 m_class( nullptr )
44 m_td.makeComplete();
45 if (! m_td.get()->bComplete)
47 throw BridgeRuntimeError(
48 "cannot make type complete: "
49 + OUString::unacquired( &m_td.get()->pTypeName )
50 + jni.get_stack_trace() );
55 void JNI_interface_type_info::destroy( JNIEnv * jni_env )
57 JNI_type_info::destruct( jni_env );
58 jni_env->DeleteGlobalRef( m_proxy_ctor );
59 jni_env->DeleteGlobalRef( m_type );
60 m_methods.reset();
61 delete this;
65 JNI_interface_type_info::JNI_interface_type_info(
66 JNI_context const & jni, typelib_TypeDescription * td_ )
67 : JNI_type_info( jni, td_ )
69 assert( m_td.get()->eTypeClass == typelib_TypeClass_INTERFACE );
71 OUString const & uno_name = OUString::unacquired( &m_td.get()->pTypeName );
72 JNI_info const * jni_info = jni.get_info();
74 JLocalAutoRef jo_class(
75 jni,
76 find_class(
77 jni,
78 ( OUStringToOString( uno_name, RTL_TEXTENCODING_JAVA_UTF8 ).
79 getStr() ) ) );
80 JLocalAutoRef jo_type( jni, create_type( jni, static_cast<jclass>(jo_class.get()) ) );
82 // get proxy ctor
83 jvalue arg;
84 arg.l = jo_class.get();
85 JLocalAutoRef jo_proxy_ctor(
86 jni, jni->CallStaticObjectMethodA(
87 jni_info->m_class_JNI_proxy,
88 jni_info->m_method_JNI_proxy_get_proxy_ctor, &arg ) );
90 if (is_XInterface( m_td.get()->pWeakRef ))
92 m_methods = nullptr; // no methods
94 else
96 // retrieve method ids for all direct members
97 try
99 typelib_InterfaceTypeDescription * td =
100 reinterpret_cast< typelib_InterfaceTypeDescription * >(
101 m_td.get() );
102 // coverity[ctor_dtor_leak] - on purpose
103 m_methods.reset(new jmethodID[ td->nMapFunctionIndexToMemberIndex ]);
104 sal_Int32 nMethodIndex = 0;
105 typelib_TypeDescriptionReference ** ppMembers = td->ppMembers;
106 sal_Int32 nMembers = td->nMembers;
108 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
110 TypeDescr member_td( ppMembers[ nPos ] );
112 OStringBuffer sig_buf( 64 );
114 if (member_td.get()->eTypeClass ==
115 typelib_TypeClass_INTERFACE_METHOD) // method
117 typelib_InterfaceMethodTypeDescription * method_td =
118 reinterpret_cast<
119 typelib_InterfaceMethodTypeDescription * >(
120 member_td.get() );
122 sig_buf.append( '(' );
123 for ( sal_Int32 i = 0; i < method_td->nParams; ++i )
125 typelib_MethodParameter const & param =
126 method_td->pParams[ i ];
127 if (param.bOut)
128 sig_buf.append( '[' );
129 JNI_info::append_sig( &sig_buf, param.pTypeRef );
131 sig_buf.append( ')' );
132 JNI_info::append_sig( &sig_buf, method_td->pReturnTypeRef );
134 OString method_signature( sig_buf.makeStringAndClear() );
135 OString method_name(
136 OUStringToOString( OUString::unacquired(
137 &method_td->aBase.pMemberName ),
138 RTL_TEXTENCODING_JAVA_UTF8 ) );
140 m_methods[ nMethodIndex ] = jni->GetMethodID(
141 static_cast<jclass>(jo_class.get()), method_name.getStr(),
142 method_signature.getStr() );
143 jni.ensure_no_exception();
144 assert( m_methods[ nMethodIndex ] != nullptr );
145 ++nMethodIndex;
147 else // attribute
149 assert(
150 member_td.get()->eTypeClass ==
151 typelib_TypeClass_INTERFACE_ATTRIBUTE );
152 typelib_InterfaceAttributeTypeDescription * attribute_td =
153 reinterpret_cast<
154 typelib_InterfaceAttributeTypeDescription * >(
155 member_td.get() );
157 // type sig
158 JNI_info::append_sig(
159 &sig_buf, attribute_td->pAttributeTypeRef );
160 OString type_sig( sig_buf.makeStringAndClear() );
161 sig_buf.ensureCapacity( 64 );
162 // member name
163 OUString const & member_name =
164 OUString::unacquired(
165 &attribute_td->aBase.pMemberName );
167 // getter
168 sig_buf.append( "()" + type_sig );
169 OString method_signature( sig_buf.makeStringAndClear() );
170 OString method_name(
171 OUStringToOString(
172 rtl::Concat2View("get" + member_name),
173 RTL_TEXTENCODING_JAVA_UTF8 ) );
174 m_methods[ nMethodIndex ] = jni->GetMethodID(
175 static_cast<jclass>(jo_class.get()), method_name.getStr(),
176 method_signature.getStr() );
177 jni.ensure_no_exception();
178 assert( m_methods[ nMethodIndex ] != nullptr );
179 ++nMethodIndex;
180 if (! attribute_td->bReadOnly)
182 // setter
183 method_signature = "(" + type_sig + ")V";
184 method_name = OUStringToOString(
185 rtl::Concat2View("set" + member_name),
186 RTL_TEXTENCODING_JAVA_UTF8 );
187 m_methods[ nMethodIndex ] = jni->GetMethodID(
188 static_cast<jclass>(jo_class.get()), method_name.getStr(),
189 method_signature.getStr() );
190 jni.ensure_no_exception();
191 assert( m_methods[ nMethodIndex ] != nullptr );
192 ++nMethodIndex;
197 catch (...)
199 m_methods.reset();
200 throw;
203 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
204 m_type = jni->NewGlobalRef( jo_type.get() );
205 m_proxy_ctor = jni->NewGlobalRef( jo_proxy_ctor.get() );
209 void JNI_compound_type_info::destroy( JNIEnv * jni_env )
211 JNI_type_info::destruct( jni_env );
212 m_fields.reset();
213 delete this;
217 JNI_compound_type_info::JNI_compound_type_info(
218 JNI_context const & jni, typelib_TypeDescription * td_ )
219 : JNI_type_info( jni, td_ ),
220 m_exc_ctor( nullptr )
222 assert( m_td.get()->eTypeClass == typelib_TypeClass_STRUCT ||
223 m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION );
224 typelib_CompoundTypeDescription * td =
225 reinterpret_cast< typelib_CompoundTypeDescription * >( m_td.get() );
227 OUString const & uno_name =
228 OUString::unacquired( &td->aBase.pTypeName );
230 // Erase type arguments of instantiated polymorphic struct types:
231 std::u16string_view nucleus;
232 sal_Int32 i = uno_name.indexOf( '<' );
233 if ( i < 0 ) {
234 nucleus = uno_name;
235 } else {
236 nucleus = uno_name.subView( 0, i );
238 JLocalAutoRef jo_class(
239 jni,
240 find_class(
241 jni,
242 OUStringToOString(
243 nucleus, RTL_TEXTENCODING_JAVA_UTF8 ).getStr() ) );
245 JNI_info const * jni_info = jni.get_info();
247 if (m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION)
249 // retrieve exc ctor( msg )
250 m_exc_ctor = jni->GetMethodID(
251 static_cast<jclass>(jo_class.get()), "<init>", "(Ljava/lang/String;)V" );
252 jni.ensure_no_exception();
253 assert( m_exc_ctor != nullptr );
256 // retrieve info for base type
257 typelib_TypeDescription * base_td =
258 type_equals(
259 td->aBase.pWeakRef,
260 jni_info->m_RuntimeException_type.getTypeLibType())
261 ? nullptr
262 : reinterpret_cast< typelib_TypeDescription * >(
263 td->pBaseTypeDescription );
264 m_base = (base_td == nullptr ? nullptr : jni_info->get_type_info( jni, base_td ));
268 if (type_equals(
269 td->aBase.pWeakRef,
270 jni_info->m_Exception_type.getTypeLibType() ) ||
271 type_equals(
272 td->aBase.pWeakRef,
273 jni_info->m_RuntimeException_type.getTypeLibType() ))
275 // coverity[ctor_dtor_leak] - on purpose
276 m_fields.reset(new jfieldID[ 2 ]);
277 m_fields[ 0 ] = nullptr; // special Throwable.getMessage()
278 // field Context
279 m_fields[ 1 ] = jni->GetFieldID(
280 static_cast<jclass>(jo_class.get()), "Context", "Ljava/lang/Object;" );
281 jni.ensure_no_exception();
282 assert( m_fields[ 1 ] != nullptr );
284 else
286 // retrieve field ids for all direct members
287 sal_Int32 nMembers = td->nMembers;
288 m_fields.reset(new jfieldID[ nMembers ]);
290 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
292 OString sig;
293 if (td->aBase.eTypeClass == typelib_TypeClass_STRUCT
294 && reinterpret_cast< typelib_StructTypeDescription * >(
295 td)->pParameterizedTypes != nullptr
296 && reinterpret_cast< typelib_StructTypeDescription * >(
297 td)->pParameterizedTypes[nPos])
299 sig = "Ljava/lang/Object;"_ostr;
300 } else {
301 OStringBuffer sig_buf( 32 );
302 JNI_info::append_sig( &sig_buf, td->ppTypeRefs[ nPos ] );
303 sig = sig_buf.makeStringAndClear();
306 OString member_name(
307 OUStringToOString(
308 OUString::unacquired( &td->ppMemberNames[ nPos ] ),
309 RTL_TEXTENCODING_JAVA_UTF8 ) );
311 m_fields[ nPos ] = jni->GetFieldID(
312 static_cast<jclass>(jo_class.get()), member_name.getStr(),
313 sig.getStr() );
314 jni.ensure_no_exception();
315 assert( m_fields[ nPos ] != nullptr );
319 catch (...)
321 m_fields.reset();
322 throw;
325 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
329 JNI_type_info const * JNI_info::create_type_info(
330 JNI_context const & jni, typelib_TypeDescription * td ) const
332 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
334 JNI_type_info * new_info;
335 switch (td->eTypeClass)
337 case typelib_TypeClass_STRUCT:
338 case typelib_TypeClass_EXCEPTION:
340 new_info = new JNI_compound_type_info( jni, td );
341 break;
343 case typelib_TypeClass_INTERFACE:
345 new_info = new JNI_interface_type_info( jni, td );
346 break;
348 default:
350 throw BridgeRuntimeError(
351 "type info not supported for " + uno_name + jni.get_stack_trace() );
355 // look up
356 JNI_type_info * info;
357 std::unique_lock guard( m_mutex );
358 JNI_type_info_holder & holder = m_type_map[ uno_name ];
359 if (holder.m_info == nullptr) // new insertion
361 holder.m_info = new_info;
362 guard.unlock();
363 info = new_info;
365 else // inserted in the meantime
367 info = holder.m_info;
368 guard.unlock();
369 new_info->destroy( jni.get_jni_env() );
371 return info;
375 JNI_type_info const * JNI_info::get_type_info(
376 JNI_context const & jni, typelib_TypeDescription * td ) const
378 if (is_XInterface( td->pWeakRef ))
380 return m_XInterface_type_info;
383 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
384 JNI_type_info const * info;
385 std::unique_lock guard( m_mutex );
387 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
388 if (iFind == m_type_map.end())
390 guard.unlock();
391 info = create_type_info( jni, td );
393 else
395 info = iFind->second.m_info;
398 return info;
402 JNI_type_info const * JNI_info::get_type_info(
403 JNI_context const & jni, typelib_TypeDescriptionReference * type ) const
405 if (is_XInterface( type ))
407 return m_XInterface_type_info;
410 OUString const & uno_name = OUString::unacquired( &type->pTypeName );
411 JNI_type_info const * info;
412 std::unique_lock guard( m_mutex );
413 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
414 if (iFind == m_type_map.end())
416 guard.unlock();
417 TypeDescr td( type );
418 info = create_type_info( jni, td.get() );
420 else
422 info = iFind->second.m_info;
425 return info;
429 JNI_type_info const * JNI_info::get_type_info(
430 JNI_context const & jni, OUString const & uno_name ) const
432 if ( uno_name == "com.sun.star.uno.XInterface" )
434 return m_XInterface_type_info;
437 JNI_type_info const * info;
438 std::unique_lock guard( m_mutex );
439 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
440 if (iFind == m_type_map.end())
442 guard.unlock();
443 css::uno::TypeDescription td( uno_name );
444 if (! td.is())
446 throw BridgeRuntimeError(
447 "UNO type not found: " + uno_name + jni.get_stack_trace() );
449 info = create_type_info( jni, td.get() );
451 else
453 info = iFind->second.m_info;
456 return info;
460 JNI_info::JNI_info(
461 JNIEnv * jni_env, jobject class_loader, jclass classClass,
462 jmethodID methodForName )
463 : m_class_Class( classClass ),
464 m_method_Class_forName( methodForName ),
465 m_class_JNI_proxy( nullptr ),
466 m_XInterface_queryInterface_td(
467 (reinterpret_cast< typelib_InterfaceTypeDescription * >(
468 css::uno::TypeDescription(
469 cppu::UnoType<css::uno::XInterface>::get())
470 .get())->ppMembers[ 0 ] ) ),
471 m_Exception_type(cppu::UnoType<css::uno::Exception>::get()),
472 m_RuntimeException_type(cppu::UnoType<css::uno::RuntimeException>::get()),
473 m_void_type(cppu::UnoType<void>::get()),
474 m_XInterface_type_info( nullptr )
476 JNI_context jni( this, jni_env, class_loader ); // !no proper jni_info!
478 // class lookup
479 JLocalAutoRef jo_Object(
480 jni, find_class( jni, "java.lang.Object" ) );
481 JLocalAutoRef jo_Class(
482 jni, find_class( jni, "java.lang.Class" ) );
483 JLocalAutoRef jo_Throwable(
484 jni, find_class( jni, "java.lang.Throwable" ) );
485 JLocalAutoRef jo_Character(
486 jni, find_class( jni, "java.lang.Character" ) );
487 JLocalAutoRef jo_Boolean(
488 jni, find_class( jni, "java.lang.Boolean" ) );
489 JLocalAutoRef jo_Byte(
490 jni, find_class( jni, "java.lang.Byte" ) );
491 JLocalAutoRef jo_Short(
492 jni, find_class( jni, "java.lang.Short" ) );
493 JLocalAutoRef jo_Integer(
494 jni, find_class( jni, "java.lang.Integer" ) );
495 JLocalAutoRef jo_Long(
496 jni, find_class( jni, "java.lang.Long" ) );
497 JLocalAutoRef jo_Float(
498 jni, find_class( jni, "java.lang.Float" ) );
499 JLocalAutoRef jo_Double(
500 jni, find_class( jni, "java.lang.Double" ) );
501 JLocalAutoRef jo_String(
502 jni, find_class( jni, "java.lang.String" ) );
503 JLocalAutoRef jo_RuntimeException(
504 jni, find_class( jni, "com.sun.star.uno.RuntimeException" ) );
505 JLocalAutoRef jo_UnoRuntime(
506 jni, find_class( jni, "com.sun.star.uno.UnoRuntime" ) );
507 JLocalAutoRef jo_Any(
508 jni, find_class( jni, "com.sun.star.uno.Any" ) );
509 JLocalAutoRef jo_Enum(
510 jni, find_class( jni, "com.sun.star.uno.Enum" ) );
511 JLocalAutoRef jo_Type(
512 jni, find_class( jni, "com.sun.star.uno.Type" ) );
513 JLocalAutoRef jo_TypeClass(
514 jni, find_class( jni, "com.sun.star.uno.TypeClass" ) );
515 JLocalAutoRef jo_IEnvironment(
516 jni, find_class( jni, "com.sun.star.uno.IEnvironment" ) );
517 JLocalAutoRef jo_JNI_proxy(
518 jni, find_class( jni, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
519 JLocalAutoRef jo_AsynchronousFinalizer(
520 jni, find_class( jni, "com.sun.star.lib.util.AsynchronousFinalizer" ) );
522 // method Object.toString()
523 m_method_Object_toString = jni->GetMethodID(
524 static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
525 jni.ensure_no_exception();
526 assert( m_method_Object_toString != nullptr );
527 // method Class.getName()
528 m_method_Class_getName = jni->GetMethodID(
529 static_cast<jclass>(jo_Class.get()), "getName", "()Ljava/lang/String;" );
530 jni.ensure_no_exception();
531 assert( m_method_Class_getName != nullptr );
533 // method Throwable.getMessage()
534 m_method_Throwable_getMessage = jni->GetMethodID(
535 static_cast<jclass>(jo_Throwable.get()), "getMessage", "()Ljava/lang/String;" );
536 jni.ensure_no_exception();
537 assert( m_method_Throwable_getMessage != nullptr );
539 // method Character.charValue()
540 m_method_Character_charValue = jni->GetMethodID(
541 static_cast<jclass>(jo_Character.get()), "charValue", "()C" );
542 jni.ensure_no_exception();
543 assert( m_method_Character_charValue != nullptr );
544 // method Boolean.booleanValue()
545 m_method_Boolean_booleanValue = jni->GetMethodID(
546 static_cast<jclass>(jo_Boolean.get()), "booleanValue", "()Z" );
547 jni.ensure_no_exception();
548 assert( m_method_Boolean_booleanValue != nullptr );
549 // method Byte.byteValue()
550 m_method_Byte_byteValue = jni->GetMethodID(
551 static_cast<jclass>(jo_Byte.get()), "byteValue", "()B" );
552 jni.ensure_no_exception();
553 assert( m_method_Byte_byteValue != nullptr );
554 // method Short.shortValue()
555 m_method_Short_shortValue = jni->GetMethodID(
556 static_cast<jclass>(jo_Short.get()), "shortValue", "()S" );
557 jni.ensure_no_exception();
558 assert( m_method_Short_shortValue != nullptr );
559 // method Integer.intValue()
560 m_method_Integer_intValue = jni->GetMethodID(
561 static_cast<jclass>(jo_Integer.get()), "intValue", "()I" );
562 jni.ensure_no_exception();
563 assert( m_method_Integer_intValue != nullptr );
564 // method Long.longValue()
565 m_method_Long_longValue = jni->GetMethodID(
566 static_cast<jclass>(jo_Long.get()), "longValue", "()J" );
567 jni.ensure_no_exception();
568 assert( m_method_Long_longValue != nullptr );
569 // method Float.floatValue()
570 m_method_Float_floatValue = jni->GetMethodID(
571 static_cast<jclass>(jo_Float.get()), "floatValue", "()F" );
572 jni.ensure_no_exception();
573 assert( m_method_Float_floatValue != nullptr );
574 // method Double.doubleValue()
575 m_method_Double_doubleValue = jni->GetMethodID(
576 static_cast<jclass>(jo_Double.get()), "doubleValue", "()D" );
577 jni.ensure_no_exception();
578 assert( m_method_Double_doubleValue != nullptr );
580 // ctor Character( char )
581 m_ctor_Character_with_char = jni->GetMethodID(
582 static_cast<jclass>(jo_Character.get()), "<init>", "(C)V" );
583 jni.ensure_no_exception();
584 assert( m_ctor_Character_with_char != nullptr );
585 // ctor Boolean( boolean )
586 m_ctor_Boolean_with_boolean = jni->GetMethodID(
587 static_cast<jclass>(jo_Boolean.get()), "<init>", "(Z)V" );
588 jni.ensure_no_exception();
589 assert( m_ctor_Boolean_with_boolean != nullptr );
590 // ctor Byte( byte )
591 m_ctor_Byte_with_byte = jni->GetMethodID(
592 static_cast<jclass>(jo_Byte.get()), "<init>", "(B)V" );
593 jni.ensure_no_exception();
594 assert( m_ctor_Byte_with_byte != nullptr );
595 // ctor Short( short )
596 m_ctor_Short_with_short = jni->GetMethodID(
597 static_cast<jclass>(jo_Short.get()), "<init>", "(S)V" );
598 jni.ensure_no_exception();
599 assert( m_ctor_Short_with_short != nullptr );
600 // ctor Integer( int )
601 m_ctor_Integer_with_int = jni->GetMethodID(
602 static_cast<jclass>(jo_Integer.get()), "<init>", "(I)V" );
603 jni.ensure_no_exception();
604 assert( m_ctor_Integer_with_int != nullptr );
605 // ctor Long( long )
606 m_ctor_Long_with_long = jni->GetMethodID(
607 static_cast<jclass>(jo_Long.get()), "<init>", "(J)V" );
608 jni.ensure_no_exception();
609 assert( m_ctor_Long_with_long != nullptr );
610 // ctor Float( float )
611 m_ctor_Float_with_float = jni->GetMethodID(
612 static_cast<jclass>(jo_Float.get()), "<init>", "(F)V" );
613 jni.ensure_no_exception();
614 assert( m_ctor_Float_with_float != nullptr );
615 // ctor Double( double )
616 m_ctor_Double_with_double = jni->GetMethodID(
617 static_cast<jclass>(jo_Double.get()), "<init>", "(D)V" );
618 jni.ensure_no_exception();
619 assert( m_ctor_Double_with_double != nullptr );
621 // static method UnoRuntime.generateOid()
622 m_method_UnoRuntime_generateOid = jni->GetStaticMethodID(
623 static_cast<jclass>(jo_UnoRuntime.get()),
624 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
625 jni.ensure_no_exception();
626 assert( m_method_UnoRuntime_generateOid != nullptr );
627 // static method UnoRuntime.queryInterface()
628 m_method_UnoRuntime_queryInterface = jni->GetStaticMethodID(
629 static_cast<jclass>(jo_UnoRuntime.get()),
630 "queryInterface",
631 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
632 jni.ensure_no_exception();
633 assert( m_method_UnoRuntime_queryInterface != nullptr );
635 // field Enum.m_value
636 m_field_Enum_m_value = jni->GetFieldID(
637 static_cast<jclass>(jo_Enum.get()), "m_value", "I" );
638 jni.ensure_no_exception();
639 assert( m_field_Enum_m_value != nullptr );
641 // static method TypeClass.fromInt()
642 m_method_TypeClass_fromInt = jni->GetStaticMethodID(
643 static_cast<jclass>(jo_TypeClass.get()),
644 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
645 jni.ensure_no_exception();
646 assert( m_method_TypeClass_fromInt != nullptr );
648 // ctor Type( Class )
649 m_ctor_Type_with_Class = jni->GetMethodID(
650 static_cast<jclass>(jo_Type.get()), "<init>", "(Ljava/lang/Class;)V" );
651 jni.ensure_no_exception();
652 assert( m_ctor_Type_with_Class != nullptr );
653 // ctor Type( String, TypeClass )
654 m_ctor_Type_with_Name_TypeClass = jni->GetMethodID(
655 static_cast<jclass>(jo_Type.get()),
656 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
657 jni.ensure_no_exception();
658 assert( m_ctor_Type_with_Name_TypeClass != nullptr );
659 // field Type._typeName
660 m_field_Type_typeName = jni->GetFieldID(
661 static_cast<jclass>(jo_Type.get()), "_typeName", "Ljava/lang/String;" );
662 jni.ensure_no_exception();
663 assert( m_field_Type_typeName != nullptr );
665 // ctor Any( Type, Object )
666 m_ctor_Any_with_Type_Object = jni->GetMethodID(
667 static_cast<jclass>(jo_Any.get()),
668 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
669 jni.ensure_no_exception();
670 assert( m_ctor_Any_with_Type_Object != nullptr );
672 // field Any._type
673 m_field_Any_type = jni->GetFieldID(
674 static_cast<jclass>(jo_Any.get()), "_type", "Lcom/sun/star/uno/Type;" );
675 jni.ensure_no_exception();
676 assert( m_field_Any_type != nullptr );
677 // field Any._object
678 m_field_Any_object = jni->GetFieldID(
679 static_cast<jclass>(jo_Any.get()), "_object", "Ljava/lang/Object;" );
680 jni.ensure_no_exception();
681 assert( m_field_Any_object != nullptr );
683 // method IEnvironment.getRegisteredInterface()
684 m_method_IEnvironment_getRegisteredInterface = jni->GetMethodID(
685 static_cast<jclass>(jo_IEnvironment.get()),
686 "getRegisteredInterface",
687 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
688 jni.ensure_no_exception();
689 assert( m_method_IEnvironment_getRegisteredInterface != nullptr );
690 // method IEnvironment.registerInterface()
691 m_method_IEnvironment_registerInterface = jni->GetMethodID(
692 static_cast<jclass>(jo_IEnvironment.get()), "registerInterface",
693 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
694 "Ljava/lang/Object;" );
695 jni.ensure_no_exception();
696 assert( m_method_IEnvironment_registerInterface != nullptr );
698 // static method JNI_proxy.get_proxy_ctor()
699 m_method_JNI_proxy_get_proxy_ctor = jni->GetStaticMethodID(
700 static_cast<jclass>(jo_JNI_proxy.get()), "get_proxy_ctor",
701 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
702 jni.ensure_no_exception();
703 assert( m_method_JNI_proxy_get_proxy_ctor != nullptr );
704 // static method JNI_proxy.create()
705 m_method_JNI_proxy_create = jni->GetStaticMethodID(
706 static_cast<jclass>(jo_JNI_proxy.get()), "create",
707 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
708 "/String;Ljava/lang/reflect/Constructor;"
709 "Lcom/sun/star/lib/util/AsynchronousFinalizer;)Ljava/lang/Object;" );
710 jni.ensure_no_exception();
711 assert( m_method_JNI_proxy_create != nullptr );
712 // field JNI_proxy.m_receiver_handle
713 m_field_JNI_proxy_m_receiver_handle = jni->GetFieldID(
714 static_cast<jclass>(jo_JNI_proxy.get()), "m_receiver_handle", "J" );
715 jni.ensure_no_exception();
716 assert( m_field_JNI_proxy_m_receiver_handle != nullptr );
717 // field JNI_proxy.m_td_handle
718 m_field_JNI_proxy_m_td_handle = jni->GetFieldID(
719 static_cast<jclass>(jo_JNI_proxy.get()), "m_td_handle", "J" );
720 jni.ensure_no_exception();
721 assert( m_field_JNI_proxy_m_td_handle != nullptr );
722 // field JNI_proxy.m_type
723 m_field_JNI_proxy_m_type = jni->GetFieldID(
724 static_cast<jclass>(jo_JNI_proxy.get()), "m_type", "Lcom/sun/star/uno/Type;" );
725 jni.ensure_no_exception();
726 assert( m_field_JNI_proxy_m_type != nullptr );
727 // field JNI_proxy.m_oid
728 m_field_JNI_proxy_m_oid = jni->GetFieldID(
729 static_cast<jclass>(jo_JNI_proxy.get()), "m_oid", "Ljava/lang/String;" );
730 jni.ensure_no_exception();
731 assert( m_field_JNI_proxy_m_oid != nullptr );
733 // ctor AsynchronousFinalizer
734 m_ctor_AsynchronousFinalizer = jni->GetMethodID(
735 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "<init>", "()V" );
736 jni.ensure_no_exception();
737 assert( m_ctor_AsynchronousFinalizer != nullptr );
738 // method AsynchronousFinalizer.drain()
739 m_method_AsynchronousFinalizer_drain = jni->GetMethodID(
740 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "drain", "()V" );
741 jni.ensure_no_exception();
742 assert( m_method_AsynchronousFinalizer_drain != nullptr );
744 // get java env
745 OUString java_env_type_name( u"" UNO_LB_JAVA ""_ustr );
746 JLocalAutoRef jo_java(
747 jni, ustring_to_jstring( jni, java_env_type_name.pData ) );
748 jvalue args[ 2 ];
749 args[ 0 ].l = jo_java.get();
750 args[ 1 ].l = nullptr;
751 jmethodID method_getEnvironment = jni->GetStaticMethodID(
752 static_cast<jclass>(jo_UnoRuntime.get()), "getEnvironment",
753 "(Ljava/lang/String;Ljava/lang/Object;)"
754 "Lcom/sun/star/uno/IEnvironment;" );
755 jni.ensure_no_exception();
756 assert( method_getEnvironment != nullptr );
757 JLocalAutoRef jo_java_env(
758 jni, jni->CallStaticObjectMethodA(
759 static_cast<jclass>(jo_UnoRuntime.get()), method_getEnvironment, args ) );
761 // get com.sun.star.uno.Any.VOID
762 jfieldID field_Any_VOID = jni->GetStaticFieldID(
763 static_cast<jclass>(jo_Any.get()), "VOID", "Lcom/sun/star/uno/Any;" );
764 jni.ensure_no_exception();
765 assert( field_Any_VOID != nullptr );
766 JLocalAutoRef jo_Any_VOID(
767 jni, jni->GetStaticObjectField(
768 static_cast<jclass>(jo_Any.get()), field_Any_VOID ) );
769 // get com.sun.star.uno.Type.UNSIGNED_SHORT
770 jfieldID field_Type_UNSIGNED_SHORT = jni->GetStaticFieldID(
771 static_cast<jclass>(jo_Type.get()), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
772 jni.ensure_no_exception();
773 assert( field_Type_UNSIGNED_SHORT != nullptr );
774 JLocalAutoRef jo_Type_UNSIGNED_SHORT(
775 jni, jni->GetStaticObjectField(
776 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_SHORT ) );
777 // get com.sun.star.uno.Type.UNSIGNED_LONG
778 jfieldID field_Type_UNSIGNED_LONG = jni->GetStaticFieldID(
779 static_cast<jclass>(jo_Type.get()), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
780 jni.ensure_no_exception();
781 assert( field_Type_UNSIGNED_LONG != nullptr );
782 JLocalAutoRef jo_Type_UNSIGNED_LONG(
783 jni, jni->GetStaticObjectField(
784 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_LONG ) );
785 // get com.sun.star.uno.Type.UNSIGNED_HYPER
786 jfieldID field_Type_UNSIGNED_HYPER = jni->GetStaticFieldID(
787 static_cast<jclass>(jo_Type.get()), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
788 jni.ensure_no_exception();
789 assert( field_Type_UNSIGNED_HYPER != nullptr );
790 JLocalAutoRef jo_Type_UNSIGNED_HYPER(
791 jni, jni->GetStaticObjectField(
792 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_HYPER ) );
794 // make global refs
795 m_class_UnoRuntime =
796 static_cast<jclass>(jni->NewGlobalRef( jo_UnoRuntime.get() ));
797 m_class_RuntimeException =
798 static_cast<jclass>(jni->NewGlobalRef( jo_RuntimeException.get() ));
799 m_class_Any =
800 static_cast<jclass>(jni->NewGlobalRef( jo_Any.get() ));
801 m_class_Type =
802 static_cast<jclass>(jni->NewGlobalRef( jo_Type.get() ));
803 m_class_TypeClass =
804 static_cast<jclass>(jni->NewGlobalRef( jo_TypeClass.get() ));
805 m_class_JNI_proxy =
806 static_cast<jclass>(jni->NewGlobalRef( jo_JNI_proxy.get() ));
807 m_class_AsynchronousFinalizer =
808 static_cast<jclass>(jni->NewGlobalRef( jo_AsynchronousFinalizer.get() ));
810 m_class_Character =
811 static_cast<jclass>(jni->NewGlobalRef( jo_Character.get() ));
812 m_class_Boolean =
813 static_cast<jclass>(jni->NewGlobalRef( jo_Boolean.get() ));
814 m_class_Byte =
815 static_cast<jclass>(jni->NewGlobalRef( jo_Byte.get() ));
816 m_class_Short =
817 static_cast<jclass>(jni->NewGlobalRef( jo_Short.get() ));
818 m_class_Integer =
819 static_cast<jclass>(jni->NewGlobalRef( jo_Integer.get() ));
820 m_class_Long =
821 static_cast<jclass>(jni->NewGlobalRef( jo_Long.get() ));
822 m_class_Float =
823 static_cast<jclass>(jni->NewGlobalRef( jo_Float.get() ));
824 m_class_Double =
825 static_cast<jclass>(jni->NewGlobalRef( jo_Double.get() ));
826 m_class_String =
827 static_cast<jclass>(jni->NewGlobalRef( jo_String.get() ));
828 m_class_Object =
829 static_cast<jclass>(jni->NewGlobalRef( jo_Object.get() ));
830 m_class_Class =
831 static_cast<jclass>(jni->NewGlobalRef( m_class_Class ));
833 m_object_Any_VOID =
834 jni->NewGlobalRef( jo_Any_VOID.get() );
835 m_object_Type_UNSIGNED_SHORT =
836 jni->NewGlobalRef( jo_Type_UNSIGNED_SHORT.get() );
837 m_object_Type_UNSIGNED_LONG =
838 jni->NewGlobalRef( jo_Type_UNSIGNED_LONG.get() );
839 m_object_Type_UNSIGNED_HYPER =
840 jni->NewGlobalRef( jo_Type_UNSIGNED_HYPER.get() );
841 m_object_java_env = jni->NewGlobalRef( jo_java_env.get() );
845 css::uno::TypeDescription XInterface_td(
846 cppu::UnoType<css::uno::XInterface>::get());
847 // coverity[ctor_dtor_leak] - on purpose
848 m_XInterface_type_info =
849 new JNI_interface_type_info( jni, XInterface_td.get() );
851 catch (...)
853 destruct( jni_env );
854 throw;
859 void JNI_info::destruct( JNIEnv * jni_env )
861 for (auto & i: m_type_map)
863 i.second.m_info->destroy( jni_env );
865 if (m_XInterface_type_info != nullptr)
867 const_cast< JNI_interface_type_info * >(
868 m_XInterface_type_info )->destroy( jni_env );
871 // free global refs
872 jni_env->DeleteGlobalRef( m_object_java_env );
873 jni_env->DeleteGlobalRef( m_object_Any_VOID );
874 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT );
875 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG );
876 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER );
878 jni_env->DeleteGlobalRef( m_class_Class );
879 jni_env->DeleteGlobalRef( m_class_Object );
880 jni_env->DeleteGlobalRef( m_class_String );
881 jni_env->DeleteGlobalRef( m_class_Double );
882 jni_env->DeleteGlobalRef( m_class_Float );
883 jni_env->DeleteGlobalRef( m_class_Long );
884 jni_env->DeleteGlobalRef( m_class_Integer );
885 jni_env->DeleteGlobalRef( m_class_Short );
886 jni_env->DeleteGlobalRef( m_class_Byte );
887 jni_env->DeleteGlobalRef( m_class_Boolean );
888 jni_env->DeleteGlobalRef( m_class_Character );
890 jni_env->DeleteGlobalRef( m_class_AsynchronousFinalizer );
891 jni_env->DeleteGlobalRef( m_class_JNI_proxy );
892 jni_env->DeleteGlobalRef( m_class_RuntimeException );
893 jni_env->DeleteGlobalRef( m_class_UnoRuntime );
894 jni_env->DeleteGlobalRef( m_class_TypeClass );
895 jni_env->DeleteGlobalRef( m_class_Type );
896 jni_env->DeleteGlobalRef( m_class_Any );
900 JNI_info const * JNI_info::get_jni_info(
901 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm )
903 // !!!no JNI_info available at JNI_context!!!
904 ::jvmaccess::VirtualMachine::AttachGuard guard(
905 uno_vm->getVirtualMachine() );
906 JNIEnv * jni_env = guard.getEnvironment();
907 JNI_context jni(
908 nullptr, jni_env, static_cast< jobject >(uno_vm->getClassLoader()) );
910 jclass jo_class;
911 jmethodID jo_forName;
912 jni.getClassForName( &jo_class, &jo_forName );
913 jni.ensure_no_exception();
914 JLocalAutoRef jo_JNI_info_holder(
915 jni,
916 jni.findClass(
917 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class,
918 jo_forName, false ) );
919 // field JNI_info_holder.m_jni_info_handle
920 jfieldID field_s_jni_info_handle =
921 jni->GetStaticFieldID(
922 static_cast<jclass>(jo_JNI_info_holder.get()), "s_jni_info_handle", "J" );
923 jni.ensure_no_exception();
924 assert( field_s_jni_info_handle != nullptr );
926 JNI_info const * jni_info =
927 reinterpret_cast< JNI_info const * >(
928 jni->GetStaticLongField(
929 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle ) );
930 if (jni_info == nullptr) // un-initialized?
932 JNI_info * new_info = new JNI_info(
933 jni_env, static_cast< jobject >(uno_vm->getClassLoader()), jo_class,
934 jo_forName );
936 osl::ClearableMutexGuard g( osl::Mutex::getGlobalMutex() );
937 jni_info =
938 reinterpret_cast< JNI_info const * >(
939 jni->GetStaticLongField(
940 static_cast<jclass>(jo_JNI_info_holder.get()),
941 field_s_jni_info_handle ) );
942 if (jni_info == nullptr) // still un-initialized?
944 jni->SetStaticLongField(
945 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle,
946 reinterpret_cast< jlong >( new_info ) );
947 jni_info = new_info;
949 else
951 g.clear();
952 new_info->destroy( jni_env );
956 return jni_info;
961 extern "C"
965 SAL_JNI_EXPORT void
966 JNICALL Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
967 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle ) noexcept
969 ::jni_uno::JNI_info * jni_info =
970 reinterpret_cast< ::jni_uno::JNI_info * >( jni_info_handle );
971 jni_info->destroy( jni_env );
976 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */