bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / jni_uno / jni_info.cxx
blob70839e168eb2a74288888fe41c0efab747d00ee6
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 using namespace ::std;
37 using namespace ::osl;
39 namespace jni_uno
43 JNI_type_info::JNI_type_info(
44 JNI_context const & jni, typelib_TypeDescription * td )
45 : m_td( td ),
46 m_class( nullptr )
48 m_td.makeComplete();
49 if (! m_td.get()->bComplete)
51 throw BridgeRuntimeError(
52 "cannot make type complete: "
53 + OUString::unacquired( &m_td.get()->pTypeName )
54 + jni.get_stack_trace() );
59 void JNI_interface_type_info::destroy( JNIEnv * jni_env )
61 JNI_type_info::destruct( jni_env );
62 jni_env->DeleteGlobalRef( m_proxy_ctor );
63 jni_env->DeleteGlobalRef( m_type );
64 m_methods.reset();
65 delete this;
69 JNI_interface_type_info::JNI_interface_type_info(
70 JNI_context const & jni, typelib_TypeDescription * td_ )
71 : JNI_type_info( jni, td_ )
73 assert( m_td.get()->eTypeClass == typelib_TypeClass_INTERFACE );
75 OUString const & uno_name = OUString::unacquired( &m_td.get()->pTypeName );
76 JNI_info const * jni_info = jni.get_info();
78 JLocalAutoRef jo_class(
79 jni,
80 find_class(
81 jni,
82 ( OUStringToOString( uno_name, RTL_TEXTENCODING_JAVA_UTF8 ).
83 getStr() ) ) );
84 JLocalAutoRef jo_type( jni, create_type( jni, static_cast<jclass>(jo_class.get()) ) );
86 // get proxy ctor
87 jvalue arg;
88 arg.l = jo_class.get();
89 JLocalAutoRef jo_proxy_ctor(
90 jni, jni->CallStaticObjectMethodA(
91 jni_info->m_class_JNI_proxy,
92 jni_info->m_method_JNI_proxy_get_proxy_ctor, &arg ) );
94 if (is_XInterface( m_td.get()->pWeakRef ))
96 m_methods = nullptr; // no methods
98 else
100 // retrieve method ids for all direct members
103 typelib_InterfaceTypeDescription * td =
104 reinterpret_cast< typelib_InterfaceTypeDescription * >(
105 m_td.get() );
106 // coverity[ctor_dtor_leak] - on purpose
107 m_methods.reset(new jmethodID[ td->nMapFunctionIndexToMemberIndex ]);
108 sal_Int32 nMethodIndex = 0;
109 typelib_TypeDescriptionReference ** ppMembers = td->ppMembers;
110 sal_Int32 nMembers = td->nMembers;
112 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
114 TypeDescr member_td( ppMembers[ nPos ] );
116 OStringBuffer sig_buf( 64 );
118 if (member_td.get()->eTypeClass ==
119 typelib_TypeClass_INTERFACE_METHOD) // method
121 typelib_InterfaceMethodTypeDescription * method_td =
122 reinterpret_cast<
123 typelib_InterfaceMethodTypeDescription * >(
124 member_td.get() );
126 sig_buf.append( '(' );
127 for ( sal_Int32 i = 0; i < method_td->nParams; ++i )
129 typelib_MethodParameter const & param =
130 method_td->pParams[ i ];
131 if (param.bOut)
132 sig_buf.append( '[' );
133 JNI_info::append_sig( &sig_buf, param.pTypeRef );
135 sig_buf.append( ')' );
136 JNI_info::append_sig( &sig_buf, method_td->pReturnTypeRef );
138 OString method_signature( sig_buf.makeStringAndClear() );
139 OString method_name(
140 OUStringToOString( OUString::unacquired(
141 &method_td->aBase.pMemberName ),
142 RTL_TEXTENCODING_JAVA_UTF8 ) );
144 m_methods[ nMethodIndex ] = jni->GetMethodID(
145 static_cast<jclass>(jo_class.get()), method_name.getStr(),
146 method_signature.getStr() );
147 jni.ensure_no_exception();
148 assert( m_methods[ nMethodIndex ] != nullptr );
149 ++nMethodIndex;
151 else // attribute
153 assert(
154 member_td.get()->eTypeClass ==
155 typelib_TypeClass_INTERFACE_ATTRIBUTE );
156 typelib_InterfaceAttributeTypeDescription * attribute_td =
157 reinterpret_cast<
158 typelib_InterfaceAttributeTypeDescription * >(
159 member_td.get() );
161 // type sig
162 JNI_info::append_sig(
163 &sig_buf, attribute_td->pAttributeTypeRef );
164 OString type_sig( sig_buf.makeStringAndClear() );
165 sig_buf.ensureCapacity( 64 );
166 // member name
167 OUString const & member_name =
168 OUString::unacquired(
169 &attribute_td->aBase.pMemberName );
171 // getter
172 sig_buf.append( "()" );
173 sig_buf.append( type_sig );
174 OString method_signature( sig_buf.makeStringAndClear() );
175 OUStringBuffer name_buf( 3 + member_name.getLength() );
176 name_buf.append( "get" );
177 name_buf.append( member_name );
178 OString method_name(
179 OUStringToOString(
180 name_buf.makeStringAndClear(),
181 RTL_TEXTENCODING_JAVA_UTF8 ) );
182 m_methods[ nMethodIndex ] = jni->GetMethodID(
183 static_cast<jclass>(jo_class.get()), method_name.getStr(),
184 method_signature.getStr() );
185 jni.ensure_no_exception();
186 assert( m_methods[ nMethodIndex ] != nullptr );
187 ++nMethodIndex;
188 if (! attribute_td->bReadOnly)
190 // setter
191 sig_buf.ensureCapacity( 64 );
192 sig_buf.append( '(' );
193 sig_buf.append( type_sig );
194 sig_buf.append( ")V" );
195 method_signature = sig_buf.makeStringAndClear();
196 name_buf.ensureCapacity( 3 + member_name.getLength() );
197 name_buf.append( "set" );
198 name_buf.append( member_name );
199 method_name = OUStringToOString(
200 name_buf.makeStringAndClear(),
201 RTL_TEXTENCODING_JAVA_UTF8 );
202 m_methods[ nMethodIndex ] = jni->GetMethodID(
203 static_cast<jclass>(jo_class.get()), method_name.getStr(),
204 method_signature.getStr() );
205 jni.ensure_no_exception();
206 assert( m_methods[ nMethodIndex ] != nullptr );
207 ++nMethodIndex;
212 catch (...)
214 m_methods.reset();
215 throw;
218 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
219 m_type = jni->NewGlobalRef( jo_type.get() );
220 m_proxy_ctor = jni->NewGlobalRef( jo_proxy_ctor.get() );
224 void JNI_compound_type_info::destroy( JNIEnv * jni_env )
226 JNI_type_info::destruct( jni_env );
227 m_fields.reset();
228 delete this;
232 JNI_compound_type_info::JNI_compound_type_info(
233 JNI_context const & jni, typelib_TypeDescription * td_ )
234 : JNI_type_info( jni, td_ ),
235 m_exc_ctor( nullptr )
237 assert( m_td.get()->eTypeClass == typelib_TypeClass_STRUCT ||
238 m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION );
239 typelib_CompoundTypeDescription * td =
240 reinterpret_cast< typelib_CompoundTypeDescription * >( m_td.get() );
242 OUString const & uno_name =
243 OUString::unacquired( &td->aBase.pTypeName );
245 // Erase type arguments of instantiated polymorphic struct types:
246 OUString nucleus;
247 sal_Int32 i = uno_name.indexOf( '<' );
248 if ( i < 0 ) {
249 nucleus = uno_name;
250 } else {
251 nucleus = uno_name.copy( 0, i );
253 JLocalAutoRef jo_class(
254 jni,
255 find_class(
256 jni,
257 OUStringToOString(
258 nucleus, RTL_TEXTENCODING_JAVA_UTF8 ).getStr() ) );
260 JNI_info const * jni_info = jni.get_info();
262 if (m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION)
264 // retrieve exc ctor( msg )
265 m_exc_ctor = jni->GetMethodID(
266 static_cast<jclass>(jo_class.get()), "<init>", "(Ljava/lang/String;)V" );
267 jni.ensure_no_exception();
268 assert( m_exc_ctor != nullptr );
271 // retrieve info for base type
272 typelib_TypeDescription * base_td =
273 type_equals(
274 td->aBase.pWeakRef,
275 jni_info->m_RuntimeException_type.getTypeLibType())
276 ? nullptr
277 : reinterpret_cast< typelib_TypeDescription * >(
278 td->pBaseTypeDescription );
279 m_base = (base_td == nullptr ? nullptr : jni_info->get_type_info( jni, base_td ));
283 if (type_equals(
284 td->aBase.pWeakRef,
285 jni_info->m_Exception_type.getTypeLibType() ) ||
286 type_equals(
287 td->aBase.pWeakRef,
288 jni_info->m_RuntimeException_type.getTypeLibType() ))
290 // coverity[ctor_dtor_leak] - on purpose
291 m_fields.reset(new jfieldID[ 2 ]);
292 m_fields[ 0 ] = nullptr; // special Throwable.getMessage()
293 // field Context
294 m_fields[ 1 ] = jni->GetFieldID(
295 static_cast<jclass>(jo_class.get()), "Context", "Ljava/lang/Object;" );
296 jni.ensure_no_exception();
297 assert( m_fields[ 1 ] != nullptr );
299 else
301 // retrieve field ids for all direct members
302 sal_Int32 nMembers = td->nMembers;
303 m_fields.reset(new jfieldID[ nMembers ]);
305 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
307 OString sig;
308 if (td->aBase.eTypeClass == typelib_TypeClass_STRUCT
309 && reinterpret_cast< typelib_StructTypeDescription * >(
310 td)->pParameterizedTypes != nullptr
311 && reinterpret_cast< typelib_StructTypeDescription * >(
312 td)->pParameterizedTypes[nPos])
314 sig = OString( "Ljava/lang/Object;" );
315 } else {
316 OStringBuffer sig_buf( 32 );
317 JNI_info::append_sig( &sig_buf, td->ppTypeRefs[ nPos ] );
318 sig = sig_buf.makeStringAndClear();
321 OString member_name(
322 OUStringToOString(
323 OUString::unacquired( &td->ppMemberNames[ nPos ] ),
324 RTL_TEXTENCODING_JAVA_UTF8 ) );
326 m_fields[ nPos ] = jni->GetFieldID(
327 static_cast<jclass>(jo_class.get()), member_name.getStr(),
328 sig.getStr() );
329 jni.ensure_no_exception();
330 assert( m_fields[ nPos ] != nullptr );
334 catch (...)
336 m_fields.reset();
337 throw;
340 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
344 JNI_type_info const * JNI_info::create_type_info(
345 JNI_context const & jni, typelib_TypeDescription * td ) const
347 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
349 JNI_type_info * new_info;
350 switch (td->eTypeClass)
352 case typelib_TypeClass_STRUCT:
353 case typelib_TypeClass_EXCEPTION:
355 new_info = new JNI_compound_type_info( jni, td );
356 break;
358 case typelib_TypeClass_INTERFACE:
360 new_info = new JNI_interface_type_info( jni, td );
361 break;
363 default:
365 throw BridgeRuntimeError(
366 "type info not supported for " + uno_name + jni.get_stack_trace() );
370 // look up
371 JNI_type_info * info;
372 ClearableMutexGuard guard( m_mutex );
373 JNI_type_info_holder & holder = m_type_map[ uno_name ];
374 if (holder.m_info == nullptr) // new insertion
376 holder.m_info = new_info;
377 guard.clear();
378 info = new_info;
380 else // inserted in the meantime
382 info = holder.m_info;
383 guard.clear();
384 new_info->destroy( jni.get_jni_env() );
386 return info;
390 JNI_type_info const * JNI_info::get_type_info(
391 JNI_context const & jni, typelib_TypeDescription * td ) const
393 if (is_XInterface( td->pWeakRef ))
395 return m_XInterface_type_info;
398 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
399 JNI_type_info const * info;
400 ClearableMutexGuard guard( m_mutex );
402 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
403 if (iFind == m_type_map.end())
405 guard.clear();
406 info = create_type_info( jni, td );
408 else
410 info = iFind->second.m_info;
413 return info;
417 JNI_type_info const * JNI_info::get_type_info(
418 JNI_context const & jni, typelib_TypeDescriptionReference * type ) const
420 if (is_XInterface( type ))
422 return m_XInterface_type_info;
425 OUString const & uno_name = OUString::unacquired( &type->pTypeName );
426 JNI_type_info const * info;
427 ClearableMutexGuard guard( m_mutex );
428 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
429 if (iFind == m_type_map.end())
431 guard.clear();
432 TypeDescr td( type );
433 info = create_type_info( jni, td.get() );
435 else
437 info = iFind->second.m_info;
440 return info;
444 JNI_type_info const * JNI_info::get_type_info(
445 JNI_context const & jni, OUString const & uno_name ) const
447 if ( uno_name == "com.sun.star.uno.XInterface" )
449 return m_XInterface_type_info;
452 JNI_type_info const * info;
453 ClearableMutexGuard guard( m_mutex );
454 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
455 if (iFind == m_type_map.end())
457 guard.clear();
458 css::uno::TypeDescription td( uno_name );
459 if (! td.is())
461 throw BridgeRuntimeError(
462 "UNO type not found: " + uno_name + jni.get_stack_trace() );
464 info = create_type_info( jni, td.get() );
466 else
468 info = iFind->second.m_info;
471 return info;
475 JNI_info::JNI_info(
476 JNIEnv * jni_env, jobject class_loader, jclass classClass,
477 jmethodID methodForName )
478 : m_class_Class( classClass ),
479 m_method_Class_forName( methodForName ),
480 m_class_JNI_proxy( nullptr ),
481 m_XInterface_queryInterface_td(
482 (reinterpret_cast< typelib_InterfaceTypeDescription * >(
483 css::uno::TypeDescription(
484 cppu::UnoType<css::uno::XInterface>::get())
485 .get())->ppMembers[ 0 ] ) ),
486 m_Exception_type(cppu::UnoType<css::uno::Exception>::get()),
487 m_RuntimeException_type(cppu::UnoType<css::uno::RuntimeException>::get()),
488 m_void_type(cppu::UnoType<void>::get()),
489 m_XInterface_type_info( nullptr )
491 JNI_context jni( this, jni_env, class_loader ); // !no proper jni_info!
493 // class lookup
494 JLocalAutoRef jo_Object(
495 jni, find_class( jni, "java.lang.Object" ) );
496 JLocalAutoRef jo_Class(
497 jni, find_class( jni, "java.lang.Class" ) );
498 JLocalAutoRef jo_Throwable(
499 jni, find_class( jni, "java.lang.Throwable" ) );
500 JLocalAutoRef jo_Character(
501 jni, find_class( jni, "java.lang.Character" ) );
502 JLocalAutoRef jo_Boolean(
503 jni, find_class( jni, "java.lang.Boolean" ) );
504 JLocalAutoRef jo_Byte(
505 jni, find_class( jni, "java.lang.Byte" ) );
506 JLocalAutoRef jo_Short(
507 jni, find_class( jni, "java.lang.Short" ) );
508 JLocalAutoRef jo_Integer(
509 jni, find_class( jni, "java.lang.Integer" ) );
510 JLocalAutoRef jo_Long(
511 jni, find_class( jni, "java.lang.Long" ) );
512 JLocalAutoRef jo_Float(
513 jni, find_class( jni, "java.lang.Float" ) );
514 JLocalAutoRef jo_Double(
515 jni, find_class( jni, "java.lang.Double" ) );
516 JLocalAutoRef jo_String(
517 jni, find_class( jni, "java.lang.String" ) );
518 JLocalAutoRef jo_RuntimeException(
519 jni, find_class( jni, "com.sun.star.uno.RuntimeException" ) );
520 JLocalAutoRef jo_UnoRuntime(
521 jni, find_class( jni, "com.sun.star.uno.UnoRuntime" ) );
522 JLocalAutoRef jo_Any(
523 jni, find_class( jni, "com.sun.star.uno.Any" ) );
524 JLocalAutoRef jo_Enum(
525 jni, find_class( jni, "com.sun.star.uno.Enum" ) );
526 JLocalAutoRef jo_Type(
527 jni, find_class( jni, "com.sun.star.uno.Type" ) );
528 JLocalAutoRef jo_TypeClass(
529 jni, find_class( jni, "com.sun.star.uno.TypeClass" ) );
530 JLocalAutoRef jo_IEnvironment(
531 jni, find_class( jni, "com.sun.star.uno.IEnvironment" ) );
532 JLocalAutoRef jo_JNI_proxy(
533 jni, find_class( jni, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
534 JLocalAutoRef jo_AsynchronousFinalizer(
535 jni, find_class( jni, "com.sun.star.lib.util.AsynchronousFinalizer" ) );
537 // method Object.toString()
538 m_method_Object_toString = jni->GetMethodID(
539 static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
540 jni.ensure_no_exception();
541 assert( m_method_Object_toString != nullptr );
542 // method Class.getName()
543 m_method_Class_getName = jni->GetMethodID(
544 static_cast<jclass>(jo_Class.get()), "getName", "()Ljava/lang/String;" );
545 jni.ensure_no_exception();
546 assert( m_method_Class_getName != nullptr );
548 // method Throwable.getMessage()
549 m_method_Throwable_getMessage = jni->GetMethodID(
550 static_cast<jclass>(jo_Throwable.get()), "getMessage", "()Ljava/lang/String;" );
551 jni.ensure_no_exception();
552 assert( m_method_Throwable_getMessage != nullptr );
554 // method Character.charValue()
555 m_method_Character_charValue = jni->GetMethodID(
556 static_cast<jclass>(jo_Character.get()), "charValue", "()C" );
557 jni.ensure_no_exception();
558 assert( m_method_Character_charValue != nullptr );
559 // method Boolean.booleanValue()
560 m_method_Boolean_booleanValue = jni->GetMethodID(
561 static_cast<jclass>(jo_Boolean.get()), "booleanValue", "()Z" );
562 jni.ensure_no_exception();
563 assert( m_method_Boolean_booleanValue != nullptr );
564 // method Byte.byteValue()
565 m_method_Byte_byteValue = jni->GetMethodID(
566 static_cast<jclass>(jo_Byte.get()), "byteValue", "()B" );
567 jni.ensure_no_exception();
568 assert( m_method_Byte_byteValue != nullptr );
569 // method Short.shortValue()
570 m_method_Short_shortValue = jni->GetMethodID(
571 static_cast<jclass>(jo_Short.get()), "shortValue", "()S" );
572 jni.ensure_no_exception();
573 assert( m_method_Short_shortValue != nullptr );
574 // method Integer.intValue()
575 m_method_Integer_intValue = jni->GetMethodID(
576 static_cast<jclass>(jo_Integer.get()), "intValue", "()I" );
577 jni.ensure_no_exception();
578 assert( m_method_Integer_intValue != nullptr );
579 // method Long.longValue()
580 m_method_Long_longValue = jni->GetMethodID(
581 static_cast<jclass>(jo_Long.get()), "longValue", "()J" );
582 jni.ensure_no_exception();
583 assert( m_method_Long_longValue != nullptr );
584 // method Float.floatValue()
585 m_method_Float_floatValue = jni->GetMethodID(
586 static_cast<jclass>(jo_Float.get()), "floatValue", "()F" );
587 jni.ensure_no_exception();
588 assert( m_method_Float_floatValue != nullptr );
589 // method Double.doubleValue()
590 m_method_Double_doubleValue = jni->GetMethodID(
591 static_cast<jclass>(jo_Double.get()), "doubleValue", "()D" );
592 jni.ensure_no_exception();
593 assert( m_method_Double_doubleValue != nullptr );
595 // ctor Character( char )
596 m_ctor_Character_with_char = jni->GetMethodID(
597 static_cast<jclass>(jo_Character.get()), "<init>", "(C)V" );
598 jni.ensure_no_exception();
599 assert( m_ctor_Character_with_char != nullptr );
600 // ctor Boolean( boolean )
601 m_ctor_Boolean_with_boolean = jni->GetMethodID(
602 static_cast<jclass>(jo_Boolean.get()), "<init>", "(Z)V" );
603 jni.ensure_no_exception();
604 assert( m_ctor_Boolean_with_boolean != nullptr );
605 // ctor Byte( byte )
606 m_ctor_Byte_with_byte = jni->GetMethodID(
607 static_cast<jclass>(jo_Byte.get()), "<init>", "(B)V" );
608 jni.ensure_no_exception();
609 assert( m_ctor_Byte_with_byte != nullptr );
610 // ctor Short( short )
611 m_ctor_Short_with_short = jni->GetMethodID(
612 static_cast<jclass>(jo_Short.get()), "<init>", "(S)V" );
613 jni.ensure_no_exception();
614 assert( m_ctor_Short_with_short != nullptr );
615 // ctor Integer( int )
616 m_ctor_Integer_with_int = jni->GetMethodID(
617 static_cast<jclass>(jo_Integer.get()), "<init>", "(I)V" );
618 jni.ensure_no_exception();
619 assert( m_ctor_Integer_with_int != nullptr );
620 // ctor Long( long )
621 m_ctor_Long_with_long = jni->GetMethodID(
622 static_cast<jclass>(jo_Long.get()), "<init>", "(J)V" );
623 jni.ensure_no_exception();
624 assert( m_ctor_Long_with_long != nullptr );
625 // ctor Float( float )
626 m_ctor_Float_with_float = jni->GetMethodID(
627 static_cast<jclass>(jo_Float.get()), "<init>", "(F)V" );
628 jni.ensure_no_exception();
629 assert( m_ctor_Float_with_float != nullptr );
630 // ctor Double( double )
631 m_ctor_Double_with_double = jni->GetMethodID(
632 static_cast<jclass>(jo_Double.get()), "<init>", "(D)V" );
633 jni.ensure_no_exception();
634 assert( m_ctor_Double_with_double != nullptr );
636 // static method UnoRuntime.generateOid()
637 m_method_UnoRuntime_generateOid = jni->GetStaticMethodID(
638 static_cast<jclass>(jo_UnoRuntime.get()),
639 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
640 jni.ensure_no_exception();
641 assert( m_method_UnoRuntime_generateOid != nullptr );
642 // static method UnoRuntime.queryInterface()
643 m_method_UnoRuntime_queryInterface = jni->GetStaticMethodID(
644 static_cast<jclass>(jo_UnoRuntime.get()),
645 "queryInterface",
646 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
647 jni.ensure_no_exception();
648 assert( m_method_UnoRuntime_queryInterface != nullptr );
650 // field Enum.m_value
651 m_field_Enum_m_value = jni->GetFieldID(
652 static_cast<jclass>(jo_Enum.get()), "m_value", "I" );
653 jni.ensure_no_exception();
654 assert( m_field_Enum_m_value != nullptr );
656 // static method TypeClass.fromInt()
657 m_method_TypeClass_fromInt = jni->GetStaticMethodID(
658 static_cast<jclass>(jo_TypeClass.get()),
659 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
660 jni.ensure_no_exception();
661 assert( m_method_TypeClass_fromInt != nullptr );
663 // ctor Type( Class )
664 m_ctor_Type_with_Class = jni->GetMethodID(
665 static_cast<jclass>(jo_Type.get()), "<init>", "(Ljava/lang/Class;)V" );
666 jni.ensure_no_exception();
667 assert( m_ctor_Type_with_Class != nullptr );
668 // ctor Type( String, TypeClass )
669 m_ctor_Type_with_Name_TypeClass = jni->GetMethodID(
670 static_cast<jclass>(jo_Type.get()),
671 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
672 jni.ensure_no_exception();
673 assert( m_ctor_Type_with_Name_TypeClass != nullptr );
674 // field Type._typeName
675 m_field_Type_typeName = jni->GetFieldID(
676 static_cast<jclass>(jo_Type.get()), "_typeName", "Ljava/lang/String;" );
677 jni.ensure_no_exception();
678 assert( m_field_Type_typeName != nullptr );
680 // ctor Any( Type, Object )
681 m_ctor_Any_with_Type_Object = jni->GetMethodID(
682 static_cast<jclass>(jo_Any.get()),
683 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
684 jni.ensure_no_exception();
685 assert( m_ctor_Any_with_Type_Object != nullptr );
687 // field Any._type
688 m_field_Any_type = jni->GetFieldID(
689 static_cast<jclass>(jo_Any.get()), "_type", "Lcom/sun/star/uno/Type;" );
690 jni.ensure_no_exception();
691 assert( m_field_Any_type != nullptr );
692 // field Any._object
693 m_field_Any_object = jni->GetFieldID(
694 static_cast<jclass>(jo_Any.get()), "_object", "Ljava/lang/Object;" );
695 jni.ensure_no_exception();
696 assert( m_field_Any_object != nullptr );
698 // method IEnvironment.getRegisteredInterface()
699 m_method_IEnvironment_getRegisteredInterface = jni->GetMethodID(
700 static_cast<jclass>(jo_IEnvironment.get()),
701 "getRegisteredInterface",
702 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
703 jni.ensure_no_exception();
704 assert( m_method_IEnvironment_getRegisteredInterface != nullptr );
705 // method IEnvironment.registerInterface()
706 m_method_IEnvironment_registerInterface = jni->GetMethodID(
707 static_cast<jclass>(jo_IEnvironment.get()), "registerInterface",
708 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
709 "Ljava/lang/Object;" );
710 jni.ensure_no_exception();
711 assert( m_method_IEnvironment_registerInterface != nullptr );
713 // static method JNI_proxy.get_proxy_ctor()
714 m_method_JNI_proxy_get_proxy_ctor = jni->GetStaticMethodID(
715 static_cast<jclass>(jo_JNI_proxy.get()), "get_proxy_ctor",
716 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
717 jni.ensure_no_exception();
718 assert( m_method_JNI_proxy_get_proxy_ctor != nullptr );
719 // static method JNI_proxy.create()
720 m_method_JNI_proxy_create = jni->GetStaticMethodID(
721 static_cast<jclass>(jo_JNI_proxy.get()), "create",
722 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
723 "/String;Ljava/lang/reflect/Constructor;"
724 "Lcom/sun/star/lib/util/AsynchronousFinalizer;)Ljava/lang/Object;" );
725 jni.ensure_no_exception();
726 assert( m_method_JNI_proxy_create != nullptr );
727 // field JNI_proxy.m_receiver_handle
728 m_field_JNI_proxy_m_receiver_handle = jni->GetFieldID(
729 static_cast<jclass>(jo_JNI_proxy.get()), "m_receiver_handle", "J" );
730 jni.ensure_no_exception();
731 assert( m_field_JNI_proxy_m_receiver_handle != nullptr );
732 // field JNI_proxy.m_td_handle
733 m_field_JNI_proxy_m_td_handle = jni->GetFieldID(
734 static_cast<jclass>(jo_JNI_proxy.get()), "m_td_handle", "J" );
735 jni.ensure_no_exception();
736 assert( m_field_JNI_proxy_m_td_handle != nullptr );
737 // field JNI_proxy.m_type
738 m_field_JNI_proxy_m_type = jni->GetFieldID(
739 static_cast<jclass>(jo_JNI_proxy.get()), "m_type", "Lcom/sun/star/uno/Type;" );
740 jni.ensure_no_exception();
741 assert( m_field_JNI_proxy_m_type != nullptr );
742 // field JNI_proxy.m_oid
743 m_field_JNI_proxy_m_oid = jni->GetFieldID(
744 static_cast<jclass>(jo_JNI_proxy.get()), "m_oid", "Ljava/lang/String;" );
745 jni.ensure_no_exception();
746 assert( m_field_JNI_proxy_m_oid != nullptr );
748 // ctor AsynchronousFinalizer
749 m_ctor_AsynchronousFinalizer = jni->GetMethodID(
750 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "<init>", "()V" );
751 jni.ensure_no_exception();
752 assert( m_ctor_AsynchronousFinalizer != nullptr );
753 // method AsynchronousFinalizer.drain()
754 m_method_AsynchronousFinalizer_drain = jni->GetMethodID(
755 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "drain", "()V" );
756 jni.ensure_no_exception();
757 assert( m_method_AsynchronousFinalizer_drain != nullptr );
759 // get java env
760 OUString java_env_type_name( UNO_LB_JAVA );
761 JLocalAutoRef jo_java(
762 jni, ustring_to_jstring( jni, java_env_type_name.pData ) );
763 jvalue args[ 2 ];
764 args[ 0 ].l = jo_java.get();
765 args[ 1 ].l = nullptr;
766 jmethodID method_getEnvironment = jni->GetStaticMethodID(
767 static_cast<jclass>(jo_UnoRuntime.get()), "getEnvironment",
768 "(Ljava/lang/String;Ljava/lang/Object;)"
769 "Lcom/sun/star/uno/IEnvironment;" );
770 jni.ensure_no_exception();
771 assert( method_getEnvironment != nullptr );
772 JLocalAutoRef jo_java_env(
773 jni, jni->CallStaticObjectMethodA(
774 static_cast<jclass>(jo_UnoRuntime.get()), method_getEnvironment, args ) );
776 // get com.sun.star.uno.Any.VOID
777 jfieldID field_Any_VOID = jni->GetStaticFieldID(
778 static_cast<jclass>(jo_Any.get()), "VOID", "Lcom/sun/star/uno/Any;" );
779 jni.ensure_no_exception();
780 assert( field_Any_VOID != nullptr );
781 JLocalAutoRef jo_Any_VOID(
782 jni, jni->GetStaticObjectField(
783 static_cast<jclass>(jo_Any.get()), field_Any_VOID ) );
784 // get com.sun.star.uno.Type.UNSIGNED_SHORT
785 jfieldID field_Type_UNSIGNED_SHORT = jni->GetStaticFieldID(
786 static_cast<jclass>(jo_Type.get()), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
787 jni.ensure_no_exception();
788 assert( field_Type_UNSIGNED_SHORT != nullptr );
789 JLocalAutoRef jo_Type_UNSIGNED_SHORT(
790 jni, jni->GetStaticObjectField(
791 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_SHORT ) );
792 // get com.sun.star.uno.Type.UNSIGNED_LONG
793 jfieldID field_Type_UNSIGNED_LONG = jni->GetStaticFieldID(
794 static_cast<jclass>(jo_Type.get()), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
795 jni.ensure_no_exception();
796 assert( field_Type_UNSIGNED_LONG != nullptr );
797 JLocalAutoRef jo_Type_UNSIGNED_LONG(
798 jni, jni->GetStaticObjectField(
799 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_LONG ) );
800 // get com.sun.star.uno.Type.UNSIGNED_HYPER
801 jfieldID field_Type_UNSIGNED_HYPER = jni->GetStaticFieldID(
802 static_cast<jclass>(jo_Type.get()), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
803 jni.ensure_no_exception();
804 assert( field_Type_UNSIGNED_HYPER != nullptr );
805 JLocalAutoRef jo_Type_UNSIGNED_HYPER(
806 jni, jni->GetStaticObjectField(
807 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_HYPER ) );
809 // make global refs
810 m_class_UnoRuntime =
811 static_cast<jclass>(jni->NewGlobalRef( jo_UnoRuntime.get() ));
812 m_class_RuntimeException =
813 static_cast<jclass>(jni->NewGlobalRef( jo_RuntimeException.get() ));
814 m_class_Any =
815 static_cast<jclass>(jni->NewGlobalRef( jo_Any.get() ));
816 m_class_Type =
817 static_cast<jclass>(jni->NewGlobalRef( jo_Type.get() ));
818 m_class_TypeClass =
819 static_cast<jclass>(jni->NewGlobalRef( jo_TypeClass.get() ));
820 m_class_JNI_proxy =
821 static_cast<jclass>(jni->NewGlobalRef( jo_JNI_proxy.get() ));
822 m_class_AsynchronousFinalizer =
823 static_cast<jclass>(jni->NewGlobalRef( jo_AsynchronousFinalizer.get() ));
825 m_class_Character =
826 static_cast<jclass>(jni->NewGlobalRef( jo_Character.get() ));
827 m_class_Boolean =
828 static_cast<jclass>(jni->NewGlobalRef( jo_Boolean.get() ));
829 m_class_Byte =
830 static_cast<jclass>(jni->NewGlobalRef( jo_Byte.get() ));
831 m_class_Short =
832 static_cast<jclass>(jni->NewGlobalRef( jo_Short.get() ));
833 m_class_Integer =
834 static_cast<jclass>(jni->NewGlobalRef( jo_Integer.get() ));
835 m_class_Long =
836 static_cast<jclass>(jni->NewGlobalRef( jo_Long.get() ));
837 m_class_Float =
838 static_cast<jclass>(jni->NewGlobalRef( jo_Float.get() ));
839 m_class_Double =
840 static_cast<jclass>(jni->NewGlobalRef( jo_Double.get() ));
841 m_class_String =
842 static_cast<jclass>(jni->NewGlobalRef( jo_String.get() ));
843 m_class_Object =
844 static_cast<jclass>(jni->NewGlobalRef( jo_Object.get() ));
845 m_class_Class =
846 static_cast<jclass>(jni->NewGlobalRef( m_class_Class ));
848 m_object_Any_VOID =
849 jni->NewGlobalRef( jo_Any_VOID.get() );
850 m_object_Type_UNSIGNED_SHORT =
851 jni->NewGlobalRef( jo_Type_UNSIGNED_SHORT.get() );
852 m_object_Type_UNSIGNED_LONG =
853 jni->NewGlobalRef( jo_Type_UNSIGNED_LONG.get() );
854 m_object_Type_UNSIGNED_HYPER =
855 jni->NewGlobalRef( jo_Type_UNSIGNED_HYPER.get() );
856 m_object_java_env = jni->NewGlobalRef( jo_java_env.get() );
860 css::uno::TypeDescription XInterface_td(
861 cppu::UnoType<css::uno::XInterface>::get());
862 // coverity[ctor_dtor_leak] - on purpose
863 m_XInterface_type_info =
864 new JNI_interface_type_info( jni, XInterface_td.get() );
866 catch (...)
868 destruct( jni_env );
869 throw;
874 void JNI_info::destruct( JNIEnv * jni_env )
876 for (auto & i: m_type_map)
878 i.second.m_info->destroy( jni_env );
880 if (m_XInterface_type_info != nullptr)
882 const_cast< JNI_interface_type_info * >(
883 m_XInterface_type_info )->destroy( jni_env );
886 // free global refs
887 jni_env->DeleteGlobalRef( m_object_java_env );
888 jni_env->DeleteGlobalRef( m_object_Any_VOID );
889 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT );
890 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG );
891 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER );
893 jni_env->DeleteGlobalRef( m_class_Class );
894 jni_env->DeleteGlobalRef( m_class_Object );
895 jni_env->DeleteGlobalRef( m_class_String );
896 jni_env->DeleteGlobalRef( m_class_Double );
897 jni_env->DeleteGlobalRef( m_class_Float );
898 jni_env->DeleteGlobalRef( m_class_Long );
899 jni_env->DeleteGlobalRef( m_class_Integer );
900 jni_env->DeleteGlobalRef( m_class_Short );
901 jni_env->DeleteGlobalRef( m_class_Byte );
902 jni_env->DeleteGlobalRef( m_class_Boolean );
903 jni_env->DeleteGlobalRef( m_class_Character );
905 jni_env->DeleteGlobalRef( m_class_AsynchronousFinalizer );
906 jni_env->DeleteGlobalRef( m_class_JNI_proxy );
907 jni_env->DeleteGlobalRef( m_class_RuntimeException );
908 jni_env->DeleteGlobalRef( m_class_UnoRuntime );
909 jni_env->DeleteGlobalRef( m_class_TypeClass );
910 jni_env->DeleteGlobalRef( m_class_Type );
911 jni_env->DeleteGlobalRef( m_class_Any );
915 JNI_info const * JNI_info::get_jni_info(
916 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm )
918 // !!!no JNI_info available at JNI_context!!!
919 ::jvmaccess::VirtualMachine::AttachGuard guard(
920 uno_vm->getVirtualMachine() );
921 JNIEnv * jni_env = guard.getEnvironment();
922 JNI_context jni(
923 nullptr, jni_env, static_cast< jobject >(uno_vm->getClassLoader()) );
925 jclass jo_class;
926 jmethodID jo_forName;
927 jni.getClassForName( &jo_class, &jo_forName );
928 jni.ensure_no_exception();
929 JLocalAutoRef jo_JNI_info_holder(
930 jni,
931 jni.findClass(
932 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class,
933 jo_forName, false ) );
934 // field JNI_info_holder.m_jni_info_handle
935 jfieldID field_s_jni_info_handle =
936 jni->GetStaticFieldID(
937 static_cast<jclass>(jo_JNI_info_holder.get()), "s_jni_info_handle", "J" );
938 jni.ensure_no_exception();
939 assert( field_s_jni_info_handle != nullptr );
941 JNI_info const * jni_info =
942 reinterpret_cast< JNI_info const * >(
943 jni->GetStaticLongField(
944 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle ) );
945 if (jni_info == nullptr) // un-initialized?
947 JNI_info * new_info = new JNI_info(
948 jni_env, static_cast< jobject >(uno_vm->getClassLoader()), jo_class,
949 jo_forName );
951 ClearableMutexGuard g( Mutex::getGlobalMutex() );
952 jni_info =
953 reinterpret_cast< JNI_info const * >(
954 jni->GetStaticLongField(
955 static_cast<jclass>(jo_JNI_info_holder.get()),
956 field_s_jni_info_handle ) );
957 if (jni_info == nullptr) // still un-initialized?
959 jni->SetStaticLongField(
960 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle,
961 reinterpret_cast< jlong >( new_info ) );
962 jni_info = new_info;
964 else
966 g.clear();
967 new_info->destroy( jni_env );
971 return jni_info;
976 extern "C"
980 SAL_JNI_EXPORT void
981 JNICALL Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
982 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle )
983 SAL_THROW_EXTERN_C()
985 ::jni_uno::JNI_info * jni_info =
986 reinterpret_cast< ::jni_uno::JNI_info * >( jni_info_handle );
987 jni_info->destroy( jni_env );
992 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */