Skip to content

Commit 80a1f0d

Browse files
committed
java constant generation + tests
1 parent d1176bd commit 80a1f0d

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::doc::*;
2+
use super::*;
3+
use heck::{CamelCase, ShoutySnakeCase};
4+
use oo_bindgen::constants::*;
5+
6+
pub(crate) fn generate(
7+
f: &mut impl Printer,
8+
set: &ConstantSetHandle,
9+
lib: &Library,
10+
) -> FormattingResult<()> {
11+
fn get_type_as_string(value: &ConstantValue) -> &'static str {
12+
match value {
13+
ConstantValue::U8(_, _) => "UByte",
14+
}
15+
}
16+
17+
fn get_value_as_string(value: &ConstantValue) -> String {
18+
match value {
19+
ConstantValue::U8(x, Representation::Hex) => format!("UByte.valueOf(0x{:02X?})", x),
20+
}
21+
}
22+
23+
let set_name = set.name.to_camel_case();
24+
25+
// Documentation
26+
documentation(f, |f| javadoc_print(f, &set.doc, lib))?;
27+
28+
// class definition
29+
f.writeln(&format!("public class {}", set_name))?;
30+
blocked(f, |f| {
31+
f.writeln("// not constructable")?;
32+
f.writeln(&format!("private {}() {{}}", set_name))?;
33+
34+
// Write the values
35+
for constant in &set.values {
36+
documentation(f, |f| javadoc_print(f, &constant.doc, lib))?;
37+
f.writeln(&format!(
38+
"public static final {} {} = {};",
39+
get_type_as_string(&constant.value),
40+
constant.name.to_shouty_snake_case(),
41+
get_value_as_string(&constant.value)
42+
))?;
43+
}
44+
Ok(())
45+
})
46+
}

generators/java-oo-bindgen/src/java/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::fs;
1010

1111
mod callback;
1212
mod class;
13+
mod constant;
1314
mod conversion;
1415
mod doc;
1516
mod enumeration;
@@ -49,6 +50,7 @@ pub fn generate_java_bindings(lib: &Library, config: &JavaBindgenConfig) -> Form
4950
generate_native_func_class(lib, config)?;
5051

5152
// Generate the user-facing stuff
53+
generate_constants(lib, config)?;
5254
generate_structs(lib, config)?;
5355
generate_enums(lib, config)?;
5456
generate_classes(lib, config)?;
@@ -218,6 +220,15 @@ fn generate_native_func_class(lib: &Library, config: &JavaBindgenConfig) -> Form
218220
})
219221
}
220222

223+
fn generate_constants(lib: &Library, config: &JavaBindgenConfig) -> FormattingResult<()> {
224+
for set in lib.constants() {
225+
let mut f = create_file(&set.name.to_camel_case(), config, lib)?;
226+
constant::generate(&mut f, set, lib)?;
227+
}
228+
229+
Ok(())
230+
}
231+
221232
fn generate_structs(lib: &Library, config: &JavaBindgenConfig) -> FormattingResult<()> {
222233
for native_struct in lib.structs() {
223234
let mut f = create_file(&native_struct.name().to_camel_case(), config, lib)?;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.stepfunc.foo_test;
2+
3+
import io.stepfunc.foo.SpecialValues;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class ConstantTest {
9+
@Test
10+
void specialValues() {
11+
assertThat(SpecialValues.ONE.byteValue()).isEqualTo((byte) 0x01);
12+
assertThat(SpecialValues.TWO.byteValue()).isEqualTo((byte) 0x02);
13+
}
14+
}

0 commit comments

Comments
 (0)