Lesson 2 | Mapping for structs |
Objective | Describe and use the Java produced by the Mapping for IDL structs. |
X
is mapped to a final Java class also called X
. This Java class has instance variables for all the fields in the struct and two constructors. One constructor takes no arguments, the other takes as arguments values for all the fields.
This allows the objects to be either fully populated upon instantiation or populated later. Note that all the instance variables are public; struct classes do not use data hiding. This is because IDL structs are simply open baskets of data; making the fields public in Java captures that intent.XHelper
and XHolder
, are also generated.
WeatherReport
struct is mapped to WeatherReport.java
as well as corresponding WeatherReportHelper.java
and WeatherReportHolder.java
files. // struct.idl: module Module4 { struct WeatherReport { short high; short low; string forecast; }; interface WeatherService { WeatherReport getReport(in string city); }; }Java Class for WeatherReport
// WeatherReport.java: package Module4; public final class WeatherReport implements org.omg.CORBA.portable.IDLEntity { public short high; public short low; public java.lang.String forecast; public WeatherReport(){} public WeatherReport(short high, short low, java.lang.String forecast) { this.high = high; this.low = low; this.forecast = forecast; } }The IDL struct WeatherReport is mapped to a corresponding Java class
// struct.idl: module Module4 { struct WeatherReport { short high; short low; string forecast; }; interface WeatherService { WeatherReport getReport(in string city); }; }Java Class for WeatherReport
// WeatherReport.java: package Module4; public final class WeatherReport implements org.omg.CORBA.portable.IDLEntity { public short high; public short low; public java.lang.String forecast; public WeatherReport(){} public WeatherReport(short high, short low, java.lang.String forecast) { this.high = high; this.low = low; this.forecast = forecast; } }
// struct.idl: module Module4 { struct WeatherReport { short high; short low; string forecast; }; interface WeatherService { WeatherReport getReport(in string city); }; }
// WeatherReport.java: package Module4; public final class WeatherReport implements org.omg.CORBA.portable.IDLEntity { public short high; public short low; public java.lang.String forecast; public WeatherReport(){} public WeatherReport(short high, short low, java.lang.String forecast) { this.high = high; this.low = low; this.forecast = forecast; } }
WeatherReport
object can now be instantiated on the server and returned to the client via the getReport()
method of WeatherService
. The client can then simply access the fields to pull out the values. For example:
//In client code WeatherService wService; //Client initialization code omitted WeatherReport nyReport = wService.getReport ("New York"); System.out.println("The forecast for New York is " +nyReport.forecast);