1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #if ! defined INCLUDED_JNI_HELPER_H
29 #define INCLUDED_JNI_HELPER_H
30 
31 #include "jni_base.h"
32 #include "jni_info.h"
33 
34 
35 namespace jni_uno
36 {
37 
38 //------------------------------------------------------------------------------
39 inline void jstring_to_ustring(
40     JNI_context const & jni, rtl_uString ** out_ustr, jstring jstr )
41 {
42     if (0 == jstr)
43     {
44         rtl_uString_new( out_ustr );
45     }
46     else
47     {
48         jsize len = jni->GetStringLength( jstr );
49         ::std::auto_ptr< rtl_mem > mem(
50             rtl_mem::allocate(
51                 sizeof (rtl_uString) + (len * sizeof (sal_Unicode)) ) );
52         rtl_uString * ustr = (rtl_uString *)mem.get();
53         jni->GetStringRegion( jstr, 0, len, (jchar *) ustr->buffer );
54         jni.ensure_no_exception();
55         ustr->refCount = 1;
56         ustr->length = len;
57         ustr->buffer[ len ] = '\0';
58         mem.release();
59         if (0 != *out_ustr)
60             rtl_uString_release( *out_ustr );
61         *out_ustr = ustr;
62     }
63 }
64 
65 //------------------------------------------------------------------------------
66 inline ::rtl::OUString jstring_to_oustring(
67     JNI_context const & jni, jstring jstr )
68 {
69     rtl_uString * ustr = 0;
70     jstring_to_ustring( jni, &ustr, jstr );
71     return ::rtl::OUString( ustr, SAL_NO_ACQUIRE );
72 }
73 
74 //------------------------------------------------------------------------------
75 inline jstring ustring_to_jstring(
76     JNI_context const & jni, rtl_uString const * ustr )
77 {
78     jstring jstr = jni->NewString( (jchar const *) ustr->buffer, ustr->length );
79     jni.ensure_no_exception();
80     return jstr;
81 }
82 
83 
84 //------------------------------------------------------------------------------
85 // if inException, does not handle exceptions, in which case returned value will
86 // be null if exception occurred:
87 inline jclass find_class(
88     JNI_context const & jni, char const * class_name, bool inException = false )
89 {
90     // find_class may be called before the JNI_info is set:
91     jclass c=0;
92     jmethodID m;
93     JNI_info const * info = jni.get_info();
94     if (info == 0) {
95         jni.getClassForName(&c, &m);
96         if (c == 0) {
97             if (inException) {
98                 return 0;
99             }
100             jni.ensure_no_exception();
101         }
102     } else {
103         c = info->m_class_Class;
104         m = info->m_method_Class_forName;
105     }
106     return jni.findClass(class_name, c, m, inException);
107 }
108 
109 
110 //------------------------------------------------------------------------------
111 inline jobject create_type( JNI_context const & jni, jclass clazz )
112 {
113     JNI_info const * jni_info = jni.get_info();
114     jvalue arg;
115     arg.l = clazz;
116     jobject jo_type = jni->NewObjectA(
117         jni_info->m_class_Type, jni_info->m_ctor_Type_with_Class, &arg );
118     jni.ensure_no_exception();
119     return jo_type;
120 }
121 
122 //------------------------------------------------------------------------------
123 inline jobject create_type(
124     JNI_context const & jni, typelib_TypeDescriptionReference * type )
125 {
126     JNI_info const * jni_info = jni.get_info();
127     jvalue args[ 2 ];
128     // get type class
129     args[ 0 ].i = type->eTypeClass;
130     JLocalAutoRef jo_type_class(
131         jni, jni->CallStaticObjectMethodA(
132             jni_info->m_class_TypeClass,
133             jni_info->m_method_TypeClass_fromInt, args ) );
134     jni.ensure_no_exception();
135     // construct type
136     JLocalAutoRef jo_type_name(
137         jni, ustring_to_jstring( jni, type->pTypeName ) );
138     args[ 0 ].l = jo_type_name.get();
139     args[ 1 ].l = jo_type_class.get();
140     jobject jo_type = jni->NewObjectA(
141         jni_info->m_class_Type,
142         jni_info->m_ctor_Type_with_Name_TypeClass, args );
143     jni.ensure_no_exception();
144     return jo_type;
145 }
146 
147 //------------------------------------------------------------------------------
148 inline jobject compute_oid( JNI_context const & jni, jobject jo )
149 {
150     JNI_info const * jni_info = jni.get_info();
151     jvalue arg;
152     arg.l= jo;
153     jobject jo_oid = jni->CallStaticObjectMethodA(
154         jni_info->m_class_UnoRuntime,
155         jni_info->m_method_UnoRuntime_generateOid, &arg );
156     jni.ensure_no_exception();
157     return jo_oid;
158 }
159 
160 }
161 
162 #endif
163