Transform a map into an object.
Transform a map into an object. The class name will be generated,
will be in the org.clapper.classutil
package, and will have
a class name prefix of ScalaObjectToBean_
.
the Scala object
the desired class name
true
to recursively map nested maps, false
otherwise. Recursively mapped maps will have generated
class names.
an instantiated object representing the map
Transform a map into an object.
Transform a map into an object. The class name will be generated,
will be in the org.clapper.classutil
package, and will have
a class name prefix of ScalaObjectBean_
.
the Scala object
true
to recursively map nested maps, false
otherwise
an instantiated object representing the map
ScalaObjectToBean
maps a Scala object into a read-only Java bean. It takes a Scala object, locates the Scala accessors (using simple heuristics), and generates a new object with additional Java Beanget
andset
methods for the accessors.ScalaObjectToBean
is an alternative to using the@BeanProperty
annotation on classes, so it is useful for mapping case classes into Java Beans, or for mapping classes from other APIs into Java Beans without having to extend them.ScalaObjectToBean
uses the following heuristics to determine which fields to map.First, it recognizes that any Scala
val
orvar
is really a getter method returning some type. That is:is compiled down to the equivalent of the following Java code:
So, the mapper looks for Scala getter methods that take no parameters and return some non-void (i.e., non-
Unit
) value, and it looks for Scala setter methods that take one parameter, return void (Unit
) and have names ending in "_$eq". Then, from that set of methods, the mapper discards:- Methods starting with "get" - Methods that have a corresponding "get" method. In the above example, if there's a
getX()
method that returns anint
, the mapper will assume that it's the bean version ofx()
, and it will ignorex()
. - Methods that aren't public. - Any method injava.lang.Object
. - Any method inscala.Product
.If there are any methods in the remaining set, then the mapper returns a new wrapper object that contains Java Bean versions of those methods; otherwise, the mapper returns the original Scala object. The resulting bean delegates its calls to the original object, instead of capturing the object's method values at the time the bean is called. That way, if the underlying Scala object's methods return different values for each call, the bean will reflect those changes.