1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
require_relative '../spec_helper'
describe "A Ruby class generating a Java stub" do
def generate(script)
node = JRuby.parse(script)
# we use __FILE__ so there's something for it to read
JRuby::Compiler::JavaGenerator.generate_java node, __FILE__
end
OBJECT_VOID_BAR_PATTERN =
/public *(.*) *Object bar\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar"\);\s+return \(Object\)ruby_result\.toJava\(Object\.class\);/
OBJECT_OBJECT_BAR_PATTERN =
/public *(.*) *Object bar\(Object \w+\) {\s+IRubyObject \S+ = JavaUtil\.convertJavaToRuby\(__ruby__, \S+\);\s+IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar", .*\);\s+return \(Object\)ruby_result\.toJava\(Object\.class\);/
BYTE_VOID_BAR_PATTERN =
/public *(.*) *byte bar_byte\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_byte"\);\s+return \(Byte\)ruby_result\.toJava\(byte\.class\);/
SHORT_VOID_BAR_PATTERN =
/public *(.*) *short bar_short\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_short"\);\s+return \(Short\)ruby_result\.toJava\(short\.class\);/
CHAR_VOID_BAR_PATTERN =
/public *(.*) *char bar_char\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_char"\);\s+return \(Character\)ruby_result\.toJava\(char\.class\);/
INT_VOID_BAR_PATTERN =
/public *(.*) *int bar_int\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_int"\);\s+return \(Integer\)ruby_result\.toJava\(int\.class\);/
LONG_VOID_BAR_PATTERN =
/public *(.*) *long bar_long\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_long"\);\s+return \(Long\)ruby_result\.toJava\(long\.class\);/
FLOAT_VOID_BAR_PATTERN =
/public *(.*) *float bar_float\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_float"\);\s+return \(Float\)ruby_result\.toJava\(float\.class\);/
DOUBLE_VOID_BAR_PATTERN =
/public *(.*) *double bar_double\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_double"\);\s+return \(Double\)ruby_result\.toJava\(double\.class\);/
BOOLEAN_VOID_BAR_PATTERN =
/public *(.*) *boolean bar_boolean\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_boolean"\);\s+return \(Boolean\)ruby_result\.toJava\(boolean\.class\);/
VOID_STRING_BAR_PATTERN =
/public *(.*) *void bar\(String \w+\) {\s+IRubyObject \S+ = JavaUtil\.convertJavaToRuby\(__ruby__, \S+\);\s+IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar\S*", .*\);\s+return;/
VOID_INT_BAR_PATTERN =
/public *(.*) *void bar\(int \w+\) {\s+IRubyObject \S+ = JavaUtil\.convertJavaToRuby\(__ruby__, \S+\);\s+IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar\S*", .*\);\s+return;/
DOUBLE_ARY_VOID_BAR_PATTERN =
/public *(.*) *double\[\] bar_double_ary\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_double_ary"\);\s+return \(double\[\]\)ruby_result\.toJava\(double\[\]\.class\);/
BAZ_ARY_VOID_BAR_PATTERN =
/public *(.*) *baz\[\] bar_baz_ary\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_baz_ary"\);\s+return \(baz\[\]\)ruby_result\.toJava\(baz\[\]\.class\);/
BAZ_GENERIC_VOID_BAR_PATTERN =
/public *(.*) *Baz\<Generic\> bar_generic_baz\(\) {\s+.*IRubyObject ruby_result = Helpers\.invoke\(.*, this, "bar_generic_baz"\);\s+return \(Baz\)ruby_result\.toJava\(Baz\.class\);/
describe "with a method" do
describe "with no java_signature" do
describe "and no arguments" do
it "generates an Object bar() method" do
cls = generate("class Foo; def bar; end; end").classes[0]
method = cls.methods[0]
expect(method).to_not be nil
method.name.should == "bar"
method.constructor?.should == false
method.java_signature.to_s.should == "Object bar()"
method.args.length.should == 0
java = method.to_s
java.should match OBJECT_VOID_BAR_PATTERN
end
end
describe "and one argument" do
it "generates an Object(Object) method" do
cls = generate("class Foo; def bar(a); end; end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar"
method.constructor?.should == false
method.java_signature.to_s.should == "Object bar(Object a)"
method.args.length.should == 1
java = method.to_s
java.should match OBJECT_OBJECT_BAR_PATTERN
end
describe "defined on self (the Ruby class)" do
it "generates a static Object(Object) method" do
cls = generate("class Foo; def self.bar(a); end; end").classes[0]
method = cls.methods[0]
method.name.should == "bar"
method.static.should == true
java = method.to_s
java.should match /static/
end
end
end
end
describe "with a java_signature" do
describe "defined on self (the Ruby class)" do
it "generates a static Java method" do
cls = generate("class Foo; java_signature 'void bar(String)'; def self.bar(a); end; end").classes[0]
method = cls.methods[0]
method.name.should == "bar"
method.static.should == true
java = method.to_s
java.should match /static/
end
end
describe "and no arguments" do
describe "and a byte return type" do
it "generates a byte-returning method" do
cls = generate("
class Foo
java_signature 'byte bar_byte()'; def bar_byte; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_byte"
method.constructor?.should == false
method.java_signature.to_s.should == "byte bar_byte()"
method.args.length.should == 0
java = method.to_s
java.should match BYTE_VOID_BAR_PATTERN
end
end
describe "and a short return type" do
it "generates a short-returning method" do
cls = generate("
class Foo
java_signature 'short bar_short()'; def bar_short; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_short"
method.constructor?.should == false
method.java_signature.to_s.should == "short bar_short()"
method.args.length.should == 0
java = method.to_s
java.should match SHORT_VOID_BAR_PATTERN
end
end
describe "and a char return type" do
it "generates a char-returning method" do
cls = generate("
class Foo
java_signature 'char bar_char()'; def bar_char; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_char"
method.constructor?.should == false
method.java_signature.to_s.should == "char bar_char()"
method.args.length.should == 0
java = method.to_s
java.should match CHAR_VOID_BAR_PATTERN
end
end
describe "and an int return type" do
it "generates a int-returning method" do
cls = generate("
class Foo
java_signature 'int bar_int()'; def bar_int; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_int"
method.constructor?.should == false
method.java_signature.to_s.should == "int bar_int()"
method.args.length.should == 0
java = method.to_s
java.should match INT_VOID_BAR_PATTERN
end
end
describe "and a long return type" do
it "generates a long-returning method" do
cls = generate("
class Foo
java_signature 'long bar_long()'; def bar_long; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_long"
method.constructor?.should == false
method.java_signature.to_s.should == "long bar_long()"
method.args.length.should == 0
java = method.to_s
java.should match LONG_VOID_BAR_PATTERN
end
end
describe "and a float return type" do
it "generates a float-returning method" do
cls = generate("
class Foo
java_signature 'float bar_float()'; def bar_float; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_float"
method.constructor?.should == false
method.java_signature.to_s.should == "float bar_float()"
method.args.length.should == 0
java = method.to_s
java.should match FLOAT_VOID_BAR_PATTERN
end
end
describe "and a double return type" do
it "generates a double-returning method" do
cls = generate("
class Foo
java_signature 'double bar_double()'; def bar_double; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_double"
method.constructor?.should == false
method.java_signature.to_s.should == "double bar_double()"
method.args.length.should == 0
java = method.to_s
java.should match DOUBLE_VOID_BAR_PATTERN
end
end
describe "and a boolean return type" do
it "generates a boolean-returning method" do
cls = generate("
class Foo
java_signature 'boolean bar_boolean()'; def bar_boolean; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_boolean"
method.constructor?.should == false
method.java_signature.to_s.should == "boolean bar_boolean()"
method.args.length.should == 0
java = method.to_s
java.should match BOOLEAN_VOID_BAR_PATTERN
end
end
describe "and a double[] return type" do
it "generates a double[]-returning method" do
cls = generate("
class Foo
java_signature 'double[] bar_double_ary()'; def bar_double_ary; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_double_ary"
method.constructor?.should == false
method.java_signature.to_s.should == "double[] bar_double_ary()"
method.args.length.should == 0
java = method.to_s
java.should match DOUBLE_ARY_VOID_BAR_PATTERN
end
end
describe "and a baz[] return type" do
it "generates a baz[]-returning method" do
cls = generate("
class Foo
java_signature 'baz[] bar_baz_ary()'; def bar_baz_ary; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_baz_ary"
method.constructor?.should == false
method.java_signature.to_s.should == "baz[] bar_baz_ary()"
method.args.length.should == 0
java = method.to_s
java.should match BAZ_ARY_VOID_BAR_PATTERN
end
end
describe "and a Baz<Generic> return type" do
it "generates a Baz<Generic>-returning method" do
cls = generate("
class Foo
java_signature 'Baz<Generic> bar_generic_baz()'; def bar_generic_baz; end
end").classes[0]
method = cls.methods[0]
method.should_not be nil
method.name.should == "bar_generic_baz"
method.constructor?.should == false
method.java_signature.to_s.should == "Baz<Generic> bar_generic_baz()"
method.args.length.should == 0
java = method.to_s
java.should match BAZ_GENERIC_VOID_BAR_PATTERN
end
end
end
describe "with a void return and String arg" do
it "generates a void method(String)" do
cls = generate("class Foo; java_signature 'void bar(String)'; def bar(a); end; end").classes[0]
method = cls.methods[0]
method.name.should == "bar"
method.constructor?.should == false
method.java_signature.should_not == nil
method.java_signature.to_s.should == "void bar(String)"
method.args.length.should == 1
method.args[0].should == 'a'
java = method.to_s
java.should match VOID_STRING_BAR_PATTERN
end
end
describe "with an Object(Object) and void(String) overload" do
it "generates Object(Object) and void(String) overloaded methods" do
cls = generate("
class Foo
java_signature 'void bar(String)'
def bar_str(a); end
java_signature 'void bar(int)'
def bar_int(a); end
end").classes[0]
cls.methods.length.should == 2
vs_method = cls.methods[0]
vs_method.name.should == "bar_str"
vs_method.constructor?.should == false
vs_method.java_signature.should_not == nil
vs_method.java_signature.to_s.should == "void bar(String)"
vs_method.args.length.should == 1
vs_method.args[0].should == 'a'
vs_method.to_s.should match VOID_STRING_BAR_PATTERN
oo_method = cls.methods[1]
oo_method.name.should == "bar_int"
oo_method.constructor?.should == false
oo_method.java_signature.should_not == nil
oo_method.java_signature.to_s.should == "void bar(int)"
oo_method.args.length.should == 1
oo_method.args[0].should == 'a'
oo_method.to_s.should match VOID_INT_BAR_PATTERN
end
end
describe "with Java modifiers" do
it "generates a method with those modifiers" do
cls = generate("class Foo; java_signature 'protected static abstract final strictfp native synchronized void bar()'; def self.bar; end; end").classes[0]
method = cls.methods[0]
method.name.should == "bar"
method.static.should == true
java = method.to_s
java.should match /protected/
java.should match /static/
java.should match /abstract/
java.should match /final/
java.should match /strictfp/
java.should match /native/
java.should match /synchronized/
java.should match /void/
end
describe "with multiple visibilities" do
it "uses the first visibility only" do
cls = generate("class Foo; java_signature 'private protected public void bar()'; def bar; end; end").classes[0]
method = cls.methods[0]
method.name.should == "bar"
method.static.should == false
java = method.to_s
java.should match /private/
java.should_not match /protected/
java.should_not match /public/
end
end
end
end
describe "with throws clause" do
it "generates a throws clause" do
cls = generate("class Foo; java_signature 'public void bar() throws FooBarException'; def bar; end; end").classes[0]
method = cls.methods[0]
method.java_signature.to_s.should == 'public void bar() throws FooBarException'
end
it 'generates a throws clause for more than one exception' do
cls = generate("class Foo; java_signature 'public void bar() throws FooBarException,QuxBazException'; def bar; end; end").classes[0]
method = cls.methods[0]
method.java_signature.to_s.should == 'public void bar() throws FooBarException, QuxBazException'
end
end
describe "with a root method definition" do
it "does not error" do
cls = generate("def foo; end; class Foo; end").classes[0]
expect(cls).to_not eq nil
end
end
end
describe 'with private (Ruby) :method' do
it 'does not generate the given method' do
cls = generate("class Foo; def abar; end; private :abar; def afoo; true; end; end").classes[0]
java_source = cls.to_s
expect( java_source ).to_not include 'Object abar' + '()'
expect( java_source ).to include 'public Object afoo' + '()'
end
end
describe 'with (Ruby) visibility' do
METHOD_VISIBILITY_SOURCE = File.expand_path('files/method_visibility.rb', File.dirname(__FILE__))
it 'does map public and protected methods to Java with same modifiers' do
cls = generate(File.read(METHOD_VISIBILITY_SOURCE)).classes[0]
java_source = cls.to_s
expect( java_source ).to include 'public Object publ_method' + '()'
expect( java_source ).to include 'public Object hello' + '(Object arg)'
expect( java_source ).to include 'protected Object prot_method' + '()'
end
it 'does not generate private methods (by default)' do
cls = generate(File.read(METHOD_VISIBILITY_SOURCE)).classes[0]
java_source = cls.to_s
expect( java_source ).to_not include 'Object there' + '()'
expect( java_source ).to_not include 'Object priv_method' + '()'
end
it 'explicit java_signature with modifier overrides Ruby visibility' do
cls = generate(File.read(METHOD_VISIBILITY_SOURCE)).classes[0]
java_source = cls.to_s
expect( java_source ).to include 'public int protMethodWithJSign' + '(String str_arg)'
end
end
describe "when no class definitions are present in the target script" do
before do
require 'tempfile'
@source = Tempfile.new('jrubyc_method_spec')
end
after do
@source.close!
end
it "raises an error" do
lambda do
@source.write('def foo; end')
@source.flush
Dir.chdir(File.dirname(@source.path)) do
JRuby::Compiler.compile_argv(['--java', @source.path])
end
end.should raise_error(RuntimeError)
end
end
end
|