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