rustjava_runtime/classes/java/text/
parse_position.rs1use alloc::{format, vec};
2
3use jvm::{ClassInstanceRef, Jvm, Result, runtime::JavaLangString};
4use jvm_class_proto::{JavaFieldProto, JavaMethodProto};
5use jvm_types::{ClassAccessFlags, FieldAccessFlags, MethodAccessFlags};
6
7use crate::{
8 RuntimeClassProto, RuntimeContext,
9 classes::java::lang::{Object, String},
10};
11
12pub struct ParsePosition;
14
15impl ParsePosition {
16 pub fn as_proto() -> RuntimeClassProto {
17 RuntimeClassProto {
18 name: "java/text/ParsePosition",
19 parent_class: Some("java/lang/Object"),
20 interfaces: vec![],
21 methods: vec![
22 JavaMethodProto::new("<init>", "(I)V", Self::init, MethodAccessFlags::PUBLIC),
23 JavaMethodProto::new("getIndex", "()I", Self::get_index, MethodAccessFlags::PUBLIC),
24 JavaMethodProto::new("setIndex", "(I)V", Self::set_index, MethodAccessFlags::PUBLIC),
25 JavaMethodProto::new("getErrorIndex", "()I", Self::get_error_index, MethodAccessFlags::PUBLIC),
26 JavaMethodProto::new("setErrorIndex", "(I)V", Self::set_error_index, MethodAccessFlags::PUBLIC),
27 JavaMethodProto::new("equals", "(Ljava/lang/Object;)Z", Self::equals, MethodAccessFlags::PUBLIC),
28 JavaMethodProto::new("hashCode", "()I", Self::hash_code, MethodAccessFlags::PUBLIC),
29 JavaMethodProto::new("toString", "()Ljava/lang/String;", Self::to_string, MethodAccessFlags::PUBLIC),
30 ],
31 fields: vec![
32 JavaFieldProto::new("index", "I", FieldAccessFlags::PRIVATE),
33 JavaFieldProto::new("errorIndex", "I", FieldAccessFlags::PRIVATE),
34 ],
35 access_flags: ClassAccessFlags::PUBLIC,
36 }
37 }
38
39 async fn init(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, index: i32) -> Result<()> {
40 let _: () = jvm.invoke_special(&this, "java/lang/Object", "<init>", "()V", ()).await?;
41 jvm.put_field(&mut this, "java/text/ParsePosition", "index", "I", index).await?;
42 jvm.put_field(&mut this, "java/text/ParsePosition", "errorIndex", "I", -1).await
43 }
44
45 async fn get_index(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
46 jvm.get_field(&this, "java/text/ParsePosition", "index", "I").await
47 }
48
49 async fn set_index(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, index: i32) -> Result<()> {
50 jvm.put_field(&mut this, "java/text/ParsePosition", "index", "I", index).await
51 }
52
53 async fn get_error_index(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
54 jvm.get_field(&this, "java/text/ParsePosition", "errorIndex", "I").await
55 }
56
57 async fn set_error_index(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, index: i32) -> Result<()> {
58 jvm.put_field(&mut this, "java/text/ParsePosition", "errorIndex", "I", index).await
59 }
60
61 async fn equals(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, other: ClassInstanceRef<Object>) -> Result<bool> {
62 if other.is_null() || !jvm.is_instance(&**other, "java/text/ParsePosition") {
63 return Ok(false);
64 }
65
66 let other: ClassInstanceRef<Self> = ClassInstanceRef::new(other.instance);
67 let index: i32 = jvm.get_field(&this, "java/text/ParsePosition", "index", "I").await?;
68 let other_index: i32 = jvm.get_field(&other, "java/text/ParsePosition", "index", "I").await?;
69 let error_index: i32 = jvm.get_field(&this, "java/text/ParsePosition", "errorIndex", "I").await?;
70 let other_error_index: i32 = jvm.get_field(&other, "java/text/ParsePosition", "errorIndex", "I").await?;
71 Ok(index == other_index && error_index == other_error_index)
72 }
73
74 async fn hash_code(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> {
75 let index: i32 = jvm.get_field(&this, "java/text/ParsePosition", "index", "I").await?;
76 let error_index: i32 = jvm.get_field(&this, "java/text/ParsePosition", "errorIndex", "I").await?;
77 Ok(index ^ error_index.rotate_left(16))
78 }
79
80 async fn to_string(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> {
81 let index: i32 = jvm.get_field(&this, "java/text/ParsePosition", "index", "I").await?;
82 let error_index: i32 = jvm.get_field(&this, "java/text/ParsePosition", "errorIndex", "I").await?;
83 Ok(
84 JavaLangString::from_rust_string(jvm, &format!("java.text.ParsePosition[index={index},errorIndex={error_index}]"))
85 .await?
86 .into(),
87 )
88 }
89}