Goes through a Java source directory, checks each .java file for native methods and emits C/C++ code accordingly, both .h and .cpp files.
C/C++ code can be directly added to native methods in the Java file as block comments starting at the same line as the method signature. Custom JNI code that is not associated with a native method can be added via a special block comment as shown below.
All arguments can be accessed by the name specified in the Java native method signature (unless you use $ in your identifier which is allowed in Java).
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.
| 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
SharedLibraryLoader classes.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv String | |
| pack-priv CMethodParser | |
| pack-priv CMethodParser. | |
| pack-priv String[] | |
| pack-priv String[] | |
| pack-priv JavaMethodParser | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| pack-priv FileDescriptor | |
| pack-priv AntPathMatcher | |
| pack-priv FileDescriptor |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected void | |
| private void | emitJavaMethod(StringBuffer buffer, JavaMethodParser.
|
| private void | emitJniCleanupCode(StringBuffer buffer, JavaMethodParser.
|
| private void | |
| private void | emitJniSetupCode(StringBuffer buffer, JavaMethodParser.
|
| private void | |
| protected void | |
| private String | emitMethodSignature(StringBuffer buffer, JavaMethodParser.
|
| private String | emitMethodSignature(StringBuffer buffer, JavaMethodParser.
|
| private CMethodParser. | |
| public void | generate()
Generates .h/.cpp files from the Java files found in "src/", with their .class files being in "bin/". |
| public void | |
| public void | generate(String
the directory containing the Java files sourceDir, String the directory containing the .class files classpath, String the output directory jniDir, String[] files/directories to include, can be null (all files are used) includes, String[] files/directories to exclude, can be null (no files are excluded) excludes)Generates .h/.cpp fiels from the Java files found in |
| private void | generateCppFile(ArrayList<JavaMethodParser.
|
| private void | |
| private String | |
| private void |
| classpath | back to summary |
|---|---|
| pack-priv String classpath | |
| cMethodParser | back to summary |
|---|---|
| pack-priv CMethodParser cMethodParser | |
| cResult | back to summary |
|---|---|
| pack-priv CMethodParser. | |
| excludes | back to summary |
|---|---|
| pack-priv String[] excludes | |
| includes | back to summary |
|---|---|
| pack-priv String[] includes | |
| javaMethodParser | back to summary |
|---|---|
| pack-priv JavaMethodParser javaMethodParser | |
| JNI_ARG_PREFIX | back to summary |
|---|---|
| private static final String JNI_ARG_PREFIX | |
| JNI_METHOD_MARKER | back to summary |
|---|---|
| private static final String JNI_METHOD_MARKER | |
| JNI_RETURN_VALUE | back to summary |
|---|---|
| private static final String JNI_RETURN_VALUE | |
| JNI_WRAPPER_PREFIX | back to summary |
|---|---|
| private static final String JNI_WRAPPER_PREFIX | |
| jniDir | back to summary |
|---|---|
| pack-priv FileDescriptor jniDir | |
| matcher | back to summary |
|---|---|
| pack-priv AntPathMatcher matcher | |
| sourceDir | back to summary |
|---|---|
| pack-priv FileDescriptor sourceDir | |
| NativeCodeGenerator | back to summary |
|---|---|
| public NativeCodeGenerator() | |
| emitHeaderInclude | back to summary |
|---|---|
| protected void emitHeaderInclude(StringBuffer buffer, String fileName) | |
| emitJavaMethod | back to summary |
|---|---|
| private void emitJavaMethod(StringBuffer buffer, JavaMethodParser. | |
| emitJniCleanupCode | back to summary |
|---|---|
| private void emitJniCleanupCode(StringBuffer buffer, JavaMethodParser. | |
| emitJniSection | back to summary |
|---|---|
| private void emitJniSection(StringBuffer buffer, JavaMethodParser. | |
| emitJniSetupCode | back to summary |
|---|---|
| private void emitJniSetupCode(StringBuffer buffer, JavaMethodParser. | |
| emitLineMarker | back to summary |
|---|---|
| private void emitLineMarker(StringBuffer buffer, int line) | |
| emitMethodBody | back to summary |
|---|---|
| protected void emitMethodBody(StringBuffer buffer, JavaMethodParser. | |
| emitMethodSignature | back to summary |
|---|---|
| private String emitMethodSignature(StringBuffer buffer, JavaMethodParser. | |
| emitMethodSignature | back to summary |
|---|---|
| private String emitMethodSignature(StringBuffer buffer, JavaMethodParser. | |
| findCMethod | back to summary |
|---|---|
| private CMethodParser. | |
| generate | back to summary |
|---|---|
| public void generate() throws Exception Generates .h/.cpp files from the Java files found in "src/", with their .class files being in "bin/". The generated files will be stored in "jni/". All paths are relative to the applications working directory. | |
| generate | back to summary |
|---|---|
| public void generate(String sourceDir, String classpath, String jniDir) throws Exception Generates .h/.cpp fiels from the Java files found in | |
| generate | back to summary |
|---|---|
| public void generate(String sourceDir, String classpath, String jniDir, String[] includes, String[] excludes) throws Exception Generates .h/.cpp fiels from the Java files found in
| |
| generateCppFile | back to summary |
|---|---|
| private void generateCppFile(ArrayList<JavaMethodParser. | |
| generateHFiles | back to summary |
|---|---|
| private void generateHFiles(FileDescriptor file) throws Exception | |
| getNativeClassFileName | back to summary |
|---|---|
| private String getNativeClassFileName(FileDescriptor file) | |
| processDirectory | back to summary |
|---|---|
| private void processDirectory(FileDescriptor dir) throws Exception | |