ezand
ezand

Reputation: 79

Custom accessor strategy for Clojure `from-java`

Is there any way to customise the accessor strategy used in clojure.java.data/from-java? from-java is part of the java.data function lib.

I recently updated a third-pary Java-library that used to follow the JavaBean get and set pattern. However, after the update they went from getProperty() to property()...

I guess this change renders the from-java function not suitable in this case, no surprise since the objects are no longer proper JavaBeans.

Is there any way of making from-java aware of this accessor-pattern, or are there any other recursive mapping-mechanisms that supports this?

Upvotes: 3

Views: 84

Answers (3)

slipset
slipset

Reputation: 3078

Use the Cognitect aws-api instead :)

Upvotes: 1

Denis Fuenzalida
Denis Fuenzalida

Reputation: 3346

It seems you will have to extend the multimethod to support the classes yourself, however, you can probably use reflection (slow, I know) to build something very generic:

  • For a given object instance, find its class, then the class' DeclaredFields, and from each fields get their name and type
  • For the same instance, use .getDeclaredMethod or .getDeclaredMethods to find methods for the given name that take no params (use an empty array for this). Those methods should be the new "getters" and you can call these in your instance to extract the values.

Upvotes: 2

amalloy
amalloy

Reputation: 91857

from-java is a multimethod, do you can override it for any class you like. There is no mechanism for teaching it an alternate naming convention (and if there were such a mechanism, I imagine it would have trouble with "every method with any name at all represents a property"). Therefore you'll have to write manual conversions, but at least the recursion will be handled for you.

Upvotes: 2

Related Questions