
Creates a Java object by calling a constructor from the desired
class. The object is (almost always) stored in the Omegahat session
and a reference to it returned. One must create and initialize the
Java virtual machine before calling this function. See
.JavaInit
. .JNew
is a simple alias of
.JavaConstructor
.
.JavaConstructor(className, ..., .name="", .sigs="", .convert=FALSE)
.JNew(className, ..., .name="", .sigs="", .convert=FALSE)
.Java
calls..name
is supplied. One can also provide a function which will be called with two
arguments - a reference to the Java object and the class name of the
Java object. This is the same as the function converters one can
register via setJavaFunctionConverter
.
Also, one can specify a native routine (i.e. C/C++/Fortran)
address. This can be done using
getNativeSymbolInfo
and accessing the
address
field of the returned object. See examples in the
inst/examples/
directory.
.name
is provided, this returns a
NamedReference
to a Java object stored in the Omegahat session.
Otherwise, usually an AnonymousReference
is returned. However,
if a converter to R exists for the particular Java class being created
and no value for the .name
argument is given in the
call, the Java object will be converted directly to an R object. This
is sometimes useful when the constructor populates the object's fields
and one has no further user for the object itself, but just its
contents. For example, the basic constructor for the class
StatDataURL
takes a URL name and reads its contents. A
converter could be registered for this class that returns the lines of
text. This creates a new Java object by first converting the R arguments to
Java objects and then looking for a constructor in the target class
that accepts arguments of these types. The resulting Java object is
available for future computations as arguments to .Java
,
.JavaConstructor
, and .OmegahatExpression
. At
present, the object must be explicitly freed by the caller. This is
always true if a value is given for the .name
argument.
.Java
.OmegahatExpression
tmp <- .JavaConstructor("util.Vector", as.integer(10))
.Java(tmp, "add", "This is a string")
.Java(tmp, "add", 1.5)
b <- .JavaConstructor("JButton","R Java Button")
.Java(tmp, "add", b)
f <- .JavaConstructor("GenericFrame", b)
f <- .JNew("GenericFrame", b)
Run the code above in your browser using DataLab