Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java binding using the existing SWIG infrastructure
CMakeLists.txt: Add NLOPT_JAVA option. If set, check for C++, JNI, and Java (>= 1.5, because of varargs and enums). src/swig/nlopt-java.i: New SWIG input file (not standalone, but to be included into nlopt.i). Contains the Java-specific declarations. src/swig/nlopt.i: Add syntax highlighting declaration for KatePart-based editors. Call the module NLopt instead of nlopt for SWIGJAVA, because in Java, this is the name of the globals class (not the package, which is set separately on the SWIG command line), so it should be in CamelCase. Instantiate vector<double> as DoubleVector instead of nlopt_doublevector for SWIGJAVA, because everything is in an nlopt package in Java, so the nlopt_ part is redundant, and because it should be in CamelCase. Ignore nlopt_get_initial_step because at least for Java, SWIG wants to generate a binding for it, using SWIGTYPE_p_* types that cannot be practically used. (It is in-place just like nlopt::opt::get_initial_step, which is already ignored.) Rename nlopt::opt::get_initial_step_ to the lowerCamelCase getInitialStep instead of get_initial_step. For SWIGJAVA, %include nlopt-java.i. src/swig/CMakeLists.txt: Set UseSWIG_MODULE_VERSION to 2 so that UseSWIG cleans up old generated source files before running SWIG, useful for Java. (In particular, it prevents obsolete .java files from old .i file versions, which might not even compile anymore, from being included in the Java compilation and the JAR.) Update the path handling for the Python and Guile bindings accordingly. If JNI, Java, and SWIG were found, generate the Java binding with SWIG, and compile the JNI library with the C++ compiler and the Java JAR with the Java compiler. For the Guile binding, set and unset CMAKE_SWIG_FLAGS instead of using set_source_files_properties on nlopt.i, because nlopt.i is shared for all bindings, so setting the property on the source file does not help preventing breaking other target language bindings (such as Java). src/swig/glob_java.cmake: New helper script whose main purpose is to invoke the CMake file(GLOB ...) command at the correct stage of the build process, after the SWIG run, not when CMake is run on the main CMakeLists.txt, because the latter happens before anything at all is built. The script is invoked through cmake -P by an add_custom_command in CMakeLists.txt, whose dependencies order it to the correct spot of the build. This is the only portable way to automatically determine which *.java files SWIG has generated from the *.i files. The result is written to java_sources.txt, which is dynamically read by the add_jar command thanks to the @ indirection. In addition, it also does a replacement in DoubleVector.java, changing double[] initialElements to double... initialElements (introduced in Java 1.5), which needs to happen at the same stage of the build, so that initial elements can be passed directly to new DoubleVector without extra new double[]{ boilerplate. test/t_java.java: New test. Java port of t_tutorial.cxx/t_python.py. test/CMakeLists.txt: If JNI, Java >= 1.8, and SWIG were found, run the t_java test program with the algorithms 23 (MMA), 24 (COBYLA), 30 (augmented Lagrangian), and 39 (SLSQP). All 4 tests pass. (Java < 1.8 should be supported by the binding itself, but not by the test.) Update the PYINSTALLCHECK_ENVIRONMENT settings for the src/swig/CMakeLists.txt changes, so that the Python tests keep passing. Update the GUILE_LOAD_PATH settings for the src/swig/CMakeLists.txt changes, so that the Guile tests keep passing. This code is mostly unrelated to the old unmaintained nlopt4j binding (https://github.com/dibyendumajumdar/nlopt4j), which used handwritten JNI code. Instead, it reuses the existing SWIG binding infrastructure, adding Java as a supported language to that. Only the code in the func_java and mfunc_java wrappers is loosely inspired by the nlopt4j code. The rest of the code in this binding relies mostly on SWIG features and uses very little handwritten JNI code, so nlopt4j was not useful as a reference for it. This binding is also backwards-incompatible with nlopt4j due to differing naming conventions. Note that this binding maps the C++ class and method names identically to Java. As a result, it does not use idiomatic Java case conventions, but uses snake_case for both class and method names instead of the usual UpperCamelCase for class names and lowerCamelCase for method names in Java. The C++ namespace nlopt is mapped to the Java package nlopt. doc/docs/NLopt_Java_Reference.md: New file, based on NLopt_Python_Reference.md and NLopt_Guile_Reference.md, adapted to the Java syntax. doc/docs/NLopt_Tutorial.md: Add example in Java. doc/docs/index.md: Mention Java, linking to NLopt_Java_Reference.md.
- Loading branch information