Field/method operator for Java objects
accessOp.RdThe $ operator for jobjRef Java object references provides convenience access to object attributes and calling Java methods.
Usage
# S3 method for class 'jobjRef'
.DollarNames (x, pattern = "" )
# S3 method for class 'jarrayRef'
.DollarNames (x, pattern = "" )
# S3 method for class 'jrectRef'
.DollarNames (x, pattern = "" )
# S3 method for class 'jclassName'
.DollarNames(x, pattern = "" )Methods
$signature(x = "jobjRef"): ...$signature(x = "jclassName"): ...$<-signature(x = "jobjRef"): ...$<-signature(x = "jclassName"): ...namessignature(x = "jobjRef"): ...namessignature(x = "jarrayRef"): ...namessignature(x = "jrectRef"): ...namessignature(x = "jclassName"): ...
Details
rJava provides two levels of API: low-level JNI-API in the form of .jcall function and high-level reflection API based on the $ operator. The former is very fast, but inflexible. The latter is a convenient way to use Java-like programming at the cost of performance. The reflection API is build around the $ operator on jobjRef-class objects that allows to access Java attributes and call object methods.
$ returns either the value of the attribute or calls a method, depending on which name matches first.
$<- assigns a value to the corresponding Java attribute.
names and .DollarNames returns all fields and methods associated with the object.
Method names are followed by ( or () depending on arity.
This use of names is mainly useful for code completion, it is not intended to be used programmatically.
This is just a convenience API. Internally all calls are mapped into .jcall calls, therefore the calling conventions and returning objects use the same rules. For time-critical Java calls .jcall should be used directly.
Examples
#> Error in .jcheck(): java.lang.NoClassDefFoundError: RJavaTools.jcall("java/lang/System", "S", "getProperty", "java.class.path")new("jobjRef", jobj = <pointer: 0x590bf6c97f3a>, jclass = "java/lang/NoClassDefFoundError")
v <- new(J("java.lang.String"), "Hello World!")
#> Error in .jcall("RJavaTools", "Ljava/lang/Object;", "newInstance", class, .jarray(p, "java/lang/Object", dispatch = FALSE), .jarray(pc, "java/lang/Class", dispatch = FALSE), evalString = FALSE, evalArray = FALSE, use.true.class = TRUE): RcallMethod: cannot determine object class
v$length()
#> Error: object 'v' not found
v$indexOf("World")
#> Error: object 'v' not found
names(v)
#> Error: object 'v' not found
#> Error: object 'v' not found
J("java.lang.String")$valueOf(10)
#> Error in .jfindClass(as.character(class), class.loader = class.loader): java.lang.NoClassDefFoundError: RJavaToolsjclassName(class, class.loader = class.loader)new("jobjRef", jobj = <pointer: 0x590bf6c97f82>, jclass = "java/lang/NoClassDefFoundError")
Double <- J("java.lang.Double")
# the class pseudo field - instance of Class for the associated class
# similar to java Double.class
Double$class
#> [1] "Java-Object{class java.lang.Double}"
#> Error in .jcall("RJavaTools", "Z", "hasField", .jcast(x, "java/lang/Object"), name): RcallMethod: cannot determine object class