I spend the evening with this and now give up. Maybe someone here can help me:
To figure out how Kotlin and Java classes can be used together, I tried to make myself a matrix of the
different kinds of arrays and how they are printed in human readable form and converted to each other:
* int (from Java)
- Integer (from Java)
- List<Integer> (from Java Collections so identical in Kotlin)
- Array<Int> (from Kotlin)
I didn’t get very far though. Can anybody help me with some of the FIXME lines?
package de.lathspell.test.kotlin
import java.util.Arrays
import java.util.List
import java.util.ArrayList
import java.util.Collections
import java.util.Collection
import junit.framework.Assert
import junit.framework.TestCase
import org.apache.commons.lang3.ArrayUtils
import de.lathspell.test.kotlin.ArraysTestProvider
class ArrayTest : TestCase(“ArrayTest”) {
fun testArrays() {
/** Java primitives arrays - returns Java int */
val ia : IntArray? = ArraysTestProvider.getIntArray();
Assert.assertTrue(ia.toString().startsWith(“[I@”))
Assert.assertEquals(“[0, 0, 0, 4]”, Arrays.toString(ia))
/** Java object arrays - returns Java Integer[] */
val iia : Array<Int?>? = ArraysTestProvider.getIntegerArray()
Assert.assertTrue(iia.toString().startsWith("[Ljava.lang.Integer;@"))
// FIXME: Assert.assertEquals("[0, 0, 0, 4]", std.util.arrayList(iia))
/** Java Collections (work the same in Kotlin) */
val ci : List<Int?>? = ArraysTestProvider.getCollectionArray();
// FIXME: Assert.assertEquals("[Ljava.lang.Integer;@", typeinfo(ci).toString())
Assert.assertEquals("[0, 0, 0, 4]", ci.toString())
/** Kotlin Array<primitive> */
val kai : Array<Int> = array(0, 0, 0, 0)
Assert.assertTrue(true)
kai[kai.size -1] = 4
Assert.assertTrue(kai.toString().startsWith("[Ljava.lang.Integer;@"))
Assert.assertEquals("[0, 0, 0, 4]", kai.toList().toString())
/** Converting */
// FIXME: val ci_from_ia : List<Int?> = Arrays.asList(ArrayUtils.toObject(ia))
// FIXME: val ci_from_iia : List<Int?> = Arrays.asList(iia)
val ci_from_kai : List<Int>? = kai.toList()
// FIXME: val ia_from_ci : IntArray = ci.toArray(ia as Array<Int>?)
val ia_from_iia : IntArray = ArrayUtils.toPrimitive(iia) ?: IntArray(0)
// FIXME: val ia_from_kai : IntArray = kai.toList().
val kai_from_ia : Array<Int?> = ArrayUtils.toObject(ia) ?: Array<Int>(0)
val kai_from_iia : Array<Int?> = iia ?: Array<Int>(0)
// FIXME: val kai_from_ci : Array<Int> = ci.toArray(kai) // compiles in either IDEA or Maven?!
}
The Java class:
package de.lathspell.test.kotlin;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArraysTestProvider {
/** Get a Java primitives array. */
public static int getIntArray() {
int ia = new int[4];
ia[ia.length - 1] = 4;
return ia;
}
/** Get a Java Object array. */
public static Integer getIntegerArray() {
Integer iia = new Integer {0, 0, 0, 0}; // default is { null, null, null, null }
iia[iia.length - 1] = 4;
return iia;
}
/** Get a Java Collection array. */
public static List<Integer> getCollectionArray() {
ArrayList<Integer> ci = new ArrayList<Integer>();
Collections.addAll(ci, new Integer(0), new Integer(0), new Integer(0), new Integer(0));
ci.set(ci.size() - 1, 4);
return ci;
}
}