/usr/lib/perl5/Inline/Java/Object.pm is in libinline-java-perl 0.53-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | package Inline::Java::Object ;
@Inline::Java::Object::ISA = qw(Inline::Java::Object::Tie) ;
use strict ;
use Inline::Java::Protocol ;
use Carp ;
$Inline::Java::Object::VERSION = '0.53' ;
# Here we store as keys the knots and as values our blessed private objects
my $PRIVATES = {} ;
# Bogus constructor. We fall here if no public constructor is defined
# in the Java class.
sub new {
my $class = shift ;
croak "No public constructor defined for class $class" ;
}
# Constructor. Here we create a new object that will be linked
# to a real Java object.
sub __new {
my $class = shift ;
my $java_class = shift ;
my $inline = shift ;
my $objid = shift ;
my $proto = shift ;
my $args = shift ;
my %this = () ;
my $knot = tie %this, $class ;
my $this = bless(\%this, $class) ;
my $pkg = $inline->get_api('pkg') ;
if ($class ne "Inline::Java::Object"){
$class = Inline::Java::java2perl($pkg, $java_class) ;
}
my $priv = Inline::Java::Object::Private->new($class, $java_class, $inline) ;
$PRIVATES->{$knot} = $priv ;
if ($objid <= -1){
my $obj = undef ;
eval {
$obj = $this->__get_private()->{proto}->CreateJavaObject($java_class, $proto, $args) ;
} ;
croak $@ if $@ ;
if (! defined($this->__get_private()->{id})){
# Use created a java::lang::String or something...
return $obj ;
}
}
else{
$this->__get_private()->{id} = $objid ;
Inline::Java::debug(2, "creating object in java ($class):") ;
}
Inline::Java::debug_obj($this) ;
return $this ;
}
sub __get_private {
my $this = shift ;
my $knot = tied(%{$this}) || $this ;
my $priv = $PRIVATES->{$knot} ;
if (! defined($priv)){
croak "Unknown Java object reference $knot" ;
}
return $priv ;
}
# Checks to make sure all the arguments can be "cast" to prototype
# types.
sub __validate_prototype {
my $this = shift ;
my $method = shift ;
my $args = shift ;
my $protos = shift ;
my $inline = shift ;
my @matched = () ;
my @proto_values = values %{$protos} ;
my @errors = () ;
foreach my $s (@proto_values){
my $proto = $s->{SIGNATURE} ;
my $stat = $s->{STATIC} ;
my $idx = $s->{IDX} ;
my $new_args = undef ;
my $score = undef ;
my $sig = Inline::Java::Protocol->CreateSignature($proto) ;
Inline::Java::debug(3, "matching arguments to $method$sig") ;
eval {
($new_args, $score) = Inline::Java::Class::CastArguments($args, $proto, $inline) ;
} ;
if ($@){
if (scalar(@proto_values) == 1){
# Here we have only 1 prototype, so we return the error.
croak $@ ;
}
push @errors, $@ ;
Inline::Java::debug(3, "error trying to fit args to prototype: $@") ;
next ;
}
# We passed!
Inline::Java::debug(3, "match successful: score is $score") ;
my $h = {
PROTO => $proto,
NEW_ARGS => $new_args,
NB_ARGS => scalar(@{$new_args}),
SCORE => $score,
STATIC => $stat,
IDX => $idx,
} ;
# Tiny optimization: abort if type coerce was used and matched for
# every parameter
if (Inline::Java::Class::IsMaxArgumentsScore($new_args, $score)){
Inline::Java::debug(3, "perfect match found, aborting search") ;
@matched = () ;
push @matched, $h ;
last ;
}
else{
push @matched, $h ;
}
}
my $nb_matched = scalar(@matched) ;
if (! $nb_matched){
my $name = (ref($this) ? $this->__get_private()->{class} : $this) ;
my $sa = Inline::Java::Protocol->CreateSignature($args) ;
my $msg = "In method $method of class $name: Can't find any signature that matches " .
"the arguments passed $sa.\nAvailable signatures are:\n" ;
my $i = 0 ;
foreach my $s (@proto_values){
my $proto = $s->{SIGNATURE} ;
my $static = ($s->{STATIC} ? "static " : "") ;
my $sig = Inline::Java::Protocol->CreateSignature($proto) ;
$msg .= "\t$static$method$sig\n" ;
$msg .= "\t\terror was: $errors[$i]" ;
$i++ ;
}
chomp $msg ;
croak $msg ;
}
my $chosen = undef ;
foreach my $h (@matched){
my $idx = ($chosen ? $chosen->{IDX} : 0) ;
my $max = ($chosen ? $chosen->{SCORE} : 0) ;
my $s = $h->{SCORE} ;
my $i = $h->{IDX} ;
if ($s > $max){
$chosen = $h ;
}
elsif ($s == $max){
# Here if the scores are equal we take the last one since
# we start with inherited methods and move to class mothods
if ($i > $idx){
$chosen = $h ;
}
}
}
if ((! $chosen->{STATIC})&&(! ref($this))){
# We are trying to call an instance method without an object
# reference
croak "Method $method of class $this must be called from an object reference" ;
}
# Here we will be polite and warn the user if we had to choose a
# method by ourselves.
if ($inline->get_java_config('WARN_METHOD_SELECT')){
if (($nb_matched > 1)&&
($chosen->{SCORE} < ($chosen->{NB_ARGS} * 10))){
my $msg = "Based on the arguments passed, I had to choose between " .
"the following method signatures:\n" ;
foreach my $m (@matched){
my $s = Inline::Java::Protocol->CreateSignature($m->{PROTO}) ;
my $c = ($m eq $chosen ? "*" : " ") ;
$msg .= " $c $method$s\n" ;
}
$msg .= "I chose the one indicated by a star (*). To force " .
"the use of another signature or to disable this warning, use " .
"the casting functionality described in the documentation." ;
carp $msg ;
}
}
return (
$chosen->{PROTO},
$chosen->{NEW_ARGS},
$chosen->{STATIC},
) ;
}
sub __isa {
my $this = shift ;
my $proto = shift ;
my $ret = undef ;
eval {
$ret = $this->__get_private()->{proto}->ISA($proto) ;
} ;
if ($@){
return ($@, 0) ;
}
if ($ret == -1){
my $c = $this->__get_private()->{java_class} ;
return ("$c is not a kind of $proto", 0) ;
}
return ('', $ret) ;
}
sub __cast {
my $this = shift ;
my $class = shift ;
my $ret = $this->__get_private()->{proto}->Cast($class) ;
return $ret ;
}
sub __get_member {
my $this = shift ;
my $key = shift ;
if ($this->__get_private()->{class} eq "Inline::Java::Object"){
croak "Can't get member '$key' for an object that is not bound to Perl" ;
}
Inline::Java::debug(3, "fetching member variable '$key'") ;
my $inline = $this->__get_private()->{inline} ;
my $fields = $inline->get_fields($this->__get_private()->{class}) ;
my $types = $fields->{$key} ;
if ($types){
# We take the last one, which is more specific. Eventually
# we should use a scoring method just like for the methods
my $sign = undef ;
foreach my $s (values %{$types}){
if ((! defined($sign))||($s->{IDX} > $sign->{IDX})){
$sign = $s ;
}
}
my $proto = $sign->{TYPE} ;
my $ret = $this->__get_private()->{proto}->GetJavaMember($key, [$proto], [undef]) ;
Inline::Java::debug(3, "returning member (" . ($ret || '') . ")") ;
return $ret ;
}
else{
my $name = $this->__get_private()->{class} ;
croak "No public member variable '$key' defined for class '$name'" ;
}
}
sub __set_member {
my $this = shift ;
my $key = shift ;
my $value = shift ;
if ($this->__get_private()->{class} eq "Inline::Java::Object"){
croak "Can't set member '$key' for an object that is not bound to Perl" ;
}
my $inline = $this->__get_private()->{inline} ;
my $fields = $inline->get_fields($this->__get_private()->{class}) ;
my $types = $fields->{$key} ;
if ($types){
# We take the last one, which is more specific. Eventually
# we should use a scoring method just like for the methods
my $sign = undef ;
foreach my $s (values %{$types}){
if ((! defined($sign))||($s->{IDX} > $sign->{IDX})){
$sign = $s ;
}
}
my $proto = $sign->{TYPE} ;
my $new_args = undef ;
my $score = undef ;
($new_args, $score) = Inline::Java::Class::CastArguments([$value], [$proto], $this->__get_private()->{inline}) ;
$this->__get_private()->{proto}->SetJavaMember($key, [$proto], $new_args) ;
}
else{
my $name = $this->__get_private()->{class} ;
croak "No public member variable '$key' defined for class '$name'" ;
}
}
sub AUTOLOAD {
my $this = shift ;
my @args = @_ ;
use vars qw($AUTOLOAD) ;
my $func_name = $AUTOLOAD ;
# Strip package from $func_name, Java will take of finding the correct
# method.
$func_name =~ s/^(.*)::// ;
Inline::Java::debug(5, "$func_name") ;
my $name = (ref($this) ? $this->__get_private()->{class} : $this) ;
if ($name eq "Inline::Java::Object"){
croak "Can't call method '$func_name' on an object ($name) that is not bound to Perl" ;
}
croak "No public method '$func_name' defined for class '$name'" ;
}
sub DESTROY {
my $this = shift ;
my $knot = tied %{$this} ;
if (! $knot){
Inline::Java::debug(4, "destroying Inline::Java::Object::Tie") ;
if (! Inline::Java::get_DONE()){
my $class = $this->__get_private()->{class} ;
Inline::Java::debug(2, "destroying object in java ($class):") ;
{
local $@ ;
eval {
$this->__get_private()->{proto}->DeleteJavaObject($this) ;
} ;
if ($@){
# We croaked here. Was there already a pending $@?
my $name = $this->__get_private()->{class} ;
croak "In method DESTROY of class $name: $@" ;
}
}
# Here we have a circular reference so we need to break it
# so that the memory is collected.
my $priv = $this->__get_private() ;
my $proto = $priv->{proto} ;
$priv->{proto} = undef ;
$proto->{obj_priv} = undef ;
$PRIVATES->{$this} = undef ;
}
else{
Inline::Java::debug(4, "script marked as DONE, object destruction not propagated to Java") ;
}
}
else{
Inline::Java::debug(4, "destroying Inline::Java::Object") ;
}
}
######################## Hash Methods ########################
package Inline::Java::Object::Tie ;
@Inline::Java::Object::Tie::ISA = qw(Tie::StdHash) ;
use Tie::Hash ;
use Carp ;
sub TIEHASH {
my $class = shift ;
return $class->SUPER::TIEHASH(@_) ;
}
sub STORE {
my $this = shift ;
my $key = shift ;
my $value = shift ;
return $this->__set_member($key, $value) ;
}
sub FETCH {
my $this = shift ;
my $key = shift ;
return $this->__get_member($key) ;
}
sub FIRSTKEY {
my $this = shift ;
return $this->SUPER::FIRSTKEY() ;
}
sub NEXTKEY {
my $this = shift ;
return $this->SUPER::NEXTKEY() ;
}
sub EXISTS {
my $this = shift ;
my $key = shift ;
my $inline = $this->__get_private()->{inline} ;
my $fields = $inline->get_fields($this->__get_private()->{class}) ;
if ($fields->{$key}){
return 1 ;
}
return 0 ;
}
sub DELETE {
my $this = shift ;
my $key = shift ;
croak "Operation DELETE not supported on Java object" ;
}
sub CLEAR {
my $this = shift ;
croak "Operation CLEAR not supported on Java object" ;
}
sub DESTROY {
my $this = shift ;
}
######################## Static Member Methods ########################
package Inline::Java::Object::StaticMember ;
@Inline::Java::Object::StaticMember::ISA = qw(Tie::StdScalar) ;
use Tie::Scalar ;
use Carp ;
my $DUMMIES = {} ;
sub TIESCALAR {
my $class = shift ;
my $dummy = shift ;
my $name = shift ;
my $this = $class->SUPER::TIESCALAR(@_) ;
$DUMMIES->{$this} = [$dummy, $name] ;
return $this ;
}
sub STORE {
my $this = shift ;
my $value = shift ;
my ($obj, $key) = @{$DUMMIES->{$this}} ;
return $obj->__set_member($key, $value) ;
}
sub FETCH {
my $this = shift ;
my ($obj, $key) = @{$DUMMIES->{$this}} ;
return $obj->__get_member($key) ;
}
sub DESTROY {
my $this = shift ;
}
######################## Private Object ########################
package Inline::Java::Object::Private ;
sub new {
my $class = shift ;
my $obj_class = shift ;
my $java_class = shift ;
my $inline = shift ;
my $this = {} ;
$this->{class} = $obj_class ;
$this->{java_class} = $java_class ;
$this->{inline} = $inline ;
$this->{proto} = new Inline::Java::Protocol($this, $inline) ;
bless($this, $class) ;
return $this ;
}
sub DESTROY {
my $this = shift ;
Inline::Java::debug(4, "destroying Inline::Java::Object::Private") ;
}
1 ;
|