Skip to main content

jvm_bytecode/
array_class_definition.rs

1use alloc::{
2    boxed::Box,
3    string::{String, ToString},
4    sync::Arc,
5};
6use core::fmt::{self, Debug, Formatter};
7
8use jvm::{ArrayClassDefinition, ClassInstance, Jvm, Result};
9
10use crate::array_class_instance::ArrayClassInstanceImpl;
11
12struct ArrayClassDefinitionInner {
13    element_type_name: String,
14}
15
16#[derive(Clone)]
17pub struct ArrayClassDefinitionImpl {
18    inner: Arc<ArrayClassDefinitionInner>,
19}
20
21impl ArrayClassDefinitionImpl {
22    pub fn new(element_type_name: &str) -> Self {
23        Self {
24            inner: Arc::new(ArrayClassDefinitionInner {
25                element_type_name: element_type_name.to_string(),
26            }),
27        }
28    }
29}
30
31#[async_trait::async_trait]
32impl ArrayClassDefinition for ArrayClassDefinitionImpl {
33    fn element_type_name(&self) -> String {
34        self.inner.element_type_name.clone()
35    }
36
37    async fn instantiate_array(&self, _: &Jvm, length: usize) -> Result<Box<dyn ClassInstance>> {
38        Ok(Box::new(ArrayClassInstanceImpl::new(self, length)))
39    }
40}
41
42impl Debug for ArrayClassDefinitionImpl {
43    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
44        write!(f, "ArrayClass({})", self.inner.element_type_name)
45    }
46}