Error with cast in ZeroC ICE

306 views Asked by At

I generate the ZeroC classes through my ice file. On it, there're two classes: the "Device" and DeviceServer class, which is just an array of the first one. That array is formed by DevicePrx but the trouble happens when I want to cast the first class in order to build the array. This is the Ice.Application for the Server:

 public class Server extends Ice.Application {
  public int run(String[] args) {
    shutdownOnInterrupt();

    Ice.ObjectAdapter oa = communicator().createObjectAdapter("Servidor");
    Ice.ObjectPrx prx1 = oa.add(new DeviceI("descripciĆ³n", DeviceStatus.DEVICEON), Ice.Util.stringToIdentity("Device1"));
    Ice.ObjectPrx prx2 = oa.add(new DeviceI("descripciĆ³n2", DeviceStatus.DEVICEON), Ice.Util.stringToIdentity("Device2")); 

    DevicePrx [] oprx = new DevicePrx[2];

    oprx[0] = (DevicePrxHelper) prx2;
    DeviceServerI ds = new DeviceServerI(oprx);

    System.out.println(communicator().proxyToString(prx1) + " \n"+communicator().proxyToString(prx2));
    oa.activate(); 

    communicator().waitForShutdown();

    return 0;
  }

That cast doesn't work. When I execute the class, it shows this error:

!! 15/11/14 11:58:37:119 Servidor: error: main: unknown exception: java.lang.ClassCastException: Ice.ObjectPrxHelperBase cannot be cast to ITSUE.DevicePrxHelper at Server.run(Server.java:13) at Ice.Application.doMain(Application.java:214) at Ice.Application.main(Application.java:194) at Ice.Application.main(Application.java:71) at Server.main(Server.java:26)

The question is: How can I cast the Device Object to build the array??

ICE file:

module ITSUE {
   enum DeviceStatus {
     DEVICEON,
     DEVICEOFF
   };
  class Device {
         ["private"] string description;
         ["private"] DeviceStatus status;

        void setStatus(DeviceStatus ns);
        DeviceStatus getStatus();       

        string getDescription();

        void on();
        void off();
  };
  sequence<Device *> DeviceSeq;
  class DeviceServer {
     ["private"] DeviceSeq devices;
     DeviceSeq listDevices();
  };
};

Thank you very much!

1

There are 1 answers

0
Daniel 976034 On BEST ANSWER

I solve my question by my own. If anyone has the same problem, you have to cast using checkedCast or uncheckedCast method from the class XXXXPrxHelper. In my case: DevicePrxHelper.checkedCast()