1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include "jni_helper.h"
26 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <jvmaccess/unovirtualmachine.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/strbuf.hxx>
32 #include <uno/lbnames.h>
39 JNI_type_info::JNI_type_info(
40 JNI_context
const & jni
, typelib_TypeDescription
* td
)
45 if (! m_td
.get()->bComplete
)
47 throw BridgeRuntimeError(
48 "cannot make type complete: "
49 + OUString::unacquired( &m_td
.get()->pTypeName
)
50 + jni
.get_stack_trace() );
55 void JNI_interface_type_info::destroy( JNIEnv
* jni_env
)
57 JNI_type_info::destruct( jni_env
);
58 jni_env
->DeleteGlobalRef( m_proxy_ctor
);
59 jni_env
->DeleteGlobalRef( m_type
);
65 JNI_interface_type_info::JNI_interface_type_info(
66 JNI_context
const & jni
, typelib_TypeDescription
* td_
)
67 : JNI_type_info( jni
, td_
)
69 assert( m_td
.get()->eTypeClass
== typelib_TypeClass_INTERFACE
);
71 OUString
const & uno_name
= OUString::unacquired( &m_td
.get()->pTypeName
);
72 JNI_info
const * jni_info
= jni
.get_info();
74 JLocalAutoRef
jo_class(
78 ( OUStringToOString( uno_name
, RTL_TEXTENCODING_JAVA_UTF8
).
80 JLocalAutoRef
jo_type( jni
, create_type( jni
, static_cast<jclass
>(jo_class
.get()) ) );
84 arg
.l
= jo_class
.get();
85 JLocalAutoRef
jo_proxy_ctor(
86 jni
, jni
->CallStaticObjectMethodA(
87 jni_info
->m_class_JNI_proxy
,
88 jni_info
->m_method_JNI_proxy_get_proxy_ctor
, &arg
) );
90 if (is_XInterface( m_td
.get()->pWeakRef
))
92 m_methods
= nullptr; // no methods
96 // retrieve method ids for all direct members
99 typelib_InterfaceTypeDescription
* td
=
100 reinterpret_cast< typelib_InterfaceTypeDescription
* >(
102 // coverity[ctor_dtor_leak] - on purpose
103 m_methods
.reset(new jmethodID
[ td
->nMapFunctionIndexToMemberIndex
]);
104 sal_Int32 nMethodIndex
= 0;
105 typelib_TypeDescriptionReference
** ppMembers
= td
->ppMembers
;
106 sal_Int32 nMembers
= td
->nMembers
;
108 for ( sal_Int32 nPos
= 0; nPos
< nMembers
; ++nPos
)
110 TypeDescr
member_td( ppMembers
[ nPos
] );
112 OStringBuffer
sig_buf( 64 );
114 if (member_td
.get()->eTypeClass
==
115 typelib_TypeClass_INTERFACE_METHOD
) // method
117 typelib_InterfaceMethodTypeDescription
* method_td
=
119 typelib_InterfaceMethodTypeDescription
* >(
122 sig_buf
.append( '(' );
123 for ( sal_Int32 i
= 0; i
< method_td
->nParams
; ++i
)
125 typelib_MethodParameter
const & param
=
126 method_td
->pParams
[ i
];
128 sig_buf
.append( '[' );
129 JNI_info::append_sig( &sig_buf
, param
.pTypeRef
);
131 sig_buf
.append( ')' );
132 JNI_info::append_sig( &sig_buf
, method_td
->pReturnTypeRef
);
134 OString
method_signature( sig_buf
.makeStringAndClear() );
136 OUStringToOString( OUString::unacquired(
137 &method_td
->aBase
.pMemberName
),
138 RTL_TEXTENCODING_JAVA_UTF8
) );
140 m_methods
[ nMethodIndex
] = jni
->GetMethodID(
141 static_cast<jclass
>(jo_class
.get()), method_name
.getStr(),
142 method_signature
.getStr() );
143 jni
.ensure_no_exception();
144 assert( m_methods
[ nMethodIndex
] != nullptr );
150 member_td
.get()->eTypeClass
==
151 typelib_TypeClass_INTERFACE_ATTRIBUTE
);
152 typelib_InterfaceAttributeTypeDescription
* attribute_td
=
154 typelib_InterfaceAttributeTypeDescription
* >(
158 JNI_info::append_sig(
159 &sig_buf
, attribute_td
->pAttributeTypeRef
);
160 OString
type_sig( sig_buf
.makeStringAndClear() );
161 sig_buf
.ensureCapacity( 64 );
163 OUString
const & member_name
=
164 OUString::unacquired(
165 &attribute_td
->aBase
.pMemberName
);
168 sig_buf
.append( "()" + type_sig
);
169 OString
method_signature( sig_buf
.makeStringAndClear() );
172 rtl::Concat2View("get" + member_name
),
173 RTL_TEXTENCODING_JAVA_UTF8
) );
174 m_methods
[ nMethodIndex
] = jni
->GetMethodID(
175 static_cast<jclass
>(jo_class
.get()), method_name
.getStr(),
176 method_signature
.getStr() );
177 jni
.ensure_no_exception();
178 assert( m_methods
[ nMethodIndex
] != nullptr );
180 if (! attribute_td
->bReadOnly
)
183 method_signature
= "(" + type_sig
+ ")V";
184 method_name
= OUStringToOString(
185 rtl::Concat2View("set" + member_name
),
186 RTL_TEXTENCODING_JAVA_UTF8
);
187 m_methods
[ nMethodIndex
] = jni
->GetMethodID(
188 static_cast<jclass
>(jo_class
.get()), method_name
.getStr(),
189 method_signature
.getStr() );
190 jni
.ensure_no_exception();
191 assert( m_methods
[ nMethodIndex
] != nullptr );
203 m_class
= static_cast<jclass
>(jni
->NewGlobalRef( jo_class
.get() ));
204 m_type
= jni
->NewGlobalRef( jo_type
.get() );
205 m_proxy_ctor
= jni
->NewGlobalRef( jo_proxy_ctor
.get() );
209 void JNI_compound_type_info::destroy( JNIEnv
* jni_env
)
211 JNI_type_info::destruct( jni_env
);
217 JNI_compound_type_info::JNI_compound_type_info(
218 JNI_context
const & jni
, typelib_TypeDescription
* td_
)
219 : JNI_type_info( jni
, td_
),
220 m_exc_ctor( nullptr )
222 assert( m_td
.get()->eTypeClass
== typelib_TypeClass_STRUCT
||
223 m_td
.get()->eTypeClass
== typelib_TypeClass_EXCEPTION
);
224 typelib_CompoundTypeDescription
* td
=
225 reinterpret_cast< typelib_CompoundTypeDescription
* >( m_td
.get() );
227 OUString
const & uno_name
=
228 OUString::unacquired( &td
->aBase
.pTypeName
);
230 // Erase type arguments of instantiated polymorphic struct types:
231 std::u16string_view nucleus
;
232 sal_Int32 i
= uno_name
.indexOf( '<' );
236 nucleus
= uno_name
.subView( 0, i
);
238 JLocalAutoRef
jo_class(
243 nucleus
, RTL_TEXTENCODING_JAVA_UTF8
).getStr() ) );
245 JNI_info
const * jni_info
= jni
.get_info();
247 if (m_td
.get()->eTypeClass
== typelib_TypeClass_EXCEPTION
)
249 // retrieve exc ctor( msg )
250 m_exc_ctor
= jni
->GetMethodID(
251 static_cast<jclass
>(jo_class
.get()), "<init>", "(Ljava/lang/String;)V" );
252 jni
.ensure_no_exception();
253 assert( m_exc_ctor
!= nullptr );
256 // retrieve info for base type
257 typelib_TypeDescription
* base_td
=
260 jni_info
->m_RuntimeException_type
.getTypeLibType())
262 : reinterpret_cast< typelib_TypeDescription
* >(
263 td
->pBaseTypeDescription
);
264 m_base
= (base_td
== nullptr ? nullptr : jni_info
->get_type_info( jni
, base_td
));
270 jni_info
->m_Exception_type
.getTypeLibType() ) ||
273 jni_info
->m_RuntimeException_type
.getTypeLibType() ))
275 // coverity[ctor_dtor_leak] - on purpose
276 m_fields
.reset(new jfieldID
[ 2 ]);
277 m_fields
[ 0 ] = nullptr; // special Throwable.getMessage()
279 m_fields
[ 1 ] = jni
->GetFieldID(
280 static_cast<jclass
>(jo_class
.get()), "Context", "Ljava/lang/Object;" );
281 jni
.ensure_no_exception();
282 assert( m_fields
[ 1 ] != nullptr );
286 // retrieve field ids for all direct members
287 sal_Int32 nMembers
= td
->nMembers
;
288 m_fields
.reset(new jfieldID
[ nMembers
]);
290 for ( sal_Int32 nPos
= 0; nPos
< nMembers
; ++nPos
)
293 if (td
->aBase
.eTypeClass
== typelib_TypeClass_STRUCT
294 && reinterpret_cast< typelib_StructTypeDescription
* >(
295 td
)->pParameterizedTypes
!= nullptr
296 && reinterpret_cast< typelib_StructTypeDescription
* >(
297 td
)->pParameterizedTypes
[nPos
])
299 sig
= "Ljava/lang/Object;"_ostr
;
301 OStringBuffer
sig_buf( 32 );
302 JNI_info::append_sig( &sig_buf
, td
->ppTypeRefs
[ nPos
] );
303 sig
= sig_buf
.makeStringAndClear();
308 OUString::unacquired( &td
->ppMemberNames
[ nPos
] ),
309 RTL_TEXTENCODING_JAVA_UTF8
) );
311 m_fields
[ nPos
] = jni
->GetFieldID(
312 static_cast<jclass
>(jo_class
.get()), member_name
.getStr(),
314 jni
.ensure_no_exception();
315 assert( m_fields
[ nPos
] != nullptr );
325 m_class
= static_cast<jclass
>(jni
->NewGlobalRef( jo_class
.get() ));
329 JNI_type_info
const * JNI_info::create_type_info(
330 JNI_context
const & jni
, typelib_TypeDescription
* td
) const
332 OUString
const & uno_name
= OUString::unacquired( &td
->pTypeName
);
334 JNI_type_info
* new_info
;
335 switch (td
->eTypeClass
)
337 case typelib_TypeClass_STRUCT
:
338 case typelib_TypeClass_EXCEPTION
:
340 new_info
= new JNI_compound_type_info( jni
, td
);
343 case typelib_TypeClass_INTERFACE
:
345 new_info
= new JNI_interface_type_info( jni
, td
);
350 throw BridgeRuntimeError(
351 "type info not supported for " + uno_name
+ jni
.get_stack_trace() );
356 JNI_type_info
* info
;
357 std::unique_lock
guard( m_mutex
);
358 JNI_type_info_holder
& holder
= m_type_map
[ uno_name
];
359 if (holder
.m_info
== nullptr) // new insertion
361 holder
.m_info
= new_info
;
365 else // inserted in the meantime
367 info
= holder
.m_info
;
369 new_info
->destroy( jni
.get_jni_env() );
375 JNI_type_info
const * JNI_info::get_type_info(
376 JNI_context
const & jni
, typelib_TypeDescription
* td
) const
378 if (is_XInterface( td
->pWeakRef
))
380 return m_XInterface_type_info
;
383 OUString
const & uno_name
= OUString::unacquired( &td
->pTypeName
);
384 JNI_type_info
const * info
;
385 std::unique_lock
guard( m_mutex
);
387 t_str2type::const_iterator
iFind( m_type_map
.find( uno_name
) );
388 if (iFind
== m_type_map
.end())
391 info
= create_type_info( jni
, td
);
395 info
= iFind
->second
.m_info
;
402 JNI_type_info
const * JNI_info::get_type_info(
403 JNI_context
const & jni
, typelib_TypeDescriptionReference
* type
) const
405 if (is_XInterface( type
))
407 return m_XInterface_type_info
;
410 OUString
const & uno_name
= OUString::unacquired( &type
->pTypeName
);
411 JNI_type_info
const * info
;
412 std::unique_lock
guard( m_mutex
);
413 t_str2type::const_iterator
iFind( m_type_map
.find( uno_name
) );
414 if (iFind
== m_type_map
.end())
417 TypeDescr
td( type
);
418 info
= create_type_info( jni
, td
.get() );
422 info
= iFind
->second
.m_info
;
429 JNI_type_info
const * JNI_info::get_type_info(
430 JNI_context
const & jni
, OUString
const & uno_name
) const
432 if ( uno_name
== "com.sun.star.uno.XInterface" )
434 return m_XInterface_type_info
;
437 JNI_type_info
const * info
;
438 std::unique_lock
guard( m_mutex
);
439 t_str2type::const_iterator
iFind( m_type_map
.find( uno_name
) );
440 if (iFind
== m_type_map
.end())
443 css::uno::TypeDescription
td( uno_name
);
446 throw BridgeRuntimeError(
447 "UNO type not found: " + uno_name
+ jni
.get_stack_trace() );
449 info
= create_type_info( jni
, td
.get() );
453 info
= iFind
->second
.m_info
;
461 JNIEnv
* jni_env
, jobject class_loader
, jclass classClass
,
462 jmethodID methodForName
)
463 : m_class_Class( classClass
),
464 m_method_Class_forName( methodForName
),
465 m_class_JNI_proxy( nullptr ),
466 m_XInterface_queryInterface_td(
467 (reinterpret_cast< typelib_InterfaceTypeDescription
* >(
468 css::uno::TypeDescription(
469 cppu::UnoType
<css::uno::XInterface
>::get())
470 .get())->ppMembers
[ 0 ] ) ),
471 m_Exception_type(cppu::UnoType
<css::uno::Exception
>::get()),
472 m_RuntimeException_type(cppu::UnoType
<css::uno::RuntimeException
>::get()),
473 m_void_type(cppu::UnoType
<void>::get()),
474 m_XInterface_type_info( nullptr )
476 JNI_context
jni( this, jni_env
, class_loader
); // !no proper jni_info!
479 JLocalAutoRef
jo_Object(
480 jni
, find_class( jni
, "java.lang.Object" ) );
481 JLocalAutoRef
jo_Class(
482 jni
, find_class( jni
, "java.lang.Class" ) );
483 JLocalAutoRef
jo_Throwable(
484 jni
, find_class( jni
, "java.lang.Throwable" ) );
485 JLocalAutoRef
jo_Character(
486 jni
, find_class( jni
, "java.lang.Character" ) );
487 JLocalAutoRef
jo_Boolean(
488 jni
, find_class( jni
, "java.lang.Boolean" ) );
489 JLocalAutoRef
jo_Byte(
490 jni
, find_class( jni
, "java.lang.Byte" ) );
491 JLocalAutoRef
jo_Short(
492 jni
, find_class( jni
, "java.lang.Short" ) );
493 JLocalAutoRef
jo_Integer(
494 jni
, find_class( jni
, "java.lang.Integer" ) );
495 JLocalAutoRef
jo_Long(
496 jni
, find_class( jni
, "java.lang.Long" ) );
497 JLocalAutoRef
jo_Float(
498 jni
, find_class( jni
, "java.lang.Float" ) );
499 JLocalAutoRef
jo_Double(
500 jni
, find_class( jni
, "java.lang.Double" ) );
501 JLocalAutoRef
jo_String(
502 jni
, find_class( jni
, "java.lang.String" ) );
503 JLocalAutoRef
jo_RuntimeException(
504 jni
, find_class( jni
, "com.sun.star.uno.RuntimeException" ) );
505 JLocalAutoRef
jo_UnoRuntime(
506 jni
, find_class( jni
, "com.sun.star.uno.UnoRuntime" ) );
507 JLocalAutoRef
jo_Any(
508 jni
, find_class( jni
, "com.sun.star.uno.Any" ) );
509 JLocalAutoRef
jo_Enum(
510 jni
, find_class( jni
, "com.sun.star.uno.Enum" ) );
511 JLocalAutoRef
jo_Type(
512 jni
, find_class( jni
, "com.sun.star.uno.Type" ) );
513 JLocalAutoRef
jo_TypeClass(
514 jni
, find_class( jni
, "com.sun.star.uno.TypeClass" ) );
515 JLocalAutoRef
jo_IEnvironment(
516 jni
, find_class( jni
, "com.sun.star.uno.IEnvironment" ) );
517 JLocalAutoRef
jo_JNI_proxy(
518 jni
, find_class( jni
, "com.sun.star.bridges.jni_uno.JNI_proxy" ) );
519 JLocalAutoRef
jo_AsynchronousFinalizer(
520 jni
, find_class( jni
, "com.sun.star.lib.util.AsynchronousFinalizer" ) );
522 // method Object.toString()
523 m_method_Object_toString
= jni
->GetMethodID(
524 static_cast<jclass
>(jo_Object
.get()), "toString", "()Ljava/lang/String;" );
525 jni
.ensure_no_exception();
526 assert( m_method_Object_toString
!= nullptr );
527 // method Class.getName()
528 m_method_Class_getName
= jni
->GetMethodID(
529 static_cast<jclass
>(jo_Class
.get()), "getName", "()Ljava/lang/String;" );
530 jni
.ensure_no_exception();
531 assert( m_method_Class_getName
!= nullptr );
533 // method Throwable.getMessage()
534 m_method_Throwable_getMessage
= jni
->GetMethodID(
535 static_cast<jclass
>(jo_Throwable
.get()), "getMessage", "()Ljava/lang/String;" );
536 jni
.ensure_no_exception();
537 assert( m_method_Throwable_getMessage
!= nullptr );
539 // method Character.charValue()
540 m_method_Character_charValue
= jni
->GetMethodID(
541 static_cast<jclass
>(jo_Character
.get()), "charValue", "()C" );
542 jni
.ensure_no_exception();
543 assert( m_method_Character_charValue
!= nullptr );
544 // method Boolean.booleanValue()
545 m_method_Boolean_booleanValue
= jni
->GetMethodID(
546 static_cast<jclass
>(jo_Boolean
.get()), "booleanValue", "()Z" );
547 jni
.ensure_no_exception();
548 assert( m_method_Boolean_booleanValue
!= nullptr );
549 // method Byte.byteValue()
550 m_method_Byte_byteValue
= jni
->GetMethodID(
551 static_cast<jclass
>(jo_Byte
.get()), "byteValue", "()B" );
552 jni
.ensure_no_exception();
553 assert( m_method_Byte_byteValue
!= nullptr );
554 // method Short.shortValue()
555 m_method_Short_shortValue
= jni
->GetMethodID(
556 static_cast<jclass
>(jo_Short
.get()), "shortValue", "()S" );
557 jni
.ensure_no_exception();
558 assert( m_method_Short_shortValue
!= nullptr );
559 // method Integer.intValue()
560 m_method_Integer_intValue
= jni
->GetMethodID(
561 static_cast<jclass
>(jo_Integer
.get()), "intValue", "()I" );
562 jni
.ensure_no_exception();
563 assert( m_method_Integer_intValue
!= nullptr );
564 // method Long.longValue()
565 m_method_Long_longValue
= jni
->GetMethodID(
566 static_cast<jclass
>(jo_Long
.get()), "longValue", "()J" );
567 jni
.ensure_no_exception();
568 assert( m_method_Long_longValue
!= nullptr );
569 // method Float.floatValue()
570 m_method_Float_floatValue
= jni
->GetMethodID(
571 static_cast<jclass
>(jo_Float
.get()), "floatValue", "()F" );
572 jni
.ensure_no_exception();
573 assert( m_method_Float_floatValue
!= nullptr );
574 // method Double.doubleValue()
575 m_method_Double_doubleValue
= jni
->GetMethodID(
576 static_cast<jclass
>(jo_Double
.get()), "doubleValue", "()D" );
577 jni
.ensure_no_exception();
578 assert( m_method_Double_doubleValue
!= nullptr );
580 // ctor Character( char )
581 m_ctor_Character_with_char
= jni
->GetMethodID(
582 static_cast<jclass
>(jo_Character
.get()), "<init>", "(C)V" );
583 jni
.ensure_no_exception();
584 assert( m_ctor_Character_with_char
!= nullptr );
585 // ctor Boolean( boolean )
586 m_ctor_Boolean_with_boolean
= jni
->GetMethodID(
587 static_cast<jclass
>(jo_Boolean
.get()), "<init>", "(Z)V" );
588 jni
.ensure_no_exception();
589 assert( m_ctor_Boolean_with_boolean
!= nullptr );
591 m_ctor_Byte_with_byte
= jni
->GetMethodID(
592 static_cast<jclass
>(jo_Byte
.get()), "<init>", "(B)V" );
593 jni
.ensure_no_exception();
594 assert( m_ctor_Byte_with_byte
!= nullptr );
595 // ctor Short( short )
596 m_ctor_Short_with_short
= jni
->GetMethodID(
597 static_cast<jclass
>(jo_Short
.get()), "<init>", "(S)V" );
598 jni
.ensure_no_exception();
599 assert( m_ctor_Short_with_short
!= nullptr );
600 // ctor Integer( int )
601 m_ctor_Integer_with_int
= jni
->GetMethodID(
602 static_cast<jclass
>(jo_Integer
.get()), "<init>", "(I)V" );
603 jni
.ensure_no_exception();
604 assert( m_ctor_Integer_with_int
!= nullptr );
606 m_ctor_Long_with_long
= jni
->GetMethodID(
607 static_cast<jclass
>(jo_Long
.get()), "<init>", "(J)V" );
608 jni
.ensure_no_exception();
609 assert( m_ctor_Long_with_long
!= nullptr );
610 // ctor Float( float )
611 m_ctor_Float_with_float
= jni
->GetMethodID(
612 static_cast<jclass
>(jo_Float
.get()), "<init>", "(F)V" );
613 jni
.ensure_no_exception();
614 assert( m_ctor_Float_with_float
!= nullptr );
615 // ctor Double( double )
616 m_ctor_Double_with_double
= jni
->GetMethodID(
617 static_cast<jclass
>(jo_Double
.get()), "<init>", "(D)V" );
618 jni
.ensure_no_exception();
619 assert( m_ctor_Double_with_double
!= nullptr );
621 // static method UnoRuntime.generateOid()
622 m_method_UnoRuntime_generateOid
= jni
->GetStaticMethodID(
623 static_cast<jclass
>(jo_UnoRuntime
.get()),
624 "generateOid", "(Ljava/lang/Object;)Ljava/lang/String;" );
625 jni
.ensure_no_exception();
626 assert( m_method_UnoRuntime_generateOid
!= nullptr );
627 // static method UnoRuntime.queryInterface()
628 m_method_UnoRuntime_queryInterface
= jni
->GetStaticMethodID(
629 static_cast<jclass
>(jo_UnoRuntime
.get()),
631 "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)Ljava/lang/Object;" );
632 jni
.ensure_no_exception();
633 assert( m_method_UnoRuntime_queryInterface
!= nullptr );
635 // field Enum.m_value
636 m_field_Enum_m_value
= jni
->GetFieldID(
637 static_cast<jclass
>(jo_Enum
.get()), "m_value", "I" );
638 jni
.ensure_no_exception();
639 assert( m_field_Enum_m_value
!= nullptr );
641 // static method TypeClass.fromInt()
642 m_method_TypeClass_fromInt
= jni
->GetStaticMethodID(
643 static_cast<jclass
>(jo_TypeClass
.get()),
644 "fromInt", "(I)Lcom/sun/star/uno/TypeClass;" );
645 jni
.ensure_no_exception();
646 assert( m_method_TypeClass_fromInt
!= nullptr );
648 // ctor Type( Class )
649 m_ctor_Type_with_Class
= jni
->GetMethodID(
650 static_cast<jclass
>(jo_Type
.get()), "<init>", "(Ljava/lang/Class;)V" );
651 jni
.ensure_no_exception();
652 assert( m_ctor_Type_with_Class
!= nullptr );
653 // ctor Type( String, TypeClass )
654 m_ctor_Type_with_Name_TypeClass
= jni
->GetMethodID(
655 static_cast<jclass
>(jo_Type
.get()),
656 "<init>", "(Ljava/lang/String;Lcom/sun/star/uno/TypeClass;)V" );
657 jni
.ensure_no_exception();
658 assert( m_ctor_Type_with_Name_TypeClass
!= nullptr );
659 // field Type._typeName
660 m_field_Type_typeName
= jni
->GetFieldID(
661 static_cast<jclass
>(jo_Type
.get()), "_typeName", "Ljava/lang/String;" );
662 jni
.ensure_no_exception();
663 assert( m_field_Type_typeName
!= nullptr );
665 // ctor Any( Type, Object )
666 m_ctor_Any_with_Type_Object
= jni
->GetMethodID(
667 static_cast<jclass
>(jo_Any
.get()),
668 "<init>", "(Lcom/sun/star/uno/Type;Ljava/lang/Object;)V" );
669 jni
.ensure_no_exception();
670 assert( m_ctor_Any_with_Type_Object
!= nullptr );
673 m_field_Any_type
= jni
->GetFieldID(
674 static_cast<jclass
>(jo_Any
.get()), "_type", "Lcom/sun/star/uno/Type;" );
675 jni
.ensure_no_exception();
676 assert( m_field_Any_type
!= nullptr );
678 m_field_Any_object
= jni
->GetFieldID(
679 static_cast<jclass
>(jo_Any
.get()), "_object", "Ljava/lang/Object;" );
680 jni
.ensure_no_exception();
681 assert( m_field_Any_object
!= nullptr );
683 // method IEnvironment.getRegisteredInterface()
684 m_method_IEnvironment_getRegisteredInterface
= jni
->GetMethodID(
685 static_cast<jclass
>(jo_IEnvironment
.get()),
686 "getRegisteredInterface",
687 "(Ljava/lang/String;Lcom/sun/star/uno/Type;)Ljava/lang/Object;" );
688 jni
.ensure_no_exception();
689 assert( m_method_IEnvironment_getRegisteredInterface
!= nullptr );
690 // method IEnvironment.registerInterface()
691 m_method_IEnvironment_registerInterface
= jni
->GetMethodID(
692 static_cast<jclass
>(jo_IEnvironment
.get()), "registerInterface",
693 "(Ljava/lang/Object;[Ljava/lang/String;Lcom/sun/star/uno/Type;)"
694 "Ljava/lang/Object;" );
695 jni
.ensure_no_exception();
696 assert( m_method_IEnvironment_registerInterface
!= nullptr );
698 // static method JNI_proxy.get_proxy_ctor()
699 m_method_JNI_proxy_get_proxy_ctor
= jni
->GetStaticMethodID(
700 static_cast<jclass
>(jo_JNI_proxy
.get()), "get_proxy_ctor",
701 "(Ljava/lang/Class;)Ljava/lang/reflect/Constructor;" );
702 jni
.ensure_no_exception();
703 assert( m_method_JNI_proxy_get_proxy_ctor
!= nullptr );
704 // static method JNI_proxy.create()
705 m_method_JNI_proxy_create
= jni
->GetStaticMethodID(
706 static_cast<jclass
>(jo_JNI_proxy
.get()), "create",
707 "(JLcom/sun/star/uno/IEnvironment;JJLcom/sun/star/uno/Type;Ljava/lang"
708 "/String;Ljava/lang/reflect/Constructor;"
709 "Lcom/sun/star/lib/util/AsynchronousFinalizer;)Ljava/lang/Object;" );
710 jni
.ensure_no_exception();
711 assert( m_method_JNI_proxy_create
!= nullptr );
712 // field JNI_proxy.m_receiver_handle
713 m_field_JNI_proxy_m_receiver_handle
= jni
->GetFieldID(
714 static_cast<jclass
>(jo_JNI_proxy
.get()), "m_receiver_handle", "J" );
715 jni
.ensure_no_exception();
716 assert( m_field_JNI_proxy_m_receiver_handle
!= nullptr );
717 // field JNI_proxy.m_td_handle
718 m_field_JNI_proxy_m_td_handle
= jni
->GetFieldID(
719 static_cast<jclass
>(jo_JNI_proxy
.get()), "m_td_handle", "J" );
720 jni
.ensure_no_exception();
721 assert( m_field_JNI_proxy_m_td_handle
!= nullptr );
722 // field JNI_proxy.m_type
723 m_field_JNI_proxy_m_type
= jni
->GetFieldID(
724 static_cast<jclass
>(jo_JNI_proxy
.get()), "m_type", "Lcom/sun/star/uno/Type;" );
725 jni
.ensure_no_exception();
726 assert( m_field_JNI_proxy_m_type
!= nullptr );
727 // field JNI_proxy.m_oid
728 m_field_JNI_proxy_m_oid
= jni
->GetFieldID(
729 static_cast<jclass
>(jo_JNI_proxy
.get()), "m_oid", "Ljava/lang/String;" );
730 jni
.ensure_no_exception();
731 assert( m_field_JNI_proxy_m_oid
!= nullptr );
733 // ctor AsynchronousFinalizer
734 m_ctor_AsynchronousFinalizer
= jni
->GetMethodID(
735 static_cast<jclass
>(jo_AsynchronousFinalizer
.get()), "<init>", "()V" );
736 jni
.ensure_no_exception();
737 assert( m_ctor_AsynchronousFinalizer
!= nullptr );
738 // method AsynchronousFinalizer.drain()
739 m_method_AsynchronousFinalizer_drain
= jni
->GetMethodID(
740 static_cast<jclass
>(jo_AsynchronousFinalizer
.get()), "drain", "()V" );
741 jni
.ensure_no_exception();
742 assert( m_method_AsynchronousFinalizer_drain
!= nullptr );
745 OUString
java_env_type_name( u
"" UNO_LB_JAVA
""_ustr
);
746 JLocalAutoRef
jo_java(
747 jni
, ustring_to_jstring( jni
, java_env_type_name
.pData
) );
749 args
[ 0 ].l
= jo_java
.get();
750 args
[ 1 ].l
= nullptr;
751 jmethodID method_getEnvironment
= jni
->GetStaticMethodID(
752 static_cast<jclass
>(jo_UnoRuntime
.get()), "getEnvironment",
753 "(Ljava/lang/String;Ljava/lang/Object;)"
754 "Lcom/sun/star/uno/IEnvironment;" );
755 jni
.ensure_no_exception();
756 assert( method_getEnvironment
!= nullptr );
757 JLocalAutoRef
jo_java_env(
758 jni
, jni
->CallStaticObjectMethodA(
759 static_cast<jclass
>(jo_UnoRuntime
.get()), method_getEnvironment
, args
) );
761 // get com.sun.star.uno.Any.VOID
762 jfieldID field_Any_VOID
= jni
->GetStaticFieldID(
763 static_cast<jclass
>(jo_Any
.get()), "VOID", "Lcom/sun/star/uno/Any;" );
764 jni
.ensure_no_exception();
765 assert( field_Any_VOID
!= nullptr );
766 JLocalAutoRef
jo_Any_VOID(
767 jni
, jni
->GetStaticObjectField(
768 static_cast<jclass
>(jo_Any
.get()), field_Any_VOID
) );
769 // get com.sun.star.uno.Type.UNSIGNED_SHORT
770 jfieldID field_Type_UNSIGNED_SHORT
= jni
->GetStaticFieldID(
771 static_cast<jclass
>(jo_Type
.get()), "UNSIGNED_SHORT", "Lcom/sun/star/uno/Type;" );
772 jni
.ensure_no_exception();
773 assert( field_Type_UNSIGNED_SHORT
!= nullptr );
774 JLocalAutoRef
jo_Type_UNSIGNED_SHORT(
775 jni
, jni
->GetStaticObjectField(
776 static_cast<jclass
>(jo_Type
.get()), field_Type_UNSIGNED_SHORT
) );
777 // get com.sun.star.uno.Type.UNSIGNED_LONG
778 jfieldID field_Type_UNSIGNED_LONG
= jni
->GetStaticFieldID(
779 static_cast<jclass
>(jo_Type
.get()), "UNSIGNED_LONG", "Lcom/sun/star/uno/Type;" );
780 jni
.ensure_no_exception();
781 assert( field_Type_UNSIGNED_LONG
!= nullptr );
782 JLocalAutoRef
jo_Type_UNSIGNED_LONG(
783 jni
, jni
->GetStaticObjectField(
784 static_cast<jclass
>(jo_Type
.get()), field_Type_UNSIGNED_LONG
) );
785 // get com.sun.star.uno.Type.UNSIGNED_HYPER
786 jfieldID field_Type_UNSIGNED_HYPER
= jni
->GetStaticFieldID(
787 static_cast<jclass
>(jo_Type
.get()), "UNSIGNED_HYPER", "Lcom/sun/star/uno/Type;" );
788 jni
.ensure_no_exception();
789 assert( field_Type_UNSIGNED_HYPER
!= nullptr );
790 JLocalAutoRef
jo_Type_UNSIGNED_HYPER(
791 jni
, jni
->GetStaticObjectField(
792 static_cast<jclass
>(jo_Type
.get()), field_Type_UNSIGNED_HYPER
) );
796 static_cast<jclass
>(jni
->NewGlobalRef( jo_UnoRuntime
.get() ));
797 m_class_RuntimeException
=
798 static_cast<jclass
>(jni
->NewGlobalRef( jo_RuntimeException
.get() ));
800 static_cast<jclass
>(jni
->NewGlobalRef( jo_Any
.get() ));
802 static_cast<jclass
>(jni
->NewGlobalRef( jo_Type
.get() ));
804 static_cast<jclass
>(jni
->NewGlobalRef( jo_TypeClass
.get() ));
806 static_cast<jclass
>(jni
->NewGlobalRef( jo_JNI_proxy
.get() ));
807 m_class_AsynchronousFinalizer
=
808 static_cast<jclass
>(jni
->NewGlobalRef( jo_AsynchronousFinalizer
.get() ));
811 static_cast<jclass
>(jni
->NewGlobalRef( jo_Character
.get() ));
813 static_cast<jclass
>(jni
->NewGlobalRef( jo_Boolean
.get() ));
815 static_cast<jclass
>(jni
->NewGlobalRef( jo_Byte
.get() ));
817 static_cast<jclass
>(jni
->NewGlobalRef( jo_Short
.get() ));
819 static_cast<jclass
>(jni
->NewGlobalRef( jo_Integer
.get() ));
821 static_cast<jclass
>(jni
->NewGlobalRef( jo_Long
.get() ));
823 static_cast<jclass
>(jni
->NewGlobalRef( jo_Float
.get() ));
825 static_cast<jclass
>(jni
->NewGlobalRef( jo_Double
.get() ));
827 static_cast<jclass
>(jni
->NewGlobalRef( jo_String
.get() ));
829 static_cast<jclass
>(jni
->NewGlobalRef( jo_Object
.get() ));
831 static_cast<jclass
>(jni
->NewGlobalRef( m_class_Class
));
834 jni
->NewGlobalRef( jo_Any_VOID
.get() );
835 m_object_Type_UNSIGNED_SHORT
=
836 jni
->NewGlobalRef( jo_Type_UNSIGNED_SHORT
.get() );
837 m_object_Type_UNSIGNED_LONG
=
838 jni
->NewGlobalRef( jo_Type_UNSIGNED_LONG
.get() );
839 m_object_Type_UNSIGNED_HYPER
=
840 jni
->NewGlobalRef( jo_Type_UNSIGNED_HYPER
.get() );
841 m_object_java_env
= jni
->NewGlobalRef( jo_java_env
.get() );
845 css::uno::TypeDescription
XInterface_td(
846 cppu::UnoType
<css::uno::XInterface
>::get());
847 // coverity[ctor_dtor_leak] - on purpose
848 m_XInterface_type_info
=
849 new JNI_interface_type_info( jni
, XInterface_td
.get() );
859 void JNI_info::destruct( JNIEnv
* jni_env
)
861 for (auto & i
: m_type_map
)
863 i
.second
.m_info
->destroy( jni_env
);
865 if (m_XInterface_type_info
!= nullptr)
867 const_cast< JNI_interface_type_info
* >(
868 m_XInterface_type_info
)->destroy( jni_env
);
872 jni_env
->DeleteGlobalRef( m_object_java_env
);
873 jni_env
->DeleteGlobalRef( m_object_Any_VOID
);
874 jni_env
->DeleteGlobalRef( m_object_Type_UNSIGNED_SHORT
);
875 jni_env
->DeleteGlobalRef( m_object_Type_UNSIGNED_LONG
);
876 jni_env
->DeleteGlobalRef( m_object_Type_UNSIGNED_HYPER
);
878 jni_env
->DeleteGlobalRef( m_class_Class
);
879 jni_env
->DeleteGlobalRef( m_class_Object
);
880 jni_env
->DeleteGlobalRef( m_class_String
);
881 jni_env
->DeleteGlobalRef( m_class_Double
);
882 jni_env
->DeleteGlobalRef( m_class_Float
);
883 jni_env
->DeleteGlobalRef( m_class_Long
);
884 jni_env
->DeleteGlobalRef( m_class_Integer
);
885 jni_env
->DeleteGlobalRef( m_class_Short
);
886 jni_env
->DeleteGlobalRef( m_class_Byte
);
887 jni_env
->DeleteGlobalRef( m_class_Boolean
);
888 jni_env
->DeleteGlobalRef( m_class_Character
);
890 jni_env
->DeleteGlobalRef( m_class_AsynchronousFinalizer
);
891 jni_env
->DeleteGlobalRef( m_class_JNI_proxy
);
892 jni_env
->DeleteGlobalRef( m_class_RuntimeException
);
893 jni_env
->DeleteGlobalRef( m_class_UnoRuntime
);
894 jni_env
->DeleteGlobalRef( m_class_TypeClass
);
895 jni_env
->DeleteGlobalRef( m_class_Type
);
896 jni_env
->DeleteGlobalRef( m_class_Any
);
900 JNI_info
const * JNI_info::get_jni_info(
901 rtl::Reference
< jvmaccess::UnoVirtualMachine
> const & uno_vm
)
903 // !!!no JNI_info available at JNI_context!!!
904 ::jvmaccess::VirtualMachine::AttachGuard
guard(
905 uno_vm
->getVirtualMachine() );
906 JNIEnv
* jni_env
= guard
.getEnvironment();
908 nullptr, jni_env
, static_cast< jobject
>(uno_vm
->getClassLoader()) );
911 jmethodID jo_forName
;
912 jni
.getClassForName( &jo_class
, &jo_forName
);
913 jni
.ensure_no_exception();
914 JLocalAutoRef
jo_JNI_info_holder(
917 "com.sun.star.bridges.jni_uno.JNI_info_holder", jo_class
,
918 jo_forName
, false ) );
919 // field JNI_info_holder.m_jni_info_handle
920 jfieldID field_s_jni_info_handle
=
921 jni
->GetStaticFieldID(
922 static_cast<jclass
>(jo_JNI_info_holder
.get()), "s_jni_info_handle", "J" );
923 jni
.ensure_no_exception();
924 assert( field_s_jni_info_handle
!= nullptr );
926 JNI_info
const * jni_info
=
927 reinterpret_cast< JNI_info
const * >(
928 jni
->GetStaticLongField(
929 static_cast<jclass
>(jo_JNI_info_holder
.get()), field_s_jni_info_handle
) );
930 if (jni_info
== nullptr) // un-initialized?
932 JNI_info
* new_info
= new JNI_info(
933 jni_env
, static_cast< jobject
>(uno_vm
->getClassLoader()), jo_class
,
936 osl::ClearableMutexGuard
g( osl::Mutex::getGlobalMutex() );
938 reinterpret_cast< JNI_info
const * >(
939 jni
->GetStaticLongField(
940 static_cast<jclass
>(jo_JNI_info_holder
.get()),
941 field_s_jni_info_handle
) );
942 if (jni_info
== nullptr) // still un-initialized?
944 jni
->SetStaticLongField(
945 static_cast<jclass
>(jo_JNI_info_holder
.get()), field_s_jni_info_handle
,
946 reinterpret_cast< jlong
>( new_info
) );
952 new_info
->destroy( jni_env
);
966 JNICALL
Java_com_sun_star_bridges_jni_1uno_JNI_1info_1holder_finalize__J(
967 JNIEnv
* jni_env
, SAL_UNUSED_PARAMETER jobject
, jlong jni_info_handle
) noexcept
969 ::jni_uno::JNI_info
* jni_info
=
970 reinterpret_cast< ::jni_uno::JNI_info
* >( jni_info_handle
);
971 jni_info
->destroy( jni_env
);
976 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */