Dynamic Cast (one more) and CORBA

J

Julien Klein

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
 
M

Mike Schilling

- 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..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top