@@ -2,9 +2,9 @@ use alloc::vec;
22
33use java_class_proto:: JavaMethodProto ;
44use java_constants:: ClassAccessFlags ;
5- use jvm:: { ClassInstanceRef , Jvm , Result } ;
5+ use jvm:: { Array , ClassInstanceRef , Jvm , Result } ;
66
7- use crate :: { RuntimeClassProto , RuntimeContext } ;
7+ use crate :: { RuntimeClassProto , RuntimeContext , classes :: java :: lang :: Object } ;
88
99// abstract class java.util.AbstractCollection
1010pub struct AbstractCollection ;
@@ -14,8 +14,18 @@ impl AbstractCollection {
1414 RuntimeClassProto {
1515 name : "java/util/AbstractCollection" ,
1616 parent_class : Some ( "java/lang/Object" ) ,
17- interfaces : vec ! [ ] ,
18- methods : vec ! [ JavaMethodProto :: new( "<init>" , "()V" , Self :: init, Default :: default ( ) ) ] ,
17+ interfaces : vec ! [ "java/util/Collection" ] ,
18+ methods : vec ! [
19+ JavaMethodProto :: new( "<init>" , "()V" , Self :: init, Default :: default ( ) ) ,
20+ JavaMethodProto :: new_abstract( "size" , "()I" , Default :: default ( ) ) ,
21+ JavaMethodProto :: new( "isEmpty" , "()Z" , Self :: is_empty, Default :: default ( ) ) ,
22+ JavaMethodProto :: new( "contains" , "(Ljava/lang/Object;)Z" , Self :: contains, Default :: default ( ) ) ,
23+ JavaMethodProto :: new_abstract( "iterator" , "()Ljava/util/Iterator;" , Default :: default ( ) ) ,
24+ JavaMethodProto :: new( "toArray" , "()[Ljava/lang/Object;" , Self :: to_array, Default :: default ( ) ) ,
25+ JavaMethodProto :: new( "add" , "(Ljava/lang/Object;)Z" , Self :: add, Default :: default ( ) ) ,
26+ JavaMethodProto :: new( "remove" , "(Ljava/lang/Object;)Z" , Self :: remove, Default :: default ( ) ) ,
27+ JavaMethodProto :: new( "clear" , "()V" , Self :: clear, Default :: default ( ) ) ,
28+ ] ,
1929 fields : vec ! [ ] ,
2030 access_flags : ClassAccessFlags :: ABSTRACT ,
2131 }
@@ -28,4 +38,84 @@ impl AbstractCollection {
2838
2939 Ok ( ( ) )
3040 }
41+
42+ async fn is_empty ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < bool > {
43+ tracing:: debug!( "java.util.AbstractCollection::isEmpty({this:?})" ) ;
44+
45+ let size: i32 = jvm. invoke_virtual ( & this, "size" , "()I" , ( ) ) . await ?;
46+
47+ Ok ( size == 0 )
48+ }
49+
50+ async fn contains ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , element : ClassInstanceRef < Object > ) -> Result < bool > {
51+ tracing:: debug!( "java.util.AbstractCollection::contains({this:?}, {element:?})" ) ;
52+
53+ let iterator: ClassInstanceRef < Object > = jvm. invoke_virtual ( & this, "iterator" , "()Ljava/util/Iterator;" , ( ) ) . await ?;
54+ loop {
55+ let has_next: bool = jvm. invoke_virtual ( & iterator, "hasNext" , "()Z" , ( ) ) . await ?;
56+ if !has_next {
57+ return Ok ( false ) ;
58+ }
59+
60+ let current: ClassInstanceRef < Object > = jvm. invoke_virtual ( & iterator, "next" , "()Ljava/lang/Object;" , ( ) ) . await ?;
61+ if Self :: object_equals ( jvm, & element, & current) . await ? {
62+ return Ok ( true ) ;
63+ }
64+ }
65+ }
66+
67+ async fn to_array ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < ClassInstanceRef < Array < Object > > > {
68+ tracing:: debug!( "java.util.AbstractCollection::toArray({this:?})" ) ;
69+
70+ let iterator: ClassInstanceRef < Object > = jvm. invoke_virtual ( & this, "iterator" , "()Ljava/util/Iterator;" , ( ) ) . await ?;
71+ let mut elements = vec ! [ ] ;
72+ loop {
73+ let has_next: bool = jvm. invoke_virtual ( & iterator, "hasNext" , "()Z" , ( ) ) . await ?;
74+ if !has_next {
75+ break ;
76+ }
77+
78+ let current: ClassInstanceRef < Object > = jvm. invoke_virtual ( & iterator, "next" , "()Ljava/lang/Object;" , ( ) ) . await ?;
79+ elements. push ( current) ;
80+ }
81+
82+ let mut array: ClassInstanceRef < Array < Object > > = jvm. instantiate_array ( "Ljava/lang/Object;" , elements. len ( ) ) . await ?. into ( ) ;
83+ if !elements. is_empty ( ) {
84+ jvm. store_array ( & mut array, 0 , elements) . await ?;
85+ }
86+
87+ Ok ( array)
88+ }
89+
90+ async fn add ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , element : ClassInstanceRef < Object > ) -> Result < bool > {
91+ tracing:: debug!( "java.util.AbstractCollection::add({this:?}, {element:?})" ) ;
92+
93+ Err ( jvm. exception ( "java/lang/UnsupportedOperationException" , "AbstractCollection.add" ) . await )
94+ }
95+
96+ async fn remove ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > , element : ClassInstanceRef < Object > ) -> Result < bool > {
97+ tracing:: debug!( "java.util.AbstractCollection::remove({this:?}, {element:?})" ) ;
98+
99+ Err ( jvm
100+ . exception ( "java/lang/UnsupportedOperationException" , "AbstractCollection.remove" )
101+ . await )
102+ }
103+
104+ async fn clear ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < ( ) > {
105+ tracing:: debug!( "java.util.AbstractCollection::clear({this:?})" ) ;
106+
107+ Err ( jvm. exception ( "java/lang/UnsupportedOperationException" , "AbstractCollection.clear" ) . await )
108+ }
109+
110+ async fn object_equals ( jvm : & Jvm , left : & ClassInstanceRef < Object > , right : & ClassInstanceRef < Object > ) -> Result < bool > {
111+ if left. is_null ( ) {
112+ return Ok ( right. is_null ( ) ) ;
113+ }
114+
115+ if right. is_null ( ) {
116+ return Ok ( false ) ;
117+ }
118+
119+ jvm. invoke_virtual ( left, "equals" , "(Ljava/lang/Object;)Z" , ( right. clone ( ) , ) ) . await
120+ }
31121}
0 commit comments