|
JNIPort for Dolphin Smalltalk |
|
Back to Goodies |
Wrapper-class ExamplesThis section is an example of using the generated wrapper classes. It is equally applicable to ghost classes or to wrappers generated using the Wrapper Wizard, since they use the same naming scheme. The example reads a .ZIP file using Java's zip file handling classes. A parallel example uses the lower-level facilities of the base layer (package 'CU Java Base') to perform the same tasks. One general point about this example: we use the generated wrapper methods directly throughout, which is probably not how you would want to use them in practice. The better way is to define wrapper classes and provide convenient Smalltalk-style methods that are built on top of the automatically generated ones. However, I admit that I quite often use the ghost methods “raw”, especially from workspaces.
JNIPort does not include wrappers for all the Java classes used in this example so, unless
you have generated them yourself, JNIPort should be configured to use ghost classes.
The configuration option is that the list of 'watcherClasses' under 'jniPortSettings' should
include Ensure that a properly configured JMV is running, and open a new workspace. We start, as always, by getting a reference to the JVM object:
Now we need to get a reference to the class static standing for the Java class,
Take a look at the 'Classes' page of the Status Monitor which will show what generated wrapper methods are available. The quickest way to find the class is to use the 'Class⇒Find Java Class' menu command. It should show that there are six instance-side methods:
Three class-side methods, all of which are wrappers for constructors:
And lots of getter methods for the integer constants defined by the class.
You might want to review the JavaDoc documentation for
Now we'll open a ZIP file. For this example, we'll use the DolphinJNIHelper.zip file that should be in the Metagnostic\Extras folder. Use the file dialog to find that file:
Now we create a new ZipFile object from that filename:
In this case we passed a Smalltalk String object to the ZipFile constructor that
expected a We may as well find out how many entries there are in the ZIP file:
Now we'll iterate over the elements of the ZipFile; we start by getting the Java
The
But that isn't very Smalltalkish. Also it depends on the fact that we are using
ghost classes. JNIPort comes with a better way to iterate over a
or, if you prefer:
By the way, the adaptor object only implements a small part of the Smalltalk
<ReadableStream> protocol. You can get a more complete implementation
by either writing the other wrapper methods yourself <grin> or by
asking for a further level of adaptor by sending
Another way to get a Smalltalk-style adaptor is to use
(As an aside: this mess with lots of different kinds of adaptors, some more
complete than others, is a direct consequence of Smalltalk's lack of some
form of multiple inheritance — such as mixins. A complete
adaptor for, say, Now to read one of the files from inside the ZipFile. We start by getting the appropriate ZipEntry:
If you check the class in the Status Monitor then you'll see that you can ask the ZipEntry for a fair raft of data. What you can't do is get the contents of the file, for that you have to go back to the ZipFile (that's just the way the Java class is designed), so:
This answers an object that is of some subclass of
That's almost the end of the example. We may as well clean up properly, though:
Finally, to see an example of what happens when Java code throws exceptions, try getting a ZipEntry again now that the ZipFile has been closed:
Which should give a normal Smalltalk walkback. You can trap the error in various ways, one is:
Another:
|
Copyright © Chris Uppal, 2003-2005
Java, JNI (probably), JVM (possibly), and God knows what else, are trademarks of Sun Microsystems, Inc.