nss: upgrade to release 3.73
[LibreOffice.git] / bridges / source / jni_uno / jni_base.h
blob696ddd3655332b56ef42919fa13a6f7da717ea9f
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 #pragma once
22 #include <sal/config.h>
24 #include <cassert>
26 #include <jvmaccess/unovirtualmachine.hxx>
27 #include <jvmaccess/virtualmachine.hxx>
29 #include <osl/diagnose.h>
31 #include <rtl/alloc.h>
32 #include <rtl/ustring.hxx>
33 #include <sal/log.hxx>
35 #include <uno/environment.h>
36 #include <typelib/typedescription.h>
39 namespace jni_uno
42 class JNI_info;
44 struct BridgeRuntimeError
46 OUString m_message;
48 explicit BridgeRuntimeError( OUString const & message )
49 : m_message( message )
54 class JNI_context
56 JNI_info const * m_jni_info;
57 JNIEnv * m_env;
58 jobject m_class_loader;
60 JNI_context( JNI_context const & ) = delete;
61 JNI_context& operator = ( JNI_context const &) = delete;
63 void java_exc_occurred() const;
64 public:
65 explicit JNI_context(
66 JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
67 : m_jni_info( jni_info ),
68 m_env( env ),
69 m_class_loader( class_loader )
72 JNI_info const * get_info() const
73 { return m_jni_info; }
75 JNIEnv * operator -> () const
76 { return m_env; }
77 JNIEnv * get_jni_env() const
78 { return m_env; }
80 // does not handle exceptions, *classClass will be null if exception
81 // occurred:
82 void getClassForName(jclass * classClass, jmethodID * methodForName) const;
84 // if inException, does not handle exceptions, in which case returned value
85 // will be null if exception occurred:
86 jclass findClass(
87 char const * name, jclass classClass, jmethodID methodForName,
88 bool inException) const;
90 inline void ensure_no_exception() const; // throws BridgeRuntimeError
91 inline bool assert_no_exception() const; // asserts and clears exception
93 OUString get_stack_trace( jobject jo_exc = nullptr ) const;
96 inline void JNI_context::ensure_no_exception() const
98 if (m_env->ExceptionCheck())
100 java_exc_occurred();
104 inline bool JNI_context::assert_no_exception() const
106 if (m_env->ExceptionCheck())
108 SAL_WARN("bridges", "unexpected java exception occurred");
109 #if OSL_DEBUG_LEVEL > 0
110 m_env->ExceptionDescribe();
111 #endif
112 m_env->ExceptionClear();
113 return false;
115 return true;
119 class JNI_guarded_context
120 : private ::jvmaccess::VirtualMachine::AttachGuard,
121 public JNI_context
123 JNI_guarded_context( JNI_guarded_context const & ) = delete;
124 JNI_guarded_context& operator = ( JNI_guarded_context const &) = delete;
126 public:
127 explicit JNI_guarded_context(
128 JNI_info const * jni_info,
129 rtl::Reference<jvmaccess::UnoVirtualMachine> const & vm_access)
130 : AttachGuard( vm_access->getVirtualMachine() ),
131 JNI_context(
132 jni_info, AttachGuard::getEnvironment(),
133 static_cast< jobject >(vm_access->getClassLoader()) )
138 class JLocalAutoRef
140 JNI_context const & m_jni;
141 jobject m_jo;
143 public:
144 explicit JLocalAutoRef( JNI_context const & jni )
145 : m_jni( jni ),
146 m_jo( nullptr )
148 explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
149 : m_jni( jni ),
150 m_jo( jo )
152 inline JLocalAutoRef( JLocalAutoRef & auto_ref );
153 inline ~JLocalAutoRef();
155 jobject get() const
156 { return m_jo; }
157 bool is() const
158 { return (nullptr != m_jo); }
159 inline jobject release();
160 inline void reset( jobject jo );
161 inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
164 inline JLocalAutoRef::~JLocalAutoRef()
166 if (nullptr != m_jo)
167 m_jni->DeleteLocalRef( m_jo );
170 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
171 : m_jni( auto_ref.m_jni ),
172 m_jo( auto_ref.m_jo )
174 auto_ref.m_jo = nullptr;
177 inline jobject JLocalAutoRef::release()
179 jobject jo = m_jo;
180 m_jo = nullptr;
181 return jo;
184 inline void JLocalAutoRef::reset( jobject jo )
186 if (jo != m_jo)
188 if (nullptr != m_jo)
189 m_jni->DeleteLocalRef( m_jo );
190 m_jo = jo;
194 inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
196 assert( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
197 reset( auto_ref.m_jo );
198 auto_ref.m_jo = nullptr;
199 return *this;
204 struct rtl_mem
206 static void * operator new ( size_t nSize )
207 { return std::malloc( nSize ); }
208 static void operator delete ( void * mem )
209 { std::free( mem ); }
210 static void * operator new ( size_t, void * mem )
211 { return mem; }
212 static void operator delete ( void *, void * )
215 static inline rtl_mem * allocate( std::size_t bytes );
218 inline rtl_mem * rtl_mem::allocate( std::size_t bytes )
220 void * p = std::malloc( bytes );
221 if (nullptr == p)
222 throw BridgeRuntimeError( "out of memory!" );
223 return static_cast<rtl_mem *>(p);
227 class TypeDescr
229 typelib_TypeDescription * m_td;
231 TypeDescr( TypeDescr const & ) = delete;
232 TypeDescr& operator = ( TypeDescr const & ) = delete;
234 public:
235 inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
236 ~TypeDescr()
237 { TYPELIB_DANGER_RELEASE( m_td ); }
239 typelib_TypeDescription * get() const
240 { return m_td; }
243 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
244 : m_td( nullptr )
246 TYPELIB_DANGER_GET( &m_td, td_ref );
247 if (nullptr == m_td)
249 throw BridgeRuntimeError(
250 "cannot get comprehensive type description for " +
251 OUString::unacquired( &td_ref->pTypeName ) );
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */