| 1 | package Java::Javap::TypeCast; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Java::Javap::TypeCast - map Java types to Perl 6 equivalents | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use Java::Javap::TypeCast; | ||||||||||
| 10 | |||||||||||
| 11 | $type_caster = Java::Javap::TypeCast->new(); | ||||||||||
| 12 | |||||||||||
| 13 | $perl_type = $type_caster->cast( $java_type ); | ||||||||||
| 14 | |||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||
| 16 | |||||||||||
| 17 | Provides a mechanism to map java type names (classes and interfaces) into | ||||||||||
| 18 | corresponding perl type names (typically roles). | ||||||||||
| 19 | |||||||||||
| 20 | =head1 METHODS | ||||||||||
| 21 | |||||||||||
| 22 | =cut | ||||||||||
| 23 | |||||||||||
| 24 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 7 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | =head2 new | ||||||||||
| 30 | |||||||||||
| 31 | $type_caster = Java::Javap::TypeCast->new(); | ||||||||||
| 32 | |||||||||||
| 33 | Returns a new type caster instance with a default set of type casts. | ||||||||||
| 34 | |||||||||||
| 35 | The default set of type mappings should I<not> be relied upon as it's likely to | ||||||||||
| 36 | change over time with unpredictable results for your application. You should call | ||||||||||
| 37 | L</set_type_casts> and perhaps a method like L</add_type_casts_from_file> to | ||||||||||
| 38 | load in your own set of type mappings. | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 43 | 0 | my $class = shift; | |||||||||
| 44 | 0 | my $self = bless { }, $class; | |||||||||
| 45 | 0 | $self->_add_type_casts_from_DATA(); | |||||||||
| 46 | 0 | return $self; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | =head2 set_type_casts | ||||||||||
| 50 | |||||||||||
| 51 | $self->set_type_casts(\%hash) | ||||||||||
| 52 | |||||||||||
| 53 | Replaces the current set of type casts with the specified set. | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub set_type_casts { | ||||||||
|
Subroutine
set_type_casts: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 58 | 0 | my ($self, $new_casts) = @_; | |||||||||
| 59 | 0 | $self->{casts} = { %$new_casts }; | |||||||||
| 60 | 0 | return; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | =head2 add_type_casts | ||||||||||
| 65 | |||||||||||
| 66 | $self->add_type_casts(\%hash) | ||||||||||
| 67 | |||||||||||
| 68 | Adds the specified set of type casts to the current set, overriding any that | ||||||||||
| 69 | have the same names. | ||||||||||
| 70 | |||||||||||
| 71 | =cut | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub add_type_casts { | ||||||||
|
Subroutine
add_type_casts: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 74 | 0 | my ($self, $new_casts) = @_; | |||||||||
| 75 | $self->{casts}{$_} = $new_casts->{$_} | ||||||||||
| 76 | 0 | for keys %$new_casts; | |||||||||
| 77 | 0 | return; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | |||||||||||
| 81 | =head2 add_type_casts_from_filehandle | ||||||||||
| 82 | |||||||||||
| 83 | $self->add_type_casts_from_filehandle($fh, $name) | ||||||||||
| 84 | |||||||||||
| 85 | Reads lines defining type mappings from the specified filehandle. | ||||||||||
| 86 | Each is specified as two non-blank fields separated by whitespace. | ||||||||||
| 87 | The first specified a Java type and the second a corresponding Perl type. | ||||||||||
| 88 | Comments starting with a # character are ignored, as are blank lines. | ||||||||||
| 89 | |||||||||||
| 90 | A warning is issued for lines that aren't in the correct format. | ||||||||||
| 91 | The $name argument is only used in that warnig message. | ||||||||||
| 92 | |||||||||||
| 93 | =cut | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | sub add_type_casts_from_filehandle { | ||||||||
|
Subroutine
add_type_casts_from_filehandle: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 96 | 0 | my ($self, $fh, $name) = @_; | |||||||||
| 97 | 0 | ▶ | while (<$fh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | chomp; | |||||||||
| 99 | 0 | s/#.*//; # remove comments | |||||||||
| 100 | 0 | ▶ | next if m/^ \s* $/x; # ignore blank lines | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | my @items = split /\s+/; | |||||||||
| 102 | 0 | ▶ | if (@items != 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | warn "Ignored line $. in $name: $_\n"; | |||||||||
| 104 | 0 | next; | |||||||||
| 105 | } | ||||||||||
| 106 | 0 | my ($javatype, $perltype) = @items; | |||||||||
| 107 | 0 | $self->{casts}{$javatype} = $perltype; | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | =head2 add_type_casts_from_file | ||||||||||
| 112 | |||||||||||
| 113 | $self->add_type_casts_from_file($filename) | ||||||||||
| 114 | |||||||||||
| 115 | Opens $filename for reading and calls L</add_type_casts_from_filehandle>. | ||||||||||
| 116 | |||||||||||
| 117 | =cut | ||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | sub add_type_casts_from_file { | ||||||||
|
Subroutine
add_type_casts_from_file: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 120 | 0 | my ($self, $filename) = @_; | |||||||||
| 121 | 0 | ▶ | open my $fh, '<', $filename | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | or croak "Unable to open '$filename' for reading: $!"; | ||||||||||
| 123 | 0 | return $self->add_type_casts_from_filehandle($fh, $filename); | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | |||||||||||
| 127 | # _add_type_casts_from_DATA - private | ||||||||||
| 128 | |||||||||||
| 129 | 0 | ▶ | sub _add_type_casts_from_DATA { | ||||||||
|
Subroutine
_add_type_casts_from_DATA: not called CC=1 SCAR=6.9
| |||||||||||
| 130 | 0 | my ($self, $filename) = @_; | |||||||||
| 131 | 0 | local $.; # don't add chunk/filename to future warning messages | |||||||||
| 132 | 0 | my $position = tell( DATA ); | |||||||||
| 133 | 0 | $self->add_type_casts_from_filehandle(\*DATA, 'default DATA'); | |||||||||
| 134 | 0 | seek DATA, $position, 0; # Reset the filehandle for next time | |||||||||
| 135 | 0 | return; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | =head2 cast | ||||||||||
| 140 | |||||||||||
| 141 | $perl_type = $type_caster->defined_cast( $java_type ); | ||||||||||
| 142 | |||||||||||
| 143 | Returns a perl type for the corresponding java type argument if an type mapping | ||||||||||
| 144 | has been defined, else undef. | ||||||||||
| 145 | |||||||||||
| 146 | Firstly the java type is looked up verbatim in the type mapping. | ||||||||||
| 147 | If a defined value is found then it's returned. | ||||||||||
| 148 | |||||||||||
| 149 | If there's no verbatim match for the full type name then defined_cast() checks for | ||||||||||
| 150 | wildcard matches by removing trailing words and appending a '*'. For example, | ||||||||||
| 151 | if there's no entry for 'sun.lang.annotation.foo' the defined_cast() would look for | ||||||||||
| 152 | each of these in turn: | ||||||||||
| 153 | |||||||||||
| 154 | sun.lang.annotation.foo | ||||||||||
| 155 | sun.lang.annotation.* | ||||||||||
| 156 | sun.lang.* | ||||||||||
| 157 | sun.* | ||||||||||
| 158 | * | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | sub defined_cast { | ||||||||
|
Subroutine
defined_cast: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 163 | 0 | my $self = shift; | |||||||||
| 164 | 0 | my $java_type = shift; | |||||||||
| 165 | |||||||||||
| 166 | 0 | my $casts = $self->{casts}; | |||||||||
| 167 | |||||||||||
| 168 | 0 | my $perl6_type = $casts->{ $java_type }; | |||||||||
| 169 | |||||||||||
| 170 | 0 | ▶ | if (not defined $perl6_type) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | # no specific type cast so look for wildcard ones | ||||||||||
| 172 | 0 | my @parts = split /\./, $java_type; | |||||||||
| 173 | 0 | ▶ | while (@parts) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 0 | $parts[-1] = '*'; # replace last word with * | |||||||||
| 175 | 0 | $perl6_type = $casts->{ join '.', @parts }; | |||||||||
| 176 | 0 | ▶ | last if defined $perl6_type; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | 0 | pop @parts; | |||||||||
| 178 | } | ||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 0 | return $perl6_type; | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | |||||||||||
| 185 | |||||||||||
| 186 | =head2 fallback_cast() | ||||||||||
| 187 | |||||||||||
| 188 | $perl_type = $type_caster->cast( $java_type ); | ||||||||||
| 189 | |||||||||||
| 190 | Returns a perl type for the corresponding java type argument by editing the | ||||||||||
| 191 | java type name, without consulting the type mapping. | ||||||||||
| 192 | |||||||||||
| 193 | - dots are changed to double colons | ||||||||||
| 194 | - dollar symbols are changed to _PRIVATE_ | ||||||||||
| 195 | |||||||||||
| 196 | =cut | ||||||||||
| 197 | |||||||||||
| 198 | 0 | ▶ | sub fallback_cast { | ||||||||
|
Subroutine
fallback_cast: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 199 | 0 | my $self = shift; | |||||||||
| 200 | 0 | my $java_type = shift; | |||||||||
| 201 | |||||||||||
| 202 | 0 | (my $perl6_type = $java_type) =~ s/\./::/g; | |||||||||
| 203 | 0 | $perl6_type =~ s/\$/_PRIVATE_/g; # handle '$' in type names | |||||||||
| 204 | |||||||||||
| 205 | 0 | return $perl6_type; | |||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | |||||||||||
| 209 | =head2 cast | ||||||||||
| 210 | |||||||||||
| 211 | $perl_type = $type_caster->cast( $java_type ); | ||||||||||
| 212 | |||||||||||
| 213 | Returns the result of calling L</defined_cast>, if defined, else returns the | ||||||||||
| 214 | result of calling L</fallback_cast>. | ||||||||||
| 215 | |||||||||||
| 216 | =cut | ||||||||||
| 217 | |||||||||||
| 218 | 0 | ▶ | sub cast { | ||||||||
|
Subroutine
cast: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 219 | 0 | my $self = shift; | |||||||||
| 220 | 0 | my $java_type = shift; | |||||||||
| 221 | 0 | my $perl6_type = $self->defined_cast($java_type); | |||||||||
| 222 | |||||||||||
| 223 | 0 | ▶ | if (not defined $perl6_type) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 224 | 0 | $perl6_type = $self->fallback_cast($java_type); | |||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | 0 | return $perl6_type; | |||||||||
| 228 | } | ||||||||||
| 229 | |||||||||||
| 230 | =head1 AUTHOR | ||||||||||
| 231 | |||||||||||
| 232 | Tim Bunce, E<lt>tim.bunce@pobox.comE<gt>, | ||||||||||
| 233 | Phil Crow, E<lt>crow.phil@gmail.comE<gt> | ||||||||||
| 234 | |||||||||||
| 235 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 236 | |||||||||||
| 237 | Copyright (C) 2010, Tim Bunce | ||||||||||
| 238 | Copyright (C) 2007, Phil Crow | ||||||||||
| 239 | |||||||||||
| 240 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 241 | it under the same terms as Perl itself, either Perl version 5.8.6 or, | ||||||||||
| 242 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 243 | |||||||||||
| 244 | =cut | ||||||||||
| 245 | |||||||||||
| 246 | 7 | 1; | |||||||||
| 247 | |||||||||||
| 248 | __DATA__ | ||||||||||