| 1 | package Java::Javap; | ||||||||||||
| 2 | |||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 7 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 7 | our $VERSION = '0.06'; | |||||||||||
| 8 | 7 | our $JAVAP_EXECUTABLE = 'javap'; | |||||||||||
| 9 | |||||||||||||
| 10 | 7 | ▶ | use Java::Javap::TypeCast; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | sub get_referenced_typenames { | ||||||||||
|
Subroutine
get_referenced_typenames: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||
| 13 | 0 | shift; # invoked through this class, discard our name | |||||||||||
| 14 | 0 | my $tree = shift; | |||||||||||
| 15 | |||||||||||||
| 16 | 0 | my %answers; | |||||||||||
| 17 | |||||||||||||
| 18 | 0 | for my $type_name ( $tree->{ parent }, @{ $tree->{implements} } ) { | |||||||||||
| 19 | 0 | ▶ | $answers{ $type_name }++ if $type_name; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 0 | foreach my $element ( @{ $tree->{contents} } ) { | |||||||||||
| 23 | |||||||||||||
| 24 | 0 | ▶ | $answers{ $element->{type}->{name} }++ if $element->{type}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | |||||||||||||
| 26 | 0 | ▶ | if (my $args = $element->{args}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 0 | $answers{ $_->{name} }++ for @$args; | |||||||||||
| 28 | } | ||||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | # remove own class | ||||||||||||
| 32 | 0 | delete $answers{ $tree->{java_qualified_name} }; | |||||||||||
| 33 | 0 | delete $answers{void}; | |||||||||||
| 34 | |||||||||||||
| 35 | 0 | return keys %answers; | |||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | 0 | ▶ | sub get_included_types { # XXX change name | ||||||||||
|
Subroutine
get_included_types: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 39 | 0 | my ($class, $tree, $caster) = @_; | |||||||||||
| 40 | 0 | my @types = $class->get_referenced_typenames($tree); | |||||||||||
| 41 | 0 | return [ grep { not $caster->defined_cast($_) } @types ]; | |||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | |||||||||||||
| 45 | 6 | ▶ | sub javap { | ||||||||||
|
Subroutine
javap: called CC=7 SCAR=28.2
Pod
documented
| |||||||||||||
| 46 | 6 | my ($self, $classes, $options) = @_; | |||||||||||
| 47 | 6 | ▶ | $options ||= []; | ||||||||||
|
Condition50%1/2
$options ||= []
MC/DC0%0/1
$options ||= []
$options
Truth table50%1/2
A
A= $options
| |||||||||||||
| 48 | |||||||||||||
| 49 | 6 | ▶ | croak "No classes to be parsed" if not $classes; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 50 | 6 | ▶ | $classes = [ $classes ] if not ref $classes; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | |||||||||||||
| 52 | # Open the real javap executable and read output from it | ||||||||||||
| 53 | 6 | my @cmd = ($JAVAP_EXECUTABLE, @$options, @$classes); | |||||||||||
| 54 | |||||||||||||
| 55 | 6 | ▶ | open(my $javap_fh, '-|', @cmd) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | or croak "@cmd failed: $!"; | ||||||||||||
| 57 | |||||||||||||
| 58 | 0 | my $javap_output = q{}; | |||||||||||
| 59 | |||||||||||||
| 60 | 0 | local $_; | |||||||||||
| 61 | 0 | ▶ | while (<$javap_fh>) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | $javap_output .= $_; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | # When dealing with a pipe, we also want to get errors from close | ||||||||||||
| 66 | 0 | ▶ | close $javap_fh | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | or croak "@cmd failed on close: $!"; | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | return $javap_output; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | # Shortcut for the test suite | ||||||||||||
| 73 | 6 | ▶ | sub javap_test { | ||||||||||
|
Subroutine
javap_test: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 74 | 6 | my ($self) = @_; | |||||||||||
| 75 | 6 | my $output; | |||||||||||
| 76 | 6 | eval { $output = $self->javap(['java.lang.String']) }; | |||||||||||
| 77 | 6 | ▶ | return $output ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 7 | 1; | |||||||||||
| 81 | |||||||||||||
| 82 | =head1 NAME | ||||||||||||
| 83 | |||||||||||||
| 84 | Java::Javap - Java to Perl 6 API translator | ||||||||||||
| 85 | |||||||||||||
| 86 | =head1 SYNOPSIS | ||||||||||||
| 87 | |||||||||||||
| 88 | java2perl6api java.somepackage.Module | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 DESCRIPTION | ||||||||||||
| 91 | |||||||||||||
| 92 | See C<java2perl6api> for instructions and advice on use of this module. | ||||||||||||
| 93 | |||||||||||||
| 94 | This particular module is only a place holder for the version number | ||||||||||||
| 95 | of this project (see below). | ||||||||||||
| 96 | |||||||||||||
| 97 | =head1 METHODS | ||||||||||||
| 98 | |||||||||||||
| 99 | =head2 get_included_types | ||||||||||||
| 100 | |||||||||||||
| 101 | Call this as a class method. | ||||||||||||
| 102 | |||||||||||||
| 103 | Parameters: an abstract syntax tree you got from calling comp_unit | ||||||||||||
| 104 | an a C<Java::Javap::Grammar> object. | ||||||||||||
| 105 | |||||||||||||
| 106 | Returns: an array reference of the types in arguments to methods, or | ||||||||||||
| 107 | return values (thrown types are not reported). | ||||||||||||
| 108 | |||||||||||||
| 109 | =head2 javap | ||||||||||||
| 110 | |||||||||||||
| 111 | Invokes the C<javap> process and return the output. | ||||||||||||
| 112 | Throws an exception if something goes wrong, like C<javap> is | ||||||||||||
| 113 | not found. | ||||||||||||
| 114 | |||||||||||||
| 115 | =head3 Parameters | ||||||||||||
| 116 | |||||||||||||
| 117 | =over | ||||||||||||
| 118 | |||||||||||||
| 119 | =item \@classes | ||||||||||||
| 120 | |||||||||||||
| 121 | List of classes to be decompiled. It can also be supplied as a string, | ||||||||||||
| 122 | if a single class should be decompiled. | ||||||||||||
| 123 | |||||||||||||
| 124 | =item \@options | ||||||||||||
| 125 | |||||||||||||
| 126 | Options to be passed to the C<javap> process. | ||||||||||||
| 127 | |||||||||||||
| 128 | =back | ||||||||||||
| 129 | |||||||||||||
| 130 | =head3 Example | ||||||||||||
| 131 | |||||||||||||
| 132 | my @classes = ('java.lang.String'); | ||||||||||||
| 133 | my @options = (); | ||||||||||||
| 134 | my $output = Java::Javap->javap(\@classes, \@options); | ||||||||||||
| 135 | |||||||||||||
| 136 | # or ... | ||||||||||||
| 137 | |||||||||||||
| 138 | my $output = Java::Javap->javap('java.lang.String'); | ||||||||||||
| 139 | |||||||||||||
| 140 | =head1 SEE ALSO | ||||||||||||
| 141 | |||||||||||||
| 142 | =over | ||||||||||||
| 143 | |||||||||||||
| 144 | =item C<java2perl6api> | ||||||||||||
| 145 | |||||||||||||
| 146 | =item C<Java::Javap::Generator> | ||||||||||||
| 147 | |||||||||||||
| 148 | =item C<Java::Javap::Generator::Perl6> | ||||||||||||
| 149 | |||||||||||||
| 150 | =item C<javap.grammar> | ||||||||||||
| 151 | |||||||||||||
| 152 | =back | ||||||||||||
| 153 | |||||||||||||
| 154 | =head1 AUTHORS | ||||||||||||
| 155 | |||||||||||||
| 156 | Philip Crow, E<lt>crow.phil@gmail.comE<gt> | ||||||||||||
| 157 | Cosimo Streppone, E<lt>cosimo@cpan.orgE<gt> | ||||||||||||
| 158 | |||||||||||||
| 159 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 160 | |||||||||||||
| 161 | Copyright (C) 2007 by Philip Crow | ||||||||||||
| 162 | |||||||||||||
| 163 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 164 | it under the same terms as Perl itself, either Perl version 5.8.6 or, | ||||||||||||
| 165 | at your option, any later version of Perl 5 you may have available. | ||||||||||||
| 166 | |||||||||||||
| 167 | =cut | ||||||||||||