Generating Java Code

As an experimental feature, Specware provides the possibility to generate Java code from constructive Metaslang specs. The Java code generator can either be called from the Specware Shell or using the generate construct inside a .sw file. In both cases, an additional "option" spec can be supplied, which is used to specify certain parameters that govern aspects of code generation. For the format of the option spec, see the Section called The Option Spec.

The command has the form:
   gen-java spec-term [option-spec-term]
where the result of elaborating the spec term gives the spec to be translated into Java and the second spec term gives the option spec (see below).

The Option Spec

The option spec is used as an attribute store to be able to control certain parameters used by the Java code generator. The option spec is a regular Metaslang spec. The parameters are given by constant ops defined inside the option spec. The following list contains the op names and types that are currently interpreted as parameters by the Java code generator:

Op name & typeUsed asDefault value
package : StringName of the Java package for all generated Java class files. The package name also determines the relative path of the generated .java files (see the basedir parameter) "specware.generated"
basedir : StringThe base directory used for the generated Java class files. The full path is determine by this parameter and the relative path derived from the package name. For instance, if the value of basedir is the string "/a/b" and the package name is c.d.e, then the generated Java class files would go into the directory /a/b/c/d/e. "."
public : List StringThe list of op names that are to be declared as public in the generated Java code. Only unqualified identifiers can be used in this list. The ops in this list determine the "entry points" into the generated Java code, if it is embedded in another Java application. []
imports : List StringThe list of imports that are to be used for all generated Java class files. Each element of this list has the usual format of the argument used by Java's import statement; e.g., "java.util.*" []

Example option spec:
  spec
    def package = "test.sw.gen"
    def imports = ["java.util.*"]
    def public = ["test2"]
    def basedir = "~/myjavaapps"
  endspec
If no option spec is specified in the gen-java command, default values are used for all option parameters.

Translation of Inbuilt Ops

The following table shows the translation of some inbuilt Metaslang ops into Java:

MetaslangJava
String.writeLine(t)
String.toScreen(t)
System.out.println(t)
String.concat(t1,t2),
t1 ++ t2,
t1 ^ t2
t1 + t2
String.newline
System.getProperty
  ("line.separator")
String.lengtht.length()
String.substring(s,n1,n2)s.substring(n1,n2)
Nat.toString(n)
Nat.natToString(n)
Nat.show(n)
Integer.toString(n)
Integer.intToString(n)
Integer.show(n)
String.valueOf(n)
Nat.stringToNat(s)
Integer.stringToInt(s)
Integer.parseInt(s)
t1 && t2t1 && t2
t1 || t2t1 || t2
t1 => t2t1 ? t2 : true
t1 <=> t2t1 ? t2 : !t2

Metaslang/Java Interface

In order to use Java methods and classes inside a Metaslang spec, the following conventions are used by the Java code generator:

Type Conversion between Java and Metaslang

The following table shows the conversion of Java types to Metaslang, which can be used when accessing Java methods from Metaslang

JavaMetaslang
intInteger
booleanBoolean
charChar
void()
byte
short
float
double
not implemented
Any Java class nameMetaslang type with the same name (type must be declared in the spec)