Skip to content
Snippets Groups Projects
Commit cdfb542a authored by Tomas Stupka's avatar Tomas Stupka
Browse files

more functionality in rJava pkg

parent 495ba3d2
No related branches found
No related tags found
No related merge requests found
......@@ -5,11 +5,15 @@ export(.jaddLibrary)
export(.jarray)
export(.jbyte)
export(.jcall)
export(.jcast)
export(.jchar)
export(.jcheck)
export(.jclear)
export(.jevalArray)
export(.jfield)
export(.jfindClass)
export(.jfloat)
export(.jgetEx)
export(.jinit)
export(.jlong)
export(.jnew)
......@@ -17,4 +21,7 @@ export(.jnull)
export(.jpackage)
export(.jshort)
export(.jsimplify)
export(.jstrVal)
export(.jthrow)
export(J)
export(is.jnull)
......@@ -10,91 +10,95 @@
##
#' @export
.jnew <- function (class, ..., check = TRUE, silent = !check)
{
.jnew <- function (class, ..., check = TRUE, silent = !check) {
class <- gsub("/", ".", as.character(class))
co <- new.java.class(class)
o <- new.external(co, ...)
invisible(o)
args <- .fromS4(co, ...)
o <- .fastr.interop.try(function() { do.call(new.external, args) }, check)
new("jobjRef", jobj=o, jclass=class)
}
#' @export
.jcall <- function (obj, returnSig = "V", method, ..., evalArray = TRUE,
evalString = TRUE, check = TRUE, interface = "RcallMethod",
simplify = FALSE, use.true.class = FALSE)
{
if(is.character(obj)) {
simplify = FALSE, use.true.class = FALSE) {
if (isS4(obj)) {
obj <- obj@jobj
}
args <- .fromS4(...)
if (is.character(obj)) {
obj <- gsub("/", ".", as.character(obj))
co <- new.java.class(obj)
r <- co[method](...)
} else {
r <- obj[method](...)
obj <- new.java.class(obj)
}
r
extMethod <- function(...) {obj[method](...)}
r <- .fastr.interop.try(function() { do.call(extMethod, args) }, check)
if (is.null(r) && returnSig == "V") {
return(invisible(NULL))
}
.toS4(r)
}
#' @export
.jfield <- function (obj, sig = NULL, name, true.class = is.null(sig), convert = TRUE)
{
if(is.character(obj)) {
.jfield <- function (obj, sig = NULL, name, true.class = is.null(sig), convert = TRUE) {
if (isS4(obj)) {
obj <- obj@jobj
}
if (is.character(obj)) {
co <- new.java.class(obj)
r <- co[name]
} else {
r <- obj[name]
}
r
.toS4(r)
}
#' @export
.jarray <- function (x, contents.class = NULL, dispatch = FALSE)
{
.jarray <- function (x, contents.class = NULL, dispatch = FALSE) {
as.java.array(x, ,TRUE)
}
#' @export
.jevalArray <- function (x, contents.class = NULL, dispatch = FALSE)
{
.jevalArray <- function (x, contents.class = NULL, dispatch = FALSE) {
.fastr.interop.fromArray(x)
}
#' @export
.jbyte <- function (x)
{
.jbyte <- function (x) {
x <- as.external.byte(x)
invisible(x)
}
#' @export
.jchar <- function (x)
{
.jchar <- function (x) {
x <- as.external.char(x)
invisible(x)
}
#' @export
.jshort <- function (x)
{
.jshort <- function (x) {
x <- as.external.short(x)
invisible(x)
}
#' @export
.jlong <- function (x)
{
.jlong <- function (x) {
x <- as.external.long(x)
invisible(x)
}
#' @export
.jfloat <- function (x)
{
.jfloat <- function (x) {
x <- as.external.float(x)
invisible(x)
}
#' @export
J <- function (class, method, ...)
{
J <- function (class, method, ...) {
class <- gsub("/", ".", as.character(class))
javaClass <- new.java.class(class)
if (nargs() == 1L && missing(method)) {
......@@ -105,8 +109,7 @@ J <- function (class, method, ...)
}
#' @export
.jpackage <- function (name, jars='*', morePaths='', nativeLibrary=FALSE, lib.loc=NULL)
{
.jpackage <- function (name, jars='*', morePaths='', nativeLibrary=FALSE, lib.loc=NULL) {
classes <- system.file("java", package = name, lib.loc = lib.loc)
if (nchar(classes)) {
.jaddClassPath(classes)
......@@ -144,41 +147,74 @@ J <- function (class, method, ...)
}
#' @export
.jaddClassPath <- function (path)
{
.jaddClassPath <- function (path) {
java.addToClasspath(path)
}
#
# noop stubs
#
#' @export
.jfindClass <- function (cl, silent = FALSE) {
new.java.class(cl)
}
.toS4 <- function(obj) {
res <- obj
if (is.external(obj)) {
if (is.external.array(obj)) {
res <- as.vector(obj)
} else {
res <- new("jobjRef", jobj=obj, jclass=java.class(obj))
}
}
res
}
.fromS4 <- function(...) {
l <- list(...)
if (length(l)) {
for (i in 1:length(l)) {
if (isS4(l[[i]])) {
o <- l[[i]]@jobj
if (is.null(o)) {
l[i] <- list(NULL)
} else {
l[[i]] <- o
}
}
}
}
l
}
#' @export
.jinit <- function (...)
{
# do nothing
.jgetEx <- function (clear = FALSE) {
interopEx <- .fastr.interop.getTryException(clear)
if (is.null(interopEx))
return(NULL)
new("jobjRef", jobj = interopEx, jclass = "java/lang/Throwable")
}
#' @export
.jsimplify <- function (x)
{
x
.jclear <- function () {
invisible(.fastr.interop.clearTryException())
}
#' @export
.jcheck <- function(silent = FALSE) {
FALSE
.jinit <- function (classpath = NULL, parameters = getOption("java.parameters"), ..., silent = FALSE, force.init = FALSE) {
if (!is.null(classpath)) java.addToClasspath(classpath)
}
#' @export
.jnull <- function (class)
{
# do nothing
.jnull <- function (class = "java/lang/Object") {
new("jobjRef", jobj=NULL, jclass=class)
}
#' @export
.jaddLibrary <- function (name, path)
{
is.jnull <- function (x) {
is.null(x) || is.external.null(x) || (is(x,"jobjRef") && is.null(x@jobj))
}
#' @export
.jaddLibrary <- function (name, path) {
cat(paste0("********************************************************\n",
"*** WARNING!!!\n",
"*** .jaddLibrary is not yet implemented.\n",
......@@ -187,3 +223,97 @@ J <- function (class, method, ...)
"*** are set on LD_LIBRARY_PATH or java.library.path\n",
"********************************************************\n"))
}
#' @export
.jcast <- function(obj, new.class="java/lang/Object", check = FALSE, convert.array = FALSE) {
if (!is(obj,"jobjRef"))
stop("cannot cast anything but Java objects")
# TODO implement checks
# if( check && !.jinstanceof( obj, new.class) ){
# stop( sprintf( "cannot cast object to '%s'", new.class ) )
# }
new.class <- gsub("\\.","/", as.character(new.class)) # allow non-JNI specifiation
# if( convert.array && !is( obj, "jarrayRef" ) && isJavaArray( obj ) ){
# r <- .jcastToArray( obj, signature = new.class)
# } else {
r <- obj
r@jclass <- new.class
# }
r
}
#' @export
.jstrVal <- function (obj) {
if (is.character(obj))
return(obj)
r <- NULL
if (!is(obj, "jobjRef"))
stop("can get value of Java objects only")
if (!is.null(obj@jclass) && obj@jclass == "lang/java/String")
r <- .External(RgetStringValue, obj@jobj)
else r <- obj@jobj["toString"]()
r
}
#
# S4
#
setClass("truffle.object", representation(jobj="ANY"))
setClassUnion("TruffleObjectOrNull",members=c("truffle.object", "NULL"))
# jobjRef
setClass("jobjRef", representation(jobj="TruffleObjectOrNull", jclass="character"), prototype=list(jobj=NULL, jclass="java/lang/Object"))
._jobjRef_dollar <- function(x, name) {
if(name %in% names(x@jobj)) {
if(is.external.executable(x@jobj[name])) {
function(...) { .jcall(x, , name, ...) }
} else {
.jfield(x, , name)
}
} else if( is.character(name) && length(name) == 1L && name == "length" && is.external.array(x) ){
length( x@obj )
} else {
stop(sprintf( "no field, method or inner class called '%s' ", name))
}
}
setMethod("$", c(x="jobjRef"), ._jobjRef_dollar )
._jobjRef_dollargets <- function(x, name, value) {
if(name %in% names(x@jobj)) {
if(!is.external.executable(x@jobj[name])) {
if(isS4(value)) {
value <- value@jobj
}
x@jobj[name] <- value
}
}
x
}
setMethod("$<-", c(x="jobjRef"), ._jobjRef_dollargets )
setMethod("show", c(object="jobjRef"), function(object) {
if (is.jnull(object)) show("Java-Object<null>") else show(paste("Java-Object{", .jstrVal(object), "}", sep=''))
invisible(NULL)
})
#
# noop stubs
#
#' @export
.jsimplify <- function (x) {
x
}
#' @export
.jcheck <- function(silent = FALSE) {
FALSE
}
#' @export
.jthrow <- function (exception, message = NULL) {
# do nothing
}
# prerequisites:
# - 'testthat' package has to be installed: install.packages("testthat")
# - FastR`s rJava package has to be installed: bin/r CMD INSTALL com.oracle.truffle.r.pkgs/rjava
# - mxbuild/dists/fastr-unit-tests.jar has to be on FastR classpath
library(testthat)
library(rJava)
.jaddClassPath(paste0(Sys.getenv("R_HOME"), "/mxbuild/dists/fastr-unit-tests.jar"))
testName <- "test .jarray"
test_that(testName, {
cat(paste0(testName, "\n"))
a <- .jarray(c(1.1, 2.1, 3.1))
expect_true(is.external.array(a))
expect_equal(length(a), 3)
......@@ -43,8 +44,10 @@ test_that(testName, {
a <- .jarray(to)
expect_true(is.external.array(a))
expect_equal(length(a), 1)
expect_equal(a[1], to)
# fails at the moment
# testthat passes 'to' into .Call("find_label_", quote(to), environment())
# expect_equal(a[1], to)
to <- .jnew('java.util.ArrayList')
a <- .jarray(c(to, to))
expect_true(is.external.array(a))
......@@ -98,4 +101,4 @@ test_that(testName, {
}
}
})
\ No newline at end of file
})
# prerequisites:
# - 'testthat' package has to be installed: install.packages("testthat")
# - FastR`s rJava package has to be installed: bin/r CMD INSTALL com.oracle.truffle.r.pkgs/rjava
# - mxbuild/dists/fastr-unit-tests.jar has to be on FastR classpath
library(testthat)
library(rJava)
.jaddClassPath(paste0(Sys.getenv("R_HOME"), "/mxbuild/dists/fastr-unit-tests.jar"))
bo <- TRUE
bt <- 123
......
# prerequisites:
# - 'testthat' package has to be installed: install.packages("testthat")
# - FastR`s rJava package has to be installed: bin/r CMD INSTALL com.oracle.truffle.r.pkgs/rjava
# - mxbuild/dists/fastr-unit-tests.jar has to be on FastR classpath
library(testthat)
library(rJava)
.jaddClassPath(paste0(Sys.getenv("R_HOME"), "/mxbuild/dists/fastr-unit-tests.jar"))
testName <- "test J function"
test_that(testName, {
cat(paste0(testName, "\n"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment