Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / bridges / source / jni_uno / jni_info.cxx
blob9b43356856afb563945d6ded4faa0569260d8886
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]
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 ),
236 m_fields( nullptr )
238 assert( m_td.get()->eTypeClass == typelib_TypeClass_STRUCT ||
239 m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION );
240 typelib_CompoundTypeDescription * td =
241 reinterpret_cast< typelib_CompoundTypeDescription * >( m_td.get() );
243 OUString const & uno_name =
244 OUString::unacquired( &td->aBase.pTypeName );
246 // Erase type arguments of instantiated polymorphic struct types:
247 OUString nucleus;
248 sal_Int32 i = uno_name.indexOf( '<' );
249 if ( i < 0 ) {
250 nucleus = uno_name;
251 } else {
252 nucleus = uno_name.copy( 0, i );
254 JLocalAutoRef jo_class(
255 jni,
256 find_class(
257 jni,
258 OUStringToOString(
259 nucleus, RTL_TEXTENCODING_JAVA_UTF8 ).getStr() ) );
261 JNI_info const * jni_info = jni.get_info();
263 if (m_td.get()->eTypeClass == typelib_TypeClass_EXCEPTION)
265 // retrieve exc ctor( msg )
266 m_exc_ctor = jni->GetMethodID(
267 static_cast<jclass>(jo_class.get()), "<init>", "(Ljava/lang/String;)V" );
268 jni.ensure_no_exception();
269 assert( m_exc_ctor != nullptr );
272 // retrieve info for base type
273 typelib_TypeDescription * base_td =
274 type_equals(
275 td->aBase.pWeakRef,
276 jni_info->m_RuntimeException_type.getTypeLibType())
277 ? nullptr
278 : reinterpret_cast< typelib_TypeDescription * >(
279 td->pBaseTypeDescription );
280 m_base = (base_td == nullptr ? nullptr : jni_info->get_type_info( jni, base_td ));
284 if (type_equals(
285 td->aBase.pWeakRef,
286 jni_info->m_Exception_type.getTypeLibType() ) ||
287 type_equals(
288 td->aBase.pWeakRef,
289 jni_info->m_RuntimeException_type.getTypeLibType() ))
291 // coverity [ctor_dtor_leak]
292 m_fields.reset(new jfieldID[ 2 ]);
293 m_fields[ 0 ] = nullptr; // special Throwable.getMessage()
294 // field Context
295 m_fields[ 1 ] = jni->GetFieldID(
296 static_cast<jclass>(jo_class.get()), "Context", "Ljava/lang/Object;" );
297 jni.ensure_no_exception();
298 assert( m_fields[ 1 ] != nullptr );
300 else
302 // retrieve field ids for all direct members
303 sal_Int32 nMembers = td->nMembers;
304 m_fields.reset(new jfieldID[ nMembers ]);
306 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
308 OString sig;
309 if (td->aBase.eTypeClass == typelib_TypeClass_STRUCT
310 && reinterpret_cast< typelib_StructTypeDescription * >(
311 td)->pParameterizedTypes != nullptr
312 && reinterpret_cast< typelib_StructTypeDescription * >(
313 td)->pParameterizedTypes[nPos])
315 sig = OString( "Ljava/lang/Object;" );
316 } else {
317 OStringBuffer sig_buf( 32 );
318 JNI_info::append_sig( &sig_buf, td->ppTypeRefs[ nPos ] );
319 sig = sig_buf.makeStringAndClear();
322 OString member_name(
323 OUStringToOString(
324 OUString::unacquired( &td->ppMemberNames[ nPos ] ),
325 RTL_TEXTENCODING_JAVA_UTF8 ) );
327 m_fields[ nPos ] = jni->GetFieldID(
328 static_cast<jclass>(jo_class.get()), member_name.getStr(),
329 sig.getStr() );
330 jni.ensure_no_exception();
331 assert( m_fields[ nPos ] != nullptr );
335 catch (...)
337 m_fields.reset();
338 throw;
341 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
345 JNI_type_info const * JNI_info::create_type_info(
346 JNI_context const & jni, typelib_TypeDescription * td ) const
348 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
350 JNI_type_info * new_info;
351 switch (td->eTypeClass)
353 case typelib_TypeClass_STRUCT:
354 case typelib_TypeClass_EXCEPTION:
356 new_info = new JNI_compound_type_info( jni, td );
357 break;
359 case typelib_TypeClass_INTERFACE:
361 new_info = new JNI_interface_type_info( jni, td );
362 break;
364 default:
366 throw BridgeRuntimeError(
367 "type info not supported for " + uno_name + jni.get_stack_trace() );
371 // look up
372 JNI_type_info * info;
373 ClearableMutexGuard guard( m_mutex );
374 JNI_type_info_holder & holder = m_type_map[ uno_name ];
375 if (holder.m_info == nullptr) // new insertion
377 holder.m_info = new_info;
378 guard.clear();
379 info = new_info;
381 else // inserted in the meantime
383 info = holder.m_info;
384 guard.clear();
385 new_info->destroy( jni.get_jni_env() );
387 return info;
391 JNI_type_info const * JNI_info::get_type_info(
392 JNI_context const & jni, typelib_TypeDescription * td ) const
394 if (is_XInterface( td->pWeakRef ))
396 return m_XInterface_type_info;
399 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
400 JNI_type_info const * info;
401 ClearableMutexGuard guard( m_mutex );
403 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
404 if (iFind == m_type_map.end())
406 guard.clear();
407 info = create_type_info( jni, td );
409 else
411 info = iFind->second.m_info;
414 return info;
418 JNI_type_info const * JNI_info::get_type_info(
419 JNI_context const & jni, typelib_TypeDescriptionReference * type ) const
421 if (is_XInterface( type ))
423 return m_XInterface_type_info;
426 OUString const & uno_name = OUString::unacquired( &type->pTypeName );
427 JNI_type_info const * info;
428 ClearableMutexGuard guard( m_mutex );
429 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
430 if (iFind == m_type_map.end())
432 guard.clear();
433 TypeDescr td( type );
434 info = create_type_info( jni, td.get() );
436 else
438 info = iFind->second.m_info;
441 return info;
445 JNI_type_info const * JNI_info::get_type_info(
446 JNI_context const & jni, OUString const & uno_name ) const
448 if ( uno_name == "com.sun.star.uno.XInterface" )
450 return m_XInterface_type_info;
453 JNI_type_info const * info;
454 ClearableMutexGuard guard( m_mutex );
455 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
456 if (iFind == m_type_map.end())
458 guard.clear();
459 css::uno::TypeDescription td( uno_name );
460 if (! td.is())
462 throw BridgeRuntimeError(
463 "UNO type not found: " + uno_name + jni.get_stack_trace() );
465 info = create_type_info( jni, td.get() );
467 else
469 info = iFind->second.m_info;
472 return info;
476 JNI_info::JNI_info(
477 JNIEnv * jni_env, jobject class_loader, jclass classClass,
478 jmethodID methodForName )
479 : m_class_Class( classClass ),
480 m_method_Class_forName( methodForName ),
481 m_class_JNI_proxy( nullptr ),
482 m_XInterface_queryInterface_td(
483 (reinterpret_cast< typelib_InterfaceTypeDescription * >(
484 css::uno::TypeDescription(
485 cppu::UnoType<css::uno::XInterface>::get())
486 .get())->ppMembers[ 0 ] ) ),
487 m_Exception_type(cppu::UnoType<css::uno::Exception>::get()),
488 m_RuntimeException_type(cppu::UnoType<css::uno::RuntimeException>::get()),
489 m_void_type(cppu::UnoType<void>::get()),
490 m_XInterface_type_info( nullptr )
492 JNI_context jni( this, jni_env, class_loader ); // !no proper jni_info!
494 // class lookup
495 JLocalAutoRef jo_Object(
496 jni, find_class( jni, "java.lang.Object" ) );
497 JLocalAutoRef jo_Class(
498 jni, find_class( jni, "java.lang.Class" ) );
499 JLocalAutoRef jo_Throwable(
500 jni, find_class( jni, "java.lang.Throwable" ) );
501 JLocalAutoRef jo_Character(
502 jni, find_class( jni, "java.lang.Character" ) );
503 JLocalAutoRef jo_Boolean(
504 jni, find_class( jni, "java.lang.Boolean" ) );
505 JLocalAutoRef jo_Byte(
506 jni, find_class( jni, "java.lang.Byte" ) );
507 JLocalAutoRef jo_Short(
508 jni, find_class( jni, "java.lang.Short" ) );
509 JLocalAutoRef jo_Integer(
510 jni, find_class( jni, "java.lang.Integer" ) );
511 JLocalAutoRef jo_Long(
512 jni, find_class( jni, "java.lang.Long" ) );
513 JLocalAutoRef jo_Float(
514 jni, find_class( jni, "java.lang.Float" ) );
515 JLocalAutoRef jo_Double(
516 jni, find_class( jni, "java.lang.Double" ) );
517 JLocalAutoRef jo_String(
518 jni, find_class( jni, "java.lang.String" ) );
519 JLocalAutoRef jo_RuntimeException(
520 jni, find_class( jni, "com.sun.star.uno.RuntimeException" ) );
521 JLocalAutoRef jo_UnoRuntime(
522 jni, find_class( jni, "com.sun.star.uno.UnoRuntime" ) );
523 JLocalAutoRef jo_Any(
524 jni, find_class( jni, "com.sun.star.uno.Any" ) );
525 JLocalAutoRef jo_Enum(
526 jni, find_class( jni, "com.sun.star.uno.Enum" ) );
527 JLocalAutoRef jo_Type(
528 jni, find_class( jni, "com.sun.star.uno.Type" ) );
529 JLocalAutoRef jo_TypeClass(
530 jni, find_class( jni, "com.sun.star.uno.TypeClass" ) );
531 JLocalAutoRef jo_IEnvironment(
532 jni, find_class( jni, "com.sun.star.uno.IEnvironment" ) );
533 JLocalAutoRef jo_JNI_proxy(
534 jni, find_class( jni, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
535 JLocalAutoRef jo_AsynchronousFinalizer(
536 jni, find_class( jni, "com.sun.star.lib.util.AsynchronousFinalizer" ) );
538 // method Object.toString()
539 m_method_Object_toString = jni->GetMethodID(
540 static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
541 jni.ensure_no_exception();
542 assert( m_method_Object_toString != nullptr );
543 // method Class.getName()
544 m_method_Class_getName = jni->GetMethodID(
545 static_cast<jclass>(jo_Class.get()), "getName", "()Ljava/lang/String;" );
546 jni.ensure_no_exception();
547 assert( m_method_Class_getName != nullptr );
549 // method Throwable.getMessage()
550 m_method_Throwable_getMessage = jni->GetMethodID(
551 static_cast<jclass>(jo_Throwable.get()), "getMessage", "()Ljava/lang/String;" );
552 jni.ensure_no_exception();
553 assert( m_method_Throwable_getMessage != nullptr );
555 // method Character.charValue()
556 m_method_Character_charValue = jni->GetMethodID(
557 static_cast<jclass>(jo_Character.get()), "charValue", "()C" );
558 jni.ensure_no_exception();
559 assert( m_method_Character_charValue != nullptr );
560 // method Boolean.booleanValue()
561 m_method_Boolean_booleanValue = jni->GetMethodID(
562 static_cast<jclass>(jo_Boolean.get()), "booleanValue", "()Z" );
563 jni.ensure_no_exception();
564 assert( m_method_Boolean_booleanValue != nullptr );
565 // method Byte.byteValue()
566 m_method_Byte_byteValue = jni->GetMethodID(
567 static_cast<jclass>(jo_Byte.get()), "byteValue", "()B" );
568 jni.ensure_no_exception();
569 assert( m_method_Byte_byteValue != nullptr );
570 // method Short.shortValue()
571 m_method_Short_shortValue = jni->GetMethodID(
572 static_cast<jclass>(jo_Short.get()), "shortValue", "()S" );
573 jni.ensure_no_exception();
574 assert( m_method_Short_shortValue != nullptr );
575 // method Integer.intValue()
576 m_method_Integer_intValue = jni->GetMethodID(
577 static_cast<jclass>(jo_Integer.get()), "intValue", "()I" );
578 jni.ensure_no_exception();
579 assert( m_method_Integer_intValue != nullptr );
580 // method Long.longValue()
581 m_method_Long_longValue = jni->GetMethodID(
582 static_cast<jclass>(jo_Long.get()), "longValue", "()J" );
583 jni.ensure_no_exception();
584 assert( m_method_Long_longValue != nullptr );
585 // method Float.floatValue()
586 m_method_Float_floatValue = jni->GetMethodID(
587 static_cast<jclass>(jo_Float.get()), "floatValue", "()F" );
588 jni.ensure_no_exception();
589 assert( m_method_Float_floatValue != nullptr );
590 // method Double.doubleValue()
591 m_method_Double_doubleValue = jni->GetMethodID(
592 static_cast<jclass>(jo_Double.get()), "doubleValue", "()D" );
593 jni.ensure_no_exception();
594 assert( m_method_Double_doubleValue != nullptr );
596 // ctor Character( char )
597 m_ctor_Character_with_char = jni->GetMethodID(
598 static_cast<jclass>(jo_Character.get()), "<init>", "(C)V" );
599 jni.ensure_no_exception();
600 assert( m_ctor_Character_with_char != nullptr );
601 // ctor Boolean( boolean )
602 m_ctor_Boolean_with_boolean = jni->GetMethodID(
603 static_cast<jclass>(jo_Boolean.get()), "<init>", "(Z)V" );
604 jni.ensure_no_exception();
605 assert( m_ctor_Boolean_with_boolean != nullptr );
606 // ctor Byte( byte )
607 m_ctor_Byte_with_byte = jni->GetMethodID(
608 static_cast<jclass>(jo_Byte.get()), "<init>", "(B)V" );
609 jni.ensure_no_exception();
610 assert( m_ctor_Byte_with_byte != nullptr );
611 // ctor Short( short )
612 m_ctor_Short_with_short = jni->GetMethodID(
613 static_cast<jclass>(jo_Short.get()), "<init>", "(S)V" );
614 jni.ensure_no_exception();
615 assert( m_ctor_Short_with_short != nullptr );
616 // ctor Integer( int )
617 m_ctor_Integer_with_int = jni->GetMethodID(
618 static_cast<jclass>(jo_Integer.get()), "<init>", "(I)V" );
619 jni.ensure_no_exception();
620 assert( m_ctor_Integer_with_int != nullptr );
621 // ctor Long( long )
622 m_ctor_Long_with_long = jni->GetMethodID(
623 static_cast<jclass>(jo_Long.get()), "<init>", "(J)V" );
624 jni.ensure_no_exception();
625 assert( m_ctor_Long_with_long != nullptr );
626 // ctor Float( float )
627 m_ctor_Float_with_float = jni->GetMethodID(
628 static_cast<jclass>(jo_Float.get()), "<init>", "(F)V" );
629 jni.ensure_no_exception();
630 assert( m_ctor_Float_with_float != nullptr );
631 // ctor Double( double )
632 m_ctor_Double_with_double = jni->GetMethodID(
633 static_cast<jclass>(jo_Double.get()), "<init>", "(D)V" );
634 jni.ensure_no_exception();
635 assert( m_ctor_Double_with_double != nullptr );
637 // static method UnoRuntime.generateOid()
638 m_method_UnoRuntime_generateOid = jni->GetStaticMethodID(
639 static_cast<jclass>(jo_UnoRuntime.get()),
640 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
641 jni.ensure_no_exception();
642 assert( m_method_UnoRuntime_generateOid != nullptr );
643 // static method UnoRuntime.queryInterface()
644 m_method_UnoRuntime_queryInterface = jni->GetStaticMethodID(
645 static_cast<jclass>(jo_UnoRuntime.get()),
646 "queryInterface",
647 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
648 jni.ensure_no_exception();
649 assert( m_method_UnoRuntime_queryInterface != nullptr );
651 // field Enum.m_value
652 m_field_Enum_m_value = jni->GetFieldID(
653 static_cast<jclass>(jo_Enum.get()), "m_value", "I" );
654 jni.ensure_no_exception();
655 assert( m_field_Enum_m_value != nullptr );
657 // static method TypeClass.fromInt()
658 m_method_TypeClass_fromInt = jni->GetStaticMethodID(
659 static_cast<jclass>(jo_TypeClass.get()),
660 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
661 jni.ensure_no_exception();
662 assert( m_method_TypeClass_fromInt != nullptr );
664 // ctor Type( Class )
665 m_ctor_Type_with_Class = jni->GetMethodID(
666 static_cast<jclass>(jo_Type.get()), "<init>", "(Ljava/lang/Class;)V" );
667 jni.ensure_no_exception();
668 assert( m_ctor_Type_with_Class != nullptr );
669 // ctor Type( String, TypeClass )
670 m_ctor_Type_with_Name_TypeClass = jni->GetMethodID(
671 static_cast<jclass>(jo_Type.get()),
672 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
673 jni.ensure_no_exception();
674 assert( m_ctor_Type_with_Name_TypeClass != nullptr );
675 // field Type._typeName
676 m_field_Type_typeName = jni->GetFieldID(
677 static_cast<jclass>(jo_Type.get()), "_typeName", "Ljava/lang/String;" );
678 jni.ensure_no_exception();
679 assert( m_field_Type_typeName != nullptr );
681 // ctor Any( Type, Object )
682 m_ctor_Any_with_Type_Object = jni->GetMethodID(
683 static_cast<jclass>(jo_Any.get()),
684 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
685 jni.ensure_no_exception();
686 assert( m_ctor_Any_with_Type_Object != nullptr );
688 // field Any._type
689 m_field_Any_type = jni->GetFieldID(
690 static_cast<jclass>(jo_Any.get()), "_type", "Lcom/sun/star/uno/Type;" );
691 jni.ensure_no_exception();
692 assert( m_field_Any_type != nullptr );
693 // field Any._object
694 m_field_Any_object = jni->GetFieldID(
695 static_cast<jclass>(jo_Any.get()), "_object", "Ljava/lang/Object;" );
696 jni.ensure_no_exception();
697 assert( m_field_Any_object != nullptr );
699 // method IEnvironment.getRegisteredInterface()
700 m_method_IEnvironment_getRegisteredInterface = jni->GetMethodID(
701 static_cast<jclass>(jo_IEnvironment.get()),
702 "getRegisteredInterface",
703 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
704 jni.ensure_no_exception();
705 assert( m_method_IEnvironment_getRegisteredInterface != nullptr );
706 // method IEnvironment.registerInterface()
707 m_method_IEnvironment_registerInterface = jni->GetMethodID(
708 static_cast<jclass>(jo_IEnvironment.get()), "registerInterface",
709 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
710 "Ljava/lang/Object;" );
711 jni.ensure_no_exception();
712 assert( m_method_IEnvironment_registerInterface != nullptr );
714 // static method JNI_proxy.get_proxy_ctor()
715 m_method_JNI_proxy_get_proxy_ctor = jni->GetStaticMethodID(
716 static_cast<jclass>(jo_JNI_proxy.get()), "get_proxy_ctor",
717 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
718 jni.ensure_no_exception();
719 assert( m_method_JNI_proxy_get_proxy_ctor != nullptr );
720 // static method JNI_proxy.create()
721 m_method_JNI_proxy_create = jni->GetStaticMethodID(
722 static_cast<jclass>(jo_JNI_proxy.get()), "create",
723 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
724 "/String;Ljava/lang/reflect/Constructor;"
725 "Lcom/sun/star/lib/util/AsynchronousFinalizer;)Ljava/lang/Object;" );
726 jni.ensure_no_exception();
727 assert( m_method_JNI_proxy_create != nullptr );
728 // field JNI_proxy.m_receiver_handle
729 m_field_JNI_proxy_m_receiver_handle = jni->GetFieldID(
730 static_cast<jclass>(jo_JNI_proxy.get()), "m_receiver_handle", "J" );
731 jni.ensure_no_exception();
732 assert( m_field_JNI_proxy_m_receiver_handle != nullptr );
733 // field JNI_proxy.m_td_handle
734 m_field_JNI_proxy_m_td_handle = jni->GetFieldID(
735 static_cast<jclass>(jo_JNI_proxy.get()), "m_td_handle", "J" );
736 jni.ensure_no_exception();
737 assert( m_field_JNI_proxy_m_td_handle != nullptr );
738 // field JNI_proxy.m_type
739 m_field_JNI_proxy_m_type = jni->GetFieldID(
740 static_cast<jclass>(jo_JNI_proxy.get()), "m_type", "Lcom/sun/star/uno/Type;" );
741 jni.ensure_no_exception();
742 assert( m_field_JNI_proxy_m_type != nullptr );
743 // field JNI_proxy.m_oid
744 m_field_JNI_proxy_m_oid = jni->GetFieldID(
745 static_cast<jclass>(jo_JNI_proxy.get()), "m_oid", "Ljava/lang/String;" );
746 jni.ensure_no_exception();
747 assert( m_field_JNI_proxy_m_oid != nullptr );
749 // ctor AsynchronousFinalizer
750 m_ctor_AsynchronousFinalizer = jni->GetMethodID(
751 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "<init>", "()V" );
752 jni.ensure_no_exception();
753 assert( m_ctor_AsynchronousFinalizer != nullptr );
754 // method AsynchronousFinalizer.drain()
755 m_method_AsynchronousFinalizer_drain = jni->GetMethodID(
756 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "drain", "()V" );
757 jni.ensure_no_exception();
758 assert( m_method_AsynchronousFinalizer_drain != nullptr );
760 // get java env
761 OUString java_env_type_name( UNO_LB_JAVA );
762 JLocalAutoRef jo_java(
763 jni, ustring_to_jstring( jni, java_env_type_name.pData ) );
764 jvalue args[ 2 ];
765 args[ 0 ].l = jo_java.get();
766 args[ 1 ].l = nullptr;
767 jmethodID method_getEnvironment = jni->GetStaticMethodID(
768 static_cast<jclass>(jo_UnoRuntime.get()), "getEnvironment",
769 "(Ljava/lang/String;Ljava/lang/Object;)"
770 "Lcom/sun/star/uno/IEnvironment;" );
771 jni.ensure_no_exception();
772 assert( method_getEnvironment != nullptr );
773 JLocalAutoRef jo_java_env(
774 jni, jni->CallStaticObjectMethodA(
775 static_cast<jclass>(jo_UnoRuntime.get()), method_getEnvironment, args ) );
777 // get com.sun.star.uno.Any.VOID
778 jfieldID field_Any_VOID = jni->GetStaticFieldID(
779 static_cast<jclass>(jo_Any.get()), "VOID", "Lcom/sun/star/uno/Any;" );
780 jni.ensure_no_exception();
781 assert( field_Any_VOID != nullptr );
782 JLocalAutoRef jo_Any_VOID(
783 jni, jni->GetStaticObjectField(
784 static_cast<jclass>(jo_Any.get()), field_Any_VOID ) );
785 // get com.sun.star.uno.Type.UNSIGNED_SHORT
786 jfieldID field_Type_UNSIGNED_SHORT = jni->GetStaticFieldID(
787 static_cast<jclass>(jo_Type.get()), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
788 jni.ensure_no_exception();
789 assert( field_Type_UNSIGNED_SHORT != nullptr );
790 JLocalAutoRef jo_Type_UNSIGNED_SHORT(
791 jni, jni->GetStaticObjectField(
792 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_SHORT ) );
793 // get com.sun.star.uno.Type.UNSIGNED_LONG
794 jfieldID field_Type_UNSIGNED_LONG = jni->GetStaticFieldID(
795 static_cast<jclass>(jo_Type.get()), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
796 jni.ensure_no_exception();
797 assert( field_Type_UNSIGNED_LONG != nullptr );
798 JLocalAutoRef jo_Type_UNSIGNED_LONG(
799 jni, jni->GetStaticObjectField(
800 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_LONG ) );
801 // get com.sun.star.uno.Type.UNSIGNED_HYPER
802 jfieldID field_Type_UNSIGNED_HYPER = jni->GetStaticFieldID(
803 static_cast<jclass>(jo_Type.get()), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
804 jni.ensure_no_exception();
805 assert( field_Type_UNSIGNED_HYPER != nullptr );
806 JLocalAutoRef jo_Type_UNSIGNED_HYPER(
807 jni, jni->GetStaticObjectField(
808 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_HYPER ) );
810 // make global refs
811 m_class_UnoRuntime =
812 static_cast<jclass>(jni->NewGlobalRef( jo_UnoRuntime.get() ));
813 m_class_RuntimeException =
814 static_cast<jclass>(jni->NewGlobalRef( jo_RuntimeException.get() ));
815 m_class_Any =
816 static_cast<jclass>(jni->NewGlobalRef( jo_Any.get() ));
817 m_class_Type =
818 static_cast<jclass>(jni->NewGlobalRef( jo_Type.get() ));
819 m_class_TypeClass =
820 static_cast<jclass>(jni->NewGlobalRef( jo_TypeClass.get() ));
821 m_class_JNI_proxy =
822 static_cast<jclass>(jni->NewGlobalRef( jo_JNI_proxy.get() ));
823 m_class_AsynchronousFinalizer =
824 static_cast<jclass>(jni->NewGlobalRef( jo_AsynchronousFinalizer.get() ));
826 m_class_Character =
827 static_cast<jclass>(jni->NewGlobalRef( jo_Character.get() ));
828 m_class_Boolean =
829 static_cast<jclass>(jni->NewGlobalRef( jo_Boolean.get() ));
830 m_class_Byte =
831 static_cast<jclass>(jni->NewGlobalRef( jo_Byte.get() ));
832 m_class_Short =
833 static_cast<jclass>(jni->NewGlobalRef( jo_Short.get() ));
834 m_class_Integer =
835 static_cast<jclass>(jni->NewGlobalRef( jo_Integer.get() ));
836 m_class_Long =
837 static_cast<jclass>(jni->NewGlobalRef( jo_Long.get() ));
838 m_class_Float =
839 static_cast<jclass>(jni->NewGlobalRef( jo_Float.get() ));
840 m_class_Double =
841 static_cast<jclass>(jni->NewGlobalRef( jo_Double.get() ));
842 m_class_String =
843 static_cast<jclass>(jni->NewGlobalRef( jo_String.get() ));
844 m_class_Object =
845 static_cast<jclass>(jni->NewGlobalRef( jo_Object.get() ));
846 m_class_Class =
847 static_cast<jclass>(jni->NewGlobalRef( m_class_Class ));
849 m_object_Any_VOID =
850 jni->NewGlobalRef( jo_Any_VOID.get() );
851 m_object_Type_UNSIGNED_SHORT =
852 jni->NewGlobalRef( jo_Type_UNSIGNED_SHORT.get() );
853 m_object_Type_UNSIGNED_LONG =
854 jni->NewGlobalRef( jo_Type_UNSIGNED_LONG.get() );
855 m_object_Type_UNSIGNED_HYPER =
856 jni->NewGlobalRef( jo_Type_UNSIGNED_HYPER.get() );
857 m_object_java_env = jni->NewGlobalRef( jo_java_env.get() );
861 css::uno::TypeDescription XInterface_td(
862 cppu::UnoType<css::uno::XInterface>::get());
863 // coverity [ctor_dtor_leak]
864 m_XInterface_type_info =
865 new JNI_interface_type_info( jni, XInterface_td.get() );
867 catch (...)
869 destruct( jni_env );
870 throw;
875 void JNI_info::destruct( JNIEnv * jni_env )
877 for (auto & i: m_type_map)
879 i.second.m_info->destroy( jni_env );
881 if (m_XInterface_type_info != nullptr)
883 const_cast< JNI_interface_type_info * >(
884 m_XInterface_type_info )->destroy( jni_env );
887 // free global refs
888 jni_env->DeleteGlobalRef( m_object_java_env );
889 jni_env->DeleteGlobalRef( m_object_Any_VOID );
890 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT );
891 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG );
892 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER );
894 jni_env->DeleteGlobalRef( m_class_Class );
895 jni_env->DeleteGlobalRef( m_class_Object );
896 jni_env->DeleteGlobalRef( m_class_String );
897 jni_env->DeleteGlobalRef( m_class_Double );
898 jni_env->DeleteGlobalRef( m_class_Float );
899 jni_env->DeleteGlobalRef( m_class_Long );
900 jni_env->DeleteGlobalRef( m_class_Integer );
901 jni_env->DeleteGlobalRef( m_class_Short );
902 jni_env->DeleteGlobalRef( m_class_Byte );
903 jni_env->DeleteGlobalRef( m_class_Boolean );
904 jni_env->DeleteGlobalRef( m_class_Character );
906 jni_env->DeleteGlobalRef( m_class_AsynchronousFinalizer );
907 jni_env->DeleteGlobalRef( m_class_JNI_proxy );
908 jni_env->DeleteGlobalRef( m_class_RuntimeException );
909 jni_env->DeleteGlobalRef( m_class_UnoRuntime );
910 jni_env->DeleteGlobalRef( m_class_TypeClass );
911 jni_env->DeleteGlobalRef( m_class_Type );
912 jni_env->DeleteGlobalRef( m_class_Any );
916 JNI_info const * JNI_info::get_jni_info(
917 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm )
919 // !!!no JNI_info available at JNI_context!!!
920 ::jvmaccess::VirtualMachine::AttachGuard guard(
921 uno_vm->getVirtualMachine() );
922 JNIEnv * jni_env = guard.getEnvironment();
923 JNI_context jni(
924 nullptr, jni_env, static_cast< jobject >(uno_vm->getClassLoader()) );
926 jclass jo_class;
927 jmethodID jo_forName;
928 jni.getClassForName( &jo_class, &jo_forName );
929 jni.ensure_no_exception();
930 JLocalAutoRef jo_JNI_info_holder(
931 jni,
932 jni.findClass(
933 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class,
934 jo_forName, false ) );
935 // field JNI_info_holder.m_jni_info_handle
936 jfieldID field_s_jni_info_handle =
937 jni->GetStaticFieldID(
938 static_cast<jclass>(jo_JNI_info_holder.get()), "s_jni_info_handle", "J" );
939 jni.ensure_no_exception();
940 assert( field_s_jni_info_handle != nullptr );
942 JNI_info const * jni_info =
943 reinterpret_cast< JNI_info const * >(
944 jni->GetStaticLongField(
945 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle ) );
946 if (jni_info == nullptr) // un-initialized?
948 JNI_info * new_info = new JNI_info(
949 jni_env, static_cast< jobject >(uno_vm->getClassLoader()), jo_class,
950 jo_forName );
952 ClearableMutexGuard g( Mutex::getGlobalMutex() );
953 jni_info =
954 reinterpret_cast< JNI_info const * >(
955 jni->GetStaticLongField(
956 static_cast<jclass>(jo_JNI_info_holder.get()),
957 field_s_jni_info_handle ) );
958 if (jni_info == nullptr) // still un-initialized?
960 jni->SetStaticLongField(
961 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle,
962 reinterpret_cast< jlong >( new_info ) );
963 jni_info = new_info;
965 else
967 g.clear();
968 new_info->destroy( jni_env );
972 return jni_info;
977 extern "C"
981 SAL_JNI_EXPORT void
982 JNICALL Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
983 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle )
984 SAL_THROW_EXTERN_C()
986 ::jni_uno::JNI_info * jni_info =
987 reinterpret_cast< ::jni_uno::JNI_info * >( jni_info_handle );
988 jni_info->destroy( jni_env );
993 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */