1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jni_base.h,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #if ! defined INCLUDED_JNI_BASE_H
32 #define INCLUDED_JNI_BASE_H
34 #if defined (__SUNPRO_CC) || defined (__SUNPRO_C)
35 // workaround solaris include trouble on jumbo
39 typedef __va_list
va_list;
44 #include "jvmaccess/unovirtualmachine.hxx"
45 #include "jvmaccess/virtualmachine.hxx"
47 #include "osl/diagnose.h"
49 #include "rtl/alloc.h"
50 #include "rtl/ustring.hxx"
52 #include "uno/environment.h"
53 #include "typelib/typedescription.h"
55 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
63 //==============================================================================
64 struct BridgeRuntimeError
66 ::rtl::OUString m_message
;
68 inline BridgeRuntimeError( ::rtl::OUString
const & message
)
69 : m_message( message
)
74 //==============================================================================
77 JNI_info
const * m_jni_info
;
79 jobject m_class_loader
;
81 JNI_context( JNI_context
& ); // not impl
82 void operator = ( JNI_context
); // not impl
84 void java_exc_occured() const;
86 inline explicit JNI_context(
87 JNI_info
const * jni_info
, JNIEnv
* env
, jobject class_loader
)
88 : m_jni_info( jni_info
),
90 m_class_loader( class_loader
)
93 inline JNI_info
const * get_info() const
94 { return m_jni_info
; }
96 inline JNIEnv
* operator -> () const
98 inline JNIEnv
* get_jni_env() const
101 // does not handle exceptions, *classClass will be null if exception
103 void getClassForName(jclass
* classClass
, jmethodID
* methodForName
) const;
105 // if inException, does not handle exceptions, in which case returned value
106 // will be null if exception occurred:
108 char const * name
, jclass classClass
, jmethodID methodForName
,
109 bool inException
) const;
111 inline void ensure_no_exception() const; // throws BridgeRuntimeError
112 inline bool assert_no_exception() const; // asserts and clears exception
114 ::rtl::OUString
get_stack_trace( jobject jo_exc
= 0 ) const;
117 //______________________________________________________________________________
118 inline void JNI_context::ensure_no_exception() const
120 if (JNI_FALSE
!= m_env
->ExceptionCheck())
126 //______________________________________________________________________________
127 inline bool JNI_context::assert_no_exception() const
129 if (JNI_FALSE
!= m_env
->ExceptionCheck())
131 m_env
->ExceptionClear();
132 OSL_ENSURE( 0, "unexpected java exception occured!" );
139 //==============================================================================
140 class JNI_guarded_context
141 : private ::jvmaccess::VirtualMachine::AttachGuard
,
144 JNI_guarded_context( JNI_guarded_context
& ); // not impl
145 void operator = ( JNI_guarded_context
); // not impl
148 inline explicit JNI_guarded_context(
149 JNI_info
const * jni_info
, ::jvmaccess::UnoVirtualMachine
* vm_access
)
150 : AttachGuard( vm_access
->getVirtualMachine() ),
152 jni_info
, AttachGuard::getEnvironment(),
153 static_cast< jobject
>(vm_access
->getClassLoader()) )
158 //==============================================================================
161 JNI_context
const & m_jni
;
165 inline JLocalAutoRef( JNI_context
const & jni
)
169 inline explicit JLocalAutoRef( JNI_context
const & jni
, jobject jo
)
173 inline JLocalAutoRef( JLocalAutoRef
& auto_ref
);
174 inline ~JLocalAutoRef() SAL_THROW( () );
176 inline jobject
get() const
178 inline bool is() const
179 { return (0 != m_jo
); }
180 inline jobject
release();
182 inline void reset( jobject jo
);
183 inline JLocalAutoRef
& operator = ( JLocalAutoRef
& auto_ref
);
186 //______________________________________________________________________________
187 inline JLocalAutoRef::~JLocalAutoRef() SAL_THROW( () )
190 m_jni
->DeleteLocalRef( m_jo
);
193 //______________________________________________________________________________
194 inline JLocalAutoRef::JLocalAutoRef( JLocalAutoRef
& auto_ref
)
195 : m_jni( auto_ref
.m_jni
),
196 m_jo( auto_ref
.m_jo
)
201 //______________________________________________________________________________
202 inline jobject
JLocalAutoRef::release()
209 //______________________________________________________________________________
210 inline void JLocalAutoRef::reset()
213 m_jni
->DeleteLocalRef( m_jo
);
217 //______________________________________________________________________________
218 inline void JLocalAutoRef::reset( jobject jo
)
223 m_jni
->DeleteLocalRef( m_jo
);
228 //______________________________________________________________________________
229 inline JLocalAutoRef
& JLocalAutoRef::operator = ( JLocalAutoRef
& auto_ref
)
231 OSL_ASSERT( m_jni
.get_jni_env() == auto_ref
.m_jni
.get_jni_env() );
232 reset( auto_ref
.m_jo
);
238 //==============================================================================
241 inline static void * operator new ( size_t nSize
)
242 { return rtl_allocateMemory( nSize
); }
243 inline static void operator delete ( void * mem
)
244 { if (mem
) rtl_freeMemory( mem
); }
245 inline static void * operator new ( size_t, void * mem
)
247 inline static void operator delete ( void *, void * )
250 static inline ::std::auto_ptr
< rtl_mem
> allocate( ::std::size_t bytes
);
253 //______________________________________________________________________________
254 inline ::std::auto_ptr
< rtl_mem
> rtl_mem::allocate( ::std::size_t bytes
)
256 void * p
= rtl_allocateMemory( bytes
);
258 throw BridgeRuntimeError( OUSTR("out of memory!") );
259 return ::std::auto_ptr
< rtl_mem
>( (rtl_mem
*)p
);
263 //==============================================================================
266 typelib_TypeDescription
* m_td
;
268 TypeDescr( TypeDescr
& ); // not impl
269 void operator = ( TypeDescr
); // not impl
272 inline explicit TypeDescr( typelib_TypeDescriptionReference
* td_ref
);
273 inline ~TypeDescr() SAL_THROW( () )
274 { TYPELIB_DANGER_RELEASE( m_td
); }
276 inline typelib_TypeDescription
* get() const
280 //______________________________________________________________________________
281 inline TypeDescr::TypeDescr( typelib_TypeDescriptionReference
* td_ref
)
284 TYPELIB_DANGER_GET( &m_td
, td_ref
);
287 throw BridgeRuntimeError(
288 OUSTR("cannot get comprehensive type description for ") +
289 ::rtl::OUString::unacquired( &td_ref
->pTypeName
) );