Add Java bindings (#18)
This commit is contained in:
committed by
Malte Langkabel
parent
2ffb5b2af0
commit
28e8cb8f33
@@ -0,0 +1,51 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
|
||||
# --- Find Java ---
|
||||
|
||||
find_package(Java REQUIRED COMPONENTS Runtime Development)
|
||||
include(UseJava)
|
||||
|
||||
set(EXAMPLE_NAME "java_api_example")
|
||||
|
||||
# --- Configure Sourcetrail Project File ---
|
||||
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/za/co/spazzymoto/example.java ${CMAKE_CURRENT_BINARY_DIR}/za/co/spazzymoto/example.java COPYONLY)
|
||||
|
||||
add_custom_target(
|
||||
CopyJar ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${JAVA_BINDING_OUTPUT_DIR}/SourcetrailDB.jar ${CMAKE_CURRENT_BINARY_DIR}/SourcetrailDB.jar
|
||||
)
|
||||
|
||||
add_jar(
|
||||
${EXAMPLE_NAME}
|
||||
SOURCES
|
||||
${CMAKE_CURRENT_BINARY_DIR}/za/co/spazzymoto/example.java
|
||||
INCLUDE_JARS
|
||||
${CMAKE_CURRENT_BINARY_DIR}/SourcetrailDB.jar
|
||||
ENTRY_POINT za.co.spazzymoto.example
|
||||
)
|
||||
|
||||
add_dependencies( ${EXAMPLE_NAME} CopyJar )
|
||||
add_dependencies( CopyJar ${JAVA_BINDING_TARGET_NAME} )
|
||||
|
||||
if (MSVC)
|
||||
STRING(REGEX REPLACE "/" "\\\\" BACKSLASHED_JAVA_BINDING_OUTPUT_DIR ${JAVA_BINDING_OUTPUT_DIR})
|
||||
STRING(REGEX REPLACE "/" "\\\\" BACKSLASHED_CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
COMMAND xcopy \"${BACKSLASHED_JAVA_BINDING_OUTPUT_DIR}\\$(Configuration)\\_sourcetraildb*\" \"${BACKSLASHED_CMAKE_CURRENT_BINARY_DIR}\" /Y
|
||||
)
|
||||
else()
|
||||
add_custom_command(
|
||||
TARGET ${EXAMPLE_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${JAVA_BINDING_OUTPUT_DIR}/lib_sourcetraildb* ${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
endif()
|
||||
|
||||
set(JAVA_API_EXAMPLE_DATA_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data")
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_NAME}.srctrlprj.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_NAME}.srctrlprj"
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
|
||||
## Java API Example
|
||||
|
||||
### Requirements
|
||||
|
||||
* CMake
|
||||
* C++ Compiler
|
||||
* Java JDK
|
||||
* SWIG
|
||||
|
||||
### Running from Command Line
|
||||
|
||||
* Run cmake with `BUILD_BINDINGS_JAVA=ON`
|
||||
* Build `make`
|
||||
* Copy two files from the `bindings_java` build directory into `SourcetrailDB/examples/java_api_example`:
|
||||
- SourcetrailDB.jar
|
||||
- Windows: `_sourcetraildb.dll`
|
||||
- Linux: `lib_sourcetraildb.so`
|
||||
- macOS: `lib_sourcetraildb.jnilib`
|
||||
* Run example from the directory `SourcetrailDB/examples/java_api_example` with:
|
||||
|
||||
## Windows
|
||||
```
|
||||
$ javac -cp SourcetrailDB.jar src/za/co/spazzymoto/example.java
|
||||
$ java -cp "SourcetrailDB.jar;src" -Djava.library.path=. za.co.spazzymoto.example --database-file-path=absolute/path/to/SourcetrailDB/examples/java_api_example/src/example.srctrldb --source-file-path=absolute/path/to/SourcetrailDB/examples/java_api_example/data/file.py
|
||||
```
|
||||
|
||||
## Unix/macOS
|
||||
```
|
||||
$ javac -cp SourcetrailDB.jar src/za/co/spazzymoto/example.java
|
||||
$ java -cp "SourcetrailDB.jar:src" -Djava.library.path=. za.co.spazzymoto.example --database-file-path=absolute/path/to/SourcetrailDB/examples/java_api_example/src/example.srctrldb --source-file-path=absolute/path/to/SourcetrailDB/examples/java_api_example/data/file.py
|
||||
```
|
||||
|
||||
### Running with Sourcetrail
|
||||
|
||||
* Run cmake with `BUILD_EXAMPLES=ON` and `BUILD_BINDINGS_JAVA=ON`
|
||||
* Build `make`.
|
||||
* Open `java_api_example.srctrlprj` located in the build directory of `java_api_example` with Sourcetrail and index the project.
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
class MyType:
|
||||
|
||||
my_member = true
|
||||
|
||||
def my_method():
|
||||
return my_member
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<config>
|
||||
<source_groups>
|
||||
<source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
|
||||
<custom_command>java -cp "*" -Djava.library.path=. za.co.spazzymoto.example --database-file-path=%{DATABASE_FILE_PATH} --source-file-path=%{SOURCE_FILE_PATH}</custom_command>
|
||||
<name>Custom Command Source Group</name>
|
||||
<source_extensions>
|
||||
<source_extension>.py</source_extension>
|
||||
</source_extensions>
|
||||
<source_paths>
|
||||
<source_path>@JAVA_API_EXAMPLE_DATA_PATH@/file.py</source_path>
|
||||
</source_paths>
|
||||
<status>enabled</status>
|
||||
<type>Custom Command Source Group</type>
|
||||
</source_group_78efd997-84bf-4db6-9736-f5ddd702053c>
|
||||
</source_groups>
|
||||
<version>7</version>
|
||||
</config>
|
||||
@@ -0,0 +1,120 @@
|
||||
package za.co.spazzymoto;
|
||||
|
||||
import com.sourcetrail.*;
|
||||
|
||||
public class example {
|
||||
private static int DBVersion;
|
||||
private static String DBFilePath;
|
||||
private static String SourceFilePath;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
for (String arg : args) {
|
||||
String name = "";
|
||||
String value = "";
|
||||
try {
|
||||
name = arg.split("=")[0];
|
||||
value = arg.split("=")[1];
|
||||
} catch(Exception e) {
|
||||
System.out.println("Invalid parameter:" + arg);
|
||||
System.exit(1);
|
||||
}
|
||||
switch(name) {
|
||||
case "--database-file-path":
|
||||
example.DBFilePath = value;
|
||||
break;
|
||||
|
||||
case "--source-file-path":
|
||||
example.SourceFilePath = value;
|
||||
break;
|
||||
|
||||
case "--database-version":
|
||||
try {
|
||||
example.DBVersion = Integer.parseInt(value);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid DB version:" + value);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
System.out.println("Invalid parameter:" + name);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("SourcetrailDB Java API Example");
|
||||
System.out.println("Supported database version: " + sourcetraildb.getSupportedDatabaseVersion());
|
||||
|
||||
if (example.DBVersion > 0 && example.DBVersion != sourcetraildb.getSupportedDatabaseVersion()) {
|
||||
System.out.println("ERROR: Only supports database version: " + sourcetraildb.getSupportedDatabaseVersion() +
|
||||
". Requested version: " + example.DBVersion);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
sourcetraildb.open(example.DBFilePath);
|
||||
} catch (Exception e) {
|
||||
System.out.println(e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
System.out.println("Clearing loaded database now...");
|
||||
sourcetraildb.clear();
|
||||
|
||||
System.out.println("start indexing");
|
||||
sourcetraildb.beginTransaction();
|
||||
|
||||
int fileId = sourcetraildb.recordFile(example.SourceFilePath);
|
||||
sourcetraildb.recordFileLanguage(fileId, "python");
|
||||
|
||||
if (sourcetraildb.getLastError().length() > 0) {
|
||||
System.out.println(sourcetraildb.getLastError());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
int symbolId = sourcetraildb.recordSymbol(
|
||||
"{ \"name_delimiter\": \".\", \"name_elements\": [ " +
|
||||
"{ \"prefix\": \"\", \"name\": \"MyType\", \"postfix\": \"\" } " +
|
||||
"] }");
|
||||
sourcetraildb.recordSymbolDefinitionKind(symbolId, DefinitionKind.DEFINITION_EXPLICIT);
|
||||
sourcetraildb.recordSymbolKind(symbolId, SymbolKind.SYMBOL_CLASS);
|
||||
sourcetraildb.recordSymbolLocation(symbolId, fileId, 2, 7, 2, 12);
|
||||
sourcetraildb.recordSymbolScopeLocation(symbolId, fileId, 2, 1, 7, 1);
|
||||
|
||||
int memberId = sourcetraildb.recordSymbol(
|
||||
"{ \"name_delimiter\": \".\", \"name_elements\": [ " +
|
||||
"{ \"prefix\": \"\", \"name\": \"MyType\", \"postfix\": \"\" }, " +
|
||||
"{ \"prefix\": \"\", \"name\": \"my_member\", \"postfix\": \"\" } " +
|
||||
"] }");
|
||||
sourcetraildb.recordSymbolDefinitionKind(memberId, DefinitionKind.DEFINITION_EXPLICIT);
|
||||
sourcetraildb.recordSymbolKind(memberId, SymbolKind.SYMBOL_FIELD);
|
||||
sourcetraildb.recordSymbolLocation(memberId, fileId, 4, 2, 4, 10);
|
||||
|
||||
int methodId = sourcetraildb.recordSymbol(
|
||||
"{ \"name_delimiter\": \".\", \"name_elements\": [ " +
|
||||
"{ \"prefix\": \"\", \"name\": \"MyType\", \"postfix\": \"\" }, " +
|
||||
"{ \"prefix\": \"\", \"name\": \"my_method\", \"postfix\": \"\" } " +
|
||||
"] }");
|
||||
sourcetraildb.recordSymbolDefinitionKind(methodId, DefinitionKind.DEFINITION_EXPLICIT);
|
||||
sourcetraildb.recordSymbolKind(methodId, SymbolKind.SYMBOL_METHOD);
|
||||
sourcetraildb.recordSymbolLocation(methodId, fileId, 6, 6, 6, 14);
|
||||
sourcetraildb.recordSymbolScopeLocation(methodId, fileId, 6, 1, 7, 1);
|
||||
|
||||
int useageId = sourcetraildb.recordReference(methodId, memberId, ReferenceKind.REFERENCE_USAGE);
|
||||
sourcetraildb.recordReferenceLocation(useageId, fileId, 7, 10, 7, 18);
|
||||
|
||||
sourcetraildb.commitTransaction();
|
||||
|
||||
if (sourcetraildb.getLastError().length() > 0) {
|
||||
System.out.println(sourcetraildb.getLastError());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
if (!sourcetraildb.close()) {
|
||||
System.out.println(sourcetraildb.getLastError());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
System.out.println("done");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user