[libinline-java-perl] 68/398: *** empty log message ***
Jonas Smedegaard
dr at jones.dk
Thu Feb 26 11:42:48 UTC 2015
This is an automated email from the git hooks/post-receive script.
js pushed a commit to tag 0.55
in repository libinline-java-perl.
commit 4969bd4c03ef13b14d004ea2e48e4736100dbae3
Author: patrick <>
Date: Tue Apr 17 19:51:10 2001 +0000
*** empty log message ***
---
Java.pod | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
Java/Makefile.PL | 160 ++++++++++++++++++++++++++++++++++++++++---
README | 63 ++++++++++++++++-
3 files changed, 398 insertions(+), 29 deletions(-)
diff --git a/Java.pod b/Java.pod
index cdb56eb..874a24f 100644
--- a/Java.pod
+++ b/Java.pod
@@ -112,6 +112,17 @@ behavior of C<Inline::Java>:
Adds the specified CLASSPATH to the environment CLASSPATH.
Ex: CLASSPATH => 'my/other/java/classses'
+ WARN_METHOD_SELECT:
+ Throws a warning when C<Inline::Java> has to 'choose' between
+ different method signatures. The warning states the possible
+ choices and the sugnature chosen.
+ Ex: WARN_METHOD_SELECT => 1
+
+ USE_JNI:
+ Enables the JNI (Java Native Interface) mode instead of the default
+ client/server mode. This mode is still experimental.
+ Ex: USE_JNI => 1
+
=head1 CLASSES AND OBJECTS
Because Java is object oriented, any interface between Perl and Java
@@ -216,7 +227,7 @@ will be automatically converted to the correct type:
my $obj2 = new Foo2(5, "toto", $obj) ;
will work fine. These objects can be of any type, even if these types
-are not know to C<Inline::Java>. This is also true for return types:
+are not known to C<Inline::Java>. This is also true for return types:
use Inline Java => <<'END';
import java.util.* ;
@@ -244,39 +255,196 @@ can't call any of their methods.
=head1 MEMBER VARIABLES
-Currently public member variables are not visible from the Perl space.
-This will be implemented in a future version. But you can certainly create
-get/set methods to access them.
+You can also access all public member variables (static or not) from Perl.
+As with method arguments, the types of these variables does not need to
+be known to Perl:
+
+ use Inline Java => <<'END';
+ import java.util.* ;
+
+ class Foo4 {
+ public int i ;
+ public static HashMap hm ;
+
+ public Foo4() {
+ }
+ }
+ END
+
+ my $obj = new Foo4() ;
+ $obj->{i} = 2 ;
+ my $hm1 = $obj->{hm} ; # instance way
+ my $hm2 = Foo4::hm ; # static way
+
+Note: Watch out for typos when accessing members in the static fashion,
+'use strict' will not catch them since they have a package name...
=head1 ARRAYS
-Currently array are not supported in C<Inline::Java>.
-This will be implemented in a future version. But remember that you can
-always ask Java to return an array, or modify it. See Methods section.
+You can also send and receive arrays. This is done by using Perl lists:
+
+ use Inline Java => <<'END';
+ import java.util.* ;
+
+ class Foo5 {
+ public int i[] = {5, 6, 7} ;
+
+ public Foo5() {
+ }
+
+ public String [] f(String a[]){
+ return a ;
+ }
+
+ public String [][] f(String a[][]){
+ return a ;
+ }
+ }
+ END
+
+ my $obj = new Foo5() ;
+ my $i_2 = $obj->{i}->[2] ; # 7
+ my $a1 = $obj->f(["a", "b", "c"]) ; # String []
+
+ my $a2 = $obj->f([
+ ["00", "01"],
+ ["10", "11"],
+ ]) ; # String [][]
+ print $a2->[1]->[0] ; # "10"
+
+=head1 TYPE CASTING
+
+Sometimes when a class as many signatures for the same method,
+C<Inline::Java> will have to select one of the signatures based on
+the arguments that are passed:
+
+ use Inline Java => <<'END';
+ class Foo6 {
+ public Foo6() {
+ }
+
+ public void f(int i){
+ }
+
+ public void f(char c){
+ }
+ }
+ END
+
+ my $obj = new Foo6() ;
+ $obj->f('5') ;
+
+
+In this case, C<Inline::Java> will call f(int i), because '5' is an integer.
+But '5' is a valid char as well. So to force the call of f(char c), do the
+following:
+
+ use Inline::Java qw(cast) ;
+ $obj->f(cast('char', '5')) ;
+ # or
+ $obj->f(Inline::Java::cast('char', '5')) ;
+
+The cast function forces the selection of the matching signature.
+
+Another case where type casting is need is when one wants to pass an array
+as a java.lang.Object:
+
+ use Inline Java => <<'END';
+ Object o ;
+ int a[] = {1, 2, 3} ;
+
+ class Foo7 {
+ public Foo7() {
+ }
+ }
+ END
+
+ my $obj = new Foo7() ;
+ $obj->{o} = [1, 2, 3] ; # No!
+
+The reason why this will not work is simple. When C<Inline::Java> sees an
+array, it checks the Java type you are trying to match it against to validate
+the construction of your Perl list. But in this case, it can't validate
+the array because you're assigning it to an Object. You must use the 3
+parameter version of the cast function to do this:
+
+ $obj->{o} = Inline::Java::cast(
+ "java.lang.Object",
+ [1, 2, 3],
+ "[Ljava.lang.String;") ;
+
+This tells C<Inline::Java> to validate your Perl list as a String [], and
+then cast it as an Object.
+
+Here is how to construct the array type representations:
+
+ [<type> -> 1 dimensional <type> array
+ [[<type> -> 2 dimensional <type> array
+ ...
+
+ where <type>is one of:
+ B byte S short I int J long
+ F float D double C char Z boolean
+
+ L<class>; array of <class> objects
+
+This is described in more detail in most Java books that talk about
+reflection.
+
+But you only need to do this if you have a Perl list. If you already have a
+Java array reference obtained from elsewhere, you don't even need to cast:
+
+ $obj->{o} = $obj->{a} ;
+
+
+=head1 JNI (JAVA NATIVE INTERFACE)
+
+Starting in version 0.20, there has been some support for the JNI
+(Java Native Interface). This enables C<Inline::Java> to load the
+Java virtual machine as a shared object instead of running it as a
+stand-alone server. This brings a dramatic improvement in performance.
+
+To enable the JNI mode, you first must compile the JNI extension when
+you install the c<Inline::Java> module. See README for more information.
+
+Once you have built the JNI extension, you can enable the JNI mode
+by doing one of the following:
+
+ - set the USE_JNI configuration option to any true value
+ - set the PERL_INLINE_JAVA_USE_JNI environment variable to any true value
+
+Once JNI mode is enabled, your scripts will use create the Java virtual
+machine in their own memory space (using a shared object), instead of
+forking and running the Java virtual machine as a server.
=head1 SUPPORTED PLATFORMS
This is an ALPHA release of Inline::Java. Further testing and
expanded support for other operating systems and platforms will be a
-focus for future releases. It has been tested on Solaris 2.5.1, with
-Perl 5.6 and Java SDK 1.2.1. It likely will work with many other
-configuration under Unix, and possibly under Windows.
+focus for future releases. It has been tested on:
+
+ - Solaris 2.5.1 + Perl 5.6 + Java SDK 1.2.1
+ - Solaris 2.8 + Perl 5.6 + Java SDK 1.3.0
+ - Windows 2000 + Perl 5.6 + Java SDK 1.3.0
+ - Windows 95 + Perl 5.6 + Java SDK 1.2.2 (fix required in Makefile)
+
+It likely will work with many other configurations.
=head1 HOW IT WORKS
This is how C<Inline::Java> works. Once the user's code is compiled by the
javac binary, C<Inline::Java>'s own Java code is compiled. This code
-implements a server that receives requests from Perl to create objects,
-call methods, destroy objects, etc. It is also capable of analyzing Java code
-tp extract the public symbols. Once this code is compiled, it is executed to
-extract the symbols from the Java code.
+implements a server (or not if you use the JNI mode) that receives requests
+from Perl to create objects, call methods, destroy objects, etc. It is also
+capable of analyzing Java code to extract the public symbols. Once this
+code is compiled, it is executed to extract the symbols from the Java code.
Once this is done, the user's code information is fetched and is bound to
Perl namespaces. Then C<Inline::Java>'s code is run to launch the server. The
-Perl script then connects to the server using a TCP socket. Then each object
-creation or method invocation on "Java objects" send requests to the server,
-which processes them and returns object ids to Perl which keeps them the
-reference te objects in the future.
+Perl script then connects to the server using a TCP socket (or not if you use
+the JNI mode). Then each object creation or method invocation on "Java
+objects" send requests to the server, which processes them and returns
+object ids to Perl which keeps them the reference te objects in the future.
=head1 SEE ALSO
diff --git a/Java/Makefile.PL b/Java/Makefile.PL
index e9a4036..9bb01ad 100644
--- a/Java/Makefile.PL
+++ b/Java/Makefile.PL
@@ -1,13 +1,155 @@
-use ExtUtils::MakeMaker;
+use ExtUtils::MakeMaker ;
+use File::Find ;
-WriteMakefile(
+use strict ;
+
+# Define these variables with the correct values to bypass the
+# rest of the code that looks for them.
+my $java_include = undef ;
+my $java_include_os = undef ;
+my $java_lib = undef ;
+my $java_so = undef ;
+
+my $jvm_lib = ($^O eq "MSWin32" ? "jvm.lib" : "libjvm.so") ;
+my $jvm_so = ($^O eq "MSWin32" ? "jvm.dll" : "libjvm.so") ;
+
+
+my $q = "\nInline::Java has an optional JNI extension (see README for more " .
+ "information).\nDo you want to build the JNI extension" ;
+
+my $jni = AskYN($q) ;
+
+if ($jni){
+ if ((! defined($java_include))&&
+ (! defined($java_include_os))&&
+ (! defined($java_lib))&&
+ (! defined($java_so))){
+ # We need to find the files in order to be able to build
+ $q = "\nWhat is the path to your Java installation" ;
+ my $jh = Ask($q) ;
+ chomp($jh) ;
+ if (! -d $jh){
+ die("Directory $jh does not exist.") ;
+ }
+ print "\n" ;
+ find(\&wanted, $jh) ;
+
+ if (! defined($java_include)){
+ die("Couldn't find jni.h.") ;
+ }
+ if (! defined($java_include_os)){
+ die("Couldn't find jni_md.h.") ;
+ }
+ if (! defined($java_lib)){
+ die("Couldn't find $jvm_lib.") ;
+ }
+ if (! defined($java_so)){
+ die("Couldn't find $jvm_so.") ;
+ }
+ }
+
+ if ($^O eq "MSWin32"){
+ my $l = join(" or ", @{$java_so}) ;
+ print "\nNote: You will need to add $l to your PATH environment " .
+ "variable in order to be able to use the JNI mode.\n" ;
+ }
+
+ print "\n" ;
+
+}
+else{
+ # We need to rename the JNI.xs file so that the extension is not built
+ rename("JNI.xs", "JNI.xs_") ;
+}
+
+
+my %h = (
NAME => 'Inline::Java::JNI',
VERSION_FROM => 'JNI.pm',
- INC => join(" ",
- '-I/usr/java1.2/include',
- '-I/usr/java1.2/include/solaris'
- ),
- LIBS => [
- '-L/usr/java1.2/jre/lib/sparc -ljvm'
- ],
) ;
+
+if ($jni){
+ $h{INC} = join(" ",
+ "-I$java_include",
+ "-I$java_include_os"
+ ) ;
+ $h{LIBS} = [
+ "-L$java_lib -ljvm"
+ ] ;
+}
+
+ExtUtils::MakeMaker::WriteMakefile(%h) ;
+
+
+# Restore the JNI.xs file
+rename("JNI.xs_", "JNI.xs") ;
+
+
+#################################################
+
+
+sub wanted {
+ my $file = $_ ;
+
+ if ($file eq "jni.h"){
+ print "Found jni.h in $File::Find::dir...\n" ;
+ $java_include = $File::Find::dir ;
+ }
+ if ($file eq "jni_md.h"){
+ print "Found jni_md.h in $File::Find::dir...\n" ;
+ $java_include_os = $File::Find::dir ;
+ }
+ if ($file eq $jvm_lib){
+ print "Found $jvm_lib in $File::Find::dir...\n" ;
+ $java_lib = $File::Find::dir ;
+ }
+ if ($file eq $jvm_so){
+ print "Found $jvm_so in $File::Find::dir...\n" ;
+ if (! defined($java_so)){
+ $java_so = [] ;
+ }
+ push @{$java_so}, $File::Find::dir ;
+ }
+}
+
+
+# Gets yes/no from stdin
+sub AskYN {
+ my $ques = shift ;
+
+ my $str = $ques . " [yn]: " ;
+ print($str) ;
+
+ my $ans = '' ;
+ while (<STDIN>){
+ $ans = $_ ;
+ chomp($ans) ;
+ if ($ans =~ /^(y|n)$/i){
+ last ;
+ }
+ else{
+ print($str) ;
+ }
+ }
+
+ if ($ans eq "y"){
+ return 1 ;
+ }
+ else{
+ return 0 ;
+ }
+}
+
+
+# Gets string from stdin
+sub Ask {
+ my $ques = shift ;
+
+ my $str = $ques . " : " ;
+ print($str) ;
+ my $ans = '' ;
+ $ans = <STDIN> ;
+
+ return $ans ;
+}
+
diff --git a/README b/README
index a88a8c8..c0fffef 100644
--- a/README
+++ b/README
@@ -20,6 +20,53 @@ When run, this complete program prints:
Just Another Inline Hacker
+-------------------------------------------------------------------------------
+JNI (JAVA NATIVE INTERFACE) EXTENSION:
+
+When you do 'perl Makefile.PL', Makefile.PL will ask you if you want to
+build the JNI extension.
+
+The JNI extension that allows you to load the Java virtual machine as a
+shared object instead of running it as a separate process. Building the JNI
+extension does not force you to use it, it simply gives you that option.
+
+To use it, you must use the USE_JNI config option or the
+PERL_INLINE_JAVA_USE_JNI environment variable.
+
+The JNI extension has been built and tested on:
+- Solaris 2.5.1
+- Windows 2000
+- Windows 95
+
+Depending on your platform, the build is more or less complex:
+
+MSWin32:
+- Follow the instructions provided by Makefile.PL. Don't forget to add to
+ your PATH the directory where jvm.dll is located.
+
+Solaris:
+- In order to get the JNI extension running on Solaris, you will
+ most probably need to recompile Perl. At the end of the Perl
+ configuration, you will be asked if you want to edit the config.sh
+ file. Say 'yes' and look for the following lines:
+
+ libs='...'
+ Change this to:
+ libs='-lthread ...'
+
+ usemymalloc='...'
+ Change this to:
+ usemymalloc='n'
+
+- Then finish building Perl as usual.
+- Note: On my setup, 'make test' failed one test: pragma/warnings #179.
+ But when I followed the instructions:
+ ### Since most tests were successful, you have a good chance to
+ ### get information with better granularity by running
+ ### ./perl harness
+ ### in directory ./t.
+ all tests where successful.
+
-------------------------------------------------------------------------------
INSTALLATION:
@@ -34,7 +81,7 @@ To install Inline::Java do this:
perl Makefile.PL
make
-make test (see Note 2)
+make test (see Note 2, 3)
make install
You have to 'make install' before you can run it successfully.
@@ -51,6 +98,10 @@ directory to your Java binaries in either your PATH environment variable
or in the PERL_INLINE_JAVA_BIN environment variable. 'make test' will remind you
if you don't do this.
+Note 3: If you have built the JNI extension and want the test suite to use
+it, set the PERL_INLINE_JAVA_USE_JNI environment variable to a true value
+before running 'make test'.
+
-------------------------------------------------------------------------------
FEATURES:
@@ -59,8 +110,16 @@ WARNING: THIS IS ALPHA SOFTWARE. It is incomplete and possibly unreliable.
It is also possible that some elements of the interface (API) will
change in future releases.
-Inline::Java version 0.01 includes:
+Inline::Java version 0.20 includes:
++ All public member variables are now exported to Perl.
++ Arrays can now be passed as method parameters and assigned to
+ member variables.
++ Type casting is now available to force the selection of specific
+ method signatures.
++ Optional JNI (Java Native Interface) extension improves performance
+ dramatically.
+Inline::Java version 0.01 includes:
+ All classes and their public methods are exported to Perl, relative
to your current package.
+ All objects (except arrays) and primitive Java types are supported as
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libinline-java-perl.git
More information about the Pkg-perl-cvs-commits
mailing list