Search code examples
javarrjava

Java Wrapper using rJava in R


I use rJava package to create a wrapper for java implementation in R. Currently, I want to create a wrapper for only two methods (put and search) of GeneralizedSuffixTree class present in the mentioned java implementation.

The signature of search() method of GeneralizedSuffixTree class is

public Collection<Integer> search(String word){
        return search(word, -1);
    }

Correspondingly, I have created a following wrapper method:

   callsearch <- function(key){
     hook2 <- .jnew("GeneralizedSuffixTree") # instance of class
     out <- .jcall(hook2,"Ljava/lang/Object","search",as.character(key), evalArray= FALSE, evalString = FALSE)
     return(out)
}

So, whenever I call the search method from rstudio with callsearch("abcdea"), I used to get following error

Error in .jcall(hook2, "Ljava/lang/Object", "search", as.character(key),  : 
  method search with signature (Ljava/lang/String;)Ljava/lang/Object not found

I think I am doing some wrong casting for Integer collection in R. May I know where I am doing wrong?

Complete under-development wrapper package is present at the link


Solution

  • The problem is with the JNI type. Since search method returns a collection, and for the collection JNI specifies as Ljava/util/Collection;

    Therefore the correct wrapper method is:

    callsearch <- function(key){
         hook2 <- .jnew("GeneralizedSuffixTree") # instance of class
         out <- .jcall(hook2,"Ljava/util/Collection;","search",as.character(key), evalArray= FALSE, evalString = FALSE)
         return(out)
    }
    

    Additional Info: For any java class, the JNI type can be found at command prompt

     javap -s <java-classname>
    

    Example for collections: javap -s java.util.Collections