with.jobjRef

0th

Percentile

with and within methods for Java objects and class names

Convenience wrapper that allow calling methods of Java object and classes from within the object (or class).

Keywords
classes
Usage
## S3 method for class 'jobjRef':
with(data, expr, ...)
## S3 method for class 'jobjRef':
within(data, expr, ...)

## S3 method for class 'jarrayRef': with(data, expr, ...) ## S3 method for class 'jarrayRef': within(data, expr, ...)

## S3 method for class 'jclassName': with(data, expr, ...) ## S3 method for class 'jclassName': within(data, expr, ...)

Arguments
data
A Java object reference or a java class name. See J
expr
R expression to evaluate
...
ignored
Details

The expression is evaluated in an environment that contains a mapping between the public fields and methods of the object.

The methods of the object are mapped to standard R functions in the environment. In case of classes, only static methods are used.

The fields of the object are mapped to active bindings (see makeActiveBinding) so that they can be accessed and modified from within the environment. For classes, only static fields are used.

Value

  • with returns the value of the expression and within returns the data argument

References

the java.lang.reflect package: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/package-summary.html

Aliases
  • with.jobjRef
  • within.jobjRef
  • with.jarrayRef
  • within.jarrayRef
  • with.jclassName
  • within.jclassName
Examples
.jinit()

if (!nzchar(Sys.getenv("NOAWT"))) {
  p <- .jnew( "java/awt/Point", 0L, 0L )
  with( p, {
	# x and y and now 0
	move( 10L, 10L )
	# x and y are now 10
	x <- x + y
  } )

  f <- within( .jnew( "javax/swing/JFrame" ) , {
	layout <- .jnew( "java/awt/BorderLayout" )
	setLayout( layout )
	add( .jnew( "javax/swing/JLabel", "north" ), layout$NORTH )
	add( .jnew( "javax/swing/JLabel", "south" ), layout$SOUTH )
	add( .jnew( "javax/swing/JLabel", "west" ), layout$WEST )
	add( .jnew( "javax/swing/JLabel", "east" ), layout$EAST )
	setSize( .jnew( "java/awt/Dimension", 400L, 400L ) )
	setVisible( TRUE )
  } )
}

Double <- J("java.lang.Double")
with( Double, MIN_VALUE )
with( Double, parseDouble( "10.2" ) )

# inner class exampleHashMap <- J("java.util.HashMap")
with( HashMap, new( SimpleEntry, "key", "value" ) )
with( HashMap, SimpleEntry )

with( J("java.lang.System"), getProperty("java.home") )

stopifnot( with( Double, parseDouble("10.0") ) == 10.0 )
d <- new( Double, "10.0") 
stopifnot( with( d, doubleValue() ) == 10.0 )
Documentation reproduced from package rJava, version 0.9-7, License: GPL-2

Community examples

Looks like there are no examples yet.