1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jni_info.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
33 #include "jni_bridge.h"
35 #include "com/sun/star/uno/RuntimeException.hpp"
37 #include "jvmaccess/unovirtualmachine.hxx"
38 #include "rtl/string.hxx"
39 #include "rtl/strbuf.hxx"
40 #include "rtl/ustrbuf.hxx"
42 #include "uno/lbnames.h"
45 namespace css
= ::com::sun::star
;
46 using namespace ::std
;
47 using namespace ::osl
;
48 using namespace ::rtl
;
53 //______________________________________________________________________________
54 JNI_type_info::JNI_type_info(
55 JNI_context
const & jni
, typelib_TypeDescription
* td
)
60 if (! m_td
.get()->bComplete
)
62 OUStringBuffer
buf( 128 );
64 RTL_CONSTASCII_STRINGPARAM("cannot make type complete: ") );
65 buf
.append( OUString::unacquired( &m_td
.get()->pTypeName
) );
66 buf
.append( jni
.get_stack_trace() );
67 throw BridgeRuntimeError( buf
.makeStringAndClear() );
72 //______________________________________________________________________________
73 void JNI_interface_type_info::destroy( JNIEnv
* jni_env
)
75 JNI_type_info::destruct( jni_env
);
76 jni_env
->DeleteGlobalRef( m_proxy_ctor
);
77 jni_env
->DeleteGlobalRef( m_type
);
82 //______________________________________________________________________________
83 JNI_interface_type_info::JNI_interface_type_info(
84 JNI_context
const & jni
, typelib_TypeDescription
* td_
)
85 : JNI_type_info( jni
, td_
)
87 OSL_ASSERT( typelib_TypeClass_INTERFACE
== m_td
.get()->eTypeClass
);
89 OUString
const & uno_name
= OUString::unacquired( &m_td
.get()->pTypeName
);
90 JNI_info
const * jni_info
= jni
.get_info();
92 JLocalAutoRef
jo_class(
96 ( OUStringToOString( uno_name
, RTL_TEXTENCODING_JAVA_UTF8
).
98 JLocalAutoRef
jo_type( jni
, create_type( jni
, (jclass
) jo_class
.get() ) );
102 arg
.l
= jo_class
.get();
103 JLocalAutoRef
jo_proxy_ctor(
104 jni
, jni
->CallStaticObjectMethodA(
105 jni_info
->m_class_JNI_proxy
,
106 jni_info
->m_method_JNI_proxy_get_proxy_ctor
, &arg
) );
108 if (is_XInterface( m_td
.get()->pWeakRef
))
110 m_methods
= 0; // no methods
114 // retrieve method ids for all direct members
117 typelib_InterfaceTypeDescription
* td
=
118 reinterpret_cast< typelib_InterfaceTypeDescription
* >(
120 m_methods
= new jmethodID
[ td
->nMapFunctionIndexToMemberIndex
];
121 sal_Int32 nMethodIndex
= 0;
122 typelib_TypeDescriptionReference
** ppMembers
= td
->ppMembers
;
123 sal_Int32 nMembers
= td
->nMembers
;
125 for ( sal_Int32 nPos
= 0; nPos
< nMembers
; ++nPos
)
127 TypeDescr
member_td( ppMembers
[ nPos
] );
129 OStringBuffer
sig_buf( 64 );
131 if (typelib_TypeClass_INTERFACE_METHOD
==
132 member_td
.get()->eTypeClass
) // method
134 typelib_InterfaceMethodTypeDescription
* method_td
=
136 typelib_InterfaceMethodTypeDescription
* >(
139 sig_buf
.append( '(' );
140 for ( sal_Int32 i
= 0; i
< method_td
->nParams
; ++i
)
142 typelib_MethodParameter
const & param
=
143 method_td
->pParams
[ i
];
145 sig_buf
.append( '[' );
146 JNI_info::append_sig( &sig_buf
, param
.pTypeRef
);
148 sig_buf
.append( ')' );
149 JNI_info::append_sig( &sig_buf
, method_td
->pReturnTypeRef
);
151 OString
method_signature( sig_buf
.makeStringAndClear() );
153 OUStringToOString( OUString::unacquired(
154 &method_td
->aBase
.pMemberName
),
155 RTL_TEXTENCODING_JAVA_UTF8
) );
157 m_methods
[ nMethodIndex
] = jni
->GetMethodID(
158 (jclass
) jo_class
.get(), method_name
.getStr(),
159 method_signature
.getStr() );
160 jni
.ensure_no_exception();
161 OSL_ASSERT( 0 != m_methods
[ nMethodIndex
] );
167 typelib_TypeClass_INTERFACE_ATTRIBUTE
==
168 member_td
.get()->eTypeClass
);
169 typelib_InterfaceAttributeTypeDescription
* attribute_td
=
171 typelib_InterfaceAttributeTypeDescription
* >(
175 JNI_info::append_sig(
176 &sig_buf
, attribute_td
->pAttributeTypeRef
);
177 OString
type_sig( sig_buf
.makeStringAndClear() );
178 sig_buf
.ensureCapacity( 64 );
180 OUString
const & member_name
=
181 OUString::unacquired(
182 &attribute_td
->aBase
.pMemberName
);
185 sig_buf
.append( RTL_CONSTASCII_STRINGPARAM("()") );
186 sig_buf
.append( type_sig
);
187 OString
method_signature( sig_buf
.makeStringAndClear() );
188 OUStringBuffer
name_buf( 3 + member_name
.getLength() );
189 name_buf
.appendAscii( RTL_CONSTASCII_STRINGPARAM("get") );
190 name_buf
.append( member_name
);
193 name_buf
.makeStringAndClear(),
194 RTL_TEXTENCODING_JAVA_UTF8
) );
195 m_methods
[ nMethodIndex
] = jni
->GetMethodID(
196 (jclass
) jo_class
.get(), method_name
.getStr(),
197 method_signature
.getStr() );
198 jni
.ensure_no_exception();
199 OSL_ASSERT( 0 != m_methods
[ nMethodIndex
] );
201 if (! attribute_td
->bReadOnly
)
204 sig_buf
.ensureCapacity( 64 );
205 sig_buf
.append( '(' );
206 sig_buf
.append( type_sig
);
207 sig_buf
.append( RTL_CONSTASCII_STRINGPARAM(")V") );
208 method_signature
= sig_buf
.makeStringAndClear();
209 name_buf
.ensureCapacity( 3 + member_name
.getLength() );
210 name_buf
.appendAscii(
211 RTL_CONSTASCII_STRINGPARAM("set") );
212 name_buf
.append( member_name
);
213 method_name
= OUStringToOString(
214 name_buf
.makeStringAndClear(),
215 RTL_TEXTENCODING_JAVA_UTF8
);
216 m_methods
[ nMethodIndex
] = jni
->GetMethodID(
217 (jclass
) jo_class
.get(), method_name
.getStr(),
218 method_signature
.getStr() );
219 jni
.ensure_no_exception();
220 OSL_ASSERT( 0 != m_methods
[ nMethodIndex
] );
232 m_class
= (jclass
) jni
->NewGlobalRef( jo_class
.get() );
233 m_type
= jni
->NewGlobalRef( jo_type
.get() );
234 m_proxy_ctor
= jni
->NewGlobalRef( jo_proxy_ctor
.get() );
238 //______________________________________________________________________________
239 void JNI_compound_type_info::destroy( JNIEnv
* jni_env
)
241 JNI_type_info::destruct( jni_env
);
246 //______________________________________________________________________________
247 JNI_compound_type_info::JNI_compound_type_info(
248 JNI_context
const & jni
, typelib_TypeDescription
* td_
)
249 : JNI_type_info( jni
, td_
),
253 OSL_ASSERT( typelib_TypeClass_STRUCT
== m_td
.get()->eTypeClass
||
254 typelib_TypeClass_EXCEPTION
== m_td
.get()->eTypeClass
);
255 typelib_CompoundTypeDescription
* td
=
256 reinterpret_cast< typelib_CompoundTypeDescription
* >( m_td
.get() );
258 OUString
const & uno_name
=
259 OUString::unacquired( &((typelib_TypeDescription
*)td
)->pTypeName
);
261 // Erase type arguments of instantiated polymorphic struct types:
263 sal_Int32 i
= uno_name
.indexOf( '<' );
267 nucleus
= uno_name
.copy( 0, i
);
269 JLocalAutoRef
jo_class(
274 nucleus
, RTL_TEXTENCODING_JAVA_UTF8
).getStr() ) );
276 JNI_info
const * jni_info
= jni
.get_info();
278 if (typelib_TypeClass_EXCEPTION
== m_td
.get()->eTypeClass
)
280 // retrieve exc ctor( msg )
281 m_exc_ctor
= jni
->GetMethodID(
282 (jclass
) jo_class
.get(), "<init>", "(Ljava/lang/String;)V" );
283 jni
.ensure_no_exception();
284 OSL_ASSERT( 0 != m_exc_ctor
);
287 // retrieve info for base type
288 typelib_TypeDescription
* base_td
=
289 reinterpret_cast< typelib_TypeDescription
* >(
290 td
->pBaseTypeDescription
);
291 m_base
= (0 == base_td
? 0 : jni_info
->get_type_info( jni
, base_td
));
296 ((typelib_TypeDescription
*)td
)->pWeakRef
,
297 jni_info
->m_Exception_type
.getTypeLibType() ) ||
299 ((typelib_TypeDescription
*)td
)->pWeakRef
,
300 jni_info
->m_RuntimeException_type
.getTypeLibType() ))
302 m_fields
= new jfieldID
[ 2 ];
303 m_fields
[ 0 ] = 0; // special Throwable.getMessage()
305 m_fields
[ 1 ] = jni
->GetFieldID(
306 (jclass
) jo_class
.get(), "Context", "Ljava/lang/Object;" );
307 jni
.ensure_no_exception();
308 OSL_ASSERT( 0 != m_fields
[ 1 ] );
312 // retrieve field ids for all direct members
313 sal_Int32 nMembers
= td
->nMembers
;
314 m_fields
= new jfieldID
[ nMembers
];
316 for ( sal_Int32 nPos
= 0; nPos
< nMembers
; ++nPos
)
319 if (td
->aBase
.eTypeClass
== typelib_TypeClass_STRUCT
320 && reinterpret_cast< typelib_StructTypeDescription
* >(
321 td
)->pParameterizedTypes
!= 0
322 && reinterpret_cast< typelib_StructTypeDescription
* >(
323 td
)->pParameterizedTypes
[nPos
])
326 RTL_CONSTASCII_STRINGPARAM("Ljava/lang/Object;"));
328 OStringBuffer
sig_buf( 32 );
329 JNI_info::append_sig( &sig_buf
, td
->ppTypeRefs
[ nPos
] );
330 sig
= sig_buf
.makeStringAndClear();
335 OUString::unacquired( &td
->ppMemberNames
[ nPos
] ),
336 RTL_TEXTENCODING_JAVA_UTF8
) );
338 m_fields
[ nPos
] = jni
->GetFieldID(
339 (jclass
) jo_class
.get(), member_name
.getStr(),
341 jni
.ensure_no_exception();
342 OSL_ASSERT( 0 != m_fields
[ nPos
] );
352 m_class
= (jclass
) jni
->NewGlobalRef( jo_class
.get() );
356 //______________________________________________________________________________
357 JNI_type_info
const * JNI_info::create_type_info(
358 JNI_context
const & jni
, typelib_TypeDescription
* td
) const
360 OUString
const & uno_name
= OUString::unacquired( &td
->pTypeName
);
362 JNI_type_info
* new_info
;
363 switch (td
->eTypeClass
)
365 case typelib_TypeClass_STRUCT
:
366 case typelib_TypeClass_EXCEPTION
:
368 new_info
= new JNI_compound_type_info( jni
, td
);
371 case typelib_TypeClass_INTERFACE
:
373 new_info
= new JNI_interface_type_info( jni
, td
);
378 OUStringBuffer
buf( 128 );
380 RTL_CONSTASCII_STRINGPARAM("type info not supported for ") );
381 buf
.append( uno_name
);
382 buf
.append( jni
.get_stack_trace() );
383 throw BridgeRuntimeError( buf
.makeStringAndClear() );
388 JNI_type_info
* info
;
389 ClearableMutexGuard
guard( m_mutex
);
390 JNI_type_info_holder
& holder
= m_type_map
[ uno_name
];
391 if (0 == holder
.m_info
) // new insertion
393 holder
.m_info
= new_info
;
397 else // inserted in the meantime
399 info
= holder
.m_info
;
401 new_info
->destroy( jni
.get_jni_env() );
406 //______________________________________________________________________________
407 JNI_type_info
const * JNI_info::get_type_info(
408 JNI_context
const & jni
, typelib_TypeDescription
* td
) const
410 if (is_XInterface( td
->pWeakRef
))
412 return m_XInterface_type_info
;
415 OUString
const & uno_name
= OUString::unacquired( &td
->pTypeName
);
416 JNI_type_info
const * info
;
417 ClearableMutexGuard
guard( m_mutex
);
419 t_str2type::const_iterator
iFind( m_type_map
.find( uno_name
) );
420 if (iFind
== m_type_map
.end())
423 info
= create_type_info( jni
, td
);
427 info
= iFind
->second
.m_info
;
433 //______________________________________________________________________________
434 JNI_type_info
const * JNI_info::get_type_info(
435 JNI_context
const & jni
, typelib_TypeDescriptionReference
* type
) const
437 if (is_XInterface( type
))
439 return m_XInterface_type_info
;
442 OUString
const & uno_name
= OUString::unacquired( &type
->pTypeName
);
443 JNI_type_info
const * info
;
444 ClearableMutexGuard
guard( m_mutex
);
445 t_str2type::const_iterator
iFind( m_type_map
.find( uno_name
) );
446 if (iFind
== m_type_map
.end())
449 TypeDescr
td( type
);
450 info
= create_type_info( jni
, td
.get() );
454 info
= iFind
->second
.m_info
;
460 //______________________________________________________________________________
461 JNI_type_info
const * JNI_info::get_type_info(
462 JNI_context
const & jni
, OUString
const & uno_name
) const
464 if (uno_name
.equalsAsciiL(
465 RTL_CONSTASCII_STRINGPARAM("com.sun.star.uno.XInterface") ))
467 return m_XInterface_type_info
;
470 JNI_type_info
const * info
;
471 ClearableMutexGuard
guard( m_mutex
);
472 t_str2type::const_iterator
iFind( m_type_map
.find( uno_name
) );
473 if (iFind
== m_type_map
.end())
476 css::uno::TypeDescription
td( uno_name
);
479 OUStringBuffer
buf( 128 );
481 RTL_CONSTASCII_STRINGPARAM("UNO type not found: ") );
482 buf
.append( uno_name
);
483 buf
.append( jni
.get_stack_trace() );
484 throw BridgeRuntimeError( buf
.makeStringAndClear() );
486 info
= create_type_info( jni
, td
.get() );
490 info
= iFind
->second
.m_info
;
496 //______________________________________________________________________________
498 JNIEnv
* jni_env
, jobject class_loader
, jclass classClass
,
499 jmethodID methodForName
)
500 : m_class_Class( classClass
),
501 m_method_Class_forName( methodForName
),
502 m_class_JNI_proxy( 0 ),
503 m_XInterface_queryInterface_td(
504 (reinterpret_cast< typelib_InterfaceTypeDescription
* >(
505 css::uno::TypeDescription(
507 (css::uno::Reference
< css::uno::XInterface
> const *)0 ) )
508 .get())->ppMembers
[ 0 ] ) ),
509 m_Exception_type( ::getCppuType( (css::uno::Exception
const *)0 ) ),
510 m_RuntimeException_type(
511 ::getCppuType( (css::uno::RuntimeException
const *)0 ) ),
512 m_void_type( ::getCppuVoidType() ),
513 m_XInterface_type_info( 0 )
515 JNI_context
jni( this, jni_env
, class_loader
); // !no proper jni_info!
518 JLocalAutoRef
jo_Object(
519 jni
, find_class( jni
, "java.lang.Object" ) );
520 JLocalAutoRef
jo_Class(
521 jni
, find_class( jni
, "java.lang.Class" ) );
522 JLocalAutoRef
jo_Throwable(
523 jni
, find_class( jni
, "java.lang.Throwable" ) );
524 JLocalAutoRef
jo_Character(
525 jni
, find_class( jni
, "java.lang.Character" ) );
526 JLocalAutoRef
jo_Boolean(
527 jni
, find_class( jni
, "java.lang.Boolean" ) );
528 JLocalAutoRef
jo_Byte(
529 jni
, find_class( jni
, "java.lang.Byte" ) );
530 JLocalAutoRef
jo_Short(
531 jni
, find_class( jni
, "java.lang.Short" ) );
532 JLocalAutoRef
jo_Integer(
533 jni
, find_class( jni
, "java.lang.Integer" ) );
534 JLocalAutoRef
jo_Long(
535 jni
, find_class( jni
, "java.lang.Long" ) );
536 JLocalAutoRef
jo_Float(
537 jni
, find_class( jni
, "java.lang.Float" ) );
538 JLocalAutoRef
jo_Double(
539 jni
, find_class( jni
, "java.lang.Double" ) );
540 JLocalAutoRef
jo_String(
541 jni
, find_class( jni
, "java.lang.String" ) );
542 JLocalAutoRef
jo_RuntimeException(
543 jni
, find_class( jni
, "com.sun.star.uno.RuntimeException" ) );
544 JLocalAutoRef
jo_UnoRuntime(
545 jni
, find_class( jni
, "com.sun.star.uno.UnoRuntime" ) );
546 JLocalAutoRef
jo_Any(
547 jni
, find_class( jni
, "com.sun.star.uno.Any" ) );
548 JLocalAutoRef
jo_Enum(
549 jni
, find_class( jni
, "com.sun.star.uno.Enum" ) );
550 JLocalAutoRef
jo_Type(
551 jni
, find_class( jni
, "com.sun.star.uno.Type" ) );
552 JLocalAutoRef
jo_TypeClass(
553 jni
, find_class( jni
, "com.sun.star.uno.TypeClass" ) );
554 JLocalAutoRef
jo_IEnvironment(
555 jni
, find_class( jni
, "com.sun.star.uno.IEnvironment" ) );
556 JLocalAutoRef
jo_JNI_proxy(
557 jni
, find_class( jni
, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
559 // method Object.toString()
560 m_method_Object_toString
= jni
->GetMethodID(
561 (jclass
) jo_Object
.get(), "toString", "()Ljava/lang/String;" );
562 jni
.ensure_no_exception();
563 OSL_ASSERT( 0 != m_method_Object_toString
);
564 // method Class.getName()
565 m_method_Class_getName
= jni
->GetMethodID(
566 (jclass
) jo_Class
.get(), "getName", "()Ljava/lang/String;" );
567 jni
.ensure_no_exception();
568 OSL_ASSERT( 0 != m_method_Class_getName
);
570 // method Throwable.getMessage()
571 m_method_Throwable_getMessage
= jni
->GetMethodID(
572 (jclass
) jo_Throwable
.get(), "getMessage", "()Ljava/lang/String;" );
573 jni
.ensure_no_exception();
574 OSL_ASSERT( 0 != m_method_Throwable_getMessage
);
576 // method Character.charValue()
577 m_method_Character_charValue
= jni
->GetMethodID(
578 (jclass
) jo_Character
.get(), "charValue", "()C" );
579 jni
.ensure_no_exception();
580 OSL_ASSERT( 0 != m_method_Character_charValue
);
581 // method Boolean.booleanValue()
582 m_method_Boolean_booleanValue
= jni
->GetMethodID(
583 (jclass
) jo_Boolean
.get(), "booleanValue", "()Z" );
584 jni
.ensure_no_exception();
585 OSL_ASSERT( 0 != m_method_Boolean_booleanValue
);
586 // method Byte.byteValue()
587 m_method_Byte_byteValue
= jni
->GetMethodID(
588 (jclass
) jo_Byte
.get(), "byteValue", "()B" );
589 jni
.ensure_no_exception();
590 OSL_ASSERT( 0 != m_method_Byte_byteValue
);
591 // method Short.shortValue()
592 m_method_Short_shortValue
= jni
->GetMethodID(
593 (jclass
) jo_Short
.get(), "shortValue", "()S" );
594 jni
.ensure_no_exception();
595 OSL_ASSERT( 0 != m_method_Short_shortValue
);
596 // method Integer.intValue()
597 m_method_Integer_intValue
= jni
->GetMethodID(
598 (jclass
) jo_Integer
.get(), "intValue", "()I" );
599 jni
.ensure_no_exception();
600 OSL_ASSERT( 0 != m_method_Integer_intValue
);
601 // method Long.longValue()
602 m_method_Long_longValue
= jni
->GetMethodID(
603 (jclass
) jo_Long
.get(), "longValue", "()J" );
604 jni
.ensure_no_exception();
605 OSL_ASSERT( 0 != m_method_Long_longValue
);
606 // method Float.floatValue()
607 m_method_Float_floatValue
= jni
->GetMethodID(
608 (jclass
) jo_Float
.get(), "floatValue", "()F" );
609 jni
.ensure_no_exception();
610 OSL_ASSERT( 0 != m_method_Float_floatValue
);
611 // method Double.doubleValue()
612 m_method_Double_doubleValue
= jni
->GetMethodID(
613 (jclass
) jo_Double
.get(), "doubleValue", "()D" );
614 jni
.ensure_no_exception();
615 OSL_ASSERT( 0 != m_method_Double_doubleValue
);
617 // ctor Character( char )
618 m_ctor_Character_with_char
= jni
->GetMethodID(
619 (jclass
) jo_Character
.get(), "<init>", "(C)V" );
620 jni
.ensure_no_exception();
621 OSL_ASSERT( 0 != m_ctor_Character_with_char
);
622 // ctor Boolean( boolean )
623 m_ctor_Boolean_with_boolean
= jni
->GetMethodID(
624 (jclass
) jo_Boolean
.get(), "<init>", "(Z)V" );
625 jni
.ensure_no_exception();
626 OSL_ASSERT( 0 != m_ctor_Boolean_with_boolean
);
628 m_ctor_Byte_with_byte
= jni
->GetMethodID(
629 (jclass
) jo_Byte
.get(), "<init>", "(B)V" );
630 jni
.ensure_no_exception();
631 OSL_ASSERT( 0 != m_ctor_Byte_with_byte
);
632 // ctor Short( short )
633 m_ctor_Short_with_short
= jni
->GetMethodID(
634 (jclass
) jo_Short
.get(), "<init>", "(S)V" );
635 jni
.ensure_no_exception();
636 OSL_ASSERT( 0 != m_ctor_Short_with_short
);
637 // ctor Integer( int )
638 m_ctor_Integer_with_int
= jni
->GetMethodID(
639 (jclass
) jo_Integer
.get(), "<init>", "(I)V" );
640 jni
.ensure_no_exception();
641 OSL_ASSERT( 0 != m_ctor_Integer_with_int
);
643 m_ctor_Long_with_long
= jni
->GetMethodID(
644 (jclass
) jo_Long
.get(), "<init>", "(J)V" );
645 jni
.ensure_no_exception();
646 OSL_ASSERT( 0 != m_ctor_Long_with_long
);
647 // ctor Float( float )
648 m_ctor_Float_with_float
= jni
->GetMethodID(
649 (jclass
) jo_Float
.get(), "<init>", "(F)V" );
650 jni
.ensure_no_exception();
651 OSL_ASSERT( 0 != m_ctor_Float_with_float
);
652 // ctor Double( double )
653 m_ctor_Double_with_double
= jni
->GetMethodID(
654 (jclass
) jo_Double
.get(), "<init>", "(D)V" );
655 jni
.ensure_no_exception();
656 OSL_ASSERT( 0 != m_ctor_Double_with_double
);
658 // static method UnoRuntime.generateOid()
659 m_method_UnoRuntime_generateOid
= jni
->GetStaticMethodID(
660 (jclass
) jo_UnoRuntime
.get(),
661 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
662 jni
.ensure_no_exception();
663 OSL_ASSERT( 0 != m_method_UnoRuntime_generateOid
);
664 // static method UnoRuntime.queryInterface()
665 m_method_UnoRuntime_queryInterface
= jni
->GetStaticMethodID(
666 (jclass
) jo_UnoRuntime
.get(),
668 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
669 jni
.ensure_no_exception();
670 OSL_ASSERT( 0 != m_method_UnoRuntime_queryInterface
);
672 // field Enum.m_value
673 m_field_Enum_m_value
= jni
->GetFieldID(
674 (jclass
) jo_Enum
.get(), "m_value", "I" );
675 jni
.ensure_no_exception();
676 OSL_ASSERT( 0 != m_field_Enum_m_value
);
678 // static method TypeClass.fromInt()
679 m_method_TypeClass_fromInt
= jni
->GetStaticMethodID(
680 (jclass
) jo_TypeClass
.get(),
681 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
682 jni
.ensure_no_exception();
683 OSL_ASSERT( 0 != m_method_TypeClass_fromInt
);
685 // ctor Type( Class )
686 m_ctor_Type_with_Class
= jni
->GetMethodID(
687 (jclass
) jo_Type
.get(), "<init>", "(Ljava/lang/Class;)V" );
688 jni
.ensure_no_exception();
689 OSL_ASSERT( 0 != m_ctor_Type_with_Class
);
690 // ctor Type( String, TypeClass )
691 m_ctor_Type_with_Name_TypeClass
= jni
->GetMethodID(
692 (jclass
) jo_Type
.get(),
693 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
694 jni
.ensure_no_exception();
695 OSL_ASSERT( 0 != m_ctor_Type_with_Name_TypeClass
);
696 // field Type._typeName
697 m_field_Type__typeName
= jni
->GetFieldID(
698 (jclass
) jo_Type
.get(), "_typeName", "Ljava/lang/String;" );
699 jni
.ensure_no_exception();
700 OSL_ASSERT( 0 != m_field_Type__typeName
);
702 // ctor Any( Type, Object )
703 m_ctor_Any_with_Type_Object
= jni
->GetMethodID(
704 (jclass
) jo_Any
.get(),
705 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
706 jni
.ensure_no_exception();
707 OSL_ASSERT( 0 != m_ctor_Any_with_Type_Object
);
710 m_field_Any__type
= jni
->GetFieldID(
711 (jclass
) jo_Any
.get(), "_type", "Lcom/sun/star/uno/Type;" );
712 jni
.ensure_no_exception();
713 OSL_ASSERT( 0 != m_field_Any__type
);
715 m_field_Any__object
= jni
->GetFieldID(
716 (jclass
) jo_Any
.get(), "_object", "Ljava/lang/Object;" );
717 jni
.ensure_no_exception();
718 OSL_ASSERT( 0 != m_field_Any__object
);
720 // method IEnvironment.getRegisteredInterface()
721 m_method_IEnvironment_getRegisteredInterface
= jni
->GetMethodID(
722 (jclass
) jo_IEnvironment
.get(),
723 "getRegisteredInterface",
724 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
725 jni
.ensure_no_exception();
726 OSL_ASSERT( 0 != m_method_IEnvironment_getRegisteredInterface
);
727 // method IEnvironment.registerInterface()
728 m_method_IEnvironment_registerInterface
= jni
->GetMethodID(
729 (jclass
) jo_IEnvironment
.get(), "registerInterface",
730 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
731 "Ljava/lang/Object;" );
732 jni
.ensure_no_exception();
733 OSL_ASSERT( 0 != m_method_IEnvironment_registerInterface
);
735 // static method JNI_proxy.get_proxy_ctor()
736 m_method_JNI_proxy_get_proxy_ctor
= jni
->GetStaticMethodID(
737 (jclass
) jo_JNI_proxy
.get(), "get_proxy_ctor",
738 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
739 jni
.ensure_no_exception();
740 OSL_ASSERT( 0 != m_method_JNI_proxy_get_proxy_ctor
);
741 // static method JNI_proxy.create()
742 m_method_JNI_proxy_create
= jni
->GetStaticMethodID(
743 (jclass
) jo_JNI_proxy
.get(), "create",
744 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
745 "/String;Ljava/lang/reflect/Constructor;)Ljava/lang/Object;" );
746 jni
.ensure_no_exception();
747 OSL_ASSERT( 0 != m_method_JNI_proxy_create
);
748 // field JNI_proxy.m_receiver_handle
749 m_field_JNI_proxy_m_receiver_handle
= jni
->GetFieldID(
750 (jclass
) jo_JNI_proxy
.get(), "m_receiver_handle", "J" );
751 jni
.ensure_no_exception();
752 OSL_ASSERT( 0 != m_field_JNI_proxy_m_receiver_handle
);
753 // field JNI_proxy.m_td_handle
754 m_field_JNI_proxy_m_td_handle
= jni
->GetFieldID(
755 (jclass
) jo_JNI_proxy
.get(), "m_td_handle", "J" );
756 jni
.ensure_no_exception();
757 OSL_ASSERT( 0 != m_field_JNI_proxy_m_td_handle
);
758 // field JNI_proxy.m_type
759 m_field_JNI_proxy_m_type
= jni
->GetFieldID(
760 (jclass
) jo_JNI_proxy
.get(), "m_type", "Lcom/sun/star/uno/Type;" );
761 jni
.ensure_no_exception();
762 OSL_ASSERT( 0 != m_field_JNI_proxy_m_type
);
763 // field JNI_proxy.m_oid
764 m_field_JNI_proxy_m_oid
= jni
->GetFieldID(
765 (jclass
) jo_JNI_proxy
.get(), "m_oid", "Ljava/lang/String;" );
766 jni
.ensure_no_exception();
767 OSL_ASSERT( 0 != m_field_JNI_proxy_m_oid
);
770 OUString
java_env_type_name( RTL_CONSTASCII_USTRINGPARAM(UNO_LB_JAVA
) );
771 JLocalAutoRef
jo_java(
772 jni
, ustring_to_jstring( jni
, java_env_type_name
.pData
) );
774 args
[ 0 ].l
= jo_java
.get();
776 jmethodID method_getEnvironment
= jni
->GetStaticMethodID(
777 (jclass
) jo_UnoRuntime
.get(), "getEnvironment",
778 "(Ljava/lang/String;Ljava/lang/Object;)"
779 "Lcom/sun/star/uno/IEnvironment;" );
780 jni
.ensure_no_exception();
781 OSL_ASSERT( 0 != method_getEnvironment
);
782 JLocalAutoRef
jo_java_env(
783 jni
, jni
->CallStaticObjectMethodA(
784 (jclass
) jo_UnoRuntime
.get(), method_getEnvironment
, args
) );
786 // get com.sun.star.uno.Any.VOID
787 jfieldID field_Any_VOID
= jni
->GetStaticFieldID(
788 (jclass
) jo_Any
.get(), "VOID", "Lcom/sun/star/uno/Any;" );
789 jni
.ensure_no_exception();
790 OSL_ASSERT( 0 != field_Any_VOID
);
791 JLocalAutoRef
jo_Any_VOID(
792 jni
, jni
->GetStaticObjectField(
793 (jclass
) jo_Any
.get(), field_Any_VOID
) );
794 // get com.sun.star.uno.Type.UNSIGNED_SHORT
795 jfieldID field_Type_UNSIGNED_SHORT
= jni
->GetStaticFieldID(
796 (jclass
) jo_Type
.get(), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
797 jni
.ensure_no_exception();
798 OSL_ASSERT( 0 != field_Type_UNSIGNED_SHORT
);
799 JLocalAutoRef
jo_Type_UNSIGNED_SHORT(
800 jni
, jni
->GetStaticObjectField(
801 (jclass
) jo_Type
.get(), field_Type_UNSIGNED_SHORT
) );
802 // get com.sun.star.uno.Type.UNSIGNED_LONG
803 jfieldID field_Type_UNSIGNED_LONG
= jni
->GetStaticFieldID(
804 (jclass
) jo_Type
.get(), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
805 jni
.ensure_no_exception();
806 OSL_ASSERT( 0 != field_Type_UNSIGNED_LONG
);
807 JLocalAutoRef
jo_Type_UNSIGNED_LONG(
808 jni
, jni
->GetStaticObjectField(
809 (jclass
) jo_Type
.get(), field_Type_UNSIGNED_LONG
) );
810 // get com.sun.star.uno.Type.UNSIGNED_HYPER
811 jfieldID field_Type_UNSIGNED_HYPER
= jni
->GetStaticFieldID(
812 (jclass
) jo_Type
.get(), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
813 jni
.ensure_no_exception();
814 OSL_ASSERT( 0 != field_Type_UNSIGNED_HYPER
);
815 JLocalAutoRef
jo_Type_UNSIGNED_HYPER(
816 jni
, jni
->GetStaticObjectField(
817 (jclass
) jo_Type
.get(), field_Type_UNSIGNED_HYPER
) );
821 (jclass
) jni
->NewGlobalRef( jo_UnoRuntime
.get() );
822 m_class_RuntimeException
=
823 (jclass
) jni
->NewGlobalRef( jo_RuntimeException
.get() );
825 (jclass
) jni
->NewGlobalRef( jo_Any
.get() );
827 (jclass
) jni
->NewGlobalRef( jo_Type
.get() );
829 (jclass
) jni
->NewGlobalRef( jo_TypeClass
.get() );
831 (jclass
) jni
->NewGlobalRef( jo_JNI_proxy
.get() );
834 (jclass
) jni
->NewGlobalRef( jo_Character
.get() );
836 (jclass
) jni
->NewGlobalRef( jo_Boolean
.get() );
838 (jclass
) jni
->NewGlobalRef( jo_Byte
.get() );
840 (jclass
) jni
->NewGlobalRef( jo_Short
.get() );
842 (jclass
) jni
->NewGlobalRef( jo_Integer
.get() );
844 (jclass
) jni
->NewGlobalRef( jo_Long
.get() );
846 (jclass
) jni
->NewGlobalRef( jo_Float
.get() );
848 (jclass
) jni
->NewGlobalRef( jo_Double
.get() );
850 (jclass
) jni
->NewGlobalRef( jo_String
.get() );
852 (jclass
) jni
->NewGlobalRef( jo_Object
.get() );
854 (jclass
) jni
->NewGlobalRef( m_class_Class
);
857 jni
->NewGlobalRef( jo_Any_VOID
.get() );
858 m_object_Type_UNSIGNED_SHORT
=
859 jni
->NewGlobalRef( jo_Type_UNSIGNED_SHORT
.get() );
860 m_object_Type_UNSIGNED_LONG
=
861 jni
->NewGlobalRef( jo_Type_UNSIGNED_LONG
.get() );
862 m_object_Type_UNSIGNED_HYPER
=
863 jni
->NewGlobalRef( jo_Type_UNSIGNED_HYPER
.get() );
864 m_object_java_env
= jni
->NewGlobalRef( jo_java_env
.get() );
868 css::uno::TypeDescription
XInterface_td(
870 (css::uno::Reference
< css::uno::XInterface
> const *)0 ) );
871 m_XInterface_type_info
=
872 new JNI_interface_type_info( jni
, XInterface_td
.get() );
881 //______________________________________________________________________________
882 void JNI_info::destruct( JNIEnv
* jni_env
)
884 t_str2type::const_iterator
iPos( m_type_map
.begin() );
885 t_str2type::const_iterator
const iEnd( m_type_map
.begin() );
886 for ( ; iPos
!= iEnd
; ++iPos
)
888 iPos
->second
.m_info
->destroy( jni_env
);
890 if (0 != m_XInterface_type_info
)
892 const_cast< JNI_interface_type_info
* >(
893 m_XInterface_type_info
)->destroy( jni_env
);
897 jni_env
->DeleteGlobalRef( m_object_java_env
);
898 jni_env
->DeleteGlobalRef( m_object_Any_VOID
);
899 jni_env
->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT
);
900 jni_env
->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG
);
901 jni_env
->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER
);
903 jni_env
->DeleteGlobalRef( m_class_Class
);
904 jni_env
->DeleteGlobalRef( m_class_Object
);
905 jni_env
->DeleteGlobalRef( m_class_String
);
906 jni_env
->DeleteGlobalRef( m_class_Double
);
907 jni_env
->DeleteGlobalRef( m_class_Float
);
908 jni_env
->DeleteGlobalRef( m_class_Long
);
909 jni_env
->DeleteGlobalRef( m_class_Integer
);
910 jni_env
->DeleteGlobalRef( m_class_Short
);
911 jni_env
->DeleteGlobalRef( m_class_Byte
);
912 jni_env
->DeleteGlobalRef( m_class_Boolean
);
913 jni_env
->DeleteGlobalRef( m_class_Character
);
915 jni_env
->DeleteGlobalRef( m_class_JNI_proxy
);
916 jni_env
->DeleteGlobalRef( m_class_RuntimeException
);
917 jni_env
->DeleteGlobalRef( m_class_UnoRuntime
);
918 jni_env
->DeleteGlobalRef( m_class_TypeClass
);
919 jni_env
->DeleteGlobalRef( m_class_Type
);
920 jni_env
->DeleteGlobalRef( m_class_Any
);
923 //______________________________________________________________________________
924 JNI_info
const * JNI_info::get_jni_info(
925 rtl::Reference
< jvmaccess::UnoVirtualMachine
> const & uno_vm
)
927 // !!!no JNI_info available at JNI_context!!!
928 ::jvmaccess::VirtualMachine::AttachGuard
guard(
929 uno_vm
->getVirtualMachine() );
930 JNIEnv
* jni_env
= guard
.getEnvironment();
932 0, jni_env
, static_cast< jobject
>(uno_vm
->getClassLoader()) );
935 jmethodID jo_forName
;
936 jni
.getClassForName( &jo_class
, &jo_forName
);
937 jni
.ensure_no_exception();
938 JLocalAutoRef
jo_JNI_info_holder(
941 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class
,
942 jo_forName
, false ) );
943 // field JNI_info_holder.m_jni_info_handle
944 jfieldID field_s_jni_info_handle
=
945 jni
->GetStaticFieldID(
946 (jclass
) jo_JNI_info_holder
.get(), "s_jni_info_handle", "J" );
947 jni
.ensure_no_exception();
948 OSL_ASSERT( 0 != field_s_jni_info_handle
);
950 JNI_info
const * jni_info
=
951 reinterpret_cast< JNI_info
const * >(
952 jni
->GetStaticLongField(
953 (jclass
) jo_JNI_info_holder
.get(), field_s_jni_info_handle
) );
954 if (0 == jni_info
) // un-initialized?
956 JNI_info
* new_info
= new JNI_info(
957 jni_env
, static_cast< jobject
>(uno_vm
->getClassLoader()), jo_class
,
960 ClearableMutexGuard
g( Mutex::getGlobalMutex() );
962 reinterpret_cast< JNI_info
const * >(
963 jni
->GetStaticLongField(
964 (jclass
) jo_JNI_info_holder
.get(),
965 field_s_jni_info_handle
) );
966 if (0 == jni_info
) // still un-initialized?
968 jni
->SetStaticLongField(
969 (jclass
) jo_JNI_info_holder
.get(), field_s_jni_info_handle
,
970 reinterpret_cast< jlong
>( new_info
) );
976 new_info
->destroy( jni_env
);
988 //------------------------------------------------------------------------------
990 JNICALL
Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
991 JNIEnv
* jni_env
, jobject
, jlong jni_info_handle
)
994 ::jni_uno::JNI_info
* jni_info
=
995 reinterpret_cast< ::jni_uno::JNI_info
* >( jni_info_handle
);
996 jni_info
->destroy( jni_env
);