Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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 that can be used to instantiate the
69 corresponding Java class, to call methods, and to pass such instances
70 through QJniObject variadic template methods with automatic, compile-time
71 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
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 \sa Q_JNI_NATIVE_METHOD, Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE
134*/
135
136/*!
137 \macro Q_DECLARE_JNI_NATIVE_METHOD(Method, JavaName)
138 \relates QtJniTypes
139 \overload
140 \since 6.8
141
142 Declares the free C or C++ function \a Method as a native method that's
143 available in Java as \a JavaName. The method can later be registered with
144 the JNI framework using QJniEnvironment::registerNativeMethod() with the
145 help of the Q_JNI_NATIVE_METHOD macro.
146
147 \sa Q_JNI_NATIVE_METHOD, Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE
148*/
149
150/*!
151 \macro Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(Method)
152 \relates QtJniTypes
153 \since 6.8
154
155 Declares the C++ static class member function \a Method as a native method.
156 The method can later be registered with the JNI framework using
157 QJniEnvironment::registerNativeMethod() with the help of the
158 Q_JNI_NATIVE_SCOPED_METHOD macro.
159
160//! [register-scoped-function]
161 \code
162 class NativeHandler
163 {
164 // ...
165 private:
166 static void handleChange(JNIEnv*, jobject, jlong id);
167 Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(handleChange)
168 };
169
170 \dots
171 QJniEnvironment env;
172 env.registerNativeMethods<QtJniTypes::MyJavaType>({
173 Q_JNI_NATIVE_SCOPED_METHOD(handleChange, NativeHandler)
174 });
175 \endcode
176//! [register-scoped-function]
177
178 \sa Q_DECLARE_JNI_NATIVE_METHOD, Q_JNI_NATIVE_SCOPED_METHOD
179*/
180
181/*!
182 \macro Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(Method, JavaName)
183 \relates QtJniTypes
184 \overload
185 \since 6.8
186
187 Declares the C++ static class member function \a Method as a native method
188 that's available in Java as \a JavaName. The method can later be registered
189 with the JNI framework using QJniEnvironment::registerNativeMethod() with
190 the help of the Q_JNI_NATIVE_METHOD macro.
191
192 \sa Q_DECLARE_JNI_NATIVE_METHOD, Q_JNI_NATIVE_SCOPED_METHOD
193*/
194
195/*!
196 \macro Q_JNI_NATIVE_METHOD(Method)
197 \relates QtJniTypes
198 \since 6.8
199
200 Makes the previously \l{Q_DECLARE_JNI_NATIVE_METHOD}{declared} native
201 method \a Method available for registration with the JNI framework. Use
202 this macro when registering the method with JNI using
203 QJniEnvironment::registerNativeMethod().
204
205 \code
206 Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
207 {
208 QJniEnvironment env;
209 env.registerNativeMethods<QtJniTypes::MyJavaType>({
210 Q_JNI_NATIVE_METHOD(nativeFunction)
211 });
212 }
213 \endcode
214
215 \sa Q_DECLARE_JNI_NATIVE_METHOD
216*/
217
218/*!
219 \macro Q_JNI_NATIVE_SCOPED_METHOD(Method, Scope)
220 \relates QtJniTypes
221 \since 6.8
222
223 Makes the previously
224 \l{Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE}{declared} native method
225 \a Method in scope \a Scope available for registration with the JNI framework.
226 Use this macro when registering the method with JNI using
227 QJniEnvironment::registerNativeMethod().
228
229 \sa Q_JNI_NATIVE_METHOD, Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE
230*/