library(SJava) .JavaInit(list(classPath=paste(Sys.getenv("OMEGA_HOME"), "..", "..", sep = .Platform$file.sep))) setJavaFunctionConverter(function(x, className) { print("This is a silly converter for a JButton") val <- .Java(x, "getText") val <- c(text = val, action = .Java(x, "getActionCommand")) print(val) return(val) }, function(obj, className){ ok <- className == "javax.swing.JButton" cat("In JButton match:", ok, "\n") return(ok) }) setJavaConvertible("JButton") testJButton = # # Note that we will see several converters being called # in this example. # > testJButton() # In match: TRUE # [1] "This is a silly converter for a JButton" # In match: FALSE # Looking for RealVariable java.lang.String # In match: FALSE # Looking for RealVariable java.lang.String # [1] "testing" "testing" # [1] "testing" "testing" # # The reason for this is that the .Java() calls within the converter # attempt to convert the String elements from the getText function() { .JavaConstructor("JButton", "testing", .convert = TRUE) } # Now we use a closure to handle both the conversion and matching # and this gives us mutable state. # # We just count the number of times the converter is called. realVariableConverterHandler <- function() { n <- 0 cvt <- function(obj, className) { n <<- n + 1 .Java(obj, "getValues") } matcher <- function(obj, className) { cat("Looking for RealVariable", className, "\n") return(className == "org.omegahat.DataStructures.Data.RealVariable") } return(list(converter=cvt, matcher=matcher, count = function(){ n } )) } rvCvt <- realVariableConverterHandler() setJavaFunctionConverter(rvCvt$converter, rvCvt$matcher) setJavaConvertible("org.omegahat.DataStructures.Data.RealVariable") testRealVariable = function() { .JavaConstructor("org.omegahat.DataStructures.Data.RealVariable", rnorm(10), .convert = TRUE) }