Top Description Fields Constructors Methods
com.badlogic.gdx.jnigen

public Class NativeCodeGenerator

extends Object
Class Inheritance
Imports
java.io.File, .InputStream, java.nio.Buffer, java.nio.file.Files, .Path, java.util.ArrayList, .Comparator, .List, com.badlogic.gdx.jnigen.parsing.CMethodParser, .CMethodParser.CMethod, .CMethodParser.CMethodParserResult, .JavaMethodParser, .JavaMethodParser.Argument, .JavaMethodParser.JavaMethod, .JavaMethodParser.JavaSegment, .JavaMethodParser.JniSection, .JniHeaderCMethodParser, .RobustJavaMethodParser

Goes through a Java source directory, checks each .java file for native methods and emits C/C++ code accordingly, both .h and .cpp files.

Augmenting Java Files with C/C++

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.

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
If you need control over setting up and cleaning up arrays/strings and direct buffers you can tell the NativeCodeGenerator to omit setup and cleanup code by starting the native code block comment with "/*MANUAL" instead of just "/*" to the method name. See libgdx's Gdx2DPixmap load() method for an example.

.h/.cpp File Generation

The .h files are created via javac, which has to be on your path. The Java classes have to be compiled and accessible to the javac tool. The name of the generated .h/.cpp files is the name of the class generated by javac, e.g. com_badlogic_jnigen_MyJniClass.h/.cpp. The generator takes the following parameters as input: A default invocation of the generator looks like this:
new NativeCodeGenerator().generate("src", "bin", "jni");

To automatically compile and load the native code, see the classes AntScriptGenerator, BuildExecutor and SharedLibraryLoader classes.

Author
mzechner

Field Summary

Modifier and TypeField and Description
pack-priv String
pack-priv CMethodParser
pack-priv CMethodParser.CMethodParserResult
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

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
private void
private void
private void
private void
emitJniSetupCode(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, StringBuffer additionalArgs, StringBuffer wrapperArgs)

private void
emitLineMarker(StringBuffer buffer, int line)

protected void
private String
emitMethodSignature(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, CMethodParser.CMethod cMethod, String additionalArguments)

private String
emitMethodSignature(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, CMethodParser.CMethod cMethod, String additionalArguments, boolean appendPrefix)

private CMethodParser.CMethod
public void
generate()

Generates .h/.cpp files from the Java files found in "src/", with their .class files being in "bin/".

public void
generate(String
the directory containing the Java files
sourceDir
,
String
the directory containing the .class files
classpath
,
String
the output directory
jniDir
)

Generates .h/.cpp fiels from the Java files found in sourceDir, with their .class files being in classpath.

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 sourceDir, with their .class files being in classpath.

private void
private void
private String
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

classpathback to summary
pack-priv String classpath
cMethodParserback to summary
pack-priv CMethodParser cMethodParser
cResultback to summary
pack-priv CMethodParser.CMethodParserResult cResult
excludesback to summary
pack-priv String[] excludes
includesback to summary
pack-priv String[] includes
javaMethodParserback to summary
pack-priv JavaMethodParser javaMethodParser
JNI_ARG_PREFIXback to summary
private static final String JNI_ARG_PREFIX
JNI_METHOD_MARKERback to summary
private static final String JNI_METHOD_MARKER
JNI_RETURN_VALUEback to summary
private static final String JNI_RETURN_VALUE
JNI_WRAPPER_PREFIXback to summary
private static final String JNI_WRAPPER_PREFIX
jniDirback to summary
pack-priv FileDescriptor jniDir
matcherback to summary
pack-priv AntPathMatcher matcher
sourceDirback to summary
pack-priv FileDescriptor sourceDir

Constructor Detail

NativeCodeGeneratorback to summary
public NativeCodeGenerator()

Method Detail

emitHeaderIncludeback to summary
protected void emitHeaderInclude(StringBuffer buffer, String fileName)
emitJavaMethodback to summary
private void emitJavaMethod(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, CMethodParser.CMethod cMethod)
emitJniCleanupCodeback to summary
private void emitJniCleanupCode(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, CMethodParser.CMethod cMethod)
emitJniSectionback to summary
private void emitJniSection(StringBuffer buffer, JavaMethodParser.JniSection section)
emitJniSetupCodeback to summary
private void emitJniSetupCode(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, StringBuffer additionalArgs, StringBuffer wrapperArgs)
emitLineMarkerback to summary
private void emitLineMarker(StringBuffer buffer, int line)
emitMethodBodyback to summary
protected void emitMethodBody(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod)
emitMethodSignatureback to summary
private String emitMethodSignature(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, CMethodParser.CMethod cMethod, String additionalArguments)
emitMethodSignatureback to summary
private String emitMethodSignature(StringBuffer buffer, JavaMethodParser.JavaMethod javaMethod, CMethodParser.CMethod cMethod, String additionalArguments, boolean appendPrefix)
findCMethodback to summary
private CMethodParser.CMethod findCMethod(JavaMethodParser.JavaMethod javaMethod, ArrayList<CMethodParser.CMethod> cMethods)
generateback 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.

generateback to summary
public void generate(String sourceDir, String classpath, String jniDir) throws Exception

Generates .h/.cpp fiels from the Java files found in 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.

Parameters
sourceDir:String

the directory containing the Java files

classpath:String

the directory containing the .class files

jniDir:String

the output directory

generateback 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 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.

Parameters
sourceDir:String

the directory containing the Java files

classpath:String

the directory containing the .class files

jniDir:String

the output directory

includes:String[]

files/directories to include, can be null (all files are used)

excludes:String[]

files/directories to exclude, can be null (no files are excluded)

generateCppFileback to summary
private void generateCppFile(ArrayList<JavaMethodParser.JavaSegment> javaSegments, List<FileDescriptor> hFiles, FileDescriptor cppFile) throws Exception
generateHFilesback to summary
private void generateHFiles(FileDescriptor file) throws Exception
getNativeClassFileNameback to summary
private String getNativeClassFileName(FileDescriptor file)
processDirectoryback to summary
private void processDirectory(FileDescriptor dir) throws Exception