Add Java bindings (#18)

This commit is contained in:
Robert Edwards
2019-12-06 09:25:26 +01:00
committed by Malte Langkabel
parent 2ffb5b2af0
commit 28e8cb8f33
12 changed files with 403 additions and 5 deletions
+30 -1
View File
@@ -50,6 +50,18 @@ matrix:
# env: # env:
# - CC_COMPILER=gcc-5 CXX_COMPILER=g++-5 PYTHON_BINDING=1 PYTHON=3.7 # - CC_COMPILER=gcc-5 CXX_COMPILER=g++-5 PYTHON_BINDING=1 PYTHON=3.7
- os: linux
compiler: gcc
dist: bionic
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-5
env:
- CC_COMPILER=gcc-5 CXX_COMPILER=g++-5 JAVA_BINDING=1
- os: osx - os: osx
compiler: clang compiler: clang
env: env:
@@ -70,6 +82,12 @@ matrix:
# env: # env:
# - PYTHON_BINDING=1 PYTHON=3.7 # - PYTHON_BINDING=1 PYTHON=3.7
- os: osx
compiler: clang
osx_image: xcode11.2
env:
- JAVA_BINDING=1
before_install: before_install:
- | - |
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
@@ -93,6 +111,11 @@ before_install:
alias python="/usr/bin/python$PYTHON" alias python="/usr/bin/python$PYTHON"
python --version python --version
fi fi
if [[ "$JAVA_BINDING" == "1" ]]; then
sudo apt-get install openjdk-8-jdk
javac -version
fi
fi fi
install: install:
@@ -103,7 +126,7 @@ script:
- | - |
mkdir build mkdir build
cd build cd build
cmake -DBUILD_BINDINGS_PYTHON=$PYTHON_BINDING -DPYTHON_VERSION=$PYTHON .. cmake -DBUILD_BINDINGS_PYTHON=$PYTHON_BINDING -DPYTHON_VERSION=$PYTHON -DBUILD_BINDINGS_JAVA=$JAVA_BINDING ..
make make
./core/test_core ./core/test_core
@@ -122,6 +145,12 @@ before_deploy:
mkdir -p $BUNDLE_NAME mkdir -p $BUNDLE_NAME
cp build/bindings_python/sourcetraildb.py $BUNDLE_NAME cp build/bindings_python/sourcetraildb.py $BUNDLE_NAME
cp build/bindings_python/_sourcetraildb* $BUNDLE_NAME/_sourcetraildb.so cp build/bindings_python/_sourcetraildb* $BUNDLE_NAME/_sourcetraildb.so
else if [[ "$JAVA_BINDING" == "1" ]]; then
BUNDLE_NAME=sourcetraildb_java_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER}
mkdir -p $BUNDLE_NAME
cp build/bindings_java/SourcetrailDB.jar $BUNDLE_NAME
cp build/bindings_java/lib_sourcetraildb* $BUNDLE_NAME
else else
BUNDLE_NAME=sourcetraildb_core_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER} BUNDLE_NAME=sourcetraildb_core_${VERSION}-${TRAVIS_OS_NAME}-64bit-${TRAVIS_COMPILER}
+19
View File
@@ -3,6 +3,7 @@ cmake_minimum_required (VERSION 2.6)
# --- Options --- # --- Options ---
set(BUILD_BINDINGS_PYTHON OFF CACHE BOOL "Build the SourcetrailDB Python bindings.") set(BUILD_BINDINGS_PYTHON OFF CACHE BOOL "Build the SourcetrailDB Python bindings.")
set(BUILD_BINDINGS_JAVA OFF CACHE BOOL "Build the SourcetrailDB Java bindings.")
set(BUILD_EXAMPLES ON CACHE BOOL "Build the examples.") set(BUILD_EXAMPLES ON CACHE BOOL "Build the examples.")
set(PROJECT_NAME "SourcetrailDB") set(PROJECT_NAME "SourcetrailDB")
@@ -56,6 +57,18 @@ else()
message(STATUS "Building the SourcetrailDB Python bindings will be skipped. You can enable building this target by setting 'BUILD_BINDINGS_PYTHON' to 'ON'.") message(STATUS "Building the SourcetrailDB Python bindings will be skipped. You can enable building this target by setting 'BUILD_BINDINGS_PYTHON' to 'ON'.")
endif() endif()
# --- Java Binding ---
if (BUILD_BINDINGS_JAVA)
message(STATUS "The SourcetrailDB Java bindings will be built.")
set(JAVA_BINDING_TARGET_NAME "bindings_java")
set(JAVA_BINDING_OUTPUT_DIR "")
add_subdirectory("${CMAKE_SOURCE_DIR}/bindings_java" "${CMAKE_BINARY_DIR}/bindings_java")
else()
message(STATUS "Building the SourcetrailDB Java bindings will be skipped. You can enable building this target by setting 'BUILD_BINDINGS_JAVA' to 'ON'.")
endif()
# --- Examples --- # --- Examples ---
@@ -69,6 +82,12 @@ if (BUILD_EXAMPLES)
else() else()
message(STATUS "Skip python binding sample. You can enable building it by setting 'BUILD_BINDINGS_PYTHON' to 'ON'.") message(STATUS "Skip python binding sample. You can enable building it by setting 'BUILD_BINDINGS_PYTHON' to 'ON'.")
endif() endif()
if (BUILD_BINDINGS_JAVA)
add_subdirectory("${CMAKE_SOURCE_DIR}/examples/java_api_example")
else()
message(STATUS "Skip java binding sample. You can enable building it by setting 'BUILD_BINDINGS_JAVA' to 'ON'.")
endif()
else() else()
message(STATUS "Building examples will be skipped. You can enable building examples by setting 'BUILD_EXAMPLES' to 'ON'.") message(STATUS "Building examples will be skipped. You can enable building examples by setting 'BUILD_EXAMPLES' to 'ON'.")
endif() endif()
+20
View File
@@ -35,6 +35,7 @@ __To get an overview on everything involved, please take a look at our [Language
Even though the core implementation is written in C++, this does not require you to write your indexer in C++ as well. Instead you can use a language binding (e.g. see [SWIG](http://www.swig.org/)). These language bindings are already available: Even though the core implementation is written in C++, this does not require you to write your indexer in C++ as well. Instead you can use a language binding (e.g. see [SWIG](http://www.swig.org/)). These language bindings are already available:
* Python (via [SWIG](http://www.swig.org/)) * Python (via [SWIG](http://www.swig.org/))
* Java (via [SWIG](http://www.swig.org/))
If the language of your choice is not covered by this list, feel free to [open an issue](https://github.com/CoatiSoftware/SourcetrailDB/issues) or provide a pull request. If the language of your choice is not covered by this list, feel free to [open an issue](https://github.com/CoatiSoftware/SourcetrailDB/issues) or provide a pull request.
@@ -110,6 +111,24 @@ $ make _sourcetraildb
Swig is configured to generate the Python binding code as a pre-build event, so you don't need to bother with updating manually. Swig is configured to generate the Python binding code as a pre-build event, so you don't need to bother with updating manually.
### Java Bindings
Requirements:
* [Java JDK](https://openjdk.java.net/) needs to be included and linked against when building the Java bindings. CMake will auto-detect your Java installation.
* [SWIG 3.0.12](http://www.swig.org/) is used to automatically generate Java binding code. Make sure that SWIG is added to your path environment variable.
If you want to build the Java bindings run:
```
$ cd path/to/SourcetrailDB
$ mkdir build
$ cd build
$ cmake -DBUILD_BINDINGS_JAVA=ON ..
$ make
```
Swig is configured to generate the Java binding code as a pre-build event, so you don't need to bother with updating manually.
### Examples ### Examples
The examples help you to understand SourcetrailDB usage in practice. Please take a look at each examples README file for build and use instructions. Each example also provides a Sourcetrail project file `.srctrlprj` showing you how to use a custom indexer directly from Sourcetrail (see [Integrating with Sourcetrail](#integrating-with-sourcetrail)). The examples help you to understand SourcetrailDB usage in practice. Please take a look at each examples README file for build and use instructions. Each example also provides a Sourcetrail project file `.srctrlprj` showing you how to use a custom indexer directly from Sourcetrail (see [Integrating with Sourcetrail](#integrating-with-sourcetrail)).
@@ -117,6 +136,7 @@ The examples help you to understand SourcetrailDB usage in practice. Please take
* [C++ API Example](examples/cpp_api_example) * [C++ API Example](examples/cpp_api_example)
* [C++ Poetry Indexer](examples/cpp_poetry_indexer) * [C++ Poetry Indexer](examples/cpp_poetry_indexer)
* [Python API Example](examples/python_api_example) * [Python API Example](examples/python_api_example)
* [Java API Example](examples/java_api_example)
## SourcetrailDB API ## SourcetrailDB API
+25 -2
View File
@@ -9,11 +9,13 @@ environment:
MSVC_VERSION: 15 MSVC_VERSION: 15
DEPLOY_CORE: 1 DEPLOY_CORE: 1
DEPLOY_PYTHON_BINDINGS: 1 DEPLOY_PYTHON_BINDINGS: 1
DEPLOY_JAVA_BINDINGS: 1
- MBITS: 64 - MBITS: 64
PYTHON_VERSION: 37 PYTHON_VERSION: 37
MSVC_VERSION: 15 MSVC_VERSION: 15
DEPLOY_CORE: 1 DEPLOY_CORE: 1
DEPLOY_PYTHON_BINDINGS: 1 DEPLOY_PYTHON_BINDINGS: 1
DEPLOY_JAVA_BINDINGS: 1
- MBITS: 32 - MBITS: 32
PYTHON_VERSION: 37 PYTHON_VERSION: 37
@@ -41,13 +43,13 @@ install:
echo "No architecture set. Build will be canceled." echo "No architecture set. Build will be canceled."
} }
# Install SWIG by Choco # Install SWIG by Choco
choco install -y --no-progress swig choco install -y --no-progress swig openjdk
before_build: before_build:
- mkdir build - mkdir build
- cd build - cd build
- cmake -G "%CMAKE_GENERATOR%" ../ -DBUILD_BINDINGS_PYTHON=ON -DPYTHON_LIBRARY="%PYTHON_PATH%/libs/python%PYTHON_VERSION%.lib" - cmake -G "%CMAKE_GENERATOR%" ../ -DBUILD_BINDINGS_PYTHON=ON -DPYTHON_LIBRARY="%PYTHON_PATH%/libs/python%PYTHON_VERSION%.lib" -DBUILD_BINDINGS_JAVA=ON
- cd ../ - cd ../
- ps: $env:CORE_VERSION = Get-Content -Path ./version.txt - ps: $env:CORE_VERSION = Get-Content -Path ./version.txt
- ps: $env:UNDERSCORED_CORE_VERSION = $env:CORE_VERSION.Replace(".", "_") - ps: $env:UNDERSCORED_CORE_VERSION = $env:CORE_VERSION.Replace(".", "_")
@@ -67,8 +69,10 @@ build_script:
after_build: after_build:
- ps: $env:CORE_PACKAGE_NAME = 'sourcetraildb_core_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit-msvc' + $env:MSVC_VERSION - ps: $env:CORE_PACKAGE_NAME = 'sourcetraildb_core_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit-msvc' + $env:MSVC_VERSION
- ps: $env:PYTHON_PACKAGE_NAME = 'sourcetrailbd_python' + $env:PYTHON_VERSION + '_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit' - ps: $env:PYTHON_PACKAGE_NAME = 'sourcetrailbd_python' + $env:PYTHON_VERSION + '_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit'
- ps: $env:JAVA_PACKAGE_NAME = 'sourcetrailbd_java_' + $env:UNDERSCORED_CORE_VERSION + '-windows-' + $env:MBITS + 'bit'
- ps: echo $env:CORE_PACKAGE_NAME - ps: echo $env:CORE_PACKAGE_NAME
- ps: echo $env:PYTHON_PACKAGE_NAME - ps: echo $env:PYTHON_PACKAGE_NAME
- ps: echo $env:JAVA_PACKAGE_NAME
- mkdir artifacts_core - mkdir artifacts_core
- cd artifacts_core - cd artifacts_core
- mkdir %CORE_PACKAGE_NAME% - mkdir %CORE_PACKAGE_NAME%
@@ -98,6 +102,19 @@ after_build:
copy external/catch/license_catch.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/ copy external/catch/license_catch.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/
copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/ copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/
copy external/json/license_json.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/ copy external/json/license_json.txt artifacts_bindings_python/$env:PYTHON_PACKAGE_NAME/license/
- mkdir artifacts_bindings_java
- cd artifacts_bindings_java
- mkdir %JAVA_PACKAGE_NAME%
- cd %JAVA_PACKAGE_NAME%
- mkdir license
- cd ../..
- ps: |
copy build/bindings_java/release/_sourcetraildb.dll artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/
copy build/bindings_java/SourcetrailDB.jar artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/
copy LICENSE.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/license_sourcetraildb.txt
copy external/catch/license_catch.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/
copy external/cpp_sqlite/license_cpp_sqlite.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/
copy external/json/license_json.txt artifacts_bindings_java/$env:JAVA_PACKAGE_NAME/license/
- ps: | - ps: |
$env:ARTIFACTY_TO_DEPLOY = "" $env:ARTIFACTY_TO_DEPLOY = ""
if ($env:DEPLOY_CORE -eq "1") { if ($env:DEPLOY_CORE -eq "1") {
@@ -106,6 +123,9 @@ after_build:
if ($env:DEPLOY_PYTHON_BINDINGS -eq "1") { if ($env:DEPLOY_PYTHON_BINDINGS -eq "1") {
$env:ARTIFACTY_TO_DEPLOY = $env:ARTIFACTY_TO_DEPLOY + "," + $env:PYTHON_PACKAGE_NAME $env:ARTIFACTY_TO_DEPLOY = $env:ARTIFACTY_TO_DEPLOY + "," + $env:PYTHON_PACKAGE_NAME
} }
if ($env:DEPLOY_JAVA_BINDINGS -eq "1") {
$env:ARTIFACTY_TO_DEPLOY = $env:ARTIFACTY_TO_DEPLOY + "," + $env:JAVA_PACKAGE_NAME
}
$env:ARTIFACTY_TO_DEPLOY = $env:ARTIFACTY_TO_DEPLOY.substring(1) $env:ARTIFACTY_TO_DEPLOY = $env:ARTIFACTY_TO_DEPLOY.substring(1)
echo $env:ARTIFACTY_TO_DEPLOY echo $env:ARTIFACTY_TO_DEPLOY
@@ -121,6 +141,9 @@ artifacts:
- path: artifacts_bindings_python - path: artifacts_bindings_python
name: $(PYTHON_PACKAGE_NAME) name: $(PYTHON_PACKAGE_NAME)
type: Zip type: Zip
- path: artifacts_bindings_java
name: $(JAVA_PACKAGE_NAME)
type: Zip
deploy: deploy:
+62
View File
@@ -0,0 +1,62 @@
cmake_minimum_required (VERSION 2.6)
# --- Setup Paths ---
set(RESOURCES_SWIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../resources_swig")
set(GENERATED_SRC_DIR "${CMAKE_CURRENT_BINARY_DIR}/src")
set(JAVA_BINDING_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
set(SWIG_INTERFACE_FILE "${RESOURCES_SWIG_DIR}/interface/sourcetraildb.i")
set(CMAKE_SWIG_FLAGS -package com.sourcetrail)
# --- Find Java ---
find_package(Java REQUIRED COMPONENTS Runtime Development)
find_package(JNI REQUIRED)
include(UseJava)
# --- Find Swig ---
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
# --- Configure Target ---
set_source_files_properties(${SWIG_INTERFACE_FILE} PROPERTIES CPLUSPLUS ON)
include_directories(
"${RESOURCES_SWIG_DIR}/include"
"${CORE_SOURCE_DIR}/include"
${JNI_INCLUDE_DIRS}
)
swig_add_library(
SharedLibrary
TYPE SHARED
LANGUAGE java
OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/com/sourcetrail
OUTFILE_DIR ${GENERATED_SRC_DIR}
SOURCES ${SWIG_INTERFACE_FILE} ${RESOURCES_SWIG_DIR}/src/sourcetraildb.cpp
)
swig_link_libraries(SharedLibrary ${JNI_LIBRARIES} ${LIB_CORE_TARGET_NAME})
set_target_properties(SharedLibrary
PROPERTIES OUTPUT_NAME _sourcetraildb)
set(CMAKE_JNI_TARGET TRUE)
add_jar(
${JAVA_BINDING_TARGET_NAME}
OUTPUT_NAME SourcetrailDB
SOURCES
${CMAKE_CURRENT_BINARY_DIR}/com/sourcetrail/DefinitionKind.java
${CMAKE_CURRENT_BINARY_DIR}/com/sourcetrail/ReferenceKind.java
${CMAKE_CURRENT_BINARY_DIR}/com/sourcetrail/sourcetraildb.java
${CMAKE_CURRENT_BINARY_DIR}/com/sourcetrail/sourcetraildbJNI.java
${CMAKE_CURRENT_BINARY_DIR}/com/sourcetrail/SymbolKind.java
)
add_dependencies( ${JAVA_BINDING_TARGET_NAME} SharedLibrary )
+51
View File
@@ -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"
)
+38
View File
@@ -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.
+7
View File
@@ -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");
}
}
+1 -1
View File
@@ -27,4 +27,4 @@ $ python example.py --database-file-path=absolute/path/to/SourcetrailDB/examples
* Run cmake with `BUILD_EXAMPLES=ON` and `BUILD_BINDINGS_PYTHON=ON` * Run cmake with `BUILD_EXAMPLES=ON` and `BUILD_BINDINGS_PYTHON=ON`
* Build target `_sourcetraildb` and `python_api_example`. * Build target `_sourcetraildb` and `python_api_example`.
* Open `python_api_exampl.srctrlprj` located in the build directory of `python_api_example` with Sourcetrail and index the project. * Open `python_api_example.srctrlprj` located in the build directory of `python_api_example` with Sourcetrail and index the project.
+12 -1
View File
@@ -9,4 +9,15 @@
%} %}
//double-check that this is indeed %include !!! //double-check that this is indeed %include !!!
%include "sourcetraildb.h" %include "sourcetraildb.h"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("_sourcetraildb");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}