-
Notifications
You must be signed in to change notification settings - Fork 6
/
java2u8vec.sh
executable file
·58 lines (50 loc) · 3.41 KB
/
java2u8vec.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash
export TEMP_JAVA_NAME=Xyz
export TEMP_JAVA_FILE=/tmp/$TEMP_JAVA_NAME.java
export TEMP_CLASS_FILE=/tmp/$TEMP_JAVA_NAME.class
export TARGET_FILE=/tmp/class_buf.rs
function compile_java() {
echo "package rust;" > $TEMP_JAVA_FILE
echo "" >> $TEMP_JAVA_FILE
echo "public class $TEMP_JAVA_NAME {" >> $TEMP_JAVA_FILE
echo " public static void hello() {" >> $TEMP_JAVA_FILE
echo " System.out.println(\"Hello, World!\");" >> $TEMP_JAVA_FILE
echo " }" >> $TEMP_JAVA_FILE
echo "" >> $TEMP_JAVA_FILE
echo " public static int calc(int a, int b) {" >> $TEMP_JAVA_FILE
echo " return a + b;" >> $TEMP_JAVA_FILE
echo " }" >> $TEMP_JAVA_FILE
echo "}" >> $TEMP_JAVA_FILE
work_dir=$(pwd)
cd /tmp
javac $TEMP_JAVA_NAME.java
cd $work_dir
}
function dump_bytes() {
class_size=$(ls -l $TEMP_CLASS_FILE | cut -d" " -f 5)
echo "// $TEMP_JAVA_FILE" >> $TARGET_FILE
echo "#[allow(non_upper_case_globals, non_snake_case)]" >> $TARGET_FILE
echo "pub(crate) const CLASS_NAME_$TEMP_JAVA_NAME: &str = \"rust/$TEMP_JAVA_NAME\";" >> $TARGET_FILE
echo "#[allow(non_upper_case_globals, non_snake_case)]" >> $TARGET_FILE
echo "pub(crate) const CLASS_$TEMP_JAVA_NAME: [u8; $class_size] = [" >> $TARGET_FILE
hexdump -C $TEMP_CLASS_FILE | \
sed "/^.\{8\}$/d" | \
sed "s/^.\{10\}//g" | \
sed "s/ |.*$//g" | \
sed "s/ / /g" | \
sed "s/ \{2,\}//g" | \
sed "s/ /,0x/g" | \
sed "s/^/0x/g" | \
sed "s/,/, /g" | \
sed "s/$/,/g" | \
sed "s/^/ /g" >> $TARGET_FILE
echo "];" >> $TARGET_FILE
}
rm -rf $TARGET_FILE
echo "/// Auto generated by java2u8vec.sh" > $TARGET_FILE
echo "/// Do not edit by hands" >> $TARGET_FILE
echo "/// Java class bytecode, JDK8" >> $TARGET_FILE
echo "" >> $TARGET_FILE
compile_java
dump_bytes
cp $TARGET_FILE sample/src/samples/s008_class_buf.rs