bump product version to 5.0.4.1
[LibreOffice.git] / bridges / source / jni_uno / jni_info.cxx
blobfc49ea4a1855577289150cc53d582d8b029af058
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( 0 )
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() );
60 void JNI_interface_type_info::destroy( JNIEnv * jni_env )
62 JNI_type_info::destruct( jni_env );
63 jni_env->DeleteGlobalRef( m_proxy_ctor );
64 jni_env->DeleteGlobalRef( m_type );
65 delete [] m_methods;
66 delete this;
70 JNI_interface_type_info::JNI_interface_type_info(
71 JNI_context const & jni, typelib_TypeDescription * td_ )
72 : JNI_type_info( jni, td_ )
74 assert( typelib_TypeClass_INTERFACE == m_td.get()->eTypeClass );
76 OUString const & uno_name = OUString::unacquired( &m_td.get()->pTypeName );
77 JNI_info const * jni_info = jni.get_info();
79 JLocalAutoRef jo_class(
80 jni,
81 find_class(
82 jni,
83 ( OUStringToOString( uno_name, RTL_TEXTENCODING_JAVA_UTF8 ).
84 getStr() ) ) );
85 JLocalAutoRef jo_type( jni, create_type( jni, static_cast<jclass>(jo_class.get()) ) );
87 // get proxy ctor
88 jvalue arg;
89 arg.l = jo_class.get();
90 JLocalAutoRef jo_proxy_ctor(
91 jni, jni->CallStaticObjectMethodA(
92 jni_info->m_class_JNI_proxy,
93 jni_info->m_method_JNI_proxy_get_proxy_ctor, &arg ) );
95 if (is_XInterface( m_td.get()->pWeakRef ))
97 m_methods = 0; // no methods
99 else
101 // retrieve method ids for all direct members
104 typelib_InterfaceTypeDescription * td =
105 reinterpret_cast< typelib_InterfaceTypeDescription * >(
106 m_td.get() );
107 // coverity [ctor_dtor_leak]
108 m_methods = new jmethodID[ td->nMapFunctionIndexToMemberIndex ];
109 sal_Int32 nMethodIndex = 0;
110 typelib_TypeDescriptionReference ** ppMembers = td->ppMembers;
111 sal_Int32 nMembers = td->nMembers;
113 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
115 TypeDescr member_td( ppMembers[ nPos ] );
117 OStringBuffer sig_buf( 64 );
119 if (typelib_TypeClass_INTERFACE_METHOD ==
120 member_td.get()->eTypeClass) // method
122 typelib_InterfaceMethodTypeDescription * method_td =
123 reinterpret_cast<
124 typelib_InterfaceMethodTypeDescription * >(
125 member_td.get() );
127 sig_buf.append( '(' );
128 for ( sal_Int32 i = 0; i < method_td->nParams; ++i )
130 typelib_MethodParameter const & param =
131 method_td->pParams[ i ];
132 if (param.bOut)
133 sig_buf.append( '[' );
134 JNI_info::append_sig( &sig_buf, param.pTypeRef );
136 sig_buf.append( ')' );
137 JNI_info::append_sig( &sig_buf, method_td->pReturnTypeRef );
139 OString method_signature( sig_buf.makeStringAndClear() );
140 OString method_name(
141 OUStringToOString( OUString::unacquired(
142 &method_td->aBase.pMemberName ),
143 RTL_TEXTENCODING_JAVA_UTF8 ) );
145 m_methods[ nMethodIndex ] = jni->GetMethodID(
146 static_cast<jclass>(jo_class.get()), method_name.getStr(),
147 method_signature.getStr() );
148 jni.ensure_no_exception();
149 assert( 0 != m_methods[ nMethodIndex ] );
150 ++nMethodIndex;
152 else // attribute
154 assert(
155 typelib_TypeClass_INTERFACE_ATTRIBUTE ==
156 member_td.get()->eTypeClass );
157 typelib_InterfaceAttributeTypeDescription * attribute_td =
158 reinterpret_cast<
159 typelib_InterfaceAttributeTypeDescription * >(
160 member_td.get() );
162 // type sig
163 JNI_info::append_sig(
164 &sig_buf, attribute_td->pAttributeTypeRef );
165 OString type_sig( sig_buf.makeStringAndClear() );
166 sig_buf.ensureCapacity( 64 );
167 // member name
168 OUString const & member_name =
169 OUString::unacquired(
170 &attribute_td->aBase.pMemberName );
172 // getter
173 sig_buf.append( "()" );
174 sig_buf.append( type_sig );
175 OString method_signature( sig_buf.makeStringAndClear() );
176 OUStringBuffer name_buf( 3 + member_name.getLength() );
177 name_buf.append( "get" );
178 name_buf.append( member_name );
179 OString method_name(
180 OUStringToOString(
181 name_buf.makeStringAndClear(),
182 RTL_TEXTENCODING_JAVA_UTF8 ) );
183 m_methods[ nMethodIndex ] = jni->GetMethodID(
184 static_cast<jclass>(jo_class.get()), method_name.getStr(),
185 method_signature.getStr() );
186 jni.ensure_no_exception();
187 assert( 0 != m_methods[ nMethodIndex ] );
188 ++nMethodIndex;
189 if (! attribute_td->bReadOnly)
191 // setter
192 sig_buf.ensureCapacity( 64 );
193 sig_buf.append( '(' );
194 sig_buf.append( type_sig );
195 sig_buf.append( ")V" );
196 method_signature = sig_buf.makeStringAndClear();
197 name_buf.ensureCapacity( 3 + member_name.getLength() );
198 name_buf.append( "set" );
199 name_buf.append( member_name );
200 method_name = OUStringToOString(
201 name_buf.makeStringAndClear(),
202 RTL_TEXTENCODING_JAVA_UTF8 );
203 m_methods[ nMethodIndex ] = jni->GetMethodID(
204 static_cast<jclass>(jo_class.get()), method_name.getStr(),
205 method_signature.getStr() );
206 jni.ensure_no_exception();
207 assert( 0 != m_methods[ nMethodIndex ] );
208 ++nMethodIndex;
213 catch (...)
215 delete [] m_methods;
216 throw;
219 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
220 m_type = jni->NewGlobalRef( jo_type.get() );
221 m_proxy_ctor = jni->NewGlobalRef( jo_proxy_ctor.get() );
226 void JNI_compound_type_info::destroy( JNIEnv * jni_env )
228 JNI_type_info::destruct( jni_env );
229 delete [] m_fields;
230 delete this;
234 JNI_compound_type_info::JNI_compound_type_info(
235 JNI_context const & jni, typelib_TypeDescription * td_ )
236 : JNI_type_info( jni, td_ ),
237 m_exc_ctor( 0 ),
238 m_fields( 0 )
240 assert( typelib_TypeClass_STRUCT == m_td.get()->eTypeClass ||
241 typelib_TypeClass_EXCEPTION == m_td.get()->eTypeClass );
242 typelib_CompoundTypeDescription * td =
243 reinterpret_cast< typelib_CompoundTypeDescription * >( m_td.get() );
245 OUString const & uno_name =
246 OUString::unacquired( &td->aBase.pTypeName );
248 // Erase type arguments of instantiated polymorphic struct types:
249 OUString nucleus;
250 sal_Int32 i = uno_name.indexOf( '<' );
251 if ( i < 0 ) {
252 nucleus = uno_name;
253 } else {
254 nucleus = uno_name.copy( 0, i );
256 JLocalAutoRef jo_class(
257 jni,
258 find_class(
259 jni,
260 OUStringToOString(
261 nucleus, RTL_TEXTENCODING_JAVA_UTF8 ).getStr() ) );
263 JNI_info const * jni_info = jni.get_info();
265 if (typelib_TypeClass_EXCEPTION == m_td.get()->eTypeClass)
267 // retrieve exc ctor( msg )
268 m_exc_ctor = jni->GetMethodID(
269 static_cast<jclass>(jo_class.get()), "<init>", "(Ljava/lang/String;)V" );
270 jni.ensure_no_exception();
271 assert( 0 != m_exc_ctor );
274 // retrieve info for base type
275 typelib_TypeDescription * base_td =
276 type_equals(
277 td->aBase.pWeakRef,
278 jni_info->m_RuntimeException_type.getTypeLibType())
280 : reinterpret_cast< typelib_TypeDescription * >(
281 td->pBaseTypeDescription );
282 m_base = (0 == base_td ? 0 : jni_info->get_type_info( jni, base_td ));
286 if (type_equals(
287 td->aBase.pWeakRef,
288 jni_info->m_Exception_type.getTypeLibType() ) ||
289 type_equals(
290 td->aBase.pWeakRef,
291 jni_info->m_RuntimeException_type.getTypeLibType() ))
293 // coverity [ctor_dtor_leak]
294 m_fields = new jfieldID[ 2 ];
295 m_fields[ 0 ] = 0; // special Throwable.getMessage()
296 // field Context
297 m_fields[ 1 ] = jni->GetFieldID(
298 static_cast<jclass>(jo_class.get()), "Context", "Ljava/lang/Object;" );
299 jni.ensure_no_exception();
300 assert( 0 != m_fields[ 1 ] );
302 else
304 // retrieve field ids for all direct members
305 sal_Int32 nMembers = td->nMembers;
306 m_fields = new jfieldID[ nMembers ];
308 for ( sal_Int32 nPos = 0; nPos < nMembers; ++nPos )
310 OString sig;
311 if (td->aBase.eTypeClass == typelib_TypeClass_STRUCT
312 && reinterpret_cast< typelib_StructTypeDescription * >(
313 td)->pParameterizedTypes != 0
314 && reinterpret_cast< typelib_StructTypeDescription * >(
315 td)->pParameterizedTypes[nPos])
317 sig = OString( "Ljava/lang/Object;" );
318 } else {
319 OStringBuffer sig_buf( 32 );
320 JNI_info::append_sig( &sig_buf, td->ppTypeRefs[ nPos ] );
321 sig = sig_buf.makeStringAndClear();
324 OString member_name(
325 OUStringToOString(
326 OUString::unacquired( &td->ppMemberNames[ nPos ] ),
327 RTL_TEXTENCODING_JAVA_UTF8 ) );
329 m_fields[ nPos ] = jni->GetFieldID(
330 static_cast<jclass>(jo_class.get()), member_name.getStr(),
331 sig.getStr() );
332 jni.ensure_no_exception();
333 assert( 0 != m_fields[ nPos ] );
337 catch (...)
339 delete [] m_fields;
340 throw;
343 m_class = static_cast<jclass>(jni->NewGlobalRef( jo_class.get() ));
348 JNI_type_info const * JNI_info::create_type_info(
349 JNI_context const & jni, typelib_TypeDescription * td ) const
351 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
353 JNI_type_info * new_info;
354 switch (td->eTypeClass)
356 case typelib_TypeClass_STRUCT:
357 case typelib_TypeClass_EXCEPTION:
359 new_info = new JNI_compound_type_info( jni, td );
360 break;
362 case typelib_TypeClass_INTERFACE:
364 new_info = new JNI_interface_type_info( jni, td );
365 break;
367 default:
369 throw BridgeRuntimeError(
370 "type info not supported for " + uno_name + jni.get_stack_trace() );
374 // look up
375 JNI_type_info * info;
376 ClearableMutexGuard guard( m_mutex );
377 JNI_type_info_holder & holder = m_type_map[ uno_name ];
378 if (0 == holder.m_info) // new insertion
380 holder.m_info = new_info;
381 guard.clear();
382 info = new_info;
384 else // inserted in the meantime
386 info = holder.m_info;
387 guard.clear();
388 new_info->destroy( jni.get_jni_env() );
390 return info;
394 JNI_type_info const * JNI_info::get_type_info(
395 JNI_context const & jni, typelib_TypeDescription * td ) const
397 if (is_XInterface( td->pWeakRef ))
399 return m_XInterface_type_info;
402 OUString const & uno_name = OUString::unacquired( &td->pTypeName );
403 JNI_type_info const * info;
404 ClearableMutexGuard guard( m_mutex );
406 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
407 if (iFind == m_type_map.end())
409 guard.clear();
410 info = create_type_info( jni, td );
412 else
414 info = iFind->second.m_info;
417 return info;
421 JNI_type_info const * JNI_info::get_type_info(
422 JNI_context const & jni, typelib_TypeDescriptionReference * type ) const
424 if (is_XInterface( type ))
426 return m_XInterface_type_info;
429 OUString const & uno_name = OUString::unacquired( &type->pTypeName );
430 JNI_type_info const * info;
431 ClearableMutexGuard guard( m_mutex );
432 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
433 if (iFind == m_type_map.end())
435 guard.clear();
436 TypeDescr td( type );
437 info = create_type_info( jni, td.get() );
439 else
441 info = iFind->second.m_info;
444 return info;
448 JNI_type_info const * JNI_info::get_type_info(
449 JNI_context const & jni, OUString const & uno_name ) const
451 if ( uno_name == "com.sun.star.uno.XInterface" )
453 return m_XInterface_type_info;
456 JNI_type_info const * info;
457 ClearableMutexGuard guard( m_mutex );
458 t_str2type::const_iterator iFind( m_type_map.find( uno_name ) );
459 if (iFind == m_type_map.end())
461 guard.clear();
462 css::uno::TypeDescription td( uno_name );
463 if (! td.is())
465 throw BridgeRuntimeError(
466 "UNO type not found: " + uno_name + jni.get_stack_trace() );
468 info = create_type_info( jni, td.get() );
470 else
472 info = iFind->second.m_info;
475 return info;
479 JNI_info::JNI_info(
480 JNIEnv * jni_env, jobject class_loader, jclass classClass,
481 jmethodID methodForName )
482 : m_class_Class( classClass ),
483 m_method_Class_forName( methodForName ),
484 m_class_JNI_proxy( 0 ),
485 m_XInterface_queryInterface_td(
486 (reinterpret_cast< typelib_InterfaceTypeDescription * >(
487 css::uno::TypeDescription(
488 cppu::UnoType<css::uno::XInterface>::get())
489 .get())->ppMembers[ 0 ] ) ),
490 m_Exception_type(cppu::UnoType<css::uno::Exception>::get()),
491 m_RuntimeException_type(cppu::UnoType<css::uno::RuntimeException>::get()),
492 m_void_type(cppu::UnoType<void>::get()),
493 m_XInterface_type_info( 0 )
495 JNI_context jni( this, jni_env, class_loader ); // !no proper jni_info!
497 // class lookup
498 JLocalAutoRef jo_Object(
499 jni, find_class( jni, "java.lang.Object" ) );
500 JLocalAutoRef jo_Class(
501 jni, find_class( jni, "java.lang.Class" ) );
502 JLocalAutoRef jo_Throwable(
503 jni, find_class( jni, "java.lang.Throwable" ) );
504 JLocalAutoRef jo_Character(
505 jni, find_class( jni, "java.lang.Character" ) );
506 JLocalAutoRef jo_Boolean(
507 jni, find_class( jni, "java.lang.Boolean" ) );
508 JLocalAutoRef jo_Byte(
509 jni, find_class( jni, "java.lang.Byte" ) );
510 JLocalAutoRef jo_Short(
511 jni, find_class( jni, "java.lang.Short" ) );
512 JLocalAutoRef jo_Integer(
513 jni, find_class( jni, "java.lang.Integer" ) );
514 JLocalAutoRef jo_Long(
515 jni, find_class( jni, "java.lang.Long" ) );
516 JLocalAutoRef jo_Float(
517 jni, find_class( jni, "java.lang.Float" ) );
518 JLocalAutoRef jo_Double(
519 jni, find_class( jni, "java.lang.Double" ) );
520 JLocalAutoRef jo_String(
521 jni, find_class( jni, "java.lang.String" ) );
522 JLocalAutoRef jo_RuntimeException(
523 jni, find_class( jni, "com.sun.star.uno.RuntimeException" ) );
524 JLocalAutoRef jo_UnoRuntime(
525 jni, find_class( jni, "com.sun.star.uno.UnoRuntime" ) );
526 JLocalAutoRef jo_Any(
527 jni, find_class( jni, "com.sun.star.uno.Any" ) );
528 JLocalAutoRef jo_Enum(
529 jni, find_class( jni, "com.sun.star.uno.Enum" ) );
530 JLocalAutoRef jo_Type(
531 jni, find_class( jni, "com.sun.star.uno.Type" ) );
532 JLocalAutoRef jo_TypeClass(
533 jni, find_class( jni, "com.sun.star.uno.TypeClass" ) );
534 JLocalAutoRef jo_IEnvironment(
535 jni, find_class( jni, "com.sun.star.uno.IEnvironment" ) );
536 JLocalAutoRef jo_JNI_proxy(
537 jni, find_class( jni, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
538 JLocalAutoRef jo_AsynchronousFinalizer(
539 jni, find_class( jni, "com.sun.star.lib.util.AsynchronousFinalizer" ) );
541 // method Object.toString()
542 m_method_Object_toString = jni->GetMethodID(
543 static_cast<jclass>(jo_Object.get()), "toString", "()Ljava/lang/String;" );
544 jni.ensure_no_exception();
545 assert( 0 != m_method_Object_toString );
546 // method Class.getName()
547 m_method_Class_getName = jni->GetMethodID(
548 static_cast<jclass>(jo_Class.get()), "getName", "()Ljava/lang/String;" );
549 jni.ensure_no_exception();
550 assert( 0 != m_method_Class_getName );
552 // method Throwable.getMessage()
553 m_method_Throwable_getMessage = jni->GetMethodID(
554 static_cast<jclass>(jo_Throwable.get()), "getMessage", "()Ljava/lang/String;" );
555 jni.ensure_no_exception();
556 assert( 0 != m_method_Throwable_getMessage );
558 // method Character.charValue()
559 m_method_Character_charValue = jni->GetMethodID(
560 static_cast<jclass>(jo_Character.get()), "charValue", "()C" );
561 jni.ensure_no_exception();
562 assert( 0 != m_method_Character_charValue );
563 // method Boolean.booleanValue()
564 m_method_Boolean_booleanValue = jni->GetMethodID(
565 static_cast<jclass>(jo_Boolean.get()), "booleanValue", "()Z" );
566 jni.ensure_no_exception();
567 assert( 0 != m_method_Boolean_booleanValue );
568 // method Byte.byteValue()
569 m_method_Byte_byteValue = jni->GetMethodID(
570 static_cast<jclass>(jo_Byte.get()), "byteValue", "()B" );
571 jni.ensure_no_exception();
572 assert( 0 != m_method_Byte_byteValue );
573 // method Short.shortValue()
574 m_method_Short_shortValue = jni->GetMethodID(
575 static_cast<jclass>(jo_Short.get()), "shortValue", "()S" );
576 jni.ensure_no_exception();
577 assert( 0 != m_method_Short_shortValue );
578 // method Integer.intValue()
579 m_method_Integer_intValue = jni->GetMethodID(
580 static_cast<jclass>(jo_Integer.get()), "intValue", "()I" );
581 jni.ensure_no_exception();
582 assert( 0 != m_method_Integer_intValue );
583 // method Long.longValue()
584 m_method_Long_longValue = jni->GetMethodID(
585 static_cast<jclass>(jo_Long.get()), "longValue", "()J" );
586 jni.ensure_no_exception();
587 assert( 0 != m_method_Long_longValue );
588 // method Float.floatValue()
589 m_method_Float_floatValue = jni->GetMethodID(
590 static_cast<jclass>(jo_Float.get()), "floatValue", "()F" );
591 jni.ensure_no_exception();
592 assert( 0 != m_method_Float_floatValue );
593 // method Double.doubleValue()
594 m_method_Double_doubleValue = jni->GetMethodID(
595 static_cast<jclass>(jo_Double.get()), "doubleValue", "()D" );
596 jni.ensure_no_exception();
597 assert( 0 != m_method_Double_doubleValue );
599 // ctor Character( char )
600 m_ctor_Character_with_char = jni->GetMethodID(
601 static_cast<jclass>(jo_Character.get()), "<init>", "(C)V" );
602 jni.ensure_no_exception();
603 assert( 0 != m_ctor_Character_with_char );
604 // ctor Boolean( boolean )
605 m_ctor_Boolean_with_boolean = jni->GetMethodID(
606 static_cast<jclass>(jo_Boolean.get()), "<init>", "(Z)V" );
607 jni.ensure_no_exception();
608 assert( 0 != m_ctor_Boolean_with_boolean );
609 // ctor Byte( byte )
610 m_ctor_Byte_with_byte = jni->GetMethodID(
611 static_cast<jclass>(jo_Byte.get()), "<init>", "(B)V" );
612 jni.ensure_no_exception();
613 assert( 0 != m_ctor_Byte_with_byte );
614 // ctor Short( short )
615 m_ctor_Short_with_short = jni->GetMethodID(
616 static_cast<jclass>(jo_Short.get()), "<init>", "(S)V" );
617 jni.ensure_no_exception();
618 assert( 0 != m_ctor_Short_with_short );
619 // ctor Integer( int )
620 m_ctor_Integer_with_int = jni->GetMethodID(
621 static_cast<jclass>(jo_Integer.get()), "<init>", "(I)V" );
622 jni.ensure_no_exception();
623 assert( 0 != m_ctor_Integer_with_int );
624 // ctor Long( long )
625 m_ctor_Long_with_long = jni->GetMethodID(
626 static_cast<jclass>(jo_Long.get()), "<init>", "(J)V" );
627 jni.ensure_no_exception();
628 assert( 0 != m_ctor_Long_with_long );
629 // ctor Float( float )
630 m_ctor_Float_with_float = jni->GetMethodID(
631 static_cast<jclass>(jo_Float.get()), "<init>", "(F)V" );
632 jni.ensure_no_exception();
633 assert( 0 != m_ctor_Float_with_float );
634 // ctor Double( double )
635 m_ctor_Double_with_double = jni->GetMethodID(
636 static_cast<jclass>(jo_Double.get()), "<init>", "(D)V" );
637 jni.ensure_no_exception();
638 assert( 0 != m_ctor_Double_with_double );
640 // static method UnoRuntime.generateOid()
641 m_method_UnoRuntime_generateOid = jni->GetStaticMethodID(
642 static_cast<jclass>(jo_UnoRuntime.get()),
643 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
644 jni.ensure_no_exception();
645 assert( 0 != m_method_UnoRuntime_generateOid );
646 // static method UnoRuntime.queryInterface()
647 m_method_UnoRuntime_queryInterface = jni->GetStaticMethodID(
648 static_cast<jclass>(jo_UnoRuntime.get()),
649 "queryInterface",
650 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
651 jni.ensure_no_exception();
652 assert( 0 != m_method_UnoRuntime_queryInterface );
654 // field Enum.m_value
655 m_field_Enum_m_value = jni->GetFieldID(
656 static_cast<jclass>(jo_Enum.get()), "m_value", "I" );
657 jni.ensure_no_exception();
658 assert( 0 != m_field_Enum_m_value );
660 // static method TypeClass.fromInt()
661 m_method_TypeClass_fromInt = jni->GetStaticMethodID(
662 static_cast<jclass>(jo_TypeClass.get()),
663 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
664 jni.ensure_no_exception();
665 assert( 0 != m_method_TypeClass_fromInt );
667 // ctor Type( Class )
668 m_ctor_Type_with_Class = jni->GetMethodID(
669 static_cast<jclass>(jo_Type.get()), "<init>", "(Ljava/lang/Class;)V" );
670 jni.ensure_no_exception();
671 assert( 0 != m_ctor_Type_with_Class );
672 // ctor Type( String, TypeClass )
673 m_ctor_Type_with_Name_TypeClass = jni->GetMethodID(
674 static_cast<jclass>(jo_Type.get()),
675 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
676 jni.ensure_no_exception();
677 assert( 0 != m_ctor_Type_with_Name_TypeClass );
678 // field Type._typeName
679 m_field_Type__typeName = jni->GetFieldID(
680 static_cast<jclass>(jo_Type.get()), "_typeName", "Ljava/lang/String;" );
681 jni.ensure_no_exception();
682 assert( 0 != m_field_Type__typeName );
684 // ctor Any( Type, Object )
685 m_ctor_Any_with_Type_Object = jni->GetMethodID(
686 static_cast<jclass>(jo_Any.get()),
687 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
688 jni.ensure_no_exception();
689 assert( 0 != m_ctor_Any_with_Type_Object );
691 // field Any._type
692 m_field_Any__type = jni->GetFieldID(
693 static_cast<jclass>(jo_Any.get()), "_type", "Lcom/sun/star/uno/Type;" );
694 jni.ensure_no_exception();
695 assert( 0 != m_field_Any__type );
696 // field Any._object
697 m_field_Any__object = jni->GetFieldID(
698 static_cast<jclass>(jo_Any.get()), "_object", "Ljava/lang/Object;" );
699 jni.ensure_no_exception();
700 assert( 0 != m_field_Any__object );
702 // method IEnvironment.getRegisteredInterface()
703 m_method_IEnvironment_getRegisteredInterface = jni->GetMethodID(
704 static_cast<jclass>(jo_IEnvironment.get()),
705 "getRegisteredInterface",
706 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
707 jni.ensure_no_exception();
708 assert( 0 != m_method_IEnvironment_getRegisteredInterface );
709 // method IEnvironment.registerInterface()
710 m_method_IEnvironment_registerInterface = jni->GetMethodID(
711 static_cast<jclass>(jo_IEnvironment.get()), "registerInterface",
712 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
713 "Ljava/lang/Object;" );
714 jni.ensure_no_exception();
715 assert( 0 != m_method_IEnvironment_registerInterface );
717 // static method JNI_proxy.get_proxy_ctor()
718 m_method_JNI_proxy_get_proxy_ctor = jni->GetStaticMethodID(
719 static_cast<jclass>(jo_JNI_proxy.get()), "get_proxy_ctor",
720 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
721 jni.ensure_no_exception();
722 assert( 0 != m_method_JNI_proxy_get_proxy_ctor );
723 // static method JNI_proxy.create()
724 m_method_JNI_proxy_create = jni->GetStaticMethodID(
725 static_cast<jclass>(jo_JNI_proxy.get()), "create",
726 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
727 "/String;Ljava/lang/reflect/Constructor;"
728 "Lcom/sun/star/lib/util/AsynchronousFinalizer;)Ljava/lang/Object;" );
729 jni.ensure_no_exception();
730 assert( 0 != m_method_JNI_proxy_create );
731 // field JNI_proxy.m_receiver_handle
732 m_field_JNI_proxy_m_receiver_handle = jni->GetFieldID(
733 static_cast<jclass>(jo_JNI_proxy.get()), "m_receiver_handle", "J" );
734 jni.ensure_no_exception();
735 assert( 0 != m_field_JNI_proxy_m_receiver_handle );
736 // field JNI_proxy.m_td_handle
737 m_field_JNI_proxy_m_td_handle = jni->GetFieldID(
738 static_cast<jclass>(jo_JNI_proxy.get()), "m_td_handle", "J" );
739 jni.ensure_no_exception();
740 assert( 0 != m_field_JNI_proxy_m_td_handle );
741 // field JNI_proxy.m_type
742 m_field_JNI_proxy_m_type = jni->GetFieldID(
743 static_cast<jclass>(jo_JNI_proxy.get()), "m_type", "Lcom/sun/star/uno/Type;" );
744 jni.ensure_no_exception();
745 assert( 0 != m_field_JNI_proxy_m_type );
746 // field JNI_proxy.m_oid
747 m_field_JNI_proxy_m_oid = jni->GetFieldID(
748 static_cast<jclass>(jo_JNI_proxy.get()), "m_oid", "Ljava/lang/String;" );
749 jni.ensure_no_exception();
750 assert( 0 != m_field_JNI_proxy_m_oid );
752 // ctor AsynchronousFinalizer
753 m_ctor_AsynchronousFinalizer = jni->GetMethodID(
754 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "<init>", "()V" );
755 jni.ensure_no_exception();
756 assert( 0 != m_ctor_AsynchronousFinalizer );
757 // method AsynchronousFinalizer.drain()
758 m_method_AsynchronousFinalizer_drain = jni->GetMethodID(
759 static_cast<jclass>(jo_AsynchronousFinalizer.get()), "drain", "()V" );
760 jni.ensure_no_exception();
761 assert( 0 != m_method_AsynchronousFinalizer_drain );
763 // get java env
764 OUString java_env_type_name( UNO_LB_JAVA );
765 JLocalAutoRef jo_java(
766 jni, ustring_to_jstring( jni, java_env_type_name.pData ) );
767 jvalue args[ 2 ];
768 args[ 0 ].l = jo_java.get();
769 args[ 1 ].l = 0;
770 jmethodID method_getEnvironment = jni->GetStaticMethodID(
771 static_cast<jclass>(jo_UnoRuntime.get()), "getEnvironment",
772 "(Ljava/lang/String;Ljava/lang/Object;)"
773 "Lcom/sun/star/uno/IEnvironment;" );
774 jni.ensure_no_exception();
775 assert( 0 != method_getEnvironment );
776 JLocalAutoRef jo_java_env(
777 jni, jni->CallStaticObjectMethodA(
778 static_cast<jclass>(jo_UnoRuntime.get()), method_getEnvironment, args ) );
780 // get com.sun.star.uno.Any.VOID
781 jfieldID field_Any_VOID = jni->GetStaticFieldID(
782 static_cast<jclass>(jo_Any.get()), "VOID", "Lcom/sun/star/uno/Any;" );
783 jni.ensure_no_exception();
784 assert( 0 != field_Any_VOID );
785 JLocalAutoRef jo_Any_VOID(
786 jni, jni->GetStaticObjectField(
787 static_cast<jclass>(jo_Any.get()), field_Any_VOID ) );
788 // get com.sun.star.uno.Type.UNSIGNED_SHORT
789 jfieldID field_Type_UNSIGNED_SHORT = jni->GetStaticFieldID(
790 static_cast<jclass>(jo_Type.get()), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
791 jni.ensure_no_exception();
792 assert( 0 != field_Type_UNSIGNED_SHORT );
793 JLocalAutoRef jo_Type_UNSIGNED_SHORT(
794 jni, jni->GetStaticObjectField(
795 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_SHORT ) );
796 // get com.sun.star.uno.Type.UNSIGNED_LONG
797 jfieldID field_Type_UNSIGNED_LONG = jni->GetStaticFieldID(
798 static_cast<jclass>(jo_Type.get()), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
799 jni.ensure_no_exception();
800 assert( 0 != field_Type_UNSIGNED_LONG );
801 JLocalAutoRef jo_Type_UNSIGNED_LONG(
802 jni, jni->GetStaticObjectField(
803 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_LONG ) );
804 // get com.sun.star.uno.Type.UNSIGNED_HYPER
805 jfieldID field_Type_UNSIGNED_HYPER = jni->GetStaticFieldID(
806 static_cast<jclass>(jo_Type.get()), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
807 jni.ensure_no_exception();
808 assert( 0 != field_Type_UNSIGNED_HYPER );
809 JLocalAutoRef jo_Type_UNSIGNED_HYPER(
810 jni, jni->GetStaticObjectField(
811 static_cast<jclass>(jo_Type.get()), field_Type_UNSIGNED_HYPER ) );
813 // make global refs
814 m_class_UnoRuntime =
815 static_cast<jclass>(jni->NewGlobalRef( jo_UnoRuntime.get() ));
816 m_class_RuntimeException =
817 static_cast<jclass>(jni->NewGlobalRef( jo_RuntimeException.get() ));
818 m_class_Any =
819 static_cast<jclass>(jni->NewGlobalRef( jo_Any.get() ));
820 m_class_Type =
821 static_cast<jclass>(jni->NewGlobalRef( jo_Type.get() ));
822 m_class_TypeClass =
823 static_cast<jclass>(jni->NewGlobalRef( jo_TypeClass.get() ));
824 m_class_JNI_proxy =
825 static_cast<jclass>(jni->NewGlobalRef( jo_JNI_proxy.get() ));
826 m_class_AsynchronousFinalizer =
827 static_cast<jclass>(jni->NewGlobalRef( jo_AsynchronousFinalizer.get() ));
829 m_class_Character =
830 static_cast<jclass>(jni->NewGlobalRef( jo_Character.get() ));
831 m_class_Boolean =
832 static_cast<jclass>(jni->NewGlobalRef( jo_Boolean.get() ));
833 m_class_Byte =
834 static_cast<jclass>(jni->NewGlobalRef( jo_Byte.get() ));
835 m_class_Short =
836 static_cast<jclass>(jni->NewGlobalRef( jo_Short.get() ));
837 m_class_Integer =
838 static_cast<jclass>(jni->NewGlobalRef( jo_Integer.get() ));
839 m_class_Long =
840 static_cast<jclass>(jni->NewGlobalRef( jo_Long.get() ));
841 m_class_Float =
842 static_cast<jclass>(jni->NewGlobalRef( jo_Float.get() ));
843 m_class_Double =
844 static_cast<jclass>(jni->NewGlobalRef( jo_Double.get() ));
845 m_class_String =
846 static_cast<jclass>(jni->NewGlobalRef( jo_String.get() ));
847 m_class_Object =
848 static_cast<jclass>(jni->NewGlobalRef( jo_Object.get() ));
849 m_class_Class =
850 static_cast<jclass>(jni->NewGlobalRef( m_class_Class ));
852 m_object_Any_VOID =
853 jni->NewGlobalRef( jo_Any_VOID.get() );
854 m_object_Type_UNSIGNED_SHORT =
855 jni->NewGlobalRef( jo_Type_UNSIGNED_SHORT.get() );
856 m_object_Type_UNSIGNED_LONG =
857 jni->NewGlobalRef( jo_Type_UNSIGNED_LONG.get() );
858 m_object_Type_UNSIGNED_HYPER =
859 jni->NewGlobalRef( jo_Type_UNSIGNED_HYPER.get() );
860 m_object_java_env = jni->NewGlobalRef( jo_java_env.get() );
864 css::uno::TypeDescription XInterface_td(
865 cppu::UnoType<css::uno::XInterface>::get());
866 // coverity [ctor_dtor_leak]
867 m_XInterface_type_info =
868 new JNI_interface_type_info( jni, XInterface_td.get() );
870 catch (...)
872 destruct( jni_env );
873 throw;
878 void JNI_info::destruct( JNIEnv * jni_env )
880 t_str2type::const_iterator iPos( m_type_map.begin() );
881 t_str2type::const_iterator const iEnd( m_type_map.begin() );
882 for ( ; iPos != iEnd; ++iPos )
884 iPos->second.m_info->destroy( jni_env );
886 if (0 != m_XInterface_type_info)
888 const_cast< JNI_interface_type_info * >(
889 m_XInterface_type_info )->destroy( jni_env );
892 // free global refs
893 jni_env->DeleteGlobalRef( m_object_java_env );
894 jni_env->DeleteGlobalRef( m_object_Any_VOID );
895 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT );
896 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG );
897 jni_env->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER );
899 jni_env->DeleteGlobalRef( m_class_Class );
900 jni_env->DeleteGlobalRef( m_class_Object );
901 jni_env->DeleteGlobalRef( m_class_String );
902 jni_env->DeleteGlobalRef( m_class_Double );
903 jni_env->DeleteGlobalRef( m_class_Float );
904 jni_env->DeleteGlobalRef( m_class_Long );
905 jni_env->DeleteGlobalRef( m_class_Integer );
906 jni_env->DeleteGlobalRef( m_class_Short );
907 jni_env->DeleteGlobalRef( m_class_Byte );
908 jni_env->DeleteGlobalRef( m_class_Boolean );
909 jni_env->DeleteGlobalRef( m_class_Character );
911 jni_env->DeleteGlobalRef( m_class_AsynchronousFinalizer );
912 jni_env->DeleteGlobalRef( m_class_JNI_proxy );
913 jni_env->DeleteGlobalRef( m_class_RuntimeException );
914 jni_env->DeleteGlobalRef( m_class_UnoRuntime );
915 jni_env->DeleteGlobalRef( m_class_TypeClass );
916 jni_env->DeleteGlobalRef( m_class_Type );
917 jni_env->DeleteGlobalRef( m_class_Any );
921 JNI_info const * JNI_info::get_jni_info(
922 rtl::Reference< jvmaccess::UnoVirtualMachine > const & uno_vm )
924 // !!!no JNI_info available at JNI_context!!!
925 ::jvmaccess::VirtualMachine::AttachGuard guard(
926 uno_vm->getVirtualMachine() );
927 JNIEnv * jni_env = guard.getEnvironment();
928 JNI_context jni(
929 0, jni_env, static_cast< jobject >(uno_vm->getClassLoader()) );
931 jclass jo_class;
932 jmethodID jo_forName;
933 jni.getClassForName( &jo_class, &jo_forName );
934 jni.ensure_no_exception();
935 JLocalAutoRef jo_JNI_info_holder(
936 jni,
937 jni.findClass(
938 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class,
939 jo_forName, false ) );
940 // field JNI_info_holder.m_jni_info_handle
941 jfieldID field_s_jni_info_handle =
942 jni->GetStaticFieldID(
943 static_cast<jclass>(jo_JNI_info_holder.get()), "s_jni_info_handle", "J" );
944 jni.ensure_no_exception();
945 assert( 0 != field_s_jni_info_handle );
947 JNI_info const * jni_info =
948 reinterpret_cast< JNI_info const * >(
949 jni->GetStaticLongField(
950 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle ) );
951 if (0 == jni_info) // un-initialized?
953 JNI_info * new_info = new JNI_info(
954 jni_env, static_cast< jobject >(uno_vm->getClassLoader()), jo_class,
955 jo_forName );
957 ClearableMutexGuard g( Mutex::getGlobalMutex() );
958 jni_info =
959 reinterpret_cast< JNI_info const * >(
960 jni->GetStaticLongField(
961 static_cast<jclass>(jo_JNI_info_holder.get()),
962 field_s_jni_info_handle ) );
963 if (0 == jni_info) // still un-initialized?
965 jni->SetStaticLongField(
966 static_cast<jclass>(jo_JNI_info_holder.get()), field_s_jni_info_handle,
967 reinterpret_cast< jlong >( new_info ) );
968 jni_info = new_info;
970 else
972 g.clear();
973 new_info->destroy( jni_env );
977 return jni_info;
982 extern "C"
986 SAL_JNI_EXPORT void
987 JNICALL Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
988 JNIEnv * jni_env, SAL_UNUSED_PARAMETER jobject, jlong jni_info_handle )
989 SAL_THROW_EXTERN_C()
991 ::jni_uno::JNI_info * jni_info =
992 reinterpret_cast< ::jni_uno::JNI_info * >( jni_info_handle );
993 jni_info->destroy( jni_env );
998 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */