Hi
I need to write a generic wrapper for DDS-RTI Java. The wrapper should be able to handle any datatype I have defined under idl. But the problem is RTi is creating class for each data type e.g. HelloworldSupport. My requirement is based on the datatype passed I should create corrospoding *Support Object
For C++ I can see C++ trait is there to handle this situtation. Can you please give an excample how to handle this in Java. I am newbie in Java
Hi mr_deb,
Unfortunately, there's no equivalent to the C++ traits for Java. However, there is an alternative that makes use of Java reflection. Below is the code for registering a type named "Foo":
Class<?> type = resolver.resolveClass("FooTypeSupport");
if (type == null) {
throw new Exception("Cannot resolve class " + className);
}
Method getInstanceMethod = null;
Method getTypeNameMethod = null;
Method registerTypeMethod = null;
try {
// retrieve the "get_instance" method (e.g. FooTypeSupport.get_instance())
getInstanceMethod = type.getMethod("get_instance");
// retrieve the "get_type_name" method (e.g. FooTypeSupport.get_type_name())
getTypeNameMethod = type.getMethod("get_type_name");
// retrieve "register_type" method (e.g. FooTypeSupport.register_type())
registerTypeMethod = type.getMethod("register_type", DomainParticipant.class, String.class);
} catch (Exception e) {
throw new Exception("Invalid TypeSupport class " + className);
}
// invoke get_instance method
TypeSupportImpl inst = (TypeSupportImpl)getInstanceMethod.invoke(null);
// invoke get_type_name method
String typeName = (String)getTypeNameMethod.invoke(null);
if (typeName != null && inst != null) {
// invoke register_type method
registerTypeMethod.invoke(null, getValue(), typeName);
}
The fully qualified name of the TypeSupportImpl class is
com.rti.dds.topic.TypeSupportImpl
. It's the base interface for all TypeSupport classes.Second, you can use the *_untyped() API when writing or reading data. For example, when you create a DataWriter for a given topic and type, you use the
create_datawriter()
call on acom.rti.dds.publication.Publisher
. This method returns an object of typecom.rti.dds.publication.DataWriter
, which is the base interface for all DataWriters.This DataWriter interface contains a method called "
write_untyped()
" that takes an object of type "Object" as argument. Note that you cannot pass an arbitrary object; it still must be an object that is of the same type (class) as for the topic itself. So if you've registered a type Foo with the participant, then created a topic using type Foo, and then created a DataWriter for this topic ... you must pass in a Foo object to the write_untyped() method.Hope this helps.
-sumeet
Hi Sumeet
Thanks a lot for your detail answer. I can also see FooDataWriter also at the end calls write_untype method for wrting. I will try it and will bother you if I need help.
We followed the above design for writing the wrapper.and we are facing the problem similar to (http://stackoverflow.com/questions/22905914/java-passing-generic-type-to-an-another-generic-type-class).
Hi mr_deb,
Can you provide more details about the specific problem you are seeing? A code sample plus the exception or error would be helpful.
Thanks,
-sumeet
I would be interested to know if this problem, of a generic wrapper can be address with Java generics. If so, an example would be helpful.
Thanks
Nico