Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
jni.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2024 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\namespace QtJniTypes
6
\inmodule QtCore
7
\ingroup frameworks-technologies
8
\keyword JNI
9
\since 6.8
10
11
\brief The QtJniTypes namespace declares C++ types that correspond to Java types.
12
13
The \l{Java Native Interface Specification}{Java Native Interface}
14
framework enables native C or C++ code to call Java APIs, and to register
15
native C or C++ functions that Java code should be able to call with the
16
JVM. In Qt, the QJniObject and QJniEnvironment types provide convenient
17
wrappers for using JNI.
18
19
Since the Java language, similar to C++, supports overloading, functions
20
need to be specified including their entire signature string. This is
21
complex, repetitive, and error prone, especially when functions take
22
several parameters. QJniObject provides variadic template APIs that can
23
deduce the signature string for a function call at compile time, based on
24
the C++ types passed into the template. For this to work, the mapping of
25
those C++ types to their corresponding JNI string needs to be known at
26
compile time.
27
28
Qt implements this mapping for the standard \l {JNI types}.
29
By using the Q_DECLARE_JNI_CLASS macro, the mapping can be extended for
30
arbitrary Java types.
31
32
\sa Q_DECLARE_JNI_CLASS, Q_DECLARE_JNI_NATIVE_METHOD, Q_JNI_NATIVE_METHOD
33
*/
34
35
/*!
36
\macro Q_DECLARE_JNI_TYPE(Type, JavaSignature)
37
\internal
38
\relates QtJniTypes
39
\since 6.8
40
41
Declares a C++ type \a Type in the QtJniTypes namespace that wraps the Java
42
type \a JavaSignature. The signature needs to start with \c{[L} to indicate
43
an array of a class, or with \c{L} for classes, and end with a semicolon
44
\c{;}.
45
46
\code
47
Q_DECLARE_JNI_TYPE(StringArray, "[Ljava/lang/String;")
48
\endcode
49
50
\sa Q_DECLARE_JNI_CLASS
51
*/
52
53
/*!
54
\macro Q_DECLARE_JNI_CLASS(Type, JavaSignature)
55
\relates QtJniTypes
56
\since 6.8
57
58
Declares a C++ type \a Type in the QtJniTypes namespace that wraps the Java
59
class \a JavaSignature. The Java class name in \a JavaSignature needs to be
60
fully qualified, using \c{/} as the separator.
61
62
\code
63
Q_DECLARE_JNI_CLASS(File, "java/io/File")
64
Q_DECLARE_JNI_CLASS(FileWriter, "java/io/FileWriter")
65
\endcode
66
67
The C++ classes \c{QtJniTypes::File} and \c{QtJniTypes::FileWriter} are
68
then QJniObject-like types (specializations of QtJniTypes::JObject, to be
69
precise) that can be used to instantiate the corresponding Java class, to
70
call methods, and to pass such instances through QJniObject variadic
71
template methods with automatic, compile-time signature deduction.
72
73
\code
74
using namespace QtJniTypes;
75
76
File file("path/to/file"); // instantiates the java.io.File type in Java
77
if (file.callMethod<bool>("createNewFile")) {
78
FileWriter writer(file); // instantiates a java.io.FileWriter that operates on file
79
writer.callMethod("write", 42);
80
}
81
\endcode
82
83
In addition to the QJniObject API, those C++ classes also have a static
84
\c{registerNativeMethods} member function that can be used like this:
85
86
\code
87
QtJniTypes::File::registerNativeMethods({
88
Q_JNI_NATIVE_METHOD(freeFunction)
89
});
90
\endcode
91
92
\sa Q_DECLARE_JNI_NATIVE_METHOD, Q_JNI_NATIVE_METHOD, QtJniTypes::JObject
93
*/
94
95
/*!
96
\macro Q_DECLARE_JNI_NATIVE_METHOD(Method)
97
\relates QtJniTypes
98
\since 6.8
99
100
Declares the free C or C++ function \a Method as a native method. The
101
method can later be registered with the JNI framework using
102
QJniEnvironment::registerNativeMethod() with the help of the
103
Q_JNI_NATIVE_METHOD macro.
104
105
//! [register-free-function]
106
\code
107
// C++ side
108
109
Q_DECLARE_JNI_CLASS(MyJavaType, "my/java/Type")
110
111
static void nativeFunction(JNIEnv *env, jobject thiz, jlong id)
112
{
113
// ...
114
}
115
Q_DECLARE_JNI_NATIVE_METHOD(nativeFunction)
116
117
Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
118
{
119
QJniEnvironment env;
120
env.registerNativeMethods<QtJniTypes::MyJavaType>({
121
Q_JNI_NATIVE_METHOD(nativeFunction)
122
});
123
}
124
125
// Java side
126
public class MyJavaType
127
{
128
native public nativeFunction(long id);
129
}
130
\endcode
131
//! [register-free-function]
132
133
//! [native-qt-types]
134
Since 6.13, the function may take and return Qt types rather than JNI
135
types. Arguments and the return value are converted, and the Java
136
signature is deduced from the C++ types.
137
138
\code
139
Q_DECLARE_JNI_CLASS(Rect, "android/graphics/Rect")
140
141
static QtJniTypes::Rect boundsOf(JNIEnv *, jobject, jint id)
142
{
143
return QJniObject::construct<QtJniTypes::Rect>(0, 0, 10, 10);
144
}
145
Q_DECLARE_JNI_NATIVE_METHOD(boundsOf)
146
\endcode
147
148
\c boundsOf registers as \c{(I)Landroid/graphics/Rect;}. QString and
149
QJniArray work the same way. Returning an object that Qt owns a reference
150
to is safe, because the reference handed back is one that Java owns. The
151
Qt object may go out of scope as usual.
152
//! [native-qt-types]
153
154
\sa Q_JNI_NATIVE_METHOD, Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE
155
*/
156
157
/*!
158
\macro Q_DECLARE_JNI_NATIVE_METHOD(Method, JavaName)
159
\relates QtJniTypes
160
\overload
161
\since 6.8
162
163
Declares the free C or C++ function \a Method as a native method that's
164
available in Java as \a JavaName. The method can later be registered with
165
the JNI framework using QJniEnvironment::registerNativeMethod() with the
166
help of the Q_JNI_NATIVE_METHOD macro.
167
168
\sa Q_JNI_NATIVE_METHOD, Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE
169
*/
170
171
/*!
172
\macro Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(Method)
173
\relates QtJniTypes
174
\since 6.8
175
176
Declares the C++ static class member function \a Method as a native method.
177
The method can later be registered with the JNI framework using
178
QJniEnvironment::registerNativeMethod() with the help of the
179
Q_JNI_NATIVE_SCOPED_METHOD macro.
180
181
//! [register-scoped-function]
182
\code
183
class NativeHandler
184
{
185
// ...
186
private:
187
static void handleChange(JNIEnv*, jobject, jlong id);
188
Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(handleChange)
189
};
190
191
\dots
192
QJniEnvironment env;
193
env.registerNativeMethods<QtJniTypes::MyJavaType>({
194
Q_JNI_NATIVE_SCOPED_METHOD(handleChange, NativeHandler)
195
});
196
\endcode
197
//! [register-scoped-function]
198
199
\sa Q_DECLARE_JNI_NATIVE_METHOD, Q_JNI_NATIVE_SCOPED_METHOD
200
*/
201
202
/*!
203
\macro Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(Method, JavaName)
204
\relates QtJniTypes
205
\overload
206
\since 6.8
207
208
Declares the C++ static class member function \a Method as a native method
209
that's available in Java as \a JavaName. The method can later be registered
210
with the JNI framework using QJniEnvironment::registerNativeMethod() with
211
the help of the Q_JNI_NATIVE_METHOD macro.
212
213
\sa Q_DECLARE_JNI_NATIVE_METHOD, Q_JNI_NATIVE_SCOPED_METHOD
214
*/
215
216
/*!
217
\macro Q_JNI_NATIVE_METHOD(Method)
218
\relates QtJniTypes
219
\since 6.8
220
221
Makes the previously \l{Q_DECLARE_JNI_NATIVE_METHOD}{declared} native
222
method \a Method available for registration with the JNI framework. Use
223
this macro when registering the method with JNI using
224
QJniEnvironment::registerNativeMethod().
225
226
\code
227
Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
228
{
229
QJniEnvironment env;
230
env.registerNativeMethods<QtJniTypes::MyJavaType>({
231
Q_JNI_NATIVE_METHOD(nativeFunction)
232
});
233
}
234
\endcode
235
236
\sa Q_DECLARE_JNI_NATIVE_METHOD
237
*/
238
239
/*!
240
\macro Q_JNI_NATIVE_SCOPED_METHOD(Method, Scope)
241
\relates QtJniTypes
242
\since 6.8
243
244
Makes the previously
245
\l{Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE}{declared} native method
246
\a Method in scope \a Scope available for registration with the JNI framework.
247
Use this macro when registering the method with JNI using
248
QJniEnvironment::registerNativeMethod().
249
250
\sa Q_JNI_NATIVE_METHOD, Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE
251
*/
qtbase
src
corelib
doc
src
jni.qdoc
Generated on
for Qt by
1.18.0