Create initial Java infrastructure

This commit is contained in:
Wilson Lin 2020-01-18 15:19:38 +11:00
parent b4f8a041b2
commit ec708a0f64
8 changed files with 119 additions and 0 deletions

2
java/.cargo/config Normal file
View File

@ -0,0 +1,2 @@
[build]
target-dir = "./target/rust"

2
java/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/Cargo.lock
/target

13
java/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "hyperbuild-java"
version = "0.0.14"
authors = ["Wilson Lin <code@wilsonl.in>"]
edition = "2018"
[dependencies]
hyperbuild = "0.0.14"
jni = "0.14.0"
[lib]
crate-type = ["cdylib"]
path = "src/main/rust/src/lib.rs"

24
java/build.sh Executable file
View File

@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -e
shopt -s globstar
pushd "$(dirname "$0")"
if [ "$1" = "release" ]; then
rust_build_arg="--release"
rust_build_dir="release"
else
rust_build_arg=""
rust_build_dir="debug"
fi
rm -rf target/java
mkdir -p target/java
javac -d target/java src/main/java/**/*.java
cargo build $rust_build_arg
cp target/rust/$rust_build_dir/libhyperbuild_java.so target/java/in/wilsonl/hyperbuild/.
popd

23
java/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>in.wilsonl.hyperbuild</groupId>
<artifactId>hyperbuild</artifactId>
<version>0.0.14</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,23 @@
package in.wilsonl.hyperbuild;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.UTF_8;
public class Hyperbuild {
static {
String nativeLibPath = Hyperbuild.class.getResource("libhyperbuild_java.so").getPath();
System.load(nativeLibPath);
}
public static native int minifyInPlace(byte[] code) throws HyperbuildException;
public static byte[] minify(byte[] code) throws HyperbuildException {
int size = minifyInPlace(code);
return Arrays.copyOf(code, size);
}
public static String minify(String code) throws HyperbuildException {
return new String(Hyperbuild.minify(code.getBytes(UTF_8)), UTF_8);
}
}

View File

@ -0,0 +1,7 @@
package in.wilsonl.hyperbuild;
public class HyperbuildException extends RuntimeException {
public HyperbuildException(String message) {
super(message);
}
}

View File

@ -0,0 +1,25 @@
use hyperbuild::hyperbuild;
use jni::JNIEnv;
use jni::objects::JClass;
use jni::sys::{jbyteArray, jint};
#[no_mangle]
pub extern "system" fn Java_in_wilsonl_hyperbuild_Hyperbuild_minifyInPlace(
env: JNIEnv,
_class: JClass,
input: jbyteArray,
)
-> jint {
let source = &mut env.convert_byte_array(input).unwrap();
(match hyperbuild(source) {
Ok(out_len) => out_len,
Err((err, pos)) => {
env.throw_new(
"in/wilsonl/hyperbuild/HyperbuildException",
format!("{} [Character {}]", err.message(), pos),
).unwrap();
0
}
}) as jint
}