Dynamic Cast (one more) and CORBA

Discussion in 'Java' started by Julien Klein, May 4, 2004.

  1. Julien Klein

    Julien Klein Guest

    Hi !

    I know this subject has already been discusses several times, but no
    one situation seems to correspond to mine, so I decided to start a new
    thread about it. I will try to be as clear as possible, yet if need
    more information, just tell me..

    To begin, just something about CORBA :
    - I have a reference on an object (thanks to an IOR) which type is
    org.omg.CORBA.Object

    - I need to "narrow" this object to get a "real object"

    - in the simple case (which is not mine), I just need to do the
    following :

    (1) pfmalarmService service =
    (pfmalarmService)pfmalarmServiceHelper.narrow(obj)

    where pfmalarmService is an interface and pfmalarmServiceHelper a
    class (which implements no interface)

    - my problem is that the name of the interface I need to cast my
    object with is given as a string (here pfmalarmService)

    - how can I do (is it possible ?), whatever the name X I receive as a
    string, to execute the following (X is an interface) :

    (2) X service = (X)XHelper.narrow(obj)

    - I had a look on reflection in Java, I read a lot of threads about
    dynamic casting and I am a bit lost in all these information. Yet, it
    seems that there is no way for me to do what I want...

    - During compile time, pfmalarm... classes are unknown, they are
    generated later from an IDL file, so I can't modify these generated
    files.

    - all the interfaces I need to use (X interfaces as used above)
    extends another interface which is called inService, so I can write :

    (3) inService service = (inService)inServiceHelper.narrow(obj)

    but after, it is not possible for me to call methods specific to one
    particular X interface.

    Let's take an example : imagine my interface pfmalarmeService extends
    inService interface. In pfmalarmService, I have a method called
    count(...) which is not defined in inService interface. So if I
    execute (3), it is not possible for me to call count(...) method,
    isn't it ?

    Does someone have a idea or can someone tell me it is not possible for
    me to do what I want ?

    Thanks !!

    Julien
    Julien Klein, May 4, 2004
    #1
    1. Advertising

  2. >
    > - in the simple case (which is not mine), I just need to do the
    > following :
    >
    > (1) pfmalarmService service =
    > (pfmalarmService)pfmalarmServiceHelper.narrow(obj)
    >
    > where pfmalarmService is an interface and pfmalarmServiceHelper a
    > class (which implements no interface)
    >
    > - my problem is that the name of the interface I need to cast my
    > object with is given as a string (here pfmalarmService)
    >
    > - how can I do (is it possible ?), whatever the name X I receive as a
    > string, to execute the following (X is an interface) :
    >
    > (2) X service = (X)XHelper.narrow(obj)
    >
    > - I had a look on reflection in Java, I read a lot of threads about
    > dynamic casting and I am a bit lost in all these information. Yet, it
    > seems that there is no way for me to do what I want...
    >
    > - During compile time, pfmalarm... classes are unknown, they are
    > generated later from an IDL file, so I can't modify these generated
    > files.
    >
    > - all the interfaces I need to use (X interfaces as used above)
    > extends another interface which is called inService, so I can write :
    >
    > (3) inService service = (inService)inServiceHelper.narrow(obj)
    >
    > but after, it is not possible for me to call methods specific to one
    > particular X interface.
    >
    > Let's take an example : imagine my interface pfmalarmeService extends
    > inService interface. In pfmalarmService, I have a method called
    > count(...) which is not defined in inService interface. So if I
    > execute (3), it is not possible for me to call count(...) method,
    > isn't it ?


    The difficulty I always have with dicussions like this is that people say
    what they don't know when writing the code, but not what they do know. In
    this case, you don't know the type of the object. Do you know the method
    name you want to call, or its signature, or what argumetns you want to pass
    to it, or its return type? If, hypothetically, you knew everything but the
    object type, the method name, and the return type, you could easily do
    class.forName() to get the Class object followed by a reflective method call
    using java.lang.reflect.Method.invoke(). It's trickier if you don't know
    the signature, since you'd somehow have to write code that determines at
    runtime how many arguments to pass in

    Another option, of course, is to generate classes to make these calls using
    the information in the IDL files, and compile them together with the
    IDL-generated files..
    Mike Schilling, May 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gk
    Replies:
    1
    Views:
    286
    Thomas Richter
    Feb 13, 2007
  2. Tomek
    Replies:
    1
    Views:
    1,017
    Jack Klein
    Jun 1, 2006
  3. Pavel
    Replies:
    7
    Views:
    526
    Pavel
    Sep 19, 2010
  4. Steven D'Aprano
    Replies:
    0
    Views:
    90
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    82
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page