Next: , Previous: , Up: The Jasper Library   [Contents][Index]


10.20.8.3 Conversion between Prolog Arguments and Java Types

The following table lists the possible values of arguments of the argument type specification to jasper_call/4 and jasper_new_object/5 (see Jasper Library Predicates). The value specifies which conversion between corresponding Prolog argument and Java type will take place.

There is currently no mechanism for specifying Java arrays in this way.

In the following the package prefix (java/lang or se/sics/jasper) has been left out for brevity.

For several of the numerical types there is the possibility that the target type cannot accurately represent the source type, e.g. when converting from a Prolog integer to a Java byte. The behavior in such cases is unspecified.

Prolog: +integer
Java: int

The argument should be a number. It is converted to a Java int, a 32 bit signed integer.

Prolog: +byte
Java: byte

The argument should be a number. It is converted to a Java byte.

Prolog: +short
Java: short

The argument should be a number. It is converted to a Java short, a 16 bit signed integer.

Prolog: +long
Java: long

The argument should be a number. It is converted to a Java long, a 64-bit signed integer.

In releases prior to 3.9.1, the value was truncated to 32 bits when passed between Java and Prolog. This is no longer the case.

Prolog: +float
Java: float

The argument should be a number. It is converted to a Java float.

Prolog: +double
Java: double

The argument should be a number. It is converted to a Java double.

Prolog: +term
Java: SPTerm

The argument can be any term. It is passed to Java as an object of the class SPTerm.

Prolog: +object(Class)
Java: Class

The argument should be the Prolog representation of a Java object of class Class. Unless it is the first argument in a non-static method (in which case is it treated as the object on which the method should be invoked), it is passed to the Java method as an object of class Class.

Prolog: +atom   obsolescent
Java: SPCanonicalAtom

The argument should be an atom. The Java method will be passed an object of class SPCanonicalAtom. Often +string, see below, is more useful.

Prolog: +boolean
Java: boolean

The argument should be an atom in {true,false}. The Java method will receive a boolean.

Prolog: +chars
Java: String

The argument should be a code list. The Java method will receive an object of class String.

Prolog: +codes
Java: String

+codes is an alias for +chars.

Prolog: +string
Java: String

The argument should be an atom. The Java method will receive an object of class String.

Note.

When using +chars, +codes or +string the automatic type conversion mechanism will also create a type signature of the form java/lang/String. If you want to call a method that accepts a String object as a parameter, but has different signature, then the method lookup will fail. A workaround is to explicitly create a String object and then call the method. For example:

:- use_module(library(jasper)).

main :-
    jasper_initialize([],JVM),
    jasper_new_object(JVM,
		      'java/lang/String',
		      init(+chars),
		      init("hamburger"),
		      H),
    Str = "urge",
    jasper_new_object(JVM,
 		      'java/lang/String',
 		      init(+chars),
 		      init(Str),
 		      S),
    jasper_call(JVM,
		method('', contains, [instance]),
		contains(+object(''),
			+object('java/lang/CharSequence'),
			[-boolean]),
		contains(H, S, B)),
    format('Contains? ~a~n', [B]).
Prolog: -atom   obsolescent
Java: SPTerm

The Java method will receive an object of class SPTerm, which should be set to an atom (e.g. using SPTerm.putString). The argument will be bound to the value of the atom when the method returns. Often -term, see below, is more useful.

Prolog: -chars
Java: StringBuffer

The Java method will receive an (empty) object of type StringBuffer, which can be modified. The argument will be bound to a code list of the StringBuffer object.

Prolog: -codes
Java: StringBuffer

-codes is an alias for -chars.

Prolog: -string
Java: StringBuffer

The Java method will receive an object of type StringBuffer, which can be modified. The argument will be bound to an atom converted from the StringBuffer object.

Prolog: -term
Java: SPTerm

The Java method will receive an object of class SPTerm, which can be set to a term (e.g. using SPTerm.consFunctor). The argument will be bound to the term when the method returns.

Prolog: [-integer]
Java: int M()

The Java method should return an int. The value will be converted to a Prolog integer.

Prolog: [-byte]
Java: byte M()

The Java method should return a byte. The value will be converted to a Prolog integer.

Prolog: [-short]
Java: short M()

The Java method should return a short. The value will be converted to a Prolog integer.

Prolog: [-long]
Java: long M()

The Java method should return a long, a 64 bit signed integer. The value will be converted to a Prolog integer.

Prolog: [-float]
Java: float M()

The Java method should return a float. The value will be converted to a Prolog float.

Prolog: [-double]
Java: double M()

The Java method should return a double. The value will be converted to a Prolog float.

Prolog: [-term]
Java: SPTerm M()

The Java method should return an object of class SPTerm, which will be converted to a Prolog term.

Prolog: [-object(Class)]
Java: SPTerm M()

The Java method should return an object of class Class, which will be converted to the internal Prolog representation of the Java object.

Prolog: [-atom]  obsolescent
Java: SPTerm M()

The Java method should return an object of class SPCanonicalAtom, which will be converted to a Prolog atom. Often [-term], see above, is more useful.

Prolog: [-boolean]
Java: boolean M()

The Java should return a boolean. The value will be converted to a Prolog atom in {true,false}.

Prolog: [-chars]
Java: String M()

The Java method should return an object of class String, which will be converted to a code list.

Prolog: [-codes]
Java: String M()

[-codes] is an alias for [-chars].

Prolog: [-string]
Java: String M()

The Java method should return an object of class String, which will be converted to an atom.



Send feedback on this subject.