rJava (version 0.7-0)

with.jobjRef: with and within methods for Java objects

Description

Convenience wrapper that allow calling methods of Java object from within the object.

Usage

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

Arguments

data
A Java object reference. See jobjRef-class
expr
expression to evaluate
...
ignored

Value

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

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.

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

References

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

Examples

Run this code
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 )
		} )

Run the code above in your browser using DataLab