org.clapper.classutil

ScalaObjectToBean

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
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

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

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

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

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

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

    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

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

    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

  8. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  9. def clone(): AnyRef

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

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

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

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

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

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

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

    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

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

    Definition Classes
    AnyRef
  20. def toString(): String

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

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped