Calling Java from Gosu
You can write Gosu code that uses Java types. Gosu code can instantiate Java types, access properties of Java types, and call methods of Java types. Instead of writing Java code for your Gosu to call, if you write that code directly in Gosu, you can do everything that you can do in Java. For example, you can directly call existing Java classes and Java libraries. You can even write Gosu code enhancements that add properties and methods to Java types, and the new members are accessible from all Gosu code.
This topic describes how to write Gosu code that works with Java and how to write Java code that works with Gosu and works with Guidewire products.
Because Gosu is based on the Java Virtual Machine (JVM), Gosu code can directly use Java
types. From Gosu, Java classes are first-class types and are used like native Gosu classes. In
most ways, Java syntax also works in Gosu. For example, you can instantiate a Java class with
the new keyword:
var bd = new java.math.BigDecimal(5, 25)
Gosu provides additional optional features and more concise syntax than Java, such as optional type inference and optional semicolons.
Gosu code can integrate with Java types in the following ways:
- Instantiate Java types.
- Manipulate Java classes as native Gosu objects.
- Manipulate Java objects as native Gosu objects.
- Manipulate Java primitives as native Gosu objects.
Gosu converts between primitives and the equivalent object types automatically in most cases.
- Call methods on Java types.
For methods that look like getters and setters, Gosu exposes the methods also as Gosu properties.
- Get instance variables and static variables from Java types.
- Add new methods to Java types by using Gosu enhancements.
- Add new properties to Java types by using Gosu enhancements.
- Add new properties to Java types automatically for methods that look like getters and setters.
- Create Gosu classes that extend Java classes.
- Create Gosu interfaces that extend Java interfaces.
- Use Java enumerations.
- Use Java annotations.
- Use Java properties files.
All these features work with built-in Java types as well as your own Java classes and libraries.
If you do not use Guidewire entity types, you do not need to do anything other than to set the class path so that it can access the files or to put compiled classes in a special directory.
If your Java code needs to get, set, or query Guidewire entity data, you must understand how PolicyCenter works with entity data.
See also
- Gosu introduction
- Notable differences between Gosu and Java
- Java and OSGi support
- Overview of plugins
Locations for Java classes and libraries
Place your non-OSGi Java classes and libraries in the following locations. Subdirectories must match the package hierarchy.
If the class implements a single PolicyCenter plugin interface, in Guidewire Studio, you can define a separate plugin directory in the Plugins registry for that interface. In the following paths, PLUGIN_DIR represents the plugin directory that you define in the Plugins registry. If the Plugin Directory field in the Studio Plugins registry is empty, the default is the plugin directory name shared.
- If your code uses entities:
- To share code among multiple plugin interfaces, or to share between plugin code and non-plugin code, use the value shared.
- If you are not implementing a plugin interface, use the value Gosu. Notice the capitalization of Gosu. For example, if the only code that calls your Java code is your own Gosu code, use the value Gosu.
- If your code does not use entities:
- To share code among multiple plugin interfaces, or to share between plugin code and non-plugin code, use the value shared/basic.
- If you are not implementing a plugin interface, use the value Gosu/basic. Notice the capitalization of Gosu. For example, if the only code that calls your Java code is your own Gosu code, use the value Gosu/basic.
The PolicyCenter/modules/configuration/plugins/Gosu directory has subdirectories called gclasses, gresources, and idea-gclasses. These subdirectories are for internal use as compilation output paths. Never use, modify, or rely upon the contents of the gclasses, gresources, and idea-gclasses directories.
Place non-OSGi Java classes in the following locations as the root directory of directories organized by package.
PolicyCenter/modules/configuration/plugins/PLUGIN_DIR/classes
The following example references a class file using the fully qualified name mycompany.MyClass.
PolicyCenter/modules/configuration/plugins/PLUGIN_DIR/classes/mycompany/MyClass.class
Place your Java libraries and any third-party libraries in the following locations.
PolicyCenter/modules/configuration/plugins/PLUGIN_DIR/lib
