|  | Home · Overviews · Examples |

# The Qt Jambi Generator

The generator is a Qt application which can be used to map C++ based APIs onto equivalent Java APIs, enabling C++ programmers to easily integrate their own Qt code with Java.

The generator supports a selected subset of C++, covering the most common constructs in Qt. It creates the Java Api by parsing the C++ header files and generating Java source files. It also generates code to tie the Java classes to the C++ classes. Based on the Java Native Interface (JNI), this code ensures that method calls made in Java are redirected to the corresponding functions in the C++ library.

The Qt Jambi generator is a command line tool accepting a header file and a type system specification as arguments:

        ./generator [options] header-file typesystem-file

The header file should include the relevant modules of the C++ based library. The type system specification is a handwritten XML document listing the types that will be made available in the generated Java API (see the type system documentation for details).

See also: Qt Jambi Generator Example

Command Line Options
--debug-level=sparse | medium | full | Specifies the debug level of the generator which, for example, is useful to understand how the type system is loaded.
--dump-object-tree | Dumps the parsed object tree as plain text which can be helpful when debugging the type system.
--juic-file | Specifies the location of the information file that is used together with Qt Jambi's implementation of UIC (`juic`) to generate code for custom libraries.
--convert-to-jui=[uifile] | Converts the given `.ui` file to a Java user interface file (`.jui`). The `.jui` file format's datastructure is based on Java syntax.
--no-cpp-h | The generator will not generate any C++ header files.
--no-cpp-impl | The generator will not generate any C++ source files.
--no-java | The generator will not generate any Java source files.
--no-metainfo | The generator will not generate any meta-info files.
--no-suppress-warnings | All warnings are shown even if they are suppressed by the type system specification (see the type system documentation for details).
--output-directory | Specifies the output directory for the generated code.

When running the generator, the header files are preprocessed (i.e., all macros and type definitions are expanded). Then enums, namespaces and classes are mapped according to the type system specification. For each C++ class that is encountered, the generator creates a Java source file and a set of C++ implementation files.

Warning: The Qt Jambi generator is written to handle Qt- based source code, and is not intended for mapping C++ libraries in general.

- The Java Source File
- The C++ Implementation Files

## The Java Source File

The Java source file contains one public class with the same name as the original C++ class.

All public and protected members of the C++ class are included in the Java class. For each C++ function, the generator creates a native Java method, and each original member variable generates a set and get method pair since JNI only provides access to native resources through methods. For example, the C++ member variable:

        QString text;

generates

        String text();
        void setText(String text);

in the Java API.

Using the type system specification, it is also possible to rename or remove functions when generating the Java API, as well as changing the access privileges. It is even possible to use the type system to inject arbitrary code into the Java source file, such as an extra member method.

## The C++ Implementation Files

The C++ source file contain two different parts: a shell class and the implementation of the functions declared in the Java source file.

The shell class inherits the original class in the C++ based Qt library, and makes it possible to reimplement virtual functions in Java and to call protected functions in the C++ based library from Java. Whenever an instance of a class is constructed in Java, a corresponding object of the shell class is constructed. If a class has been extended by a user's custom Java class, and one or more of the virtual functions in the class have been reimplemented, the shell class ensures that it is the reimplemented Java implementations that are called.

As with the generated Java source file, it is possible to inject code in the reimplemented virtual functions in the shell classes using the type system specification.

The C++ header file is primarily an implementation detail and can in most cases be ignored.