This file is indexed.

/usr/lib/perl5/Inline/Java.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
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
package Inline::Java ;
@Inline::Java::ISA = qw(Inline Exporter) ;

# Export the cast function if wanted
@EXPORT_OK = qw(cast coerce study_classes caught jar j2sdk) ;


use strict ;
require 5.006 ;

$Inline::Java::VERSION = '0.53' ;


# DEBUG is set via the DEBUG config
if (! defined($Inline::Java::DEBUG)){
	$Inline::Java::DEBUG = 0 ;
}

# Set DEBUG stream
*DEBUG_STREAM = *STDERR ;

require Inline ;
use Carp ;
use Config ;
use File::Copy ;
use File::Spec ;
use Cwd ;
use Data::Dumper ;

use Inline::Java::Portable ;
use Inline::Java::Class ;
use Inline::Java::Object ;
use Inline::Java::Array ;
use Inline::Java::Handle ;
use Inline::Java::Protocol ;
use Inline::Java::Callback ;
# Must be last.
use Inline::Java::JVM ;
# Our default J2SK
require Inline::Java->find_default_j2sdk() ;


# This is set when the script is over.
my $DONE = 0 ;

# This is set when at least one JVM is loaded.
my $JVM = undef ;

# This list will store the $o objects...
my @INLINES = () ;

my $report_version = "V2" ;

# This stuff is to control the termination of the Java Interpreter
sub done {
	my $signal = shift ;

	# To preserve the passed exit code...
	my $ec = $? ;

	$DONE = 1 ;

	if (! $signal){
		Inline::Java::debug(1, "killed by natural death.") ;
	}
	else{
		Inline::Java::debug(1, "killed by signal SIG$signal.") ;
	}

	shutdown_JVM() ;
	Inline::Java::debug(1, "exiting with $ec") ;
	CORE::exit($ec) ;
	exit($ec) ;
}


END {
	if ($DONE < 1){
		done() ;
	}
}


# To export the cast function and others.
sub import {
	my $class = shift ;

	foreach my $a (@_){
		if ($a eq 'jar'){
			print Inline::Java::Portable::get_server_jar() ;
			exit() ;
		}
		elsif ($a eq 'j2sdk'){
			print Inline::Java->find_default_j2sdk() . " says '" .
				Inline::Java::get_default_j2sdk() . "'\n" ;
			exit() ;
		}
		elsif ($a eq 'so_dirs'){
			print Inline::Java::Portable::portable('SO_LIB_PATH_VAR') . "=" . 
				join(Inline::Java::Portable::portable('ENV_VAR_PATH_SEP'), 
				Inline::Java::get_default_j2sdk_so_dirs()) ;
			exit() ;
		}
	}
    $class->export_to_level(1, $class, @_) ;
}



######################## Inline interface ########################



# Register this module as an Inline language support module
sub register {
	return {
		language => 'Java',
		aliases => ['JAVA', 'java'],
		type => 'interpreted',
		suffix => 'jdat',
	} ;
}


# Here validate is overridden because some of the config options are needed
# at load as well.
sub validate {
	my $o = shift ;

	# This might not print since debug is set further down...
	Inline::Java::debug(1, "Starting validate.") ;
	
	my $jdk = Inline::Java::get_default_j2sdk() ;
	my $dbg = $Inline::Java::DEBUG ;
	my %opts = @_ ;
	$o->set_option('DEBUG',					$dbg,			'i', 1, \%opts) ;
	$o->set_option('J2SDK',					$jdk,			's', 1, \%opts) ;
	$o->set_option('CLASSPATH',				'',				's', 1, \%opts) ;

	$o->set_option('BIND',					'localhost',	's', 1, \%opts) ;
	$o->set_option('HOST',					'localhost',	's', 1, \%opts) ;
	$o->set_option('PORT',					-1,				'i', 1, \%opts) ;
	$o->set_option('STARTUP_DELAY',			15,				'i', 1, \%opts) ;
	$o->set_option('SHARED_JVM',			0,				'b', 1, \%opts) ;
	$o->set_option('START_JVM',				1,				'b', 1, \%opts) ;
	$o->set_option('JNI',					0,				'b', 1, \%opts) ;
	$o->set_option('EMBEDDED_JNI',			0,				'b', 1, \%opts) ;
	$o->set_option('NATIVE_DOUBLES',		0,				'b', 1, \%opts) ;

	$o->set_option('WARN_METHOD_SELECT',	0,				'b', 1, \%opts) ;
	$o->set_option('STUDY',					undef,			'a', 0, \%opts) ;
	$o->set_option('AUTOSTUDY',				0,				'b', 1, \%opts) ;

	$o->set_option('EXTRA_JAVA_ARGS',		'',				's', 1, \%opts) ;
	$o->set_option('EXTRA_JAVAC_ARGS',		'',				's', 1, \%opts) ;
	$o->set_option('DEBUGGER',				0,				'b', 1, \%opts) ;

	$o->set_option('PRIVATE',				'',				'b', 1, \%opts) ;
	$o->set_option('PACKAGE',				'',				's', 1, \%opts) ;

	my @left_overs = keys(%opts) ;
	if (scalar(@left_overs)){
		croak "'$left_overs[0]' is not a valid configuration option for Inline::Java" ;
	}

	# Now for the post processing
	$Inline::Java::DEBUG = $o->get_java_config('DEBUG') ;

	# Embedded JNI turns on regular JNI
	if ($o->get_java_config('EMBEDDED_JNI')){
		$o->set_java_config('JNI', 1) ;
	}

	if ($o->get_java_config('PORT') == -1){
		if ($o->get_java_config('SHARED_JVM')){
			$o->set_java_config('PORT', 7891) ;
		}
		else{
			$o->set_java_config('PORT', -7890) ;
		}
	}

	if (($o->get_java_config('JNI'))&&($o->get_java_config('SHARED_JVM'))){
		croak("You can't use the 'SHARED_JVM' option in 'JNI' mode") ;
	}
	if (($o->get_java_config('JNI'))&&($o->get_java_config('DEBUGGER'))){
		croak("You can't invoke the Java debugger ('DEBUGGER' option) in 'JNI' mode") ;
	}
	if ((! $o->get_java_config('SHARED_JVM'))&&(! $o->get_java_config('START_JVM'))){
		croak("Disabling the 'START_JVM' option only makes sense in 'SHARED_JVM' mode") ;
	}

	if ($o->get_java_config('JNI')){
		require Inline::Java::JNI ;
	}

	if ($o->get_java_config('DEBUGGER')){
		# Here we want to tweak a few settings to help debugging...
		Inline::Java::debug(1, "Debugger mode activated") ;
		# Add the -g compile option
		$o->set_java_config('EXTRA_JAVAC_ARGS', $o->get_java_config('EXTRA_JAVAC_ARGS') . " -g ") ;
		# Add the -sourcepath runtime option
		$o->set_java_config('EXTRA_JAVA_ARGS', $o->get_java_config('EXTRA_JAVA_ARGS') .
			" -sourcepath " . $o->get_api('build_dir') .
			Inline::Java::Portable::portable("ENV_VAR_PATH_SEP_CP") .
			Inline::Java::Portable::get_source_dir()
		) ;
	}	

	my $study = $o->get_java_config('STUDY') ;
	if ((defined($study))&&(ref($study) ne 'ARRAY')){
		croak "Configuration option 'STUDY' must be an array of Java class names" ;
	}

	Inline::Java::debug(1, "validate done.") ;
}


sub set_option {
	my $o = shift ;
	my $name = shift ;
	my $default = shift ;
	my $type = shift ;
	my $env_or = shift ;
	my $opts = shift ;
	my $desc = shift ;

	if (! exists($o->{ILSM}->{$name})){
		my $val = undef ;
		if (($env_or)&&(exists($ENV{"PERL_INLINE_JAVA_$name"}))){
			$val = $ENV{"PERL_INLINE_JAVA_$name"} ;
		}
		elsif (exists($opts->{$name})){
			$val = $opts->{$name} ;
		}
		else{
			$val = $default ;
		}

		if ($type eq 'b'){
			if (! defined($val)){
				$val = 0 ;
			}
			$val = ($val ? 1 : 0) ;
		}
		elsif ($type eq 'i'){
			if ((! defined($val))||($val !~ /\d/)){
				$val = 0 ;
			}
			$val = int($val) ;
		}

		$o->set_java_config($name, $val) ;
	}

	delete $opts->{$name} ;
}


sub get_java_config {
	my $o = shift ;
	my $param = shift ;

	return $o->{ILSM}->{$param} ;
}


sub set_java_config {
	my $o = shift ;
	my $param = shift ;
	my $value = shift ;

	return $o->{ILSM}->{$param} = $value ;
}


# In theory we shouldn't need to use this, but it seems
# it's not all accessible by the API yet.
sub get_config {
	my $o = shift ;
	my $param = shift ;

	return $o->{CONFIG}->{$param} ;
}


sub get_api {
	my $o = shift ;
	my $param = shift ;

	# Allows us to force a specific package...
	if (($param eq 'pkg')&&($o->get_config('PACKAGE'))){
		return $o->get_config('PACKAGE') ;
	}

	return $o->{API}->{$param} ;
}


# Parse and compile Java code
sub build {
	my $o = shift ;

	if ($o->get_java_config('built')){
		return ;
	}

	Inline::Java::debug(1, "Starting build.") ;

	# Grab and untaint the current directory
	my $cwd = Cwd::cwd() ;
	if ($o->get_config('UNTAINT')){
		($cwd) = $cwd =~ /(.*)/ ;
	}

	# We must grab this before we change to the build dir because
	# it could be relative...
	my $server_jar = Inline::Java::Portable::get_server_jar() ;

	# We need to add all the previous install dirs to the classpath because
	# they can access each other.
	my @prev_install_dirs = () ;
	foreach my $in (@INLINES){
		push @prev_install_dirs, File::Spec->catdir($in->get_api('install_lib'), 
			'auto', $in->get_api('modpname')) ;
	}

	my $cp = $ENV{CLASSPATH} || '' ;
	$ENV{CLASSPATH} = Inline::Java::Portable::make_classpath($server_jar, @prev_install_dirs, $o->get_java_config('CLASSPATH')) ;
	Inline::Java::debug(2, "classpath: $ENV{CLASSPATH}") ;

	# Create the build dir and go there
	my $build_dir = $o->get_api('build_dir') ;
	$o->mkpath($build_dir) ;
	chdir $build_dir ;

	my $code = $o->get_api('code') ;
	my $pcode = $code ;
	my $study_only = ($code =~ /^(STUDY|SERVER)$/) ;
	my $source = ($study_only ? '' : $o->get_api('modfname') . ".java") ;

	# Parse code to check for public class
	$pcode =~ s/\\\"//g ;
	$pcode =~ s/\"(.*?)\"//g ;
	$pcode =~ s/\/\*(.*?)\*\///gs ;
	$pcode =~ s/\/\/(.*)$//gm ;
	if ($pcode =~ /public\s+(abstract\s+)?class\s+(\w+)/){
		$source = "$2.java" ;
	}

	my $install_dir = File::Spec->catdir($o->get_api('install_lib'), 
		'auto', $o->get_api('modpname')) ;
	$o->mkpath($install_dir) ;

	if ($source){
		# Dump the source code...
		open(Inline::Java::JAVA, ">$source") or
			croak "Can't open $source: $!" ;
		print Inline::Java::JAVA $code ;
		close(Inline::Java::JAVA) ;

		# ... and compile it.
		my $javac = File::Spec->catfile($o->get_java_config('J2SDK'), 
			Inline::Java::Portable::portable("J2SDK_BIN"), 
			"javac" . Inline::Java::Portable::portable("EXE_EXTENSION")) ;
		my $redir = Inline::Java::Portable::portable("IO_REDIR") ;

		my $args = "-deprecation " . $o->get_java_config('EXTRA_JAVAC_ARGS') ;
		my $pinstall_dir = Inline::Java::Portable::portable("SUB_FIX_JAVA_PATH", $install_dir) ;
		my $cmd = Inline::Java::Portable::portable("SUB_FIX_CMD_QUOTES", 
			"\"$javac\" $args -d \"$pinstall_dir\" $source > cmd.out $redir") ;
		if ($o->get_config('UNTAINT')){
			($cmd) = $cmd =~ /(.*)/ ;
		}
		Inline::Java::debug(2, "$cmd") ;
		my $res = system($cmd) ;
		my $msg = $o->get_compile_error_msg() ;
		if ($res){
			croak $o->compile_error_msg($cmd, $msg) ;
		} ;
		if ($msg){
			warn("\n$msg\n") ;
		}

		# When we run the commands, we quote them because in WIN32 you need it if
		# the programs are in directories which contain spaces. Unfortunately, in
		# WIN9x, when you quote a command, it masks it's exit value, and 0 is always
		# returned. Therefore a command failure is not detected.
		# We need to take care of checking whether there are actually files
		# to be copied, and if not will exit the script.
		if (Inline::Java::Portable::portable('COMMAND_COM')){
			my @fl = Inline::Java::Portable::find_classes_in_dir($install_dir) ;
		 	if (! scalar(@fl)){
				croak "No class files produced. Previous command failed under command.com?" ;
			}
		 	foreach my $f (@fl){
				if (! (-s $f->{file})){
					croak "File $f->{file} has size zero. Previous command failed under command.com?" ;
				}
			}
		}
	}

	$ENV{CLASSPATH} = $cp ;
	Inline::Java::debug(2, "classpath: $ENV{CLASSPATH}") ;

	# Touch the .jdat file.
	my $jdat = File::Spec->catfile($install_dir, $o->get_api('modfname') . '.' . $o->get_api('suffix')) ;
	if (! open(Inline::Java::TOUCH, ">$jdat")){
		croak "Can't create file $jdat" ;
	}
	close(Inline::Java::TOUCH) ;

	# Go back and clean up
	chdir $cwd ;
	if (($o->get_api('cleanup'))&&(! $o->get_java_config('DEBUGGER'))){
		$o->rmpath('', $build_dir) ;
	}

	$o->set_java_config('built', 1) ;
	Inline::Java::debug(1, "build done.") ;
}


sub get_compile_error_msg {
	my $o = shift ;

	my $msg = '' ;
	if (open(Inline::Java::CMD, "<cmd.out")){
		$msg = join("", <Inline::Java::CMD>) ;
		close(Inline::Java::CMD) ;
	}

	return $msg ;
}


sub compile_error_msg {
	my $o = shift ;
	my $cmd = shift ;
	my $error = shift ;

	my $build_dir = $o->get_api('build_dir') ;

	my $lang = $o->get_api('language') ;
	return <<MSG

A problem was encountered while attempting to compile and install your Inline
$lang code. The command that failed was:
  $cmd

The build directory was:
$build_dir

The error message was:
$error

To debug the problem, cd to the build directory, and inspect the output files.

MSG
;
}


# Load and Run the Java Code.
sub load {
	my $o = shift ;

	if ($o->get_java_config('loaded')){
		return ;
	}

	Inline::Java::debug(1, "Starting load.") ;

	my $install_dir = File::Spec->catdir($o->get_api('install_lib'), 
		'auto', $o->get_api('modpname')) ;

	# If the JVM is not running, we need to start it here.
	my $cp = $ENV{CLASSPATH} || '' ;
	if (! $JVM){
		$ENV{CLASSPATH} = Inline::Java::Portable::make_classpath(
			Inline::Java::Portable::get_server_jar()) ;
		Inline::Java::debug(2, "classpath: $ENV{CLASSPATH}") ;
		$JVM = new Inline::Java::JVM($o) ;
		$ENV{CLASSPATH}	= $cp ;
		Inline::Java::debug(2, "classpath: $ENV{CLASSPATH}") ;

		my $pc = new Inline::Java::Protocol(undef, $o) ;
		$pc->AddClassPath(Inline::Java::Portable::portable("SUB_FIX_JAVA_PATH", Inline::Java::Portable::get_user_jar())) ;

		my $st = $pc->ServerType() ;
		if ((($st eq "shared")&&(! $o->get_java_config('SHARED_JVM')))||
			(($st eq "private")&&($o->get_java_config('SHARED_JVM')))){
			croak "JVM type mismatch on port " . $JVM->{port} ;
		}
	}

	$ENV{CLASSPATH}	= '' ;
	my @cp = Inline::Java::Portable::make_classpath($install_dir, $o->get_java_config('CLASSPATH')) ;
	$ENV{CLASSPATH}	= $cp ;
	
	my $pc = new Inline::Java::Protocol(undef, $o) ;
	$pc->AddClassPath(@cp) ;

	# Add our Inline object to the list.
	push @INLINES, $o ;
	$o->set_java_config('id', scalar(@INLINES) - 1) ;
	Inline::Java::debug(3, "Inline::Java object id is " . $o->get_java_config('id')) ;

	$o->study_module() ;
	if ((defined($o->get_java_config('STUDY')))&&(scalar($o->get_java_config('STUDY')))){
		$o->_study($o->get_java_config('STUDY')) ;
	}

	$o->set_java_config('loaded', 1) ;
	Inline::Java::debug(1, "load done.") ;
}


# This function 'studies' the classes generated by the inlined code.
sub study_module {
	my $o = shift ;

	my $install_dir = File::Spec->catdir($o->get_api('install_lib'),
		'auto', $o->get_api('modpname')) ;
	my $cache = $o->get_api('modfname') . '.' . $o->get_api('suffix') ;

	my $lines = [] ;
	if (! $o->get_java_config('built')){
		# Since we didn't build the module, this means that
		# it was up to date. We can therefore use the data
		# from the cache.
		Inline::Java::debug(1, "using jdat cache") ;
		my $p = File::Spec->catfile($install_dir, $cache) ;
		my $size = (-s $p) || 0 ;
		if ($size > 0){
			if (open(Inline::Java::CACHE, "<$p")){
				while (<Inline::Java::CACHE>){
					push @{$lines}, $_ ;
				}
				close(Inline::Java::CACHE) ;
			}
			else{
				croak "Can't open $p for reading: $!" ;
			}
		}
	}
	else{
		# First thing to do is get the list of classes that comprise the module.

		# We need the classes that are in the directory or under...
		my @classes = () ;
		my $cwd = Cwd::cwd() ;
		if ($o->get_config('UNTAINT')){
			($cwd) = $cwd =~ /(.*)/ ;
		}

		# We chdir to the install dir, that makes it easier to figure out
		# the packages for the classes.
		chdir($install_dir) ;
		my @fl = Inline::Java::Portable::find_classes_in_dir('.') ;
		chdir $cwd ;
		foreach my $f (@fl){
			push @classes, $f->{class} ;
		}

		# Now we ask Java the info about those classes...
		$lines = $o->report(@classes) ;

		# and we update the cache with these results.
		Inline::Java::debug(1, "updating jdat cache") ;
		my $p = File::Spec->catfile($install_dir, $cache) ;
		if (open(Inline::Java::CACHE, ">$p")){
			foreach my $l (@{$lines}){
				print Inline::Java::CACHE "$l\n" ;
			}
			close(Inline::Java::CACHE) ;
		}
		else{
			croak "Can't open $p file for writing" ;
		}
	}

	# Now we read up the symbols and bind them to Perl.
	$o->bind_jdat($o->load_jdat($lines)) ;
}


# This function 'studies' the specified classes and binds them to 
# Perl.
sub _study {
	my $o = shift ;
	my $classes = shift ;

	my @new_classes = () ;
	foreach my $class (@{$classes}){
		$class = Inline::Java::Class::ValidateClass($class) ;
		if (! Inline::Java::known_to_perl($o->get_api('pkg'), $class)){
			push @new_classes, $class ;
		}
	}
	if (! scalar(@new_classes)){
		return ;
	}
	
	my $lines = $o->report(@new_classes) ;
	# Now we read up the symbols and bind them to Perl.
	$o->bind_jdat($o->load_jdat($lines)) ;
}


sub report {
	my $o = shift ;
	my @classes = @_ ;

	my @lines = () ;
	if (scalar(@classes)){
		my $pc = new Inline::Java::Protocol(undef, $o) ;
		my $resp = $pc->Report(join(" ", @classes)) ;
		@lines = split("\n", $resp) ;
	}

	return \@lines ;
}


# Load the jdat code information file.
sub load_jdat {
	my $o = shift ;
	my $lines = shift ;

	Inline::Java::debug_obj($lines) ;

	# We need an array here since the same object can have many 
	# study sessions.
	if (! defined($o->{ILSM}->{data})){
		$o->{ILSM}->{data} = [] ;
	}
	my $d = {} ;
	my $data_idx = scalar(@{$o->{ILSM}->{data}}) ;
	push @{$o->{ILSM}->{data}}, $d ;
	
	# The original regexp didn't match anymore under the debugger...
	# Very strange indeed...
	# my $re = '[\w.\$\[;]+' ;
	my $re = '.+' ;

	my $idx = 0 ;
	my $current_class = undef ;
	if (scalar(@{$lines})){
		my $vline = shift @{$lines} ;
		chomp($vline) ;
		if ($vline ne $report_version){
			croak("Report version mismatch ($vline != $report_version). Delete your '_Inline' and try again.") ; 
		}
	}
	foreach my $line (@{$lines}){
		chomp($line) ;
		if ($line =~ /^class ($re) ($re)$/){
			# We found a class definition
			my $java_class = $1 ;
			my $parent_java_class = $2 ;
			$current_class = Inline::Java::java2perl($o->get_api('pkg'), $java_class) ;
			$d->{classes}->{$current_class} = {} ;
			$d->{classes}->{$current_class}->{java_class} = $java_class ;
			if ($parent_java_class ne "null"){
				$d->{classes}->{$current_class}->{parent_java_class} = $parent_java_class ;
			}
			$d->{classes}->{$current_class}->{constructors} = {} ;
			$d->{classes}->{$current_class}->{methods} = {} ;
			$d->{classes}->{$current_class}->{fields} = {} ;
		}
		elsif ($line =~ /^constructor \((.*)\)$/){
			my $signature = $1 ;

			$d->{classes}->{$current_class}->{constructors}->{$signature} = 
				{
					SIGNATURE => [split(", ", $signature)],
					STATIC => 1,
					IDX => $idx,
				} ;
		}
		elsif ($line =~ /^method (\w+) ($re) (\w+)\((.*)\)$/){
			my $static = $1 ;
			my $declared_in = $2 ;
			my $method = $3 ;
			my $signature = $4 ;

			if (! defined($d->{classes}->{$current_class}->{methods}->{$method})){
				$d->{classes}->{$current_class}->{methods}->{$method} = {} ;
			}

			$d->{classes}->{$current_class}->{methods}->{$method}->{$signature} = 
				{
					SIGNATURE => [split(", ", $signature)],
					STATIC => ($static eq "static" ? 1 : 0),
					IDX => $idx,
				} ;
		}
		elsif ($line =~ /^field (\w+) ($re) (\w+) ($re)$/){
			my $static = $1 ;
			my $declared_in = $2 ;
			my $field = $3 ;
			my $type = $4 ;

			if (! defined($d->{classes}->{$current_class}->{fields}->{$field})){
				$d->{classes}->{$current_class}->{fields}->{$field} = {} ;
			}

			$d->{classes}->{$current_class}->{fields}->{$field}->{$type} =  
				{
					TYPE => $type,
					STATIC => ($static eq "static" ? 1 : 0),
					IDX => $idx,
				} ;
		}
		$idx++ ;
	}

	Inline::Java::debug_obj($d) ;

	return ($d, $data_idx) ;
}


# Binds the classes and the methods to Perl
sub bind_jdat {
	my $o = shift ;
	my $d = shift ;
	my $idx = shift ;

	if (! defined($d->{classes})){
		return ;
	}

	my $inline_idx = $o->get_java_config('id') ;

	my %classes = %{$d->{classes}} ;
	foreach my $class (sort keys %classes) {
		my $class_name = $class ;
		$class_name =~ s/^(.*)::// ;

		my $java_class = $d->{classes}->{$class}->{java_class} ;
		# This parent stuff is needed for PerlNatives (so that you can call PerlNatives methods
		# from Perl...)
		my $parent_java_class = $d->{classes}->{$class}->{parent_java_class} ;
		my $parent_module = '' ;
		my $parent_module_declare = '' ;
		if (defined($parent_java_class)){
			$parent_module = java2perl($o->get_api('pkg'), $parent_java_class) ;
			$parent_module_declare = "\$$parent_module" . "::EXISTS_AS_PARENT = 1 ;" ;
			$parent_module .= ' ' ;
		}
		if (Inline::Java::known_to_perl($o->get_api('pkg'), $java_class)){
			next ;
		}

		my $colon = ":" ;
		my $dash = "-" ;
		my $ijo = 'Inline::Java::Object' ;
		
		my $code = <<CODE;
package $class ;
use vars qw(\@ISA \$INLINE \$EXISTS \$JAVA_CLASS \$DUMMY_OBJECT) ;

$parent_module_declare
\@ISA = qw($parent_module$ijo) ;
\$INLINE = \$INLINES[$inline_idx] ;
\$EXISTS = 1 ;
\$JAVA_CLASS = '$java_class' ;
\$DUMMY_OBJECT = $class$dash>__new(
	\$JAVA_CLASS, \$INLINE, 0) ;

use Carp ;

CODE

		while (my ($field, $types) = each %{$d->{classes}->{$class}->{fields}}){
			while (my ($type, $sign) = each %{$types}){
				if ($sign->{STATIC}){
					$code .= <<CODE;
tie \$$class$colon:$field, "Inline::Java::Object::StaticMember", 
	\$DUMMY_OBJECT,
	'$field' ;
CODE
					# We have at least one static version of this field,
					# that's enough.
					# Don't forget to reset the 'each' static pointer
					keys %{$types} ;
					last ;
				}
			}
		}


		if (scalar(keys %{$d->{classes}->{$class}->{constructors}})){
			$code .= <<CODE;

sub new {
	my \$class = shift ;
	my \@args = \@_ ;

	my \$o = \$INLINE ;
	my \$d = \$o->{ILSM}->{data}->[$idx] ;
	my \$signatures = \$d->{classes}->{'$class'}->{constructors} ;
	my (\$proto, \$new_args, \$static) = \$class->__validate_prototype('new', [\@args], \$signatures, \$o) ;

	my \$ret = undef ;
	eval {
		\$ret = \$class->__new(\$JAVA_CLASS, \$o, -1, \$proto, \$new_args) ;
	} ;
	croak \$@ if \$@ ;

	return \$ret ;
}


sub $class_name {
	return new(\@_) ;
}

CODE
		}

		while (my ($method, $sign) = each %{$d->{classes}->{$class}->{methods}}){
			$code .= $o->bind_method($idx, $class, $method) ;
		}

		Inline::Java::debug_obj(\$code) ;

		# open (Inline::Java::CODE, ">>code") and print CODE $code and close(CODE) ;

		# Here it seems that for the eval below to resolve the @INLINES
		# list properly, it must be used in this function...
		my $dummy = scalar(@INLINES) ;

		eval $code ;

		croak $@ if $@ ;
	}
}


sub bind_method {
	my $o = shift ;
	my $idx = shift ;
	my $class = shift ;
	my $method = shift ;
	my $static = shift ;

	my $code = <<CODE;

sub $method {
	my \$this = shift ;
	my \@args = \@_ ;

	my \$o = \$INLINE ;
	my \$d = \$o->{ILSM}->{data}->[$idx] ;
	my \$signatures = \$d->{classes}->{'$class'}->{methods}->{'$method'} ;
	my (\$proto, \$new_args, \$static) = \$this->__validate_prototype('$method', [\@args], \$signatures, \$o) ;

	if ((\$static)&&(! ref(\$this))){
		\$this = \$DUMMY_OBJECT ;
	}

	my \$ret = undef ;
	eval {
		\$ret = \$this->__get_private()->{proto}->CallJavaMethod('$method', \$proto, \$new_args) ;
	} ;
	croak \$@ if \$@ ;

	return \$ret ;
}

CODE

	return $code ; 
}


sub get_fields {
	my $o = shift ;
	my $class = shift ;

	my $fields = {} ;
	my $data_list = $o->{ILSM}->{data} ;

	foreach my $d (@{$data_list}){
		if (exists($d->{classes}->{$class})){
			while (my ($field, $value) = each %{$d->{classes}->{$class}->{fields}}){
				# Here $value is a hash that contains all the different
				# types available for the field $field
				$fields->{$field} = $value ;
			}
		}
	}

	return $fields ;
}


# Return a small report about the Java code.
sub info {
	my $o = shift ;

	if (! (($o->{INLINE}->{object_ready})||($o->get_java_config('built')))){
		$o->build() ;
	}

	if (! $o->get_java_config('loaded')){
		$o->load() ;
	}

	my $info = '' ;
	my $data_list = $o->{ILSM}->{data} ;

	foreach my $d (@{$data_list}){
		if (! defined($d->{classes})){
			next ;
		}

		my %classes = %{$d->{classes}} ;

		$info .= "The following Java classes have been bound to Perl:\n" ;
		foreach my $class (sort keys %classes) {
			$info .= "\n  class $class:\n" ;

			$info .= "    public methods:\n" ;
			while (my ($k, $v) = each %{$d->{classes}->{$class}->{constructors}}){
				my $name = $class ;
				$name =~ s/^(.*)::// ;
				$info .= "      $name($k)\n" ;
			}

			while (my ($k, $v) = each %{$d->{classes}->{$class}->{methods}}){
				while (my ($k2, $v2) = each %{$d->{classes}->{$class}->{methods}->{$k}}){
					my $static = ($v2->{STATIC} ? "static " : "") ;
					$info .= "      $static$k($k2)\n" ;
				}
			}

			$info .= "    public member variables:\n" ;
			while (my ($k, $v) = each %{$d->{classes}->{$class}->{fields}}){
				while (my ($k2, $v2) = each %{$d->{classes}->{$class}->{fields}->{$k}}){
					my $static = ($v2->{STATIC} ? "static " : "") ;
					my $type = $v2->{TYPE} ;

					$info .= "      $static$type $k\n" ;
				}
			}
		}
	}

    return $info ;
}



######################## General Functions ########################


sub __get_JVM {
	return $JVM ;
}


# For testing purposes only...
sub __clear_JVM {
	$JVM = undef ;
}


sub shutdown_JVM {
	if ($JVM){
		$JVM->shutdown() ;
		$JVM = undef ;
	}
}


sub reconnect_JVM {
	if ($JVM){
		$JVM->reconnect() ;
	}
}


sub capture_JVM {
	if ($JVM){
		$JVM->capture() ;
	}
}


sub i_am_JVM_owner {
	if ($JVM){
		return $JVM->am_owner() ;
	}
}


sub release_JVM {
	if ($JVM){
		$JVM->release() ;
	}
}


sub get_DEBUG {
	return $Inline::Java::DEBUG ;
}


sub get_DONE {
	return $DONE ;
}


sub set_DONE {
	$DONE = 1 ;
}


sub __get_INLINES {
	return \@INLINES ;
}


sub java2perl {
	my $pkg = shift ;
	my $jclass = shift ;

	$jclass =~ s/[.\$]/::/g ;

	if ((defined($pkg))&&($pkg)){
		$jclass = $pkg . "::" . $jclass ;
	}

	return $jclass ;
}


sub known_to_perl {
	my $pkg = shift ;
	my $jclass = shift ;

	my $perl_class = java2perl($pkg, $jclass) ;

	no strict 'refs' ;
	if (defined(${$perl_class . "::" . "EXISTS"})){
		Inline::Java::debug(3, "perl knows about '$jclass' ('$perl_class')") ;
		return 1 ;
	}
	else{
		Inline::Java::debug(3, "perl doesn't know about '$jclass' ('$perl_class')") ;
	}

	return 0 ;
}


sub debug {
	my $level = shift ;

	if (($Inline::Java::DEBUG)&&($Inline::Java::DEBUG >= $level)){
		my $x = " " x $level ;
		my $str = join("\n$x", @_) ;
		while (chomp($str)) {}
		print DEBUG_STREAM sprintf("[perl][%s]$x%s\n", $level, $str) ;
	}
}


sub debug_obj {
	my $obj = shift ;
	my $force = shift || 0 ;

	if (($Inline::Java::DEBUG >= 5)||($force)){
		debug(5, "Dump:\n" . Dumper($obj)) ;
		if (UNIVERSAL::isa($obj, "Inline::Java::Object")){
			# Print the guts as well...
			debug(5, "Private Dump:" . Dumper($obj->__get_private())) ;
		}
	}
}


sub dump_obj {
	my $obj = shift ;

	return debug_obj($obj, 1) ;
}


######################## Public Functions ########################


# If we are dealing with a Java object, we simply ask for a new "reference"
# with the requested class. 
sub cast {
	my $type = shift ;
	my $val = shift ;

	if (! UNIVERSAL::isa($val, "Inline::Java::Object")){
		croak("Type casting can only be used on Java objects. Use 'coerce' instead.") ;
	}

	return $val->__cast($type) ;
}


# coerce is used to force a specific prototype to be used.
sub coerce {
	my $type = shift ;
	my $val = shift ;
	my $array_type = shift ;

	if (UNIVERSAL::isa($val, "Inline::Java::Object")){
		croak("Type coercing can't be used on Java objects. Use 'cast' instead.") ;
	}

	my $o = undef ;
	eval {
		$o = new Inline::Java::Class::Coerce($type, $val, $array_type) ;
	} ;
	croak $@ if $@ ;
	
	return $o ;
}


sub study_classes {
	my $classes = shift ;
	my $package = shift || caller() ;

	my $o = undef ;
	my %pkgs = () ;
	foreach (@INLINES){
		my $i = $_ ;
		my $pkg = $i->get_api('pkg') || 'main' ;
		$pkgs{$pkg} = 1 ;
		if ($pkg eq $package){
			$o = $i ;
			last ;
		}
	}

	if (defined($o)){
		$o->_study($classes) ;
	}
	else {
		my $msg = "Can't place studied classes under package '$package' since Inline::Java was not used there. Valid packages are:\n" ;
		foreach my $pkg (keys %pkgs){
			$msg .= "  $pkg\n" ;
		}
		croak($msg) ;
	}
}


sub caught {
	my $class = shift ;

	my $e = $@ ;

	$class = Inline::Java::Class::ValidateClass($class) ;

	my $ret = 0 ;
	if (($e)&&(UNIVERSAL::isa($e, "Inline::Java::Object"))){
		my ($msg, $score) = $e->__isa($class) ;
		if ($msg){
			$ret = 0 ;
		}
		else{
			$ret = 1 ;
		}
	}
	$@ = $e ;

	return $ret ;
}


sub	find_default_j2sdk {
	my $class = shift ;

	return File::Spec->catfile('Inline', 'Java', 'default_j2sdk.pl') ;
}


1 ;