Revision: 88
Author: tim.bunce
Date: Tue Aug 11 03:11:17 2009
Log: Replaced --quiet and --verbose flags with --trace=N
Extended list of perl6 builtin types.
Added more type mappings to TypeCast.
Assorted light refactoring/tidying.

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

Modified:
 /trunk/bin/java2perl6
 /trunk/lib/Java/Javap/Generator/Std.pm
 /trunk/lib/Java/Javap/TypeCast.pm

=======================================
--- /trunk/bin/java2perl6       Mon Aug 10 23:01:36 2009
+++ /trunk/bin/java2perl6       Tue Aug 11 03:11:17 2009
@@ -10,13 +10,10 @@
 use Java::Javap::Generator;

 my $genwith   = 'Std';
-my $genoptstr = '';
 my $jpcmd     = '';
 my $nest;
 my $outdir    = '.';
 my $recurse;
-my $quiet     = 0;
-my $verbose   = 0;

 #use Parse::RecDescent; $::RD_TRACE = 1;

@@ -24,26 +21,32 @@
 #   astas - no, make this a Genterator,
 #           but do write the backends for YAML and Perl

+# XXX nest and recurse should default on
+
 GetOptions(
     'jpcmd|j=s'   => \$jpcmd,
     'genwith|g=s' => \$genwith,
-    'genopts|p=s' => \$genoptstr,
+    'genopts|p=s' => \my $opt_genopts,
     'nest|n!'     => \$nest,
     'outdir|d=s'  => \$outdir,
     'recurse|r!'  => \$recurse,
-    'quiet|q!'    => \$quiet,
-    'verbose|v!'  => \$verbose,
-    'help|h!'     => \&help,
     'check|c!'    => \my $opt_check,
+    'trace|t=i'   => \(my $opt_trace = 1),
+    'help|h!'     => \&help,
 ) or exit 1;

-my @genopts = split /\s+/, $genoptstr;
-
my @classes = @ARGV or die "usage: $0 [options] class [ class ... ]\nUse $0 --help for help\n";

+$::RD_WARN  = 1;
+$::RD_TRACE = 1 if $opt_trace >= 9;
+
 my $parser = Java::Javap::Grammar->new();
 my $caster = Java::Javap::TypeCast->new();
-my $jenny  = Java::Javap::Generator->get_generator( $genwith, @genopts );
+my $jenny  = Java::Javap::Generator->get_generator(
+    $genwith,
+    trace_level => $opt_trace,
+    split(/\s+/, $opt_genopts||''),
+);

 my %check_status;

@@ -65,13 +68,14 @@
     printf "%d ok, %d failed.\n", $passed, keys(%check_status)-$passed;
 }

-if (not $quiet) {
+if ($opt_trace >= 2) {
     print "Types used by specified classes:\n";
     printf "%s\n", $_ for sort keys %top_types;
 }

 exit 0;

+
 sub process_java_class {
     my ($class, $recurse) = @_;
     my $pad = $recurse ? (". " x ($recurse-1)) : "";
@@ -79,8 +83,9 @@
     return undef if our $process_java_class_tried->{$class}++;

     my $cmd = "javap $jpcmd $class";
-    warn "$cmd\n" if $verbose;
-    my $decomp = `javap $jpcmd $class`; # XXX check exit status
+    warn "$cmd\n" if $opt_trace >= 2;
+    my $decomp = `$cmd`; # XXX check exit status
+    warn $decomp if $opt_trace >= 3;
     my $tree   = $parser->comp_unit( $decomp )
         or die "Error parsing output of '$cmd'\n";
     my $kind = $tree->{class_or_interface};
@@ -108,7 +113,7 @@
             or die "Couldn't write to $file_name: $!\n";
         print $API_MODULE $output;
         close $API_MODULE or die "Error writing $file_name: $!\n";
-        warn "$pad$kind $class: $file_name\n" unless $quiet;
+        warn "$pad$kind $class: $file_name\n" if $opt_trace;
     }

     # tell them which types we saw
@@ -122,6 +127,7 @@

     if ($opt_check && $file_name) {
         warn "$pad$kind $class: checking $file_name\n";
+ local $ENV{PERL6LIB} = join(":",grep { $_ } $outdir,$ENV{PERL6LIB}); $check_status{$file_name} = (system("perl6", "-c", $file_name) == 0);
     }

@@ -176,18 +182,21 @@

  java2perl6 [options] class_file

-where options are:
-
+where the most frequently used options are:
+
+ --nest or -n      place output files in nested directories
+ --outdir or -d    top level directory for output
+ --check           check the generated code compiles using perl6
+
+other options
+
+ --help or -h      this message
+ --verbose or -v   sends chatter to the screen
  --jpcmd or -j     a string of command line flags for javap, example:
                    -j '-classpath /some/path'
  --genwith or -g   the name of a Java::Javap::Generator:: module
                    which will make the output, defaults to Std
  --genopts or -p   strings to pass to your -g constructor
- --nest or -n      flag indicates output should go in
-                   nested directories
- --outdir or -d    top level directory for output
- --verbose or -v   sends chatter to the screen
- --help or -h      this message

 =head1 DESCRIPTION

=======================================
--- /trunk/lib/Java/Javap/Generator/Std.pm      Mon Aug 10 14:46:02 2009
+++ /trunk/lib/Java/Javap/Generator/Std.pm      Tue Aug 11 03:11:17 2009
@@ -7,25 +7,59 @@

 # use Data::Dumper;

-# http://perlcabal.org/syn/S02.html#Built-In_Data_Types
 # XXX having this info here is suboptimal
 # should at least be integrated with TypeCaster
-my $perl_builtin_types = { map { $_=>1 } qw(
-    Any Object
-    Bool Int Num Complex Rat Str Bit Regex Set Block List Seq
-    Scalar Array Hash Buf Routine Module
-) };
+# http://perlcabal.org/syn/S02.html#Immutable_types
+my @perl_builtin_immutable = qw(
+    Bit
+    Int
+    Str
+    Num
+    Rat
+    Complex
+    Bool
+    Exception
+    Block
+    List
+    Seq
+    Range
+    Set
+    Bag
+    Signature
+    Capture
+    Blob
+    Instant
+    Duration
+);
+# http://perlcabal.org/syn/S02.html#Mutable_types
+my @perl_builtin_mutable = qw(
+    Scalar Array Hash KeyHash KeySet KeyBag Pair
+    Mapping
+    Buf
+    IO
+    Routine
+    Sub
+    Method
+    Submethod
+    Macro
+    Regex
+    Match
+    Package
+    Module
+    Class
+    Role
+    Grammar
+    Any
+    Object
+);
+my $perl_builtin_types = { map { $_=>1 } (@perl_builtin_immutable, @perl_builtin_mutable) };

 sub new {
-    my $class   = shift;
-    my $debug   = shift;
-
-    my $tt_args = { POST_CHOMP => 1 };
-
-    my $self    = bless { }, $class;
-
-    $self->{debug} = defined $debug ? $debug : 0;
-
+    my $class = shift;
+
+    my $self = bless { @_ }, $class;
+
+    my $tt_args = { POST_CHOMP => 1 };
     $self->tt_args_set( $tt_args );

     return $self;
@@ -47,9 +81,9 @@
     my $self        = shift;
     my $params      = shift;

-    my $class_file  = $params->{ class_file  };
-    my $ast         = $params->{ ast         };
-    my $debug       = defined $params->{debug} ? $params->{debug} : 0;
+    my $class_file  = $params->{class_file};
+    my $ast         = $params->{ast};
+ my $trace_level = defined $params->{trace_level} ? $params->{trace_level} : $self->{trace_level};

     my $type_caster  = Java::Javap::TypeCast->new();
     if (defined $params->{type_file}) {
@@ -61,25 +95,23 @@
     $ast->{method_list} = $self->_get_unique_methods( $ast );
     #print STDERR Dumper($ast->{method_list});

-    my $javap_flags = $params->{ javap_flags };
-
     my $template    = $self->_get_template( $ast );

-    my $tt          = Template->new( $self->tt_args );
-
     my @modules     = $self->_get_modules($ast, $type_caster);

+    my $tt = Template->new( $self->tt_args );
     my $tt_vars = {
         ast        => $ast,
         gen_time   => scalar localtime(),
         version    => $Java::Javap::VERSION,
         class_file => $class_file,
         type_caster=> $type_caster,
-        javap_flags=> $javap_flags,
+        javap_flags=> $params->{javap_flags},
         modules    => \...@modules,
     };
     my $retval;
-    $tt->process( \$template, $tt_vars, \$retval ) || die $tt->error();
+    $tt->process( \$template, $tt_vars, \$retval )
+        or die "Error processing template: ".$tt->error();

     return $retval;
 }
@@ -165,7 +197,8 @@
             $perl_types{$target}++;
         }
     }
- #warn "$ast->{perl_qualified_name} references types: @{[ keys %perl_types ]}\n"; + warn "$ast->{perl_qualified_name} references types: @{[ keys %perl_types ]}\n"
+        if $self->{trace_level} >= 3;

     for my $perl_type (keys %perl_types) {

@@ -182,7 +215,8 @@
             #or $element->{returns}->{array_text} =~ /Array of/;
             ;
     }
- #warn "$ast->{perl_qualified_name} needs to load: @{[ keys %perl_types ]}\n"; + warn "$ast->{perl_qualified_name} needs to load: @{[ keys %perl_types ]}\n"
+        if $self->{trace_level} >= 3;

     return (sort keys %perl_types);
 }
=======================================
--- /trunk/lib/Java/Javap/TypeCast.pm   Mon Aug 10 15:31:12 2009
+++ /trunk/lib/Java/Javap/TypeCast.pm   Tue Aug 11 03:11:17 2009
@@ -11,19 +11,27 @@
     float              => 'Num',
     double             => 'Num',
     boolean            => 'Bool',
-    'java.lang.Object' => 'Object',
-    'java.lang.String' => 'Str',
-    'java.lang.Number'  => 'Num',
-    'java.lang.Class'  => 'Any',
-    'java.math.BigInteger' => 'Int',
-    'java.math.BigNumber' => 'Int',
-    'java.net.URI'     => 'Str',
-    'java.net.URL'     => 'Str',
-    'java.io.InputStream' => 'IO',
+    'java.lang.Object'      => 'Object',
+    'java.lang.String'      => 'Str',
+    'java.lang.Number'      => 'Num',
+    'java.lang.Class'       => 'Any',
+    'java.lang.CharSequence'=> 'Str',
+    'java.lang.Appendable'  => 'Object',
+
+    'java.math.BigInteger'  => 'Int',
+    'java.math.BigNumber'   => 'Int',
+
+    'java.net.URI'          => 'Str',
+    'java.net.URL'          => 'Str',
+
+    'java.io.InputStream'   => 'IO',
+
+    'java.nio.ByteBuffer'   => 'Buf',
+    'java.nio.CharBuffer'   => 'Str',

     # XXX hacks
# java.security.Permission has recursive dependency with java.security.PermissionCollection
-    'java.security.Permission' => 'Object',
+    'java.security.Permission'      => 'Object',
     'java.security.BasicPermission' => 'Object',
 };

Reply via email to