Importing a Java method
Besides importing other ToolDef scripts or its tools, the ToolDef language also allows to import other functionality. As the ToolDef language is implemented in Java, importing other functionality takes the form of importing static Java methods. You can import Java methods under the following conditions:
-
It must import static Java methods.
-
The parameter types and return types of imported methods must only use Java data types that are supported by the Tooldef language:
-
The Java types
boolean,int,long,double,Boolean,Integer,Long,Double,StringandObjectare supported. -
The Java types of the methods can have type parameters, like
TinList<T>. Supported commonly used parameterized types areList<T>,Set<T>andMap<T, U>. If specified, the type of the type parametersTandUmust again be Java types that are supported. The wildcard type?can be used to import methods with unknown or irrelevant types, but the values cannot be used then since they becomeObject. -
Finally, arrays with supported element types are supported as well. These are converted to lists.
-
Importing from the classpath
Importing a method that exists in a class available from the classpath is done by giving the absolute name of the method. Note that unlike the ToolDef imports, the Java method imports do not use strings. It uses bare names, without double quotes around them.
As an example, you can convert a number to its notation in the binary numeral system using the java.lang.Integer.toBinaryString method. Using this Java method in a ToolDef script is done as follows:
import java.lang.Integer.toBinaryString;
outln("%s", toBinaryString(123));
In the example, the value 123 is converted. Running the script produces 1111011, which is the 123 value in the binary number system.
Imported methods can be renamed in a similar manner as renaming imported tools or ToolDef scripts. For example:
import java.lang.Integer.toBinaryString as toBin;
This import makes the same method available, except its name in the ToolDef script is now toBin. Using this import is done with for example toBin(123).
Importing from an Eclipse Plugin
The ESCET toolkit consists of many Eclipse plugins. Other software available on the Eclipse platform also uses plugins. Being able to access methods of such plugins is therefore also useful.
ToolDef supports usage of static methods from Eclipse plugins. These imports work in the same way as import of ToolDef tools, except they first state the name of the plugin to access. For example, the ESCET toolkit has a makeInitialUppercase method to change the initial character of a string to upper case, in ESCET’s org.eclipse.escet.common.java plugin. To import that method into ToolDef, you use the following import:
import org.eclipse.escet.common.java:org.eclipse.escet.common.java.Strings.makeInitialUppercase as toInitialUpper;
The line may seem confusing at first sight, but that is due to the convention to give a plugin the same name as the name of its outermost Java package. The org.eclipse.escet.common.java before the colon is the name of the plugin that contains the static method. Within this plugin, the second name org.eclipse.escet.common.java.Strings.makeInitialUppercase is the absolute name of the static makeInitialUppercase method that should be imported. The as toInitialUpper part renames the selected method in ToolDef to toInitialUpper. If you omit the last part, the method becomes available in ToolDef as makeInitialUppercase instead.
After the import, the imported method can be used. For example, after the import the ToolDef statement toInitialUpper("it works!") returns the string "It works!".