22.4. LiveConnect Data Types
22.4.1. The JavaPackage Class
All JavaPackage objects are contained within a parent JavaPackage; the Window property named Packages is a top-level JavaPackage that serves as the root of this package hierarchy. It has properties such as java, sun, and netscape, which are JavaPackage objects that represent the various hierarchies of Java classes that are available to the browser. For example, the JavaPackage Packages.java contains the JavaPackage Packages.java.awt. For convenience, every Window object also has java, sun, and netscape properties that are shortcuts to Packages.java, Packages.sun, and Packages.netscape. Thus, instead of typing Packages.java.awt, you can simply type java.awt.
To continue with the example, java.awt is a JavaPackage object that contains JavaClass objects such as java.awt.Button, which represents the java.awt.Button class. But it also contains yet another JavaPackage object, java.awt.image, which represents the java.awt.image package in Java.
As you can see, the property naming scheme for the JavaPackage hierarchy mirrors the naming scheme for Java packages. Note, however, that there is one big difference between the JavaPackage class and the actual Java packages that it represents. Packages in Java are collections of classes, not collections of other packages. That is, java.lang is the name of a Java package, but java is not. So the JavaPackage object named java does not actually represent a package in Java -- it is simply a convenient placeholder in the package hierarchy for other JavaPackage objects that do represent real Java packages.
On most systems, Java classes are installed in files in a directory hierarchy that corresponds to their package names. For example, the java.lang.String class is stored in the file java/lang/String.class. Actually, this file is usually contained in a ZIP file, but the directory hierarchy is still there, encoded within the archive. Therefore, instead of thinking of a JavaPackage object as representing a Java package, you may find it clearer to think of it as representing a directory or subdirectory in the directory hierarchy of Java classes.
22.4.2. The JavaClass Class
var java_console = java.lang.System.out;
Similarly, we might invoke a static method of java.lang.System with a line like this one:
var java_version = java.lang.System.getProperty("java.version");
var d = new java.lang.Double(1.23);
Finally, having created a JavaObject in this way, we can return to the getClass( ) function and show an example of its use:
var d = new java.lang.Double(1.23); // Create a JavaObject var d_class = getClass(d); // Obtain the JavaClass of the JavaObject if (d_class == java.lang.Double) ...; // This comparison will be true
When working with standard system classes like this, you can typically use the name of the system class directly rather than calling getClass( ). The getClass( ) function is more useful in obtaining the class of a non-system object, such as an applet instance.
Instead of referring to a JavaClass with a cumbersome expression like java.lang.Double, you can define a variable that serves as a shortcut:
var Double = java.lang.Double;
This mimics the Java import statement and can improve the efficiency of your program, since LiveConnect does not have to look up the lang property of java and the Double property of java.lang.
22.4.3. The JavaObject Class
n = d.doubleValue( );
and we can invoke a method of this object like this:
r = new java.awt.Rectangle( );
r.x = r.y = 0; r.width = 4; r.height = 5; var perimeter = 2*r.width + 2*r.height;
In Netscape 6.1 and later the JavaObject class makes methods available by name and by name plus argument type, which is useful when there are two or methods that share the same name but expect different types of arguments. As we saw earlier in this chapter, if a JavaObject o represents an object that has two methods named "convert", the convert property of o may refer to either of those methods. In recent versions of LiveConnect, however, o also defines properties that include the argument types, and you can specify which version of the method you want by including this type information:
var iconvert = o["convert(int)"]; // Get the method we want iconvert(3); // Invoke it
Because the name of the property includes parentheses, you can't use the regular "." notation to access it and must express it as a string within square brackets. The JavaClass type has the same capability for overridden static methods.
22.4.4. The JavaArray Class
For example, suppose we create an instance of the java.awt.Polygon class:
p = new java.awt.Polygon( );
The JavaObject p has properties xpoints and ypoints that are JavaArray objects representing Java arrays of integers. (To learn the names and types of these properties, look up the documentation for java.awt.Polygon in a Java reference manual.) We can use these JavaArray objects to randomly initialize the Java polygon with code like this:
for(var i = 0; i < p.xpoints.length; i++) p.xpoints[i] = Math.round(Math.random( )*100); for(var i = 0; i < p.ypoints.length; i++) p.ypoints[i] = Math.round(Math.random( )*100);
22.4.5. Java Methods
Copyright © 2003 O'Reilly & Associates. All rights reserved.