Copyright ©1999 by AAA+ Software Forschungs- und Entwicklungs Ges.m.b.H. All Rights Reserved.
4 Using Java from Joy
In Joy 2.1 you are able to access not only Objective-C, but also Java, from both Joy's scripting languages. This makes Joy an even more powerful tool for rapidly integrating heterogeneous software components. Java is a nice, well-designed language for high level application logic, but tends to fail when confronted with the "gory details" of interfacing to legacy software or non-Java APIs. Normally the answer is implementing "native" Java methods in C, using a runtime interface like JNI (Java Native Interface). For us, the answer is Joy! Talking to the Tumbling DukeFor a first taste of what is possible, start InterfaceBuilder. If you have changed the Joy preferences from their defaults, make sure the Java Strategy preference is set to Lazy. You can verify this by choosing the menu entry Joy/Info/Preferences.... The Java Strategy preference controls if Joy will create a Java virtual machine by itself during initialization (Greedy setting), just wait until somebody creates a Java VM and then attach to it (Lazy setting, required for this tutorial), or ignore Java altogether until you explicitly send a Joy interpreter object an enableJavaSupport message. If you had to correct the setting you must restart InterfaceBuilder. Then load the JavaBeans.palette that comes with YellowBox, if you haven't done so before. (Choose the Tools/Palettes/Open... menu entry and open the file $(NEXT_ROOT)/Developer/Palettes/JavaBeans.palette .) Then choose the menu item Joy/New Joy Application and drag the bean image (with the white background) from the JavaBeans palette into the big window called My Window. You should notice a panel popping up saying Starting Java..., followed by a modal dialog asking you to choose a Java bean. For this tutorial let's choose the famous tumbling Duke (called Tumbledemobean/TumbleItem). Then draw a connection from the Interpreter icon in the instance display to the Java bean you've just added. Accept the default and call the interpreter variable tumbleItem. Finally choose Test Interface from InterfaceBuilder's File menu.In InterfaceBuilder test mode the Duke should immediately start tumbling. Open a Joy command window, by choosing Command Window in the Joy menu. Now let's look at the object we connected to: js> tumbleItem This is an Objective-C object. The Java object which we are interested in must be accessible through some method: Not surprisingly, it is called bean. js> [tumbleItem bean] Now what's this? This is a Java object in Objective-C "disguise", a result of the magic that Apple's Objective-C/Java bridge technology is working behind the scenes. We are almost there: To make this into a real Java object, a single Joy command will do: js> duke = Java.toJava([tumbleItem bean]) Why this complicated string? Because the Joy command window converts the result of each command you type into a string for printing it out. For Java objects, this involves calling toString(). We are looking at the result of the Duke's toString() method! Now let's tell the tumbling guy to move a little faster: js> duke.setSpeed(10) This is fun, and if you're like us, you'll want to know what other methods the Duke understands! The way to find this out in Java is the reflection API: js> methods = duke.getClass().getDeclaredMethods() This is what Java arrays look like as strings. Not pretty, but fortunately Joy doesn't require you to use JNI "type signatures" and similar ugliness. To see the methods, just type: js> for (i in methods) print(methods[i]) Have fun! When you are bored sending Java messages to the Duke try the following:
How can this work? Well, Joy knows the target of an Objective-C message has to be an Objective-C object, so the duke object is automatically sent over the Java/Objective-C bridge and the setSpeed: message goes to the Objective-C proxy instead. Of course this automatic conversion will be done also for parameters, assignments to typed variables, and in the other direction (Objective-C to Java) as well. js> [@"Hello " stringByAppendingString: There is some small asymmetry in that Objective-C objects are not converted to Java just because you send them a message in Java syntax. You can request an explicit conversion in either direction however by using one of Java.toJava() or Java.toObjC(). The next section explains how you can use Joy to work with the projects you normally compile -- without changing any code! |