Creating a DBusInterface in Java

Matthew Johnson dbus at matthew.ath.cx
Wed May 21 11:31:25 PDT 2008


On Wed May 21 14:47, Urizev wrote:
> As it is supposed, hal has not the GetAllDevices method. I know it is
> possible to create the class with CreateInterface, but I do not know
> how. Beside, I think that this process would can be automatic since it
> is possible to request for the introspection data of the interface via
> DBUS. I do not know if how can I use the API. If anybody has some
> information, any reference or example, I would really appreciate it.

Because Java is a statically typed language, the interfaces you are
calling must be available at compile time. It is possible to create
these from introspection data, this is what CreateInterface does.
Alternatively, if you know the syntax of the methods you are calling,
you can write the interface file by hand, it's generally very simple.

All this should be documented here: 

http://dbus.freedesktop.org/doc/dbus-java/

It _is_ possible to do it all dynamically, using reflection support
in Java, but it's ugly, and since you will always know the name and
types, still easier to just write the interface file itself.

Either you write:

   (Manager.java)
   ...
   interface Manager extends DBusInterface
   {
      ...
      List<String> GetAllDevices();
   }

^^ this can be replaced with a call to CreateInterface

   (Test.java)
   ...
   Manager hal = conn.getRemoteObject("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager", Manager.class);
   List<String> ds = hal.GetAllDevices();

or:
   ...
   DBusInterface hal = conn.getRemoteObject("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager");
   try {
      Method m = hal.getClass().getMethod("GetAllDevices", new Class[] {});
      List<String> ds = (List<String>) m.invoke(hal, new Object[] {});
   } (....)
   
with lots of handling of possible reflection exceptions.

Matt
-- 
www.matthew.ath.cx
D-Bus Java
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://lists.freedesktop.org/archives/dbus/attachments/20080521/2b0f4f76/attachment.pgp 


More information about the dbus mailing list