Skip to content

Commit 8e0054c

Browse files
committed
[GR-19691] Update host interop docs
PullRequest: truffleruby/4073
2 parents e16a0c2 + b37a783 commit 8e0054c

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

doc/user/jruby-migration.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,28 +327,31 @@ require 'java'
327327
# With the 'require' above, you now can refer to things that are part of the
328328
# standard Java platform via their full paths.
329329
frame = javax.swing.JFrame.new("Window") # Creating a Java JFrame
330-
label = javax.swing.JLabel.new("Hello")
330+
label = javax.swing.JLabel.new("Hello World")
331331

332332
# You can transparently call Java methods on Java objects, just as if they were defined in Ruby.
333333
frame.add(label) # Invoking the Java method 'add'.
334-
frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
334+
frame.setDefaultCloseOperation(javax.swing.WindowConstants::EXIT_ON_CLOSE)
335335
frame.pack
336336
frame.setVisible(true)
337+
sleep
337338
```
338339

339340
In TruffleRuby we would write that this way instead:
340341

341342
```ruby
342343
Java.import 'javax.swing.JFrame'
343344
Java.import 'javax.swing.JLabel'
345+
Java.import 'javax.swing.WindowConstants'
344346

345347
frame = JFrame.new("Window")
346-
label = JLabel.new("Hello")
348+
label = JLabel.new("Hello World")
347349

348350
frame.add(label)
349-
frame.setDefaultCloseOperation(JFrame[:EXIT_ON_CLOSE])
351+
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
350352
frame.pack
351353
frame.setVisible(true)
354+
sleep
352355
```
353356

354357
Instead of using Ruby metaprogramming to simulate a Java package name, we explicitly import classes.
@@ -380,11 +383,31 @@ In both JRuby and TruffleRuby you call Java methods as you would a Ruby method.
380383
JRuby will rewrite method names such as `my_method` to the Java convention of `myMethod`, and convert `getFoo` to `foo`, and `setFoo` to `foo=`.
381384
TruffleRuby does not perform these conversions.
382385

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+
383407
### Referring to Constants
384408

385409
In JRuby, Java constants are modelled as Ruby constants, `MyClass::FOO`.
386-
In TruffleRuby you use the read notation to read them as a property,
387-
`MyClass[:FOO]`.
410+
In TruffleRuby you use the read notation to read them as a property, `MyClass.FOO` or `MyClass[:FOO]`.
388411

389412
### Using Classes from JAR files
390413

doc/user/polyglot.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ It is easier to use Java interoperability in JVM mode (`--jvm`). Java interopera
151151
See [here](https://github.com/oracle/graal/blob/master/docs/reference-manual/embedding/embed-languages.md#build-native-images-from-polyglot-applications) for more details.
152152

153153
`Java.type('name')` returns a Java type, given a name such as `java.lang.Integer` or `int[]`.
154-
With the type object, `.new` will create an instance, `.foo` will call the static method `foo`, `[:FOO]` will read the static field
155-
`FOO`, and so on.
154+
With the type object, `.new` will create an instance, `.foo` will call the static method `foo`, `.FOO` or `[:FOO]` will read the static field `FOO`, and so on.
156155
To access methods of the `java.lang.Class` instance, use `[:class]`, such as `MyClass[:class].getName`.
157156
You can also go from the `java.lang.Class` instance to the Java type by using `[:static]`.
158157

0 commit comments

Comments
 (0)