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