Creates a new instance of a `java.lang.String` with the given content
(
vm: &mut Vm<'a>,
call_stack: &mut CallStack<'a>,
content: &str,
)
| 15 | |
| 16 | /// Creates a new instance of a `java.lang.String` with the given content |
| 17 | pub fn new_java_lang_string_object<'a>( |
| 18 | vm: &mut Vm<'a>, |
| 19 | call_stack: &mut CallStack<'a>, |
| 20 | content: &str, |
| 21 | ) -> Result<AbstractObject<'a>, MethodCallFailed<'a>> { |
| 22 | let char_array: Vec<Value<'a>> = content |
| 23 | .encode_utf16() |
| 24 | .map(|c| Value::Int(c as i32)) |
| 25 | .collect(); |
| 26 | |
| 27 | let java_array = vm.new_array(ArrayEntryType::Base(BaseType::Char), char_array.len()); |
| 28 | char_array |
| 29 | .into_iter() |
| 30 | .enumerate() |
| 31 | .for_each(|(index, value)| java_array.set_element(index, value).unwrap()); |
| 32 | |
| 33 | // In our JRE's rt.jar, the fields for String are: |
| 34 | // private final char[] value; |
| 35 | // private int hash; |
| 36 | // private static final long serialVersionUID = -6849794470754667710L; |
| 37 | // private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0]; |
| 38 | // public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); |
| 39 | // private static final int HASHING_SEED; |
| 40 | // private transient int hash32; |
| 41 | let string_object = vm.new_object(call_stack, "java/lang/String")?; |
| 42 | string_object.set_field(0, Value::Object(java_array)); |
| 43 | string_object.set_field(1, Value::Int(0)); |
| 44 | string_object.set_field(6, Value::Int(0)); |
| 45 | Ok(string_object) |
| 46 | } |
| 47 | |
| 48 | /// Given an instance of `java.lang.String`, extracts the content as a Rust `String` |
| 49 | pub fn extract_str_from_java_lang_string<'a>( |
no test coverage detected