Object

org.clapper.classutil

ScalaObjectToBean

Related Doc: package classutil

Permalink

object ScalaObjectToBean

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 Bean get and set 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 or var is really a getter method returning some type. That is:

val x: Int = 0
var y: Int = 10

is compiled down to the equivalent of the following Java code:

private int _x = 0;
private int _y = 10;

public int x() { return _x; }
public int y() { return _y; }
public void y_$eq(int newY) { _y = newY; }

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 an int, the mapper will assume that it's the bean version of x(), and it will ignore x(). - Methods that aren't public. - Any method in java.lang.Object. - Any method in scala.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.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. ScalaObjectToBean
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. def apply(obj: Any, className: String, recurse: Boolean): AnyRef

    Permalink

    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_.

    obj

    the Scala object

    className

    the desired class name

    recurse

    true to recursively map nested maps, false otherwise. Recursively mapped maps will have generated class names.

    returns

    an instantiated object representing the map

  5. def apply(obj: Any, recurse: Boolean = true): AnyRef

    Permalink

    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_.

    obj

    the Scala object

    recurse

    true to recursively map nested maps, false otherwise

    returns

    an instantiated object representing the map

  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped