|
| 1 | +use alloc::{format, vec}; |
| 2 | + |
| 3 | +use java_class_proto::{JavaFieldProto, JavaMethodProto}; |
| 4 | +use java_constants::{ClassAccessFlags, FieldAccessFlags, MethodAccessFlags}; |
| 5 | +use jvm::{ClassInstanceRef, Jvm, Result, runtime::JavaLangString}; |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + RuntimeClassProto, RuntimeContext, |
| 9 | + classes::java::lang::{Object, String}, |
| 10 | +}; |
| 11 | + |
| 12 | +// public class java.text.FieldPosition |
| 13 | +pub struct FieldPosition; |
| 14 | + |
| 15 | +impl FieldPosition { |
| 16 | + pub fn as_proto() -> RuntimeClassProto { |
| 17 | + RuntimeClassProto { |
| 18 | + name: "java/text/FieldPosition", |
| 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("getField", "()I", Self::get_field, MethodAccessFlags::PUBLIC), |
| 24 | + JavaMethodProto::new("getBeginIndex", "()I", Self::get_begin_index, MethodAccessFlags::PUBLIC), |
| 25 | + JavaMethodProto::new("getEndIndex", "()I", Self::get_end_index, MethodAccessFlags::PUBLIC), |
| 26 | + JavaMethodProto::new("setBeginIndex", "(I)V", Self::set_begin_index, MethodAccessFlags::PUBLIC), |
| 27 | + JavaMethodProto::new("setEndIndex", "(I)V", Self::set_end_index, MethodAccessFlags::PUBLIC), |
| 28 | + JavaMethodProto::new("equals", "(Ljava/lang/Object;)Z", Self::equals, MethodAccessFlags::PUBLIC), |
| 29 | + JavaMethodProto::new("hashCode", "()I", Self::hash_code, MethodAccessFlags::PUBLIC), |
| 30 | + JavaMethodProto::new("toString", "()Ljava/lang/String;", Self::to_string, MethodAccessFlags::PUBLIC), |
| 31 | + ], |
| 32 | + fields: vec![ |
| 33 | + JavaFieldProto::new("field", "I", FieldAccessFlags::PRIVATE), |
| 34 | + JavaFieldProto::new("beginIndex", "I", FieldAccessFlags::PRIVATE), |
| 35 | + JavaFieldProto::new("endIndex", "I", FieldAccessFlags::PRIVATE), |
| 36 | + ], |
| 37 | + access_flags: ClassAccessFlags::PUBLIC, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + async fn init(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, field: i32) -> Result<()> { |
| 42 | + let _: () = jvm.invoke_special(&this, "java/lang/Object", "<init>", "()V", ()).await?; |
| 43 | + jvm.put_field(&mut this, "field", "I", field).await?; |
| 44 | + jvm.put_field(&mut this, "beginIndex", "I", 0).await?; |
| 45 | + jvm.put_field(&mut this, "endIndex", "I", 0).await |
| 46 | + } |
| 47 | + |
| 48 | + async fn get_field(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> { |
| 49 | + jvm.get_field(&this, "field", "I").await |
| 50 | + } |
| 51 | + |
| 52 | + async fn get_begin_index(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> { |
| 53 | + jvm.get_field(&this, "beginIndex", "I").await |
| 54 | + } |
| 55 | + |
| 56 | + async fn get_end_index(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> { |
| 57 | + jvm.get_field(&this, "endIndex", "I").await |
| 58 | + } |
| 59 | + |
| 60 | + async fn set_begin_index(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, value: i32) -> Result<()> { |
| 61 | + jvm.put_field(&mut this, "beginIndex", "I", value).await |
| 62 | + } |
| 63 | + |
| 64 | + async fn set_end_index(jvm: &Jvm, _: &mut RuntimeContext, mut this: ClassInstanceRef<Self>, value: i32) -> Result<()> { |
| 65 | + jvm.put_field(&mut this, "endIndex", "I", value).await |
| 66 | + } |
| 67 | + |
| 68 | + async fn equals(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>, other: ClassInstanceRef<Object>) -> Result<bool> { |
| 69 | + if other.is_null() || !jvm.is_instance(&**other, "java/text/FieldPosition") { |
| 70 | + return Ok(false); |
| 71 | + } |
| 72 | + |
| 73 | + let other: ClassInstanceRef<Self> = ClassInstanceRef::new(other.instance); |
| 74 | + let field: i32 = jvm.get_field(&this, "field", "I").await?; |
| 75 | + let other_field: i32 = jvm.get_field(&other, "field", "I").await?; |
| 76 | + let begin_index: i32 = jvm.get_field(&this, "beginIndex", "I").await?; |
| 77 | + let other_begin_index: i32 = jvm.get_field(&other, "beginIndex", "I").await?; |
| 78 | + let end_index: i32 = jvm.get_field(&this, "endIndex", "I").await?; |
| 79 | + let other_end_index: i32 = jvm.get_field(&other, "endIndex", "I").await?; |
| 80 | + Ok(field == other_field && begin_index == other_begin_index && end_index == other_end_index) |
| 81 | + } |
| 82 | + |
| 83 | + async fn hash_code(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<i32> { |
| 84 | + let field: i32 = jvm.get_field(&this, "field", "I").await?; |
| 85 | + let begin_index: i32 = jvm.get_field(&this, "beginIndex", "I").await?; |
| 86 | + let end_index: i32 = jvm.get_field(&this, "endIndex", "I").await?; |
| 87 | + Ok(field ^ begin_index.rotate_left(11) ^ end_index.rotate_left(22)) |
| 88 | + } |
| 89 | + |
| 90 | + async fn to_string(jvm: &Jvm, _: &mut RuntimeContext, this: ClassInstanceRef<Self>) -> Result<ClassInstanceRef<String>> { |
| 91 | + let field: i32 = jvm.get_field(&this, "field", "I").await?; |
| 92 | + let begin_index: i32 = jvm.get_field(&this, "beginIndex", "I").await?; |
| 93 | + let end_index: i32 = jvm.get_field(&this, "endIndex", "I").await?; |
| 94 | + Ok(JavaLangString::from_rust_string( |
| 95 | + jvm, |
| 96 | + &format!("java.text.FieldPosition[field={field},beginIndex={begin_index},endIndex={end_index}]"), |
| 97 | + ) |
| 98 | + .await? |
| 99 | + .into()) |
| 100 | + } |
| 101 | +} |
0 commit comments