Skip to content
This repository was archived by the owner on Nov 17, 2022. It is now read-only.

Commit e947ce4

Browse files
authored
Merge pull request #1239 from NativeScript/niliev/masrsh
refactor: add marshalling examples
2 parents 31dc73d + 44f0241 commit e947ce4

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

docs/core-concepts/android-runtime/marshalling/java-to-js.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ position: 2
88
# Java to JavaScript Conversion
99
The article lists the available types in Java and how they are projected to JavaScript.
1010

11-
### String & Character
11+
## String & Character
1212
Both [java.lang.String](http://developer.android.com/reference/java/lang/String.html) and [java.lang.Character](http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html) types are projected as JavaScript [String](http://www.w3schools.com/jsref/jsref_obj_string.asp):
1313

1414
```javascript
1515
var file = new java.io.File("/path/to/file");
1616
var path = file.getPath(); // returns java.lang.String, converted to JS String
1717
```
1818

19-
### Boolean & Primitive boolean
19+
## Boolean & Primitive boolean
2020
Both the primitive [boolean](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Boolean](http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html) types are projected as JavaScript [Boolean](http://www.w3schools.com/jsref/jsref_obj_boolean.asp):
2121

2222
```javascript
@@ -25,47 +25,47 @@ var button = new android.widget.Button(context);
2525
var enabled = button.isEnabled(); // returns primitive boolean, converted to JS Boolean
2626
```
2727

28-
### Byte & Primitive byte
28+
## Byte & Primitive byte
2929
Both the primitive [byte](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Byte](http://docs.oracle.com/javase/7/docs/api/java/lang/Byte.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
3030

3131
```javascript
3232
var byte = new java.lang.Byte("1");
3333
var jsByteValue = byte.byteValue(); // returns primitive byte, converted to Number
3434
```
3535

36-
### Short & Primitive short
36+
## Short & Primitive short
3737
Both the primitive [short](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Short](http://docs.oracle.com/javase/7/docs/api/java/lang/Short.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
3838

3939
```javascript
4040
var short = new java.lang.Short("1");
4141
var jsShortValue = short.shortValue(); // returns primitive short, converted to Number
4242
```
4343

44-
### Integer & Primitive int
44+
## Integer & Primitive int
4545
Both the primitive [int](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Integer](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
4646

4747
```javascript
4848
var int = new java.lang.Integer("1");
4949
var jsIntValue = int.intValue(); // returns primitive int, converted to Number
5050
```
5151

52-
### Float & Primitive float
52+
## Float & Primitive float
5353
Both the primitive [float](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Float](http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
5454

5555
```javascript
5656
var float = new java.lang.Float("1.5");
5757
var jsFloatValue = float.floatValue(); // returns primitive float, converted to Number
5858
```
5959

60-
### Double & Primitive double
60+
## Double & Primitive double
6161
Both the primitive [double](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) and reference [java.lang.Double](http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html) types are projected as JavaScript [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp):
6262

6363
```javascript
6464
var double = new java.lang.Double("1.5");
6565
var jsDoubleValue = double.doubleValue(); // returns primitive double, converted to Number
6666
```
6767

68-
### Long & Primitive long
68+
## Long & Primitive long
6969
[java.lang.Long](http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html) and its primitive equivalent are special types which are projected to JavaScript by applying the following rules:
7070

7171
* If the value is in the interval (-2^53, 2^53) then it is converted to [Number](http://www.w3schools.com/jsref/jsref_obj_number.asp)
@@ -92,7 +92,7 @@ var jsNumber = testClass.getLongNumber53Bits(); // result is JavaScript Number
9292
var specialObject = testClass.getLongNumber54Bits(); // result is the special object described above
9393
```
9494

95-
### Array
95+
## Array
9696
Array in Java is a special [java.lang.Object](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html) that have an implicit Class associated. A Java Array is projected to JavaScript as a special JavaScript proxy object with the following characteristics:
9797

9898
* Has length property
@@ -108,6 +108,7 @@ var singleFile = files[0]; // the indexed getter callback is triggered and a pro
108108

109109
>**Note:** A Java Array is intentionally not converted to a JavaScript [Array](http://www.w3schools.com/jsref/jsref_obj_array.asp) for the sake of performance, especially when it comes to large arrays.
110110
111+
### Array of Objects
111112

112113
Occasionally you have to create Java arrays from JavaScript. For this scenario we added method `create` to built-in JavaScript [`Array` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). Here are some examples how to use `Array.create` method:
113114

@@ -126,7 +127,7 @@ Array.create(elementClassName, length)
126127
Array.create(javaClassCtorFunction, length)
127128
```
128129
The first signature accepts `string` for `elementClassName`. This option is useful when you have to create Java array of primitive types (e.g. `char`, `boolean`, `byte`, `short`, `int`, `long`, `float` and `double`). It is also useful when you have to create Java jagged arrays. For this scenario `elementClassName` must be the standard JNI class notation. Here are some examples:
129-
```javascript
130+
```JavaScript
130131
// equivalent to int[][] jaggedIntArray2 = new int[10][];
131132
var jaggedIntArray2 = Array.create("[I", 10);
132133

@@ -137,15 +138,48 @@ var jaggedBooleanArray3 = Array.create("[[Z", 10);
137138
var jaggedObjectArray4 = Array.create("[[[Ljava.lang.Object;", 10);
138139
```
139140
The second signature uses `javaClassCtorFunction` which must the JavaScript constructor function for a given Java type. Here are some examples:
140-
```javascript
141+
```JavaScript
141142
// equivalent to String[] stringArr = new String[10];
142143
var stringArr = Array.create(java.lang.String, 10);
143144

144145
// equivalent to Object[] objectArr = new Object[10];
145146
var objectArr = Array.create(java.lang.Object, 10);
146147
```
147148

148-
### Null
149+
### Array of Primitive Types
150+
The automatic marshalling works only for cases with arrays of objects. In cases where you have a method that takes an array of primitive types, you need to convert it as follows:
151+
```Java
152+
public static void myMethod(int[] someParam)
153+
```
154+
Then yoy need to invoke it as follows:
155+
```JavaScript
156+
let arr = Array.create("int", 3);
157+
arr[0] = 1;
158+
arr[1] = 2;
159+
arr[2] = 3;
160+
161+
SomeObject.myMethod(arr); // assuming the method is accepting an array of primitive types
162+
```
163+
164+
### Two-Dimensional Arrays of Primitive Types
165+
166+
The above scenario gets more tricky with two-dimensional arrays. Consider a Java method that accepts as an argument a two-dimensional array:
167+
```Java
168+
public static void myMethod(java.lang.Integer[][] someParam)
169+
```
170+
The marshalled JavaScript code will look like this:
171+
```JavaScript
172+
let arr = Array.create("[Ljava.lang.Integer;", 2);
173+
let elements = Array.create("java.lang.Integer", 3);
174+
elements[0] = new java.lang.Integer(1);
175+
elements[1] = new java.lang.Integer(2);
176+
elements[2] = new java.lang.Integer(3);
177+
arr[0] = elements;
178+
179+
SomeObject.myMethod(arr); // assuming the method is accepting a two-dimensional array of primitive types
180+
```
181+
182+
## Null
149183
The Java [null literal](http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.7) (or null pointer) is projected to JavaScript [Null](http://www.w3schools.com/js/js_typeof.asp):
150184

151185
```javascript
@@ -154,7 +188,7 @@ var button = new android.widget.Button(context);
154188
var background = button.getBackground(); // if there is no background drawable method will return JS null
155189
```
156190

157-
### Android Types
191+
## Android Types
158192
All Android-declared types are projected to JavaScript using the Package and Class proxies as described in [Accessing APIs](../metadata/accessing-packages.md)
159193

160194
# See Also

0 commit comments

Comments
 (0)