tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / jni_uno / jni_base.h
blob4b74713819816be6e78d2bc3121aff88308db2cf
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 <rtl/ustring.hxx>
30 #include <sal/log.hxx>
31 #include <utility>
33 #include <typelib/typedescription.h>
36 namespace jni_uno
39 class JNI_info;
41 struct BridgeRuntimeError
43 OUString m_message;
45 explicit BridgeRuntimeError( OUString message )
46 : m_message(std::move( message ))
51 class JNI_context
53 JNI_info const * m_jni_info;
54 JNIEnv * m_env;
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;
61 public:
62 explicit JNI_context(
63 JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
64 : m_jni_info( jni_info ),
65 m_env( env ),
66 m_class_loader( class_loader )
69 JNI_info const * get_info() const
70 { return m_jni_info; }
72 JNIEnv * operator -> () const
73 { return m_env; }
74 JNIEnv * get_jni_env() const
75 { return m_env; }
77 // does not handle exceptions, *classClass will be null if exception
78 // occurred:
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:
83 jclass findClass(
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())
97 java_exc_occurred();
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();
108 #endif
109 m_env->ExceptionClear();
110 return false;
112 return true;
116 class JNI_guarded_context
117 : private ::jvmaccess::VirtualMachine::AttachGuard,
118 public JNI_context
120 JNI_guarded_context( JNI_guarded_context const & ) = delete;
121 JNI_guarded_context& operator = ( JNI_guarded_context const &) = delete;
123 public:
124 explicit JNI_guarded_context(
125 JNI_info const * jni_info,
126 rtl::Reference<jvmaccess::UnoVirtualMachine> const & vm_access)
127 : AttachGuard( vm_access->getVirtualMachine() ),
128 JNI_context(
129 jni_info, AttachGuard::getEnvironment(),
130 static_cast< jobject >(vm_access->getClassLoader()) )
135 class JLocalAutoRef
137 JNI_context const & m_jni;
138 jobject m_jo;
140 public:
141 explicit JLocalAutoRef( JNI_context const & jni )
142 : m_jni( jni ),
143 m_jo( nullptr )
145 explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
146 : m_jni( jni ),
147 m_jo( jo )
149 inline JLocalAutoRef( JLocalAutoRef & auto_ref );
150 inline ~JLocalAutoRef();
152 jobject get() const
153 { return m_jo; }
154 bool is() const
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()
163 if (nullptr != m_jo)
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()
176 jobject jo = m_jo;
177 m_jo = nullptr;
178 return jo;
181 inline void JLocalAutoRef::reset( jobject jo )
183 if (jo != m_jo)
185 if (nullptr != m_jo)
186 m_jni->DeleteLocalRef( m_jo );
187 m_jo = 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;
196 return *this;
201 struct rtl_mem
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 )
208 { return 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 );
218 if (nullptr == p)
219 throw BridgeRuntimeError( u"out of memory!"_ustr );
220 return static_cast<rtl_mem *>(p);
224 class TypeDescr
226 typelib_TypeDescription * m_td;
228 TypeDescr( TypeDescr const & ) = delete;
229 TypeDescr& operator = ( TypeDescr const & ) = delete;
231 public:
232 inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
233 ~TypeDescr()
234 { TYPELIB_DANGER_RELEASE( m_td ); }
236 typelib_TypeDescription * get() const
237 { return m_td; }
240 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
241 : m_td( nullptr )
243 TYPELIB_DANGER_GET( &m_td, td_ref );
244 if (nullptr == m_td)
246 throw BridgeRuntimeError(
247 "cannot get comprehensive type description for " +
248 OUString::unacquired( &td_ref->pTypeName ) );
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */