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 .
22 #include <sal/config.h>
26 #include <jvmaccess/unovirtualmachine.hxx>
27 #include <jvmaccess/virtualmachine.hxx>
29 #include <rtl/ustring.hxx>
30 #include <sal/log.hxx>
33 #include <typelib/typedescription.h>
41 struct BridgeRuntimeError
45 explicit BridgeRuntimeError( OUString message
)
46 : m_message(std::move( message
))
53 JNI_info
const * m_jni_info
;
55 jobject m_class_loader
;
57 JNI_context( JNI_context
const & ) = delete;
58 JNI_context
& operator = ( JNI_context
const &) = delete;
60 void java_exc_occurred() const;
63 JNI_info
const * jni_info
, JNIEnv
* env
, jobject class_loader
)
64 : m_jni_info( jni_info
),
66 m_class_loader( class_loader
)
69 JNI_info
const * get_info() const
70 { return m_jni_info
; }
72 JNIEnv
* operator -> () const
74 JNIEnv
* get_jni_env() const
77 // does not handle exceptions, *classClass will be null if exception
79 void getClassForName(jclass
* classClass
, jmethodID
* methodForName
) const;
81 // if inException, does not handle exceptions, in which case returned value
82 // will be null if exception occurred:
84 char const * name
, jclass classClass
, jmethodID methodForName
,
85 bool inException
) const;
87 inline void ensure_no_exception() const; // throws BridgeRuntimeError
88 inline bool assert_no_exception() const; // asserts and clears exception
90 OUString
get_stack_trace( jobject jo_exc
= nullptr ) const;
93 inline void JNI_context::ensure_no_exception() const
95 if (m_env
->ExceptionCheck())
101 inline bool JNI_context::assert_no_exception() const
103 if (m_env
->ExceptionCheck())
105 SAL_WARN("bridges", "unexpected java exception occurred");
106 #if OSL_DEBUG_LEVEL > 0
107 m_env
->ExceptionDescribe();
109 m_env
->ExceptionClear();
116 class JNI_guarded_context
117 : private ::jvmaccess::VirtualMachine::AttachGuard
,
120 JNI_guarded_context( JNI_guarded_context
const & ) = delete;
121 JNI_guarded_context
& operator = ( JNI_guarded_context
const &) = delete;
124 explicit JNI_guarded_context(
125 JNI_info
const * jni_info
,
126 rtl::Reference
<jvmaccess::UnoVirtualMachine
> const & vm_access
)
127 : AttachGuard( vm_access
->getVirtualMachine() ),
129 jni_info
, AttachGuard::getEnvironment(),
130 static_cast< jobject
>(vm_access
->getClassLoader()) )
137 JNI_context
const & m_jni
;
141 explicit JLocalAutoRef( JNI_context
const & jni
)
145 explicit JLocalAutoRef( JNI_context
const & jni
, jobject jo
)
149 inline JLocalAutoRef( JLocalAutoRef
& auto_ref
);
150 inline ~JLocalAutoRef();
155 { return (nullptr != m_jo
); }
156 inline jobject
release();
157 inline void reset( jobject jo
);
158 inline JLocalAutoRef
& operator = ( JLocalAutoRef
& auto_ref
);
161 inline JLocalAutoRef::~JLocalAutoRef()
164 m_jni
->DeleteLocalRef( m_jo
);
167 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef
& auto_ref
)
168 : m_jni( auto_ref
.m_jni
),
169 m_jo( auto_ref
.m_jo
)
171 auto_ref
.m_jo
= nullptr;
174 inline jobject
JLocalAutoRef::release()
181 inline void JLocalAutoRef::reset( jobject jo
)
186 m_jni
->DeleteLocalRef( m_jo
);
191 inline JLocalAutoRef
& JLocalAutoRef::operator = ( JLocalAutoRef
& auto_ref
)
193 assert( m_jni
.get_jni_env() == auto_ref
.m_jni
.get_jni_env() );
194 reset( auto_ref
.m_jo
);
195 auto_ref
.m_jo
= nullptr;
203 static void * operator new ( size_t nSize
)
204 { return std::malloc( nSize
); }
205 static void operator delete ( void * mem
)
206 { std::free( mem
); }
207 static void * operator new ( size_t, void * mem
)
209 static void operator delete ( void *, void * )
212 static inline rtl_mem
* allocate( std::size_t bytes
);
215 inline rtl_mem
* rtl_mem::allocate( std::size_t bytes
)
217 void * p
= std::malloc( bytes
);
219 throw BridgeRuntimeError( u
"out of memory!"_ustr
);
220 return static_cast<rtl_mem
*>(p
);
226 typelib_TypeDescription
* m_td
;
228 TypeDescr( TypeDescr
const & ) = delete;
229 TypeDescr
& operator = ( TypeDescr
const & ) = delete;
232 inline explicit TypeDescr( typelib_TypeDescriptionReference
* td_ref
);
234 { TYPELIB_DANGER_RELEASE( m_td
); }
236 typelib_TypeDescription
* get() const
240 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference
* td_ref
)
243 TYPELIB_DANGER_GET( &m_td
, td_ref
);
246 throw BridgeRuntimeError(
247 "cannot get comprehensive type description for " +
248 OUString::unacquired( &td_ref
->pTypeName
) );
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */