I've been playing with the rJava package recently. In a nutshell, rJava lets you create java objects and call methods on them from within the R console

col <- .jnew( "java/awt/Color", 255L, 0L, 0L )
.jcall( col, "I", "getRed" )
# [1] 255
col$getRed()
# [1] 255

The first call uses the regular function .jcall together with the JNI notation to call the getRed method of the created color, the second uses what rJava calls syntactic sugar, so that fields and methods of the object are accessed with the convenient R friendly dollar notation, great !!!

Here, I am just trying to add cream to make the coffee more tasty, by implementing the with method for java object

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

with.jobjRef <- function( data, expr, ...){
    
    env <- new.env( )
    clazz <- .jcall( data, "Ljava/lang/Class;", "getClass")
    fields <- .jcall( clazz, 
        "[Ljava/lang/reflect/Field;", "getFields" )
    lapply( fields, function(x ){
        n <- .jcall( x, "S", "getName" )
        makeActiveBinding( n, function(v){
            if( missing(v) ){
                # get 
                .jsimplify( .jcall( x, "Ljava/lang/Object;", "get", .jcast( data ) ) )
            } else {
                .jfield( data, n ) <- v
            }
        }, env ) 
    } )
    methods <- .jcall( clazz, 
        "[Ljava/lang/reflect/Method;", "getMethods" )
    lapply( methods, function(m){
        n <- .jcall( m, "S", "getName" )
        assign( n, function(...){
            .jrcall( data, n, ...) 
        }, env = env )
    } )
    assign( "this", data, env = env )
    eval( substitute( expr ), env = env )
}

This allows to call several methods on the same object, for example:

> with( col, {
+   cat( "red = ", getRed(), "\n" )
+   cat( "green = ", getGreen(), "\n" )
+   cat( "blue = ", getBlue(), "\n" )
+ })
red =  255 
green =  0 
blue =  0 
> p  with( p, {
+   move( 40L , 50L )
+   x  p
[1] "Java-Object{java.awt.Point[x=50,y=50]}"

Note in the last example that the x variable that is assigned is the "x" field of the object