IDL Constructed Types   «Prev  Next»
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

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.
  • IDL struct 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.


Structs Weather Report Mapping
1) The IDL struct WeatherReport is mapped to a corresponding Java class
1)
The image includes text and code about a module dealing with weather data. Here's the extracted content:
IDL (Interface Definition Language) for a Module
// 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
The image also includes the note: "The IDL struct WeatherReport is mapped to a corresponding Java class."
This indicates that the `IDL` definition for `WeatherReport` is directly linked to its implementation in Java, mirroring the structure defined in `IDL` in the Java class.


2) The high member of the struct is mapped to a public field and argument to the full constructor
2) The high member of the struct is mapped to a public field and an argument to the full constructor. The image contains text and code related to a weather report module.
IDL (Interface Definition Language) for a Module
// 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 image also has a note: "The high member of the struct is mapped to a public field and an argument to the full constructor."
This note explains that the `high` field from the `IDL` struct is mapped to both a public field and a parameter in the full constructor of the Java class.


The forecast member of the struct is mapped to a public field and an argument to the full constructor
3)
// 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;
   }
}

The forecast member of the struct is mapped to a public field and an argument to the full constructor.
Analysis: The image provides both IDL (Interface Definition Language) and Java code representing a data structure and an interface for a weather service. The `WeatherReport` struct in the IDL defines three members: `high`, `low`, and `forecast`. In Java, this struct is mapped to a class `WeatherReport` with corresponding public fields and constructors. The interface `WeatherService` defines a method `getReport`, which takes a city name as input and returns a `WeatherReport`. The comment at the end highlights how the `forecast` member is both a public field in the Java class and a parameter in the class constructor.

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

Click on the Quiz link below to test your knowledge IDL-to-Java mapping for structs.
IDL To Java Mapping For Structs - Quiz

SEMrush Software