xref: /trunk/main/bridges/source/jni_uno/jni_base.h (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #if ! defined INCLUDED_JNI_BASE_H
29*cdf0e10cSrcweir #define INCLUDED_JNI_BASE_H
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
32*cdf0e10cSrcweir // workaround solaris include trouble on jumbo
33*cdf0e10cSrcweir #include <stdarg.h>
34*cdf0e10cSrcweir namespace std
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir typedef __va_list va_list;
37*cdf0e10cSrcweir }
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir #include <memory>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include "jvmaccess/unovirtualmachine.hxx"
42*cdf0e10cSrcweir #include "jvmaccess/virtualmachine.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include "osl/diagnose.h"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include "rtl/alloc.h"
47*cdf0e10cSrcweir #include "rtl/ustring.hxx"
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "uno/environment.h"
50*cdf0e10cSrcweir #include "typelib/typedescription.h"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir namespace jni_uno
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir class JNI_info;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir //==============================================================================
61*cdf0e10cSrcweir struct BridgeRuntimeError
62*cdf0e10cSrcweir {
63*cdf0e10cSrcweir     ::rtl::OUString m_message;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     inline BridgeRuntimeError( ::rtl::OUString const & message )
66*cdf0e10cSrcweir         : m_message( message )
67*cdf0e10cSrcweir         {}
68*cdf0e10cSrcweir };
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir //==============================================================================
72*cdf0e10cSrcweir class JNI_context
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     JNI_info const * m_jni_info;
75*cdf0e10cSrcweir     JNIEnv *         m_env;
76*cdf0e10cSrcweir     jobject          m_class_loader;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     JNI_context( JNI_context & ); // not impl
79*cdf0e10cSrcweir     void operator = ( JNI_context ); // not impl
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     void java_exc_occured() const;
82*cdf0e10cSrcweir public:
83*cdf0e10cSrcweir     inline explicit JNI_context(
84*cdf0e10cSrcweir         JNI_info const * jni_info, JNIEnv * env, jobject class_loader )
85*cdf0e10cSrcweir         : m_jni_info( jni_info ),
86*cdf0e10cSrcweir           m_env( env ),
87*cdf0e10cSrcweir           m_class_loader( class_loader )
88*cdf0e10cSrcweir         {}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     inline JNI_info const * get_info() const
91*cdf0e10cSrcweir         { return m_jni_info; }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     inline JNIEnv * operator -> () const
94*cdf0e10cSrcweir         { return m_env; }
95*cdf0e10cSrcweir     inline JNIEnv * get_jni_env() const
96*cdf0e10cSrcweir         { return m_env; }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     // does not handle exceptions, *classClass will be null if exception
99*cdf0e10cSrcweir     // occurred:
100*cdf0e10cSrcweir     void getClassForName(jclass * classClass, jmethodID * methodForName) const;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     // if inException, does not handle exceptions, in which case returned value
103*cdf0e10cSrcweir     // will be null if exception occurred:
104*cdf0e10cSrcweir     jclass findClass(
105*cdf0e10cSrcweir         char const * name, jclass classClass, jmethodID methodForName,
106*cdf0e10cSrcweir         bool inException) const;
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     inline void ensure_no_exception() const; // throws BridgeRuntimeError
109*cdf0e10cSrcweir     inline bool assert_no_exception() const; // asserts and clears exception
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     ::rtl::OUString get_stack_trace( jobject jo_exc = 0 ) const;
112*cdf0e10cSrcweir };
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir //______________________________________________________________________________
115*cdf0e10cSrcweir inline void JNI_context::ensure_no_exception() const
116*cdf0e10cSrcweir {
117*cdf0e10cSrcweir     if (JNI_FALSE != m_env->ExceptionCheck())
118*cdf0e10cSrcweir     {
119*cdf0e10cSrcweir         java_exc_occured();
120*cdf0e10cSrcweir     }
121*cdf0e10cSrcweir }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir //______________________________________________________________________________
124*cdf0e10cSrcweir inline bool JNI_context::assert_no_exception() const
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     if (JNI_FALSE != m_env->ExceptionCheck())
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         m_env->ExceptionClear();
129*cdf0e10cSrcweir         OSL_ENSURE( 0, "unexpected java exception occured!" );
130*cdf0e10cSrcweir         return false;
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir     return true;
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir //==============================================================================
137*cdf0e10cSrcweir class JNI_guarded_context
138*cdf0e10cSrcweir     : private ::jvmaccess::VirtualMachine::AttachGuard,
139*cdf0e10cSrcweir       public JNI_context
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     JNI_guarded_context( JNI_guarded_context & ); // not impl
142*cdf0e10cSrcweir     void operator = ( JNI_guarded_context ); // not impl
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir public:
145*cdf0e10cSrcweir     inline explicit JNI_guarded_context(
146*cdf0e10cSrcweir         JNI_info const * jni_info, ::jvmaccess::UnoVirtualMachine * vm_access )
147*cdf0e10cSrcweir         : AttachGuard( vm_access->getVirtualMachine() ),
148*cdf0e10cSrcweir           JNI_context(
149*cdf0e10cSrcweir               jni_info, AttachGuard::getEnvironment(),
150*cdf0e10cSrcweir               static_cast< jobject >(vm_access->getClassLoader()) )
151*cdf0e10cSrcweir         {}
152*cdf0e10cSrcweir };
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir //==============================================================================
156*cdf0e10cSrcweir class JLocalAutoRef
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir     JNI_context const & m_jni;
159*cdf0e10cSrcweir     jobject m_jo;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir public:
162*cdf0e10cSrcweir     inline JLocalAutoRef( JNI_context const & jni )
163*cdf0e10cSrcweir         : m_jni( jni ),
164*cdf0e10cSrcweir           m_jo( 0 )
165*cdf0e10cSrcweir         {}
166*cdf0e10cSrcweir     inline explicit JLocalAutoRef( JNI_context const & jni, jobject jo )
167*cdf0e10cSrcweir         : m_jni( jni ),
168*cdf0e10cSrcweir           m_jo( jo )
169*cdf0e10cSrcweir         {}
170*cdf0e10cSrcweir     inline JLocalAutoRef( JLocalAutoRef & auto_ref );
171*cdf0e10cSrcweir     inline ~JLocalAutoRef() SAL_THROW( () );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     inline jobject get() const
174*cdf0e10cSrcweir         { return m_jo; }
175*cdf0e10cSrcweir     inline bool is() const
176*cdf0e10cSrcweir         { return (0 != m_jo); }
177*cdf0e10cSrcweir     inline jobject release();
178*cdf0e10cSrcweir     inline void reset();
179*cdf0e10cSrcweir     inline void reset( jobject jo );
180*cdf0e10cSrcweir     inline JLocalAutoRef & operator = ( JLocalAutoRef & auto_ref );
181*cdf0e10cSrcweir };
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir //______________________________________________________________________________
184*cdf0e10cSrcweir inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW( () )
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     if (0 != m_jo)
187*cdf0e10cSrcweir         m_jni->DeleteLocalRef( m_jo );
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir //______________________________________________________________________________
191*cdf0e10cSrcweir inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef & auto_ref )
192*cdf0e10cSrcweir     : m_jni( auto_ref.m_jni ),
193*cdf0e10cSrcweir       m_jo( auto_ref.m_jo )
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     auto_ref.m_jo = 0;
196*cdf0e10cSrcweir }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir //______________________________________________________________________________
199*cdf0e10cSrcweir inline jobject JLocalAutoRef::release()
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir     jobject jo = m_jo;
202*cdf0e10cSrcweir     m_jo = 0;
203*cdf0e10cSrcweir     return jo;
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir //______________________________________________________________________________
207*cdf0e10cSrcweir inline void JLocalAutoRef::reset()
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     if (0 != m_jo)
210*cdf0e10cSrcweir         m_jni->DeleteLocalRef( m_jo );
211*cdf0e10cSrcweir     m_jo = 0;
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir //______________________________________________________________________________
215*cdf0e10cSrcweir inline void JLocalAutoRef::reset( jobject jo )
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     if (jo != m_jo)
218*cdf0e10cSrcweir     {
219*cdf0e10cSrcweir         if (0 != m_jo)
220*cdf0e10cSrcweir             m_jni->DeleteLocalRef( m_jo );
221*cdf0e10cSrcweir         m_jo = jo;
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir }
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir //______________________________________________________________________________
226*cdf0e10cSrcweir inline JLocalAutoRef & JLocalAutoRef::operator = ( JLocalAutoRef & auto_ref )
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir     OSL_ASSERT( m_jni.get_jni_env() == auto_ref.m_jni.get_jni_env() );
229*cdf0e10cSrcweir     reset( auto_ref.m_jo );
230*cdf0e10cSrcweir     auto_ref.m_jo = 0;
231*cdf0e10cSrcweir     return *this;
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir //==============================================================================
236*cdf0e10cSrcweir struct rtl_mem
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	inline static void * operator new ( size_t nSize )
239*cdf0e10cSrcweir 		{ return rtl_allocateMemory( nSize ); }
240*cdf0e10cSrcweir 	inline static void operator delete ( void * mem )
241*cdf0e10cSrcweir 		{ if (mem) rtl_freeMemory( mem ); }
242*cdf0e10cSrcweir 	inline static void * operator new ( size_t, void * mem )
243*cdf0e10cSrcweir 		{ return mem; }
244*cdf0e10cSrcweir 	inline static void operator delete ( void *, void * )
245*cdf0e10cSrcweir 		{}
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir     static inline ::std::auto_ptr< rtl_mem > allocate( ::std::size_t bytes );
248*cdf0e10cSrcweir };
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir //______________________________________________________________________________
251*cdf0e10cSrcweir inline ::std::auto_ptr< rtl_mem > rtl_mem::allocate( ::std::size_t bytes )
252*cdf0e10cSrcweir {
253*cdf0e10cSrcweir     void * p = rtl_allocateMemory( bytes );
254*cdf0e10cSrcweir     if (0 == p)
255*cdf0e10cSrcweir         throw BridgeRuntimeError( OUSTR("out of memory!") );
256*cdf0e10cSrcweir     return ::std::auto_ptr< rtl_mem >( (rtl_mem *)p );
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir //==============================================================================
261*cdf0e10cSrcweir class TypeDescr
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir     typelib_TypeDescription * m_td;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     TypeDescr( TypeDescr & ); // not impl
266*cdf0e10cSrcweir     void operator = ( TypeDescr ); // not impl
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir public:
269*cdf0e10cSrcweir     inline explicit TypeDescr( typelib_TypeDescriptionReference * td_ref );
270*cdf0e10cSrcweir     inline ~TypeDescr() SAL_THROW( () )
271*cdf0e10cSrcweir         { TYPELIB_DANGER_RELEASE( m_td ); }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir     inline typelib_TypeDescription * get() const
274*cdf0e10cSrcweir         { return m_td; }
275*cdf0e10cSrcweir };
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir //______________________________________________________________________________
278*cdf0e10cSrcweir inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference * td_ref )
279*cdf0e10cSrcweir     : m_td( 0 )
280*cdf0e10cSrcweir {
281*cdf0e10cSrcweir     TYPELIB_DANGER_GET( &m_td, td_ref );
282*cdf0e10cSrcweir     if (0 == m_td)
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         throw BridgeRuntimeError(
285*cdf0e10cSrcweir             OUSTR("cannot get comprehensive type description for ") +
286*cdf0e10cSrcweir             ::rtl::OUString::unacquired( &td_ref->pTypeName ) );
287*cdf0e10cSrcweir     }
288*cdf0e10cSrcweir }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir #endif
293