Search code examples
rrjava

How to call "public void" method through rJava


In testing a Java API, I need to change a default setting. According to the API's document, it should be done using a method defined within the class using "public void setType". Suppose the class name is 'Node', which is referred using

library(rJava) 
.jinit(classpath=jarPath)
Node <- J("Node")

In an Java example from its documents, it's called as

 Node nodeX = new Node("X", new Variable[]{x});
 nodeX.setType(Type.TEMP);

The default type of nodeX is 'CONTEMP'. How the "setType" method can be called in R through rJava to change its default value to another one? Let's assume 'Type' is an enum variable which has several options, including "CONTEMP","TEMP", etc.


Solution

  • I think you want

    library(rJava) 
    .jinit(classpath=jarPath)
    variable <- .jarray(new(J("package.name.Variable", input_arg))
    Node <- new(J("package.name.Node"), variable)
    

    Then you can do

    type <- J("package.name.Type")$TEMP
    Node$setType(type)