xref: /AOO41X/main/connectivity/source/inc/java/lang/Object.hxx (revision caf5cd79edad04a48dcaf209068b3b89eae4622e)
1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir #ifndef _CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
24cdf0e10cSrcweir #define	_CONNECTIVITY_JAVA_LANG_OBJECT_HXX_
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #if STLPORT_VERSION>=321
27cdf0e10cSrcweir // jni.h needs cstdarg for std::va_list
28cdf0e10cSrcweir #include <cstdarg>
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #include <osl/thread.h>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/SQLException.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33cdf0e10cSrcweir #include <osl/diagnose.h>
34cdf0e10cSrcweir #include <jvmaccess/virtualmachine.hxx>
35cdf0e10cSrcweir #include <memory>
36cdf0e10cSrcweir #include <functional>
37cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //=====================================================================
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #ifdef HAVE_64BIT_POINTERS
42cdf0e10cSrcweir #error "no 64 bit pointer"
43cdf0e10cSrcweir #else
44cdf0e10cSrcweir #ifdef OS2
45cdf0e10cSrcweir #define PVOID_TO_INT64(x) (jlong)(sal_Int32)x
46cdf0e10cSrcweir #define INT64_TO_PVOID(x) (void *)x
47cdf0e10cSrcweir #endif // OS2
48cdf0e10cSrcweir #endif //HAVE_64BIT_POINTERS
49cdf0e10cSrcweir 
50cdf0e10cSrcweir namespace comphelper
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     class ResourceBasedEventLogger;
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir namespace connectivity
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	typedef ::boost::shared_ptr< jvmaccess::VirtualMachine::AttachGuard> TGuard;
58cdf0e10cSrcweir 	class SDBThreadAttach
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		jvmaccess::VirtualMachine::AttachGuard m_aGuard;
61cdf0e10cSrcweir 		SDBThreadAttach(SDBThreadAttach&);
62cdf0e10cSrcweir         SDBThreadAttach& operator= (SDBThreadAttach&);
63cdf0e10cSrcweir 	public:
64cdf0e10cSrcweir 		SDBThreadAttach();
65cdf0e10cSrcweir 		~SDBThreadAttach();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 		JNIEnv*	pEnv;
68cdf0e10cSrcweir 		static void addRef();
69cdf0e10cSrcweir 		static void releaseRef();
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     public:
72cdf0e10cSrcweir         JNIEnv& env() const
73cdf0e10cSrcweir         {
74cdf0e10cSrcweir             // according to the documentation of jvmaccess::VirtualMachine::AttachGuard, our env is never
75cdf0e10cSrcweir             // NULL, so why bothering with pointer checks?
76cdf0e10cSrcweir             return *pEnv;
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir 	};
79cdf0e10cSrcweir 	//=====================================================================
80cdf0e10cSrcweir     //=====================================================================
81cdf0e10cSrcweir 	class java_lang_Class;
82cdf0e10cSrcweir 	class  java_lang_Object
83cdf0e10cSrcweir 	{
84cdf0e10cSrcweir 		// Zuweisungsoperator und Copy Konstruktor sind verboten
85cdf0e10cSrcweir 		java_lang_Object& operator= (java_lang_Object&);
86cdf0e10cSrcweir 		java_lang_Object(java_lang_Object&);
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 		// nur zum Zerstoeren des C++ Pointers in vom JSbxObject
89cdf0e10cSrcweir 		// abgeleiteten Java Objekten
90cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > m_xFactory;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 	protected:
93cdf0e10cSrcweir 		// der JAVA Handle zu dieser Klasse
94cdf0e10cSrcweir 		jobject object;
95cdf0e10cSrcweir 		// Klassendefinition
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 		// neu in SJ2:
98cdf0e10cSrcweir 		static jclass theClass;				// die Klasse braucht nur einmal angefordert werden !
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         virtual jclass getMyClass() const;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	public:
103cdf0e10cSrcweir 		// der Konstruktor, der fuer die abgeleiteten Klassen verwendet
104cdf0e10cSrcweir 		// werden soll.
105cdf0e10cSrcweir 		java_lang_Object( JNIEnv * pEnv, jobject myObj );
106cdf0e10cSrcweir 		// der eigentliche Konstruktor
107cdf0e10cSrcweir         java_lang_Object(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		virtual ~java_lang_Object();
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 		void				saveRef( JNIEnv * pEnv, jobject myObj );
112cdf0e10cSrcweir 		jobject				getJavaObject() const { return object; }
113cdf0e10cSrcweir 		java_lang_Object *	GetWrapper() { return this; }
114cdf0e10cSrcweir 		void clearObject(JNIEnv& rEnv);
115cdf0e10cSrcweir 		void clearObject();
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		virtual ::rtl::OUString toString() const;
118cdf0e10cSrcweir 		::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > getORB() { return m_xFactory; }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         static void ThrowSQLException(JNIEnv * pEnv,const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface> & _rContext);
121cdf0e10cSrcweir         static void ThrowLoggedSQLException(
122cdf0e10cSrcweir             const ::comphelper::ResourceBasedEventLogger& _rLogger,
123cdf0e10cSrcweir             JNIEnv* pEnvironment,
124cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext
125cdf0e10cSrcweir         );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 		static ::rtl::Reference< jvmaccess::VirtualMachine > getVM(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxFactory=NULL);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         static jclass   findMyClass(const char* _pClassName);
130cdf0e10cSrcweir         void            obtainMethodId(JNIEnv* _pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID) const;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         sal_Bool        callBooleanMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
133cdf0e10cSrcweir         sal_Bool        callBooleanMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
134cdf0e10cSrcweir         jobject         callResultSetMethod( JNIEnv& _rEnv, const char* _pMethodName, jmethodID& _inout_MethodID ) const;
135cdf0e10cSrcweir         sal_Int32       callIntMethod( const char* _pMethodName, jmethodID& _inout_MethodID,bool _bIgnoreException = false ) const;
136cdf0e10cSrcweir         sal_Int32       callIntMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument ) const;
137cdf0e10cSrcweir         sal_Int32       callIntMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID,const ::rtl::OUString& _nArgument ) const;
138cdf0e10cSrcweir         ::rtl::OUString callStringMethod( const char* _pMethodName, jmethodID& _inout_MethodID ) const;
139cdf0e10cSrcweir         ::rtl::OUString callStringMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
140cdf0e10cSrcweir         void            callVoidMethod( const char* _pMethodName, jmethodID& _inout_MethodID) const;
141cdf0e10cSrcweir         void            callVoidMethodWithIntArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
142cdf0e10cSrcweir         void            callVoidMethodWithBoolArg( const char* _pMethodName, jmethodID& _inout_MethodID, sal_Int32 _nArgument,bool _bIgnoreException = false ) const;
143cdf0e10cSrcweir         void            callVoidMethodWithStringArg( const char* _pMethodName, jmethodID& _inout_MethodID, const ::rtl::OUString& _nArgument ) const;
144cdf0e10cSrcweir         jobject         callObjectMethod( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID ) const;
145cdf0e10cSrcweir         jobject         callObjectMethodWithIntArg( JNIEnv * pEnv, const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         template< typename T >
148cdf0e10cSrcweir                         T callMethodWithIntArg(T (JNIEnv::*pCallMethod)( jobject obj, jmethodID methodID, ... ) ,const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID , sal_Int32 _nArgument) const
149cdf0e10cSrcweir         {
150cdf0e10cSrcweir             SDBThreadAttach t;
151cdf0e10cSrcweir             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
152cdf0e10cSrcweir             T out = (t.pEnv->*pCallMethod)( object, _inout_MethodID,_nArgument);
153cdf0e10cSrcweir 			ThrowSQLException( t.pEnv, NULL );
154cdf0e10cSrcweir             return out;
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir         template< typename T >
158cdf0e10cSrcweir                         void callVoidMethod(const char* _pMethodName, const char* _pSignature, jmethodID& _inout_MethodID,sal_Int32 _nArgument, const T& _aValue) const
159cdf0e10cSrcweir         {
160cdf0e10cSrcweir             SDBThreadAttach t;
161cdf0e10cSrcweir             obtainMethodId(t.pEnv, _pMethodName,_pSignature, _inout_MethodID);
162cdf0e10cSrcweir             t.pEnv->CallVoidMethod( object, _inout_MethodID,_nArgument,_aValue);
163cdf0e10cSrcweir 			ThrowSQLException( t.pEnv, NULL );
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	};
168cdf0e10cSrcweir }
169cdf0e10cSrcweir #endif //_CONNECTIVITY_JAVA_LANG_OBJJECT_HXX_
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
172