hypfvieh / dbus-java

Improved version of java DBus library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/)
https://hypfvieh.github.io/dbus-java/
MIT License
180 stars 72 forks source link

Structs in Properties, is it possible? #228

Closed brett-smith closed 1 year ago

brett-smith commented 1 year ago

I am interfacing with org.freedesktop.resolve1.Manager, the SystemD DNS interface.

Various properties use (arrays of) structs as their signature, but I can't seem to find a way to map these structs to Java objects like you can outside of Properties.

image

Using Properties.Get() results in an ArrayList of Object[] containing each field of the struct. This is of course is usable, but involves lots of messy casting.

Is there a better way?

hypfvieh commented 1 year ago

There are only limited options on how to handle structs used as values in properties. I explained the problem in this sample

Anyways. I updated the StructHelper util to help you creating proper list of structs. See the linked example above.

brett-smith commented 1 year ago

Ah great, StructHelper is an improvement, thanks. I confess I wasn't really fully aware of the examples that exist now, I might have found StructHelper :)

On a more general note about Properties, I do wonder if they could be implemented in a more Java like way that would solve issues like this and make dealing with them easier.

Perhaps it could be possible to deserialize Properties to an actual proxied Java type. Property names would be mapped to getters and setters, annotations could be used to add missing collection type data back in. Bonus points if it can be made to work with Java 's newrecords for even less code (can you make a proxy record?). For convenience, DBusInterface implementations could somehow provide direct access to these.

Unless you can think of a good reason not to, I might try and prototype something if i get some time!

Anyway, thanks again, closing as solved.

hypfvieh commented 1 year ago

Feel free to try to improve this.

I tried to a few times myself, always got stuck at some point missing essential information or having some sort of hen/egg issue. Having a solution which forces to create some weird struct classes is also not useful...