| 1 | package Language::Tea::JavaEmitter; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Symbol; | ||
| 6 | use Language::Tea::Traverse; | ||
| 7 | use Scalar::Util qw(blessed); | ||
| 8 | use IPC::Open2; | ||
| 9 | |||
| 10 | our $IF_CONDITION_COUNTER = 0; | ||
| 11 | # This variable is used in the conversion of tdbc.TCallableStatement | ||
| 12 | our @registeredOuts; | ||
| 13 | |||
| 14 | our @functions; | ||
| 15 | our %methods; | ||
| 16 | our $compFunction; | ||
| 17 | |||
| 18 | # Hash used for type conversion | ||
| 19 | # In case you want to substitute a Tea type for a Java type, just add here | ||
| 20 | our %typeConversion = ( | ||
| 21 | "TFileInput" => "BufferedReader", | ||
| 22 | "TFileOutput" => "BufferedWriter", | ||
| 23 | "TUrlInput" => "BufferedReader", | ||
| 24 | "TConnection" => "Connection", | ||
| 25 | "TDate" => "java.util.Date", | ||
| 26 | "THashtable" => "java.util.Hashtable", | ||
| 27 | "TVector" => "java.util.Vector" | ||
| 28 | ); | ||
| 29 | |||
| 30 | # Hash used for method name conversion | ||
| 31 | # In case you want to substitute a Tea method for a Java method, just add here | ||
| 32 | our %methodConvertion = ( "readln" => "readLine", | ||
| 33 | "writeln" => "write", | ||
| 34 | "autocommit" => "setAutoCommit", | ||
| 35 | "prepare" => "prepareStatement", | ||
| 36 | "statement" => "createStatement", | ||
| 37 | "update" => "executeUpdate", | ||
| 38 | "query" => "executeQuery", | ||
| 39 | "connect" => "tdbcConnect", | ||
| 40 | "getColumnCount" => "getMetaData().getColumnCount", | ||
| 41 | "getColumnName" => "getMetaData().getColumnName", | ||
| 42 | "hasMoreRows" => "isLast", | ||
| 43 | "skip" => "relative", | ||
| 44 | "getDayOfWeek" => "getDay", | ||
| 45 | "getDay" => "getDate", | ||
| 46 | "getHour" => "getHours", | ||
| 47 | "getMinute" => "getMinutes", | ||
| 48 | "getSecond" => "getSeconds", | ||
| 49 | "isKey" => "containsKey", | ||
| 50 | "getSize" => "size", | ||
| 51 | "getAt" => "get", | ||
| 52 | "push" => "add", | ||
| 53 | "resize" => "setSize", | ||
| 54 | ); | ||
| 55 | |||
| 56 | sub emit { | ||
| 57 | my ( $root, $package ) = @_; | ||
| 58 | return Language::Tea::Traverse::visit_postfix( | ||
| 59 | $root, | ||
| 60 | sub { | ||
| 61 | my ($node) = @_; | ||
| 62 | for ( ref $node ) { | ||
| 63 | /^TeaPart::Comment$/ && do { | ||
| 64 | return ' // ' | ||
| 65 | . $node->{comment_text} | ||
| 66 | . ( $node->{context}{liner} ? "\n" : "" ); | ||
| 67 | }; | ||
| 68 | /^TeaPart::arg_symbol$/ && do { | ||
| 69 | my $name = $node->{mangled}; | ||
| 70 | $name ||= $node->{arg_symbol}; | ||
| 71 | return $name . eol($node); | ||
| 72 | }; | ||
| 73 | /^TeaPart::definition_list$/ && do { | ||
| 74 | return $node->{arg}; | ||
| 75 | }; | ||
| 76 | /^TeaPart::arg_list$/ && do { | ||
| 77 | return '(' | ||
| 78 | . ( join ', ', @{ $node->{arg_list} } ) . ')' | ||
| 79 | . eol($node); | ||
| 80 | }; | ||
| 81 | /^TeaPart::arg_string$/ && do { | ||
| 82 | return '"' . $node->{arg_string} . '"' . eol($node); | ||
| 83 | }; | ||
| 84 | /^TeaPart::arg_substitution$/ && do { | ||
| 85 | return $node->{arg_substitution} . eol($node); | ||
| 86 | }; | ||
| 87 | /^TeaPart::arg_integer$/ && do { | ||
| 88 | return 'new Integer(' | ||
| 89 | . $node->{arg_integer} . ')' | ||
| 90 | . eol($node); | ||
| 91 | }; | ||
| 92 | /^TeaPart::arg_double$/ && do { | ||
| 93 | return 'new Double(' | ||
| 94 | . $node->{arg_double} . ')' | ||
| 95 | . eol($node); | ||
| 96 | }; | ||
| 97 | /^TeaPart::arg_code$/ && do { | ||
| 98 | return "{\n" | ||
| 99 | . ( join "", @{ $node->{arg_code}{statement} } ) . "}\n"; | ||
| 100 | }; | ||
| 101 | /^TeaPart::arg_do$/ && do { | ||
| 102 | return "(" | ||
| 103 | . $node->{arg_do}{statement}[0] . ")" | ||
| 104 | . eol($node); | ||
| 105 | }; | ||
| 106 | /^TeaPart::Define$/ && do { | ||
| 107 | my $type = $node->{type} || 'TeaUnkownType'; | ||
| 108 | $type = $typeConversion{$type} | ||
| 109 | if defined $typeConversion{$type}; | ||
| 110 | my $val = $node->{statement}[0]; | ||
| 111 | $val =~ s/\(\)$//g; | ||
| 112 | if ( blessed $val || !ref $val ) { | ||
| 113 | $val = ' = ' . $val; | ||
| 114 | } | ||
| 115 | else { | ||
| 116 | $val = ''; | ||
| 117 | } | ||
| 118 | |||
| 119 | return $type . ' ' | ||
| 120 | . $node->{mangled} | ||
| 121 | . $val | ||
| 122 | . eol($node); | ||
| 123 | }; | ||
| 124 | /^TeaPart::DefineFunc$/ && do { | ||
| 125 | |||
| 126 | # the code is not here, it's saved on the top level entity. | ||
| 127 | my $type = $node->{type} || 'TeaUnknownType'; | ||
| 128 | |||
| 129 | unless ( $node->{arg_code}{statement}[0] && | ||
| 130 | ref $node->{arg_code}{statement}[0] ne 'HASH') { | ||
| 131 | # This is not a function. This is a List | ||
| 132 | my $listCode = "Vector $node->{arg_symbol} = new Vector();"; | ||
| 133 | for (@{$node->{arg_list}}) { | ||
| 134 | $listCode .= "$node->{arg_symbol}.add($_);"; | ||
| 135 | } | ||
| 136 | return $listCode; | ||
| 137 | } | ||
| 138 | |||
| 139 | my $code = 'public static ' . $type . ' ' | ||
| 140 | . $node->{arg_symbol} . "("; | ||
| 141 | |||
| 142 | $code .= join( ", ", @{ $node->{arg_list} } ) if ref $node->{arg_list} eq 'ARRAY'; | ||
| 143 | $code .= ")" . "{\n"; | ||
| 144 | |||
| 145 | $code .= ( join "", @{ $node->{arg_code}{statement} } ) if @{ $node->{arg_code}{statement} }; | ||
| 146 | $code .= "}\n"; | ||
| 147 | |||
| 148 | |||
| 149 | push @functions , $code; | ||
| 150 | |||
| 151 | return ' '; | ||
| 152 | }; | ||
| 153 | |||
| 154 | /^TeaPart::Global$/ && do { | ||
| 155 | my $type = $node->{type} || 'TeaUnkownType'; | ||
| 156 | $type = $typeConversion{$type} | ||
| 157 | if defined $typeConversion{$type}; | ||
| 158 | my $val = $node->{statement}[0]; | ||
| 159 | if ( blessed $val || !ref $val ) { | ||
| 160 | $val = ' = ' . $val; | ||
| 161 | } | ||
| 162 | else { | ||
| 163 | $val = ''; | ||
| 164 | } | ||
| 165 | return 'public static '.$type . ' ' | ||
| 166 | . $node->{mangled} | ||
| 167 | . $val | ||
| 168 | . eol($node); | ||
| 169 | }; | ||
| 170 | /^TeaPart::GlobalFunc$/ && do { | ||
| 171 | |||
| 172 | # the code is not here, it's saved on the top level entity. | ||
| 173 | my $type = $node->{type} || 'TeaUnknownType'; | ||
| 174 | |||
| 175 | my $code = | ||
| 176 | 'public static ' . $type . ' ' | ||
| 177 | . $node->{arg_symbol} . "(" | ||
| 178 | . join( ", ", @{ $node->{arg_list} } ) . ")" . "{\n" | ||
| 179 | . ( join "", @{ $node->{arg_code}{statement} } ) . "}\n"; | ||
| 180 | my $walker = $node; | ||
| 181 | while (( ref($walker) ne 'TeaProgram' ) | ||
| 182 | && ( ref($walker) ne 'TeaPart::Method' ) ) | ||
| 183 | { | ||
| 184 | $walker = $walker->{__node_parent__}; | ||
| 185 | } | ||
| 186 | $walker->{functions} ||= []; | ||
| 187 | push @{ $walker->{functions} }, $code; | ||
| 188 | |||
| 189 | #return ' '; | ||
| 190 | return $code; | ||
| 191 | }; | ||
| 192 | |||
| 193 | |||
| 194 | /^TeaPart::Apply$/ && do { | ||
| 195 | my $ereturn = ""; | ||
| 196 | $ereturn = "return " if $node->{context}{ireturn}; | ||
| 197 | return $ereturn.Language::Tea::Function::emit_java($node) | ||
| 198 | . eol($node); | ||
| 199 | }; | ||
| 200 | /^TeaPart::If$/ && do { | ||
| 201 | my $comment = ''; | ||
| 202 | $comment = $node->{comment} . "\n" if $node->{comment}; | ||
| 203 | return $comment . "if (" | ||
| 204 | . $node->{condition} . ') ' | ||
| 205 | . $node->{then} | ||
| 206 | . ( $node->{else} ? ' else ' . $node->{else} : '' ); | ||
| 207 | |||
| 208 | }; | ||
| 209 | /^TeaPart::Cond$/ && do { | ||
| 210 | my $comment = ''; | ||
| 211 | $comment = $node->{comment} . "\n" if $node->{comment}; | ||
| 212 | |||
| 213 | $node->{condition}[0] =~ s/[{\n|;\n}]//g; | ||
| 214 | #die $node->{condition}[0]; | ||
| 215 | |||
| 216 | my $code = 'if (' | ||
| 217 | .$node->{condition}[0] .')' | ||
| 218 | .$node->{instructions}[0]; | ||
| 219 | for (my $i = 1; $i < (@{$node->{condition}}); ++$i) { | ||
| 220 | $node->{condition}[$i] =~ s/[{\n|;\n}]//g; | ||
| 221 | $code .= 'else if (' | ||
| 222 | .$node->{condition}[$i] .')' | ||
| 223 | .$node->{instructions}[$i]; | ||
| 224 | } | ||
| 225 | $code .= 'else' | ||
| 226 | . $node->{else}; | ||
| 227 | |||
| 228 | return $code; | ||
| 229 | |||
| 230 | }; | ||
| 231 | /^TeaPart::While$/ && do { | ||
| 232 | my $comment = ''; | ||
| 233 | $comment = $node->{comment} . "\n" if $node->{comment}; | ||
| 234 | return $comment | ||
| 235 | . "while (" | ||
| 236 | . $node->{condition} . ') ' | ||
| 237 | . $node->{block}; | ||
| 238 | }; | ||
| 239 | /^TeaPart::foreach$/ && do { | ||
| 240 | my $comment = ''; | ||
| 241 | $comment = $node->{comment} . "\n" if $node->{comment}; | ||
| 242 | return $comment | ||
| 243 | . "for (TeaUnknownType " | ||
| 244 | . $node->{var1}.' : ' | ||
| 245 | . $node->{var2}.') ' | ||
| 246 | . $node->{block}; | ||
| 247 | }; | ||
| 248 | /^TeaProgram$/ && do { | ||
| 249 | my $code = <<START; | ||
| 250 | //package $package; | ||
| 251 | |||
| 252 | class MainProgram { | ||
| 253 | |||
| 254 | public static void main(String[] args) { | ||
| 255 | try{ | ||
| 256 | START | ||
| 257 | |||
| 258 | $code .= ( join "", @{ $node->{statement} } ); | ||
| 259 | |||
| 260 | $code .= "}catch(Exception e) { | ||
| 261 | System.out.println(e.getMessage()); | ||
| 262 | } | ||
| 263 | } \n\n"; | ||
| 264 | |||
| 265 | #print "$code \n\n\n\n\n\n"; | ||
| 266 | #$code .= join '', @{ $node->{functions} } | ||
| 267 | #if $node->{functions}; | ||
| 268 | |||
| 269 | |||
| 270 | # Is this the sort function?? | ||
| 271 | my $sortClass; | ||
| 272 | foreach my $funct (@functions) { | ||
| 273 | if (defined $compFunction && $funct =~/.* $compFunction\s*\(/) { | ||
| 274 | $funct =~ m/(\()(.*)(,)(.*)(\))/; | ||
| 275 | my $v1 = $2; | ||
| 276 | my $v2 = $4; | ||
| 277 | $funct =~ s/^.*\n//; | ||
| 278 | $funct =~ s/}$/}}/; | ||
| 279 | $sortClass = "\n\nclass Comparer implements Comparator {" | ||
| 280 | . "public int compare(Integer $v1, Integer $v2) {" | ||
| 281 | . $funct; | ||
| 282 | }else{ | ||
| 283 | $code .= "\n\n". $funct; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | $code .= "}\n"; | ||
| 289 | $code .= $sortClass if defined $sortClass; | ||
| 290 | # die $code; | ||
| 291 | return indent($code); | ||
| 292 | }; | ||
| 293 | /^TeaPart::Class$/ && do { | ||
| 294 | my $members = " "; | ||
| 295 | my $extends = ''; | ||
| 296 | $extends = "extends " .$node->{super_class}.' ' if defined $node->{super_class}; | ||
| 297 | if (ref $node->{arg_list} eq 'ARRAY'){ | ||
| 298 | $members = "\nprivate unknownType "; | ||
| 299 | $members .= join( ";\nprivate unknownType ", @{ $node->{arg_list} } ); | ||
| 300 | $members .= ";"; | ||
| 301 | } | ||
| 302 | |||
| 303 | # if we have a sort method, the we have to have a special class | ||
| 304 | # This is that special class | ||
| 305 | my $code .= | ||
| 306 | "public class $node->{class} $extends\{ "; | ||
| 307 | $code .= $members | ||
| 308 | . "\n\n" | ||
| 309 | . "//########################################################################### \n" | ||
| 310 | . "//########################### END OF PRIVATE MEMBERS ######################## \n" | ||
| 311 | . "//########################################################################### \n\n"; | ||
| 312 | |||
| 313 | if (ref $methods{$node->{class}} eq 'ARRAY'){ | ||
| 314 | $code .= join "", @{ $methods{$node->{class}} }; | ||
| 315 | }else{ | ||
| 316 | $code .= $methods{$node->{class}}; | ||
| 317 | } | ||
| 318 | $code .= join '', @{ $node->{functions} } | ||
| 319 | if $node->{functions}; | ||
| 320 | |||
| 321 | $code .= "}\n"; | ||
| 322 | return indent($code); | ||
| 323 | }; | ||
| 324 | /^TeaPart::Method/ && do { | ||
| 325 | my $code = ""; | ||
| 326 | my $type = $node->{type} || 'TeaUnknownType'; | ||
| 327 | $type = 'void' if ($node->{method} =~ /set.*/i); | ||
| 328 | |||
| 329 | my $args; | ||
| 330 | |||
| 331 | if ( ref $node->{arg_list} eq 'ARRAY' ) { | ||
| 332 | $args = join( ', ', @{ $node->{arg_list}} ); | ||
| 333 | }elsif (ref $node->{arg_list} eq 'HASH') { | ||
| 334 | $args = ""; | ||
| 335 | }else{ | ||
| 336 | $args = $node->{arg_list}; | ||
| 337 | } | ||
| 338 | |||
| 339 | if ($node->{method} eq 'constructor') { | ||
| 340 | $code .= 'public '. $node->{class}. '(' | ||
| 341 | . $args | ||
| 342 | . ')'; | ||
| 343 | } | ||
| 344 | else{ | ||
| 345 | $code .= 'public '. $type .' ' .$node->{method}.'(' | ||
| 346 | . $args.')'; | ||
| 347 | } | ||
| 348 | |||
| 349 | $code .= $node->{arg}[0]; | ||
| 350 | $code .= "\n"; | ||
| 351 | |||
| 352 | #$methods{$node->{class}} = []; | ||
| 353 | push @{$methods{$node->{class}}}, $code; | ||
| 354 | #return indent($code); | ||
| 355 | return " "; | ||
| 356 | }; | ||
| 357 | /^TeaPart::New/ && do { | ||
| 358 | my $class = $node->{class}; | ||
| 359 | return "new $typeConversion{$class} (new FileWriter(" | ||
| 360 | . join( ", ", @{ $node->{arg} } )."))" if ( $node->{class} eq "TFileOutput"); | ||
| 361 | return "new $typeConversion{$class} (new FileReader(" | ||
| 362 | . join( ", ", @{ $node->{arg} } )."))" if ( $node->{class} eq "TFileInput"); | ||
| 363 | return "new $typeConversion{$class} (new InputStreamReader( (new URL(" | ||
| 364 | . join( ", ", @{ $node->{arg} } ).")).openStream()))" if ( $node->{class} eq "TUrlInput"); | ||
| 365 | return "TDBC.tdbcConstructor(" | ||
| 366 | . join( ", ", @{ $node->{arg} } ).")" if ( $node->{class} eq "TConnection"); | ||
| 367 | return "TDBC.tdbcDateConstructor(" | ||
| 368 | . join( ", ", @{ $node->{arg} } ).")" if ( $node->{class} eq "TDate"); | ||
| 369 | |||
| 370 | |||
| 371 | return "new ". ($typeConversion{$class} || $class) . "(" | ||
| 372 | . join( ", ", @{ $node->{arg} } ) . ")"; | ||
| 373 | }; | ||
| 374 | /^TeaPart::Call/ && do { | ||
| 375 | my $methodName = $methodConvertion{ $node->{method} } | ||
| 376 | || $node->{method}; | ||
| 377 | my $code; | ||
| 378 | |||
| 379 | if($methodName eq 'registerDate' | ||
| 380 | || $methodName eq 'registerFloat' | ||
| 381 | || $methodName eq 'registerInt' | ||
| 382 | || $methodName eq 'registerString') { | ||
| 383 | |||
| 384 | my %aux; | ||
| 385 | |||
| 386 | $aux{invocant} = $node->{invocant}; | ||
| 387 | $aux{method} = $methodName; | ||
| 388 | $aux{ind} = $node->{arg}[0]; | ||
| 389 | $aux{symbol} = $node->{arg}[1]; | ||
| 390 | |||
| 391 | return registerOuts(\%aux); | ||
| 392 | }elsif ($methodName eq 'fetchOutParameters'){ | ||
| 393 | return fetchOuts(); | ||
| 394 | |||
| 395 | } | ||
| 396 | |||
| 397 | |||
| 398 | # METHODS CONVERTED INTO FUNCTIONS | ||
| 399 | if($methodName eq 'tdbcConnect'){ | ||
| 400 | $code .= 'TDBC.'.$methodName | ||
| 401 | . '(' | ||
| 402 | . $node->{invocant} | ||
| 403 | . ',' | ||
| 404 | . join( ", ", @{ $node->{arg} } ) . ")"; | ||
| 405 | }elsif( $methodName eq 'hasRows'){ | ||
| 406 | $code .= 'TDBC.tdbcHasRows(' | ||
| 407 | . $node->{invocant} | ||
| 408 | .')'; | ||
| 409 | }elsif( $methodName eq 'compare') { | ||
| 410 | $code .= 'TDBC.tdbcCompareDates(' | ||
| 411 | .$node->{invocant} | ||
| 412 | .', ' | ||
| 413 | .$node->{arg}[0] | ||
| 414 | .')'; | ||
| 415 | |||
| 416 | }elsif( $methodName eq 'format'){ | ||
| 417 | $code .= '(new SimpleDateFormat(' | ||
| 418 | . $node->{arg}[0] | ||
| 419 | . ')).format(' | ||
| 420 | . $node->{invocant} | ||
| 421 | . ')'; | ||
| 422 | }elsif( $methodName eq 'getMonth'){ | ||
| 423 | $code .= 'TDBC.tdbcGetMonth(' | ||
| 424 | . $node->{invocant} | ||
| 425 | .')'; | ||
| 426 | }elsif( $methodName eq 'getYear'){ | ||
| 427 | $code .= 'TDBC.tdbcGetYear(' | ||
| 428 | . $node->{invocant} | ||
| 429 | .')'; | ||
| 430 | }elsif( $methodName eq 'setDate'){ | ||
| 431 | $code .= 'TDBC.tdbcSetDate(' | ||
| 432 | . $node->{invocant} | ||
| 433 | . ", " | ||
| 434 | . (join ", ", @{$node->{arg}}) | ||
| 435 | .')'; | ||
| 436 | |||
| 437 | }elsif( $methodName eq 'setTime'){ | ||
| 438 | $code .= 'TDBC.tdbcSetTime(' | ||
| 439 | . $node->{invocant} | ||
| 440 | . ", " | ||
| 441 | . (join ", ", @{$node->{arg}}) | ||
| 442 | .')'; | ||
| 443 | }elsif( $methodName eq 'getElements'){ | ||
| 444 | $code .= 'Util.getElements(' | ||
| 445 | . $node->{invocant} | ||
| 446 | .')'; | ||
| 447 | }elsif( $methodName eq 'getKeys'){ | ||
| 448 | $code .= 'Util.getKeys(' | ||
| 449 | . $node->{invocant} | ||
| 450 | .')'; | ||
| 451 | |||
| 452 | }elsif( $methodName eq 'append'){ | ||
| 453 | $code .= 'Util.append(' | ||
| 454 | . $node->{invocant} | ||
| 455 | . ', new Object[] {' | ||
| 456 | . (join ", ", @{$node->{arg}}) | ||
| 457 | . '}' | ||
| 458 | .')'; | ||
| 459 | }elsif( $methodName eq 'init'){ | ||
| 460 | $code .= 'Util.init(' | ||
| 461 | . $node->{invocant} | ||
| 462 | . ', new Object[] ' | ||
| 463 | . '{' | ||
| 464 | . (join ", ", @{$node->{arg}}) | ||
| 465 | . '})'; | ||
| 466 | }elsif( $methodName eq 'pop'){ | ||
| 467 | $code .= 'Util.pop(' | ||
| 468 | . $node->{invocant} | ||
| 469 | . ')'; | ||
| 470 | #END OF METHODS CONVERTED TO FUNCTIONS | ||
| 471 | |||
| 472 | #METHODS THAT NEED SPECIAL TREATMENT | ||
| 473 | }elsif ($methodName eq 'getFloat'){ | ||
| 474 | $code .= '(double)'.$node->{invocant} | ||
| 475 | .'.' | ||
| 476 | .$methodName | ||
| 477 | .'(' | ||
| 478 | . join( ", ", @{ $node->{arg} } ) . ")"; | ||
| 479 | } | ||
| 480 | elsif ($methodName eq "write") { | ||
| 481 | $code .= $node->{invocant} | ||
| 482 | . "." | ||
| 483 | . $methodName | ||
| 484 | . '(' | ||
| 485 | . $node->{arg}[0] | ||
| 486 | . ')' | ||
| 487 | . eol($node) | ||
| 488 | . $node->{invocant} | ||
| 489 | . '.newLine()'; | ||
| 490 | |||
| 491 | }elsif ($methodName eq "addElements") { | ||
| 492 | my @arg_list= split /, /, $node->{arg}[0]; | ||
| 493 | foreach (@arg_list) { | ||
| 494 | s/(^\(*) (.*) (\d\)) (\)*$)/$2$3/xg; | ||
| 495 | s/^\(//g; | ||
| 496 | } | ||
| 497 | for (my $i = 0; $i < @arg_list; $i+=2){ | ||
| 498 | $code .= $node->{invocant} | ||
| 499 | . '.put(' | ||
| 500 | . $arg_list[$i] | ||
| 501 | .', ' | ||
| 502 | . $arg_list[$i+1] | ||
| 503 | .')'; | ||
| 504 | $code .= eol($node) if ($i+2 != @arg_list); | ||
| 505 | } | ||
| 506 | |||
| 507 | }elsif ($methodName eq "constructor") { | ||
| 508 | $code .= $node->{invocant}. '(' | ||
| 509 | . (join ", ", @{$node->{arg}}) | ||
| 510 | . ')'; | ||
| 511 | }elsif ($methodName eq "sort") { | ||
| 512 | $code .= "Collections.sort($node->{invocant}, new Comparer())"; | ||
| 513 | $compFunction = $node->{arg}[0]; | ||
| 514 | }elsif ($methodName eq "setAt") { | ||
| 515 | $code .= $node->{invocant} | ||
| 516 | . '.' | ||
| 517 | . 'set(' | ||
| 518 | . $node->{arg}[1] | ||
| 519 | . ', ' | ||
| 520 | . $node->{arg}[0] | ||
| 521 | . ')'; | ||
| 522 | }elsif ($methodName eq 'notSame'){ | ||
| 523 | $code .= '!' . $node->{invocant} | ||
| 524 | . '.' | ||
| 525 | . 'equals(' | ||
| 526 | . $node->{arg}[0] | ||
| 527 | . ')'; | ||
| 528 | }elsif ($methodName eq 'same'){ | ||
| 529 | $code .= $node->{invocant} | ||
| 530 | . '.' | ||
| 531 | . 'equals(' | ||
| 532 | . $node->{arg}[0] | ||
| 533 | . ')'; | ||
| 534 | }else{ | ||
| 535 | |||
| 536 | # DEFAULT BEHAVIOR | ||
| 537 | $code = | ||
| 538 | "$node->{invocant}.$methodName" . "(" | ||
| 539 | . join( ", ", @{ $node->{arg} } ) . ")"; | ||
| 540 | } | ||
| 541 | |||
| 542 | $code .= eol($node); | ||
| 543 | return $code; | ||
| 544 | |||
| 545 | }; | ||
| 546 | /^TeaPart::Dereference/ && do { | ||
| 547 | return $node->{arg_symbol}; | ||
| 548 | }; | ||
| 549 | } | ||
| 550 | } | ||
| 551 | ); | ||
| 552 | } | ||
| 553 | |||
| 554 | sub eol { | ||
| 555 | my $node = shift; | ||
| 556 | my $comment = $node->{comment} || ''; | ||
| 557 | if ( $node->{context}{liner} ) { | ||
| 558 | return ';' . $comment . "\n"; | ||
| 559 | } | ||
| 560 | else { | ||
| 561 | return ''; | ||
| 562 | } | ||
| 563 | } | ||
| 564 | |||
| 565 | sub indent { | ||
| 566 | my $code = shift; | ||
| 567 | my ( $in, $out ) = gensym() for 1 .. 2; | ||
| 568 | my $pid = open2( $out, $in, 'astyle', '--style=java' ) | ||
| 569 | || die 'Cannot open indentation engine: ' . $!; | ||
| 570 | print {$in} $code; | ||
| 571 | close $in; | ||
| 572 | my $ret = join '', <$out>; | ||
| 573 | waitpid( $pid, 0 ); | ||
| 574 | close $out; | ||
| 575 | return $ret; | ||
| 576 | } | ||
| 577 | |||
| 578 | sub registerOuts { | ||
| 579 | my $info = shift; | ||
| 580 | my $type; | ||
| 581 | $type = 'Types.DOUBLE' if($info->{method} eq 'registerFloat'); | ||
| 582 | $type = 'Types.INTEGER' if($info->{method} eq 'registerInt'); | ||
| 583 | $type = 'Types.VARCHAR' if($info->{method} eq 'registerString'); | ||
| 584 | $type = 'Types.DATE' if($info->{method} eq 'registerDate'); | ||
| 585 | push @registeredOuts, $info; | ||
| 586 | my $code = $info->{invocant} . '.registerOutParameter(' | ||
| 587 | . $info->{ind} | ||
| 588 | .', ' | ||
| 589 | . $type | ||
| 590 | . ');'; | ||
| 591 | } | ||
| 592 | |||
| 593 | sub fetchOuts { | ||
| 594 | my $type; | ||
| 595 | my $javaMethod; | ||
| 596 | my $code; | ||
| 597 | foreach (@registeredOuts){ | ||
| 598 | if($_->{method} eq 'registerFloat'){ | ||
| 599 | $type = 'Double'; | ||
| 600 | $javaMethod = 'getDouble'; | ||
| 601 | } | ||
| 602 | if($_->{method} eq 'registerInt') { | ||
| 603 | $type = 'Integer'; | ||
| 604 | $javaMethod = 'getInt'; | ||
| 605 | } | ||
| 606 | if($_->{method} eq 'registerString'){ | ||
| 607 | $type = 'String'; | ||
| 608 | $javaMethod = 'getString'; | ||
| 609 | } | ||
| 610 | if($_->{method} eq 'registerDate'){ | ||
| 611 | $type = 'Date'; | ||
| 612 | $javaMethod = 'getDate'; | ||
| 613 | } | ||
| 614 | |||
| 615 | $code .= $type. " " | ||
| 616 | . $_->{symbol} | ||
| 617 | . ' = ' | ||
| 618 | . $_->{invocant} | ||
| 619 | . '.'.$javaMethod | ||
| 620 | . '(' | ||
| 621 | . $_->{ind} | ||
| 622 | .');'; | ||
| 623 | |||
| 624 | } | ||
| 625 | return $code; | ||
| 626 | } | ||
| 627 | 1; | ||
| 628 |