« Core/Handler/CommandHandler/InvokeStaticMethodHandler.pm
↑ Summary
Core/Protocol/TypeSerializer.pm »
| 1 | package Javonet::Sdk::Internal::InvocationContext; | ||
| 2 | use strict; | ||
| 3 | use warnings FATAL => 'all'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use aliased 'Javonet::Sdk::Core::PerlCommand' => 'PerlCommand'; | ||
| 7 | use aliased 'Javonet::Core::Handler::PerlHandler' => 'PerlHandler'; | ||
| 8 | use aliased 'Javonet::Core::Interpreter::Interpreter' => 'Interpreter', qw(execute_); | ||
| 9 | use aliased 'Javonet::Core::Exception::ExceptionThrower' => 'ExceptionThrower'; | ||
| 10 | use aliased 'Javonet::Sdk::Core::TypesHandler' => 'TypesHandler'; | ||
| 11 | |||
| 12 | extends 'Javonet::Sdk::Internal::Abstract::AbstractInstanceContext', | ||
| 13 | 'Javonet::Sdk::Internal::Abstract::AbstractMethodInvocationContext', | ||
| 14 | 'Javonet::Sdk::Internal::Abstract::AbstractInvocationContext'; | ||
| 15 | |||
| 16 | sub new { | ||
| 17 | my $class = shift; | ||
| 18 | |||
| 19 | my $self = { | ||
| 20 | runtime_name => shift, | ||
| 21 | connection_type => shift, | ||
| 22 | tcp_address => shift, | ||
| 23 | current_command => shift, | ||
| 24 | isExecuted => shift, | ||
| 25 | response_command => 0, | ||
| 26 | }; | ||
| 27 | |||
| 28 | |||
| 29 | bless $self, $class; | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | # DESTROY { | ||
| 34 | # my $self = $_[0]; | ||
| 35 | # if ($self->{current_command}->{command_type} == Javonet::Sdk::Core::PerlCommandType::get_command_type('Reference') && | ||
| 36 | # $self->{isExecuted} == 1) { | ||
| 37 | # $self->{current_command} = PerlCommand->new( | ||
| 38 | # runtime => $self->{runtime_name}, | ||
| 39 | # command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('DestructReference'), | ||
| 40 | # payload => $self->{current_command}->{payload} | ||
| 41 | # ); | ||
| 42 | # $self->execute(); | ||
| 43 | # } | ||
| 44 | # } | ||
| 45 | |||
| 46 | #@override | ||
| 47 | sub execute { | ||
| 48 | my $self = $_[0]; | ||
| 49 | $self->{response_command} = Interpreter->execute_($self->{current_command}, $self->{connection_type}, $self->{tcp_address}); | ||
| 50 | |||
| 51 | if ($self->{response_command}->{command_type} == Javonet::Sdk::Core::PerlCommandType::get_command_type('Exception')) { | ||
| 52 | ExceptionThrower->throwException($self->{response_command}) | ||
| 53 | } | ||
| 54 | |||
| 55 | if ($self->{current_command}->{command_type} == Javonet::Sdk::Core::PerlCommandType::get_command_type('CreateClassInstance')) { | ||
| 56 | $self->{current_command} = $self->{response_command}; | ||
| 57 | $self->{isExecuted} = 1; | ||
| 58 | return $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 62 | $self->{runtime_name}, | ||
| 63 | $self->{connection_type}, | ||
| 64 | $self->{tcp_address}, | ||
| 65 | $self->{response_command}, | ||
| 66 | 1 | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | #@override | ||
| 72 | sub invoke_instance_method { | ||
| 73 | my ($self, @arguments) = @_; | ||
| 74 | my $command = PerlCommand->new( | ||
| 75 | runtime => $self->{runtime_name}, | ||
| 76 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('InvokeInstanceMethod'), | ||
| 77 | payload => \@arguments | ||
| 78 | ); | ||
| 79 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 80 | $self->{runtime_name}, | ||
| 81 | $self->{connection_type}, | ||
| 82 | $self->{tcp_address}, | ||
| 83 | $self->build_command($command), | ||
| 84 | 0 | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | |||
| 88 | #@override | ||
| 89 | sub get_instance_field { | ||
| 90 | my ($self, @arguments) = @_; | ||
| 91 | my $command = PerlCommand->new( | ||
| 92 | runtime => $self->{runtime_name}, | ||
| 93 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetInstanceField'), | ||
| 94 | payload => \@arguments | ||
| 95 | ); | ||
| 96 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 97 | $self->{runtime_name}, | ||
| 98 | $self->{connection_type}, | ||
| 99 | $self->{tcp_address}, | ||
| 100 | $self->build_command($command), | ||
| 101 | 0 | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | |||
| 105 | #@override | ||
| 106 | sub set_instance_field { | ||
| 107 | my ($self, @arguments) = @_; | ||
| 108 | my $command = PerlCommand->new( | ||
| 109 | runtime => $self->{runtime_name}, | ||
| 110 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('SetInstanceField'), | ||
| 111 | payload => \@arguments | ||
| 112 | ); | ||
| 113 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 114 | $self->{runtime_name}, | ||
| 115 | $self->{connection_type}, | ||
| 116 | $self->{tcp_address}, | ||
| 117 | $self->build_command($command), | ||
| 118 | 0 | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | #@override | ||
| 123 | sub create_instance { | ||
| 124 | my ($self, @arguments) = @_; | ||
| 125 | my $command = PerlCommand->new( | ||
| 126 | runtime => $self->{runtime_name}, | ||
| 127 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('CreateClassInstance'), | ||
| 128 | payload => \@arguments | ||
| 129 | ); | ||
| 130 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 131 | $self->{runtime_name}, | ||
| 132 | $self->{connection_type}, | ||
| 133 | $self->{tcp_address}, | ||
| 134 | $self->build_command($command), | ||
| 135 | 0 | ||
| 136 | ); | ||
| 137 | } | ||
| 138 | |||
| 139 | #@override | ||
| 140 | sub invoke_static_method { | ||
| 141 | my ($self, @arguments) = @_; | ||
| 142 | my $command = PerlCommand->new( | ||
| 143 | runtime => $self->{runtime_name}, | ||
| 144 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('InvokeStaticMethod'), | ||
| 145 | payload => \@arguments | ||
| 146 | ); | ||
| 147 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 148 | $self->{runtime_name}, | ||
| 149 | $self->{connection_type}, | ||
| 150 | $self->{tcp_address}, | ||
| 151 | $self->build_command($command), | ||
| 152 | 0 | ||
| 153 | ); | ||
| 154 | } | ||
| 155 | |||
| 156 | #@override | ||
| 157 | sub set_generic_type { | ||
| 158 | } | ||
| 159 | |||
| 160 | #@override | ||
| 161 | sub get_static_field { | ||
| 162 | my ($self, @arguments) = @_; | ||
| 163 | my $command = PerlCommand->new( | ||
| 164 | runtime => $self->{runtime_name}, | ||
| 165 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetStaticField'), | ||
| 166 | payload => \@arguments | ||
| 167 | ); | ||
| 168 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 169 | $self->{runtime_name}, | ||
| 170 | $self->{connection_type}, | ||
| 171 | $self->{tcp_address}, | ||
| 172 | $self->build_command($command), | ||
| 173 | 0 | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | |||
| 177 | #@override | ||
| 178 | sub set_static_field { | ||
| 179 | my ($self, @arguments) = @_; | ||
| 180 | my $command = PerlCommand->new( | ||
| 181 | runtime => $self->{runtime_name}, | ||
| 182 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('SetStaticField'), | ||
| 183 | payload => \@arguments | ||
| 184 | ); | ||
| 185 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 186 | $self->{runtime_name}, | ||
| 187 | $self->{connection_type}, | ||
| 188 | $self->{tcp_address}, | ||
| 189 | $self->build_command($command), | ||
| 190 | 0 | ||
| 191 | ); | ||
| 192 | } | ||
| 193 | |||
| 194 | sub get_index { | ||
| 195 | my ($self, @arguments) = @_; | ||
| 196 | my $command = PerlCommand->new( | ||
| 197 | runtime => $self->{runtime_name}, | ||
| 198 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('ArrayGetItem'), | ||
| 199 | payload => \@arguments | ||
| 200 | ); | ||
| 201 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 202 | $self->{runtime_name}, | ||
| 203 | $self->{connection_type}, | ||
| 204 | $self->{tcp_address}, | ||
| 205 | $self->build_command($command), | ||
| 206 | 0 | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub get_size { | ||
| 211 | my ($self, @arguments) = @_; | ||
| 212 | my $command = PerlCommand->new( | ||
| 213 | runtime => $self->{runtime_name}, | ||
| 214 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('ArrayGetSize'), | ||
| 215 | payload => \@arguments | ||
| 216 | ); | ||
| 217 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 218 | $self->{runtime_name}, | ||
| 219 | $self->{connection_type}, | ||
| 220 | $self->{tcp_address}, | ||
| 221 | $self->build_command($command), | ||
| 222 | 0 | ||
| 223 | ); | ||
| 224 | } | ||
| 225 | |||
| 226 | sub get_rank { | ||
| 227 | my ($self, @arguments) = @_; | ||
| 228 | my $command = PerlCommand->new( | ||
| 229 | runtime => $self->{runtime_name}, | ||
| 230 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('ArrayGetRank'), | ||
| 231 | payload => \@arguments | ||
| 232 | ); | ||
| 233 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 234 | $self->{runtime_name}, | ||
| 235 | $self->{connection_type}, | ||
| 236 | $self->{tcp_address}, | ||
| 237 | $self->build_command($command), | ||
| 238 | 0 | ||
| 239 | ); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub set_index { | ||
| 243 | my ($self, @arguments) = @_; | ||
| 244 | my $command = PerlCommand->new( | ||
| 245 | runtime => $self->{runtime_name}, | ||
| 246 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('ArraySetItem'), | ||
| 247 | payload => \@arguments | ||
| 248 | ); | ||
| 249 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 250 | $self->{runtime_name}, | ||
| 251 | $self->{connection_type}, | ||
| 252 | $self->{tcp_address}, | ||
| 253 | $self->build_command($command), | ||
| 254 | 0 | ||
| 255 | ); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub invoke_generic_static_method { | ||
| 259 | my ($self, @arguments) = @_; | ||
| 260 | my $command = PerlCommand->new( | ||
| 261 | runtime => $self->{runtime_name}, | ||
| 262 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('InvokeGenericStaticMethod'), | ||
| 263 | payload => \@arguments | ||
| 264 | ); | ||
| 265 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 266 | $self->{runtime_name}, | ||
| 267 | $self->{connection_type}, | ||
| 268 | $self->{tcp_address}, | ||
| 269 | $self->build_command($command), | ||
| 270 | 0 | ||
| 271 | ); | ||
| 272 | } | ||
| 273 | |||
| 274 | sub invoke_generic_method { | ||
| 275 | my ($self, @arguments) = @_; | ||
| 276 | my $command = PerlCommand->new( | ||
| 277 | runtime => $self->{runtime_name}, | ||
| 278 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('InvokeGenericMethod'), | ||
| 279 | payload => \@arguments | ||
| 280 | ); | ||
| 281 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 282 | $self->{runtime_name}, | ||
| 283 | $self->{connection_type}, | ||
| 284 | $self->{tcp_address}, | ||
| 285 | $self->build_command($command), | ||
| 286 | 0 | ||
| 287 | ); | ||
| 288 | } | ||
| 289 | |||
| 290 | #@override | ||
| 291 | sub get_enum_name { | ||
| 292 | my ($self, @arguments) = @_; | ||
| 293 | my $command = PerlCommand->new( | ||
| 294 | runtime => $self->{runtime_name}, | ||
| 295 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetEnumName'), | ||
| 296 | payload => \@arguments | ||
| 297 | ); | ||
| 298 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 299 | $self->{runtime_name}, | ||
| 300 | $self->{connection_type}, | ||
| 301 | $self->{tcp_address}, | ||
| 302 | $self->build_command($command), | ||
| 303 | 0 | ||
| 304 | ); | ||
| 305 | } | ||
| 306 | |||
| 307 | #@override | ||
| 308 | sub get_enum_value { | ||
| 309 | my ($self, @arguments) = @_; | ||
| 310 | my $command = PerlCommand->new( | ||
| 311 | runtime => $self->{runtime_name}, | ||
| 312 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetEnumValue'), | ||
| 313 | payload => \@arguments | ||
| 314 | ); | ||
| 315 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 316 | $self->{runtime_name}, | ||
| 317 | $self->{connection_type}, | ||
| 318 | $self->{tcp_address}, | ||
| 319 | $self->build_command($command), | ||
| 320 | 0 | ||
| 321 | ); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub get_ref_value { | ||
| 325 | my ($self, @arguments) = @_; | ||
| 326 | my $command = PerlCommand->new( | ||
| 327 | runtime => $self->{runtime_name}, | ||
| 328 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetRefValue'), | ||
| 329 | payload => \@arguments | ||
| 330 | ); | ||
| 331 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 332 | $self->{runtime_name}, | ||
| 333 | $self->{connection_type}, | ||
| 334 | $self->{tcp_address}, | ||
| 335 | $self->build_command($command), | ||
| 336 | 0 | ||
| 337 | ); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub create_null { | ||
| 341 | my ($self, @arguments) = @_; | ||
| 342 | my $command = PerlCommand->new( | ||
| 343 | runtime => $self->{runtime_name}, | ||
| 344 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('CreateNull'), | ||
| 345 | payload => \@arguments | ||
| 346 | ); | ||
| 347 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 348 | $self->{runtime_name}, | ||
| 349 | $self->{connection_type}, | ||
| 350 | $self->{tcp_address}, | ||
| 351 | $self->build_command($command), | ||
| 352 | 0 | ||
| 353 | ); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub get_static_method_as_delegate { | ||
| 357 | my ($self, @arguments) = @_; | ||
| 358 | my $command = PerlCommand->new( | ||
| 359 | runtime => $self->{runtime_name}, | ||
| 360 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetStaticMethodAsDelegate'), | ||
| 361 | payload => \@arguments | ||
| 362 | ); | ||
| 363 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 364 | $self->{runtime_name}, | ||
| 365 | $self->{connection_type}, | ||
| 366 | $self->{tcp_address}, | ||
| 367 | $self->build_command($command), | ||
| 368 | 0 | ||
| 369 | ); | ||
| 370 | } | ||
| 371 | |||
| 372 | sub get_instance_method_as_delegate { | ||
| 373 | my ($self, @arguments) = @_; | ||
| 374 | my $command = PerlCommand->new( | ||
| 375 | runtime => $self->{runtime_name}, | ||
| 376 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('GetInstanceMethodAsDelegate'), | ||
| 377 | payload => \@arguments | ||
| 378 | ); | ||
| 379 | return Javonet::Sdk::Internal::InvocationContext->new( | ||
| 380 | $self->{runtime_name}, | ||
| 381 | $self->{connection_type}, | ||
| 382 | $self->{tcp_address}, | ||
| 383 | $self->build_command($command), | ||
| 384 | 0 | ||
| 385 | ); | ||
| 386 | } | ||
| 387 | |||
| 388 | sub get_current_command { | ||
| 389 | my $self = shift; | ||
| 390 | return $self->{current_command}; | ||
| 391 | } | ||
| 392 | |||
| 393 | #@override | ||
| 394 | sub get_value { | ||
| 395 | my $self = shift; | ||
| 396 | return $self->{current_command}->{payload}[0] | ||
| 397 | } | ||
| 398 | |||
| 399 | sub build_command { | ||
| 400 | my ($self, $command) = @_; | ||
| 401 | my $payload_length = @{$command->{payload}}; | ||
| 402 | for (my $i = 0; $i < $payload_length; $i++) { | ||
| 403 | $command->{payload}[$i] = $self->encapsulate_payload_item($command->{payload}[$i]); | ||
| 404 | } | ||
| 405 | return $command->prepend_arg_to_payload($self->{current_command}); | ||
| 406 | } | ||
| 407 | |||
| 408 | sub encapsulate_payload_item { | ||
| 409 | my ($self, $payload_item) = @_; | ||
| 410 | |||
| 411 | if (!defined $payload_item) { | ||
| 412 | return PerlCommand->new( | ||
| 413 | runtime => $self->{runtime_name}, | ||
| 414 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Value'), | ||
| 415 | payload => [] | ||
| 416 | ); | ||
| 417 | } | ||
| 418 | if (ref($payload_item) eq 'ARRAY') { | ||
| 419 | my $payload_length = @$payload_item; | ||
| 420 | for (my $i = 0; $i < $payload_length; $i++) { | ||
| 421 | $payload_item->[$i] = $self->encapsulate_payload_item($payload_item->[$i]); | ||
| 422 | } | ||
| 423 | return PerlCommand->new( | ||
| 424 | runtime => $self->{runtime_name}, | ||
| 425 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Array'), | ||
| 426 | payload => $payload_item | ||
| 427 | ); | ||
| 428 | } | ||
| 429 | elsif ($payload_item->isa('Command')) { | ||
| 430 | my $payload_length = @{$payload_item->{payload}}; | ||
| 431 | for (my $i = 0; $i < $payload_length; $i++) { | ||
| 432 | $payload_item->{payload}[$i] = $self->encapsulate_payload_item($payload_item->{payload}[$i]); | ||
| 433 | } | ||
| 434 | return $payload_item; | ||
| 435 | } | ||
| 436 | elsif ($payload_item->isa('Javonet::Sdk::Internal::InvocationContext')) { | ||
| 437 | return $payload_item->get_current_command(); | ||
| 438 | } | ||
| 439 | elsif (TypesHandler->is_primitive_or_none($payload_item)) { | ||
| 440 | return PerlCommand->new( | ||
| 441 | runtime => $self->{runtime_name}, | ||
| 442 | command_type => Javonet::Sdk::Core::PerlCommandType::get_command_type('Value'), | ||
| 443 | payload => [ $payload_item ] | ||
| 444 | ); | ||
| 445 | } | ||
| 446 | else { | ||
| 447 | die "Unsupported payload item type: " . ref($payload_item) . " for payload item: $payload_item."; | ||
| 448 | } | ||
| 449 | } | ||
| 450 | |||
| 451 | no Moose; | ||
| 452 | 1; |