Skip to content

Commit b37a783

Browse files
committed
Document how to call overloaded Java methods
1 parent 5944a0a commit b37a783

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

doc/user/jruby-migration.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,27 @@ In both JRuby and TruffleRuby you call Java methods as you would a Ruby method.
383383
JRuby will rewrite method names such as `my_method` to the Java convention of `myMethod`, and convert `getFoo` to `foo`, and `setFoo` to `foo=`.
384384
TruffleRuby does not perform these conversions.
385385

386+
### Calling Overloaded Method
387+
388+
When multiple overloads are possible, it needs to be selected explicitly.
389+
For instance `java.util.concurrent.ExecutorService` has both `submit(Runnable)` and `submit(Callable<T> task)`.
390+
Calling submit without specifying which one shows the possible overloads:
391+
```
392+
$ ruby -e 'Java.type("java.util.concurrent.Executors").newFixedThreadPool(1).submit {}'
393+
-e:1:in `main': Multiple applicable overloads found for method name submit (candidates: [
394+
Method[public java.util.concurrent.Future java.util.concurrent.AbstractExecutorService.submit(java.util.concurrent.Callable)],
395+
Method[public java.util.concurrent.Future java.util.concurrent.AbstractExecutorService.submit(java.lang.Runnable)]],
396+
arguments: [RubyProc@4893b344 (RubyProc)]) (TypeError)
397+
```
398+
399+
You can select a specific overload using:
400+
```ruby
401+
executor = Java.type("java.util.concurrent.Executors").newFixedThreadPool(1)
402+
executor['submit(java.lang.Runnable)'].call(-> { 1 })
403+
# or
404+
executor.send("submit(java.lang.Runnable)") { 1 }
405+
```
406+
386407
### Referring to Constants
387408

388409
In JRuby, Java constants are modelled as Ruby constants, `MyClass::FOO`.

0 commit comments

Comments
 (0)