Search code examples
rvectorrjava

rJava: using java/lang/Vector with a certain template class


I'm currently programming an R-script which uses a java .jar that makes use of the java/lang/Vector class, which in this case uses a class in a method that is not native. In java source code:

public static Vector<ClassName> methodname(String param)

I found nothing in the documentation of rJava on how to handle a template class like vector and what to write when using jcall or any other method. I'm currently trying to do something like this:

v <- .jnew("java/util/Vector")
b <- .jcall(v, returnSig = "Ljava/util/Vector", method = "methodname",param)

but R obviously throws an exception:

method methodname with signature (Ljava/lang/String;)Ljava/util/Vector not found

How do I work the template class into this command? Or for that matter, how do I create a vector of a certain class in the first place? Is this possible?


Solution

  • rJava does not know java generics, there is no syntax that will create a Vector of a given type. You can only create Vectors of Objects.

    Why are you sticking with the old .jcall api when you can use the J system, which lets you use java objects much more nicely:

    > v <- new( J("java.util.Vector") )
    > v$add( 1:10 )
    [1] TRUE
    > v$size()
    [1] 1
    # code completion
    > v$
    v$add(                 v$getClass()           v$removeElement(
    v$addAll(              v$hashCode()           v$removeElementAt(
    v$addElement(          v$indexOf(             v$retainAll(
    v$capacity()           v$insertElementAt(     v$set(
    v$clear()              v$isEmpty()            v$setElementAt(
    v$clone()              v$iterator()           v$setSize(
    v$contains(            v$lastElement()        v$size()
    v$containsAll(         v$lastIndexOf(         v$subList(
    v$copyInto(            v$listIterator(        v$toArray(
    v$elementAt(           v$listIterator()       v$toArray()
    v$elements()           v$notify()             v$toString()
    v$ensureCapacity(      v$notifyAll()          v$trimToSize()
    v$equals(              v$remove(              v$wait(
    v$firstElement()       v$removeAll(           v$wait()
    v$get(                 v$removeAllElements()