Przewodnik po języku JavaScript 1.5:Podgląd klas LiveConnect:Komunikacja między JavaScript a Java
z Mozilla Developer Center, polskiego centrum programistów Mozilli.
UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...
Spis treści |
[edytuj] Komunikacja między JavaScript a Java
When you refer to a Java package or class, or work with a Java object or array, you use one of the special LiveConnect objects. All JavaScript access to Java takes place with these objects, which are summarized in the following table.
| Obiekt | Opis |
|---|---|
| JavaArray | A wrapped Java array, accessed from within JavaScript code. |
| JavaClass | A JavaScript reference to a Java class. |
| JavaObject | A wrapped Java object, accessed from within JavaScript code. |
| JavaPackage | A JavaScript reference to a Java package. |
Tabela: Obiekty LiveConnect
Uwaga: Because Java is a strongly typed language and JavaScript is weakly typed, the JavaScript runtime engine converts argument values into the appropriate data types for the other language when you use LiveConnect. See Data Type Conversion for complete information.
In some ways, the existence of the LiveConnect objects is transparent, because you interact with Java in a fairly intuitive way. For example, you can create a Java String object and assign it to the JavaScript variable myString by using the new operator with the Java constructor, as follows:
var myString = new java.lang.String("Hello world")
In the previous example, the variable myString is a JavaObject because it holds an instance of the Java object String. As a JavaObject, myString has access to the public instance methods of java.lang.String and its superclass, java.lang.Object. These Java methods are available in JavaScript as methods of the JavaObject, and you can call them as follows:
myString.length() // zwraca 11
[edytuj] Obiekt opakowujący
If a Java class is not part of the java, sun, or netscape packages, you access it with the Packages object. For example, suppose the Redwood corporation uses a Java package called redwood to contain various Java classes that it implements. To create an instance of the HelloWorld class in redwood, you access the constructor of the class as follows:
var red = new Packages.redwood.HelloWorld()
You can also access classes in the default package (that is, classes that don't explicitly name a package). Na przykład, if the HelloWorld class is directly in the CLASSPATH and not in a package, you can access it as follows:
var red = new Packages.HelloWorld()
Obiekty LiveConnect java, sun, i netscape provide shortcuts for commonly used Java packages. For example, you can use the following:
var myString = new java.lang.String("Hello world")
instead of the longer version:
var myString = new Packages.java.lang.String("Hello world")
[edytuj] Praca z tablicami w Javie
Kiedy jakakolwiek metoda Javy tworzy tablicę i odnosi się do którejkolwiek tablicy w JavaScript, to jest wykonana JavaArray. Na przykład; następujący kod tworzy JavaArray x z elementami typu całkowitego:
x = java.lang.reflect.Array.newInstance(java.lang.Integer, 10)
Jak w JavaScript obiekt Array, JavaArray ma dłuszą właściwość, liczba powtarzania numerów elementów w tablicy. Nie tak jak Array.length, JavaArray.length jest właściwością tylko do odczytu, ponieważ liczba elementów w tablicy w Javie są fałszowane czasem utworzenia.
[edytuj] Referencja paczki i klasy
Simple references to Java packages and classes from JavaScript create the JavaPackage and JavaClass objects. In the earlier example about the Redwood corporation, for example, the reference Packages.redwood is a JavaPackage object. Similarly, a reference such as java.lang.String is a JavaClass object.
Most of the time, you don't have to worry about the JavaPackage and JavaClass objects—you just work with Java packages and classes, and LiveConnect creates these objects transparently.
W JavaScript 1.3 i wcześniejszych wersjach, obiektu JavaClass nie są automatycznie konwertowane instancji java.lang.Class, kiedy przechodzi je jako parametr metody Javy. Marta methods—you must create a wrapper around an instance of java.lang.Class. In the following example, the forName method creates a wrapper object theClass, which is then passed to the newInstance method to create an array.
// JavaScript 1.3
theClass = java.lang.Class.forName("java.lang.String")
theArray = java.lang.reflect.Array.newInstance(theClass, 5)
W JavaScript 1.4 i późniejszych wersjach, przechodzi od obiektu JavaClass w kierunku metody, pokazanej w następującym przykładzie:
// JavaScript 1.4 theArray = java.lang.reflect.Array.newInstance(java.lang.String, 5)
[edytuj] Argumenty typu char
W JavaScript 1.4 i wersjach późniejszcyh, przechodzi z jednego znaku będącego zmienną string do metody Java, która zwraca argument typu char. Na przykład, możemy przejść ze zmiennej tekstu "H" do typu Character, w tej sytuacji konstruktor będzie wyglądał nastepująco:
c = new java.lang.Character("H")
W JavaScript 1.3 i w wersjach wcześniejszych, musimy przejść z metody typu integer, która przesyła zmienną integer do wartości znakowej Unicode. Na przykład, następujący kod przydzieli wartość "H" do zmiennej c:
c = new java.lang.Character(72)
[edytuj] Obsługa wyjątków Javy w JavaScript
When Java code fails at run time, it throws an exception. If your JavaScript code accesses a Java data member or method and fails, the Java exception is passed on to JavaScript for you to handle. Beginning with JavaScript 1.4, you can catch this exception in a try...catch statement.
Na przykład, suppose you are using the Java forName method to assign the name of a Java class to a variable called theClass. The forName method throws an exception if the value you pass it does not evaluate to the name of a Java class. Place the forName assignment statement in a try block to handle the exception, as follows:
function getClass(javaClassName) {
try {
var theClass = java.lang.Class.forName(javaClassName);
} catch (e) {
return ("The Java exception is " + e);
}
return theClass
}
W tym przykładzie, jeśli Twoje javaClassName evaluates to a legal class name, such as "java.lang.String", the assignment succeeds. If javaClassName evaluates to an invalid class name, such as "String", the getClass function catches the exception and returns something similar to the following:
The Java exception is java.lang.ClassNotFoundException: String
Zobacz Instrukcje obsługi wyjątków aby uzyskać więcej informacji o wykonywaniu kodu JavaScript.