public class NativeCodeGenerator extends Object
package com.badlogic.jnigen; public class MyJniClass { /*JNI #include <math.h> */ public native void addToArray(float[] array, int len, float value); /* for(int i = 0; i < len; i++) { array[i] = value; } */ }The generated header file is automatically included in the .cpp file. Methods and custom JNI code can be mixed throughout the Java file, their order is preserved in the generated .cpp file. Method overloading is supported but not recommended as the overloading detection is very basic. If a native method has strings, one dimensional primitive arrays or direct
Buffer
instances as arguments, JNI setup and
cleanup code is automatically generated.
The following list gives the mapping from Java to C/C++ types for arguments:
Java | C/C++ |
String | char* (UTF-8) |
boolean[] | bool* |
byte[] | char* |
char[] | unsigned short* |
short[] | short* |
int[] | int* |
long[] | long long* |
float[] | float* |
double[] | double* |
Buffer | unsigned char* |
ByteBuffer | char* |
CharBuffer | unsigned short* |
ShortBuffer | short* |
IntBuffer | int* |
LongBuffer | long long* |
FloatBuffer | float* |
DoubleBuffer | double* |
Anything else | jobject/jobjectArray |
new NativeCodeGenerator().generate("src", "bin", "jni");To automatically compile and load the native code, see the classes
AntScriptGenerator
, BuildExecutor
and
JniGenSharedLibraryLoader
classes. Constructor and Description |
---|
NativeCodeGenerator() |
Modifier and Type | Method and Description |
---|---|
void |
generate()
Generates .h/.cpp files from the Java files found in "src/", with their .class files being in "bin/".
|
void |
generate(String sourceDir,
String classpath,
String jniDir)
Generates .h/.cpp fiels from the Java files found in
sourceDir , with their .class files being in
classpath . |
void |
generate(String sourceDir,
String classpath,
String jniDir,
String[] includes,
String[] excludes)
Generates .h/.cpp fiels from the Java files found in
sourceDir , with their .class files being in
classpath . |
public void generate() throws Exception
Exception
public void generate(String sourceDir, String classpath, String jniDir) throws Exception
sourceDir
, with their .class files being in
classpath
. The generated files will be stored in jniDir
. All paths are relative to the
applications working directory.sourceDir
- the directory containing the Java filesclasspath
- the directory containing the .class filesjniDir
- the output directoryException
public void generate(String sourceDir, String classpath, String jniDir, String[] includes, String[] excludes) throws Exception
sourceDir
, with their .class files being in
classpath
. The generated files will be stored in jniDir
. The includes
and
excludes
parameters allow to specify directories and files that should be included/excluded from the
generation. These can be given in the Ant path format. All paths are relative to the applications working directory.sourceDir
- the directory containing the Java filesclasspath
- the directory containing the .class filesjniDir
- the output directoryincludes
- files/directories to include, can be null (all files are used)excludes
- files/directories to exclude, can be null (no files are excluded)Exception
Copyright © 2015. All rights reserved.