Search code examples
javarrjava

How do I access Enums using rJava?


I'm using a third-party library called CDK. I'm trying to create an instance of the class Bond using rJava:

o1 <- .jnew("org.openscience.cdk.Atom","O")
o2 <- .jnew("org.openscience.cdk.Atom","O")
J("org.openscience.cdk.Bond",o1,o2,
  "org.openscience.cdk.interfaces.IBond.Order.SINGLE")

The problem is there is an Enum defined in this interfaced IBond, I need to pass it as an argument to the constructor Bond(IAtom atom1, IAtom atom2, IBond.Order order) but I don't know how to do this using rJava.

I tried

J("org.openscience.cdk.Bond",o1,o2,
  J("org.openscience.cdk.interfaces.IBond.Order")$SINGLE)

hoping SINGLE could be accessed like any other static field/method but it didn't work.

Is there any way to pass Enum to methods using rJava?


Solution

  • I've found the way to do this, I use the dollar sign to refer to the public Enum Order declared in the IBond interface and then another dollar sign to access the enum value:

    J("org.openscience.cdk.interfaces.IBond")$Order$SINGLE