Lesson 2 | Mapping for structs |
Objective | Describe and use the Java produced by the Mapping for IDL structs. |
Java produced by the Mapping for IDL structs
The struct mapping
An IDL struct 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.
In addition to the structure class, helper and holder classes, XHelper
and XHolder
, are also generated.
Example
To understand this mapping better, take a look at the following example. We define an IDL struct, which is then used as the return type for an operation in an IDL interface.
Take a look at both the IDL and the corresponding generated Java code in the following Slide Show.
The WeatherReport
struct is mapped to WeatherReport.java
as well as corresponding WeatherReportHelper.java
and WeatherReportHolder.java
files.
Struct weatherReport Mapping
Using struct classes
A 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);
You now know both how Java code is generated for IDL structs and how to use that resulting Java code. You can now write CORBA code in Java based on IDL containing structs.
In the next lesson, you will learn the Java mapping for IDL sequences and how to use the resulting Java.
IDL To Java Mapping For Structs - Quiz