Revision: 100
Author: cosimo.streppone
Date: Sun Oct  4 06:34:16 2009
Log: Implemented method to invoke the javap subprocess (invoke_javap).
Created an additional shortcut method to be used in test scripts (javap_test).
Converted existing test scripts.


http://code.google.com/p/java2perl6/source/detail?r=100

Added:
 /trunk/t/02_invoke_javap.t
Modified:
 /trunk/lib/Java/Javap.pm
 /trunk/t/02_interface.t
 /trunk/t/03_class.t
 /trunk/t/04_java2perl6.t
 /trunk/t/05_final.t

=======================================
--- /dev/null
+++ /trunk/t/02_invoke_javap.t  Sun Oct  4 06:34:16 2009
@@ -0,0 +1,23 @@
+use strict;
+use warnings;
+
+use lib 'lib';
+
+use Test::More;
+use Java::Javap;
+
+plan skip_all => "javap from Java SDK required: $@"
+       unless Java::Javap->javap_test;
+
+plan tests => 2;
+
+my $decomp = Java::Javap->javap(
+       ['com.example.NestedIntTest'],
+       {-classpath=>'testjavas'}
+);
+
+diag($decomp);
+
+ok($decomp, 'received some output from javap');
+like($decomp, qr{Compiled from}m, 'javap output seems sane');
+
=======================================
--- /trunk/lib/Java/Javap.pm    Tue Aug  4 15:04:00 2009
+++ /trunk/lib/Java/Javap.pm    Sun Oct  4 06:34:16 2009
@@ -1,7 +1,11 @@
 package Java::Javap;
-use strict; use warnings;
-
-our $VERSION = '0.04';
+
+use strict;
+use warnings;
+use Carp;
+
+our $VERSION = '0.05';
+our $JAVAP_EXECUTABLE = 'javap';

 use Java::Javap::TypeCast;

@@ -9,7 +13,7 @@
     shift; # invoked through this class, discard our name
     my $tree     = shift;
     my $caster   = shift;
-
+
     my %answers;

     # first, get parent type
@@ -40,7 +44,42 @@
     return [ keys %answers ];
 }

-# returns true if type casts to a builtin perl6 type
+sub invoke_javap {
+       my ($self, $classes, $options) = @_;
+
+       if (! $classes) {
+               croak "No classes to be parsed";
+       }
+
+       if (! ref $classes) {
+               $classes = [ $classes ];
+       }
+
+       $options ||= {};
+
+       open(my $javap_fh, '-|', $JAVAP_EXECUTABLE, %$options, @$classes)
+               or croak "Couldn't open $JAVAP_EXECUTABLE subprocess: $!";
+
+       my $javap_output = q{};
+       while (<$javap_fh>) {
+               $javap_output .= $_;
+       }
+       close $javap_fh;
+
+       return $javap_output;
+}
+
+*javap = *invoke_javap;
+
+# Shortcut for the test suite
+sub javap_test {
+       my ($self) = @_;
+       my $output;
+       eval { $output = $self->invoke_javap(['java.lang.String']) };
+       return $output ? 1 : 0;
+}
+
+# Returns true if type casts to a builtin perl6 type
 sub _skip_it {
     my $type_name    = shift;
     my $caster  = shift;
@@ -90,16 +129,57 @@
 Returns: an array reference of the types in arguments to methods, or
 return values (thrown types are not reported).

+=head2 invoke_javap
+
+=head2 javap
+
+Invokes the C<javap> process and return the output.
+Throws an exception if something goes wrong, like C<javap> is
+not found.
+
+=head3 Parameters
+
+=over
+
+=item \...@classes
+
+List of classes to be decompiled. It can also be supplied as a string,
+if a single class should be decompiled.
+
+=item \%options
+
+Options to be passed to the C<javap> process.
+
+=back
+
+=head3 Example
+
+    my @classes = ('java.lang.String');
+    my %options = ();
+    my $output = Java::Javap->javap(\...@classes, \%options);
+
+       # or ...
+
+    my $output = Java::Javap->javap('java.lang.String');
+
 =head1 SEE ALSO

-C<java2perl6>
-C<Java::Javap::Generator>
-C<Java::Javap::Generator::Std>
-C<javap.grammar>
-
-=head1 AUTHOR
+=over
+
+=item C<java2perl6>
+
+=item C<Java::Javap::Generator>
+
+=item C<Java::Javap::Generator::Std>
+
+=item C<javap.grammar>
+
+=back
+
+=head1 AUTHORS

 Philip Crow, E<lt>crow.p...@gmail.come<gt>
+Cosimo Streppone, E<lt>cos...@cpan.orge<gt>

 =head1 COPYRIGHT AND LICENSE

=======================================
--- /trunk/t/02_interface.t     Thu Sep 10 14:04:09 2009
+++ /trunk/t/02_interface.t     Sun Oct  4 06:34:16 2009
@@ -8,8 +8,9 @@
 use Java::Javap::Grammar;
 use Java::Javap::Generator;

-system('javap');
-plan skip_all => "javap from Java SDK required: $!" if $!;
+plan skip_all => "javap from Java SDK required: $!"
+       unless Java::Javap->javap_test();
+
 plan tests    => 3;

 #--------------------------------------------------------------------
@@ -17,7 +18,7 @@
 #--------------------------------------------------------------------

 my $parser = Java::Javap::Grammar->new();
-my $decomp = `javap -classpath testjavas com.example.NestedIntTest`;
+my $decomp = Java::Javap->javap('com.example.NestedIntTest', {-classpath => 'testjavas'});
 #diag("decomp=$decomp");

 my $tree   = $parser->comp_unit( $decomp );
=======================================
--- /trunk/t/03_class.t Thu Sep 10 14:04:09 2009
+++ /trunk/t/03_class.t Sun Oct  4 06:34:16 2009
@@ -4,11 +4,13 @@
 use lib 'lib';

 use Test::More;
-use Java::Javap::Grammar;
+use Java::Javap;
 use Java::Javap::Generator;
-
-system('javap');
-plan skip_all => "javap from Java SDK required: $!" if $!;
+use Java::Javap::Grammar;
+
+plan skip_all => "javap from Java SDK required: $!"
+       unless Java::Javap->javap_test();
+
 plan tests    => 3;

 #--------------------------------------------------------------------
@@ -16,7 +18,7 @@
 #--------------------------------------------------------------------

 my $parser = Java::Javap::Grammar->new();
-my $decomp = `javap -classpath testjavas ClassTest`;
+my $decomp = Java::Javap->javap('ClassTest', {-classpath=>'testjavas'});

 my $tree   = $parser->comp_unit( $decomp );

@@ -177,7 +179,7 @@
 #--------------------------------------------------------------------
 {
   my $parser = Java::Javap::Grammar->new();
-  my $decomp = `javap -classpath testjavas dupMethodTest`;
+ my $decomp = Java::Javap->javap('dupMethodTest', {-classpath=>'testjavas'});

   my $tree   = $parser->comp_unit( $decomp );

=======================================
--- /trunk/t/04_java2perl6.t    Thu Sep 10 14:04:09 2009
+++ /trunk/t/04_java2perl6.t    Sun Oct  4 06:34:16 2009
@@ -5,9 +5,11 @@

 use Test::More;
 use File::Spec;
-
-system('javap');
-plan skip_all => "javap from Java SDK required: $!" if $!;
+use Java::Javap;
+
+plan skip_all => "javap from Java SDK required: $!"
+       unless Java::Javap->javap_test();
+
 plan tests    => 5;

 my $perl       = $^X;
=======================================
--- /trunk/t/05_final.t Thu Sep 10 14:04:09 2009
+++ /trunk/t/05_final.t Sun Oct  4 06:34:16 2009
@@ -4,11 +4,13 @@
 use lib 'lib';

 use Test::More;
-use Java::Javap::Grammar;
+use Java::Javap;
 use Java::Javap::Generator;
-
-system('javap');
-plan skip_all => "javap from Java SDK required: $!" if $!;
+use Java::Javap::Grammar;
+
+plan skip_all => "javap from Java SDK required: $!"
+       unless Java::Javap->javap_test();
+
 plan tests    => 1;

 #--------------------------------------------------------------------
@@ -16,7 +18,7 @@
 #--------------------------------------------------------------------

 my $parser = Java::Javap::Grammar->new();
-my $decomp = `javap -classpath testjavas FinalClassTest`;
+my $decomp = Java::Javap->javap('FinalClassTest', {-classpath => 'testjavas'});

 my $tree   = $parser->comp_unit( $decomp );

Reply via email to