4

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

2
  • Does it work if you change returning type from Collection<Integer> into Object in your java code? Commented Oct 6, 2015 at 16:54
  • Yes, it works then. In my java code I changed Collections<Integer> to array return type, it worked perfectly. But I am more concerned about collections. Commented Oct 7, 2015 at 10:37

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.