88use PHPJava \Exceptions \RuntimeException ;
99use PHPJava \Exceptions \UndefinedMethodException ;
1010use PHPJava \Exceptions \UndefinedOpCodeException ;
11+ use PHPJava \Imitation \java \lang \NoSuchMethodException ;
1112use PHPJava \Kernel \Attributes \AttributeInfo ;
1213use PHPJava \Kernel \Attributes \AttributeInterface ;
1314use PHPJava \Kernel \Attributes \CodeAttribute ;
1718use PHPJava \Kernel \Mnemonics \OperationInterface ;
1819use PHPJava \Kernel \Structures \_MethodInfo ;
1920use PHPJava \Utilities \Formatter ;
21+ use PHPJava \Utilities \TypeResolver ;
2022
2123trait Invokable
2224{
@@ -55,8 +57,8 @@ public function call(string $name, ...$arguments)
5557 /**
5658 * @var _MethodInfo|null $method
5759 */
58- $ methods = $ this ->methods [$ name ] ?? null ;
59- if ($ methods === null ) {
60+ $ methodReferences = $ this ->methods [$ name ] ?? null ;
61+ if ($ methodReferences === null ) {
6062 throw new UndefinedMethodException ('Call to undefined ' . $ name . ' method. ' );
6163 }
6264
@@ -65,29 +67,29 @@ public function call(string $name, ...$arguments)
6567 ->getConstantPool ()
6668 ->getEntries ();
6769
68- $ method = $ methods [0 ];
69-
70- // TODO: Refactor find to valid method.
71- // will implement to be compatible to multiple variable arguments size.
72- // And will be applied NoSuchMethodException when cannot find method.
73- if ($ name === 'main ' ) {
74- // Find same method
75- foreach ($ methods as $ method ) {
76- $ signature = Formatter::parseSignature ($ constantPool [$ method ->getDescriptorIndex ()]->getString ());
77-
78- // compare passed arguments
79- foreach ($ signature ['arguments ' ] as $ signatureArgument ) {
80- foreach ($ arguments as $ argument ) {
81- var_dump (gettype ($ argument ), $ signatureArgument );
82- if ($ argument === $ signatureArgument ) {
83-
84- }
85- }
86- }
87- var_dump ();
70+ // Find same method
71+ $ convertedPassedArguments = Formatter::buildArgumentsSignature (
72+ array_map (
73+ function ($ argument ) {
74+ return TypeResolver::convertPHPtoJava ($ argument );
75+ },
76+ $ arguments
77+ )
78+ );
79+
80+ $ method = null ;
81+ foreach ($ methodReferences as $ methodReference ) {
82+ $ methodSignature = Formatter::buildArgumentsSignature (
83+ Formatter::parseSignature ($ constantPool [$ methodReference ->getDescriptorIndex ()]->getString ())['arguments ' ]
84+ );
85+ if ($ methodSignature === $ convertedPassedArguments ) {
86+ $ method = $ methodReference ;
87+ break ;
8888 }
89+ }
8990
90- exit ();
91+ if ($ method === null ) {
92+ throw new NoSuchMethodException ('Call to undefined method ' . $ name . '. ' );
9193 }
9294
9395 $ codeAttribute = $ getCodeAttribute ($ method ->getAttributes ());
0 commit comments