0

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.

Tony
  • 43
  • 3

1 Answers1

0

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)
Matt Pollock
  • 1,063
  • 10
  • 26
  • It gives me "no static field, method or inner class called `TEMP' in `model.Node' " ('model' is the package.name). – Tony Jan 18 '17 at 23:17
  • From your question I thought the enum was `Type` with desired value `TEMP`. That line is about getting the correct enum value so that you can pass it into the `setType` method. Your comment indicates that you are trying to pull the enum value out of the `Node` class. – Matt Pollock Jan 19 '17 at 01:04
  • Works great. Thank you very much – Tony Jan 19 '17 at 14:28